Skip to content

Commit

Permalink
Publish built package (#781)
Browse files Browse the repository at this point in the history
* Introduce publish config property

* Introduce custom publish command configuration

* Prepare publish build step

* introduce publish buiuld step

* introduce publish build item to order build steps
  • Loading branch information
alex-kovalenko1982 authored Oct 5, 2024
1 parent 52c34bf commit fa365b0
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import io.quarkiverse.quinoa.deployment.items.BuiltResourcesBuildItem;
import io.quarkiverse.quinoa.deployment.items.ConfiguredQuinoaBuildItem;
import io.quarkiverse.quinoa.deployment.items.InstalledPackageManagerBuildItem;
import io.quarkiverse.quinoa.deployment.items.PublishedPackageBuildItem;
import io.quarkiverse.quinoa.deployment.items.TargetDirBuildItem;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerInstall;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner;
import io.quarkiverse.quinoa.deployment.packagemanager.types.PackageManagerType;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
Expand Down Expand Up @@ -186,6 +188,12 @@ && isDevServerMode(configuredQuinoa.resolvedConfig())) {
Set.of("quarkus.quinoa.build-dir"));
}

// doesn't make sense to copy from ui build dir to quinoa target dir
// in case of `just-build` option enabled
if (configuredQuinoa.resolvedConfig().justBuild()) {
return new TargetDirBuildItem(buildDir);
}

final Path targetBuildDir = initializeTargetDirectory(outputTarget).resolve(TARGET_BUILD_DIR_NAME);
FileUtil.deleteDirectory(targetBuildDir);
try {
Expand All @@ -201,10 +209,37 @@ && isDevServerMode(configuredQuinoa.resolvedConfig())) {
}

@BuildStep
public BuiltResourcesBuildItem prepareBuiltResources(Optional<TargetDirBuildItem> targetDir) throws IOException {
public PublishedPackageBuildItem publishBuiltPackage(
ConfiguredQuinoaBuildItem configuredQuinoa,
InstalledPackageManagerBuildItem installedPackageManager,
Optional<TargetDirBuildItem> targetDir) {
if (configuredQuinoa == null || !configuredQuinoa.resolvedConfig().publish()) {
return new PublishedPackageBuildItem(true);
}

if (targetDir.isEmpty()) {
LOG.warn("Target dir is empty - unable to run publish command.");
return new PublishedPackageBuildItem(true);
}

final PackageManagerRunner packageManagerRunner = installedPackageManager.getPackageManager();
packageManagerRunner.publish();
return new PublishedPackageBuildItem(false);
}

@BuildStep
@Consume(PublishedPackageBuildItem.class) // just to order build steps
public BuiltResourcesBuildItem prepareBuiltResources(
ConfiguredQuinoaBuildItem configuredQuinoa,
Optional<TargetDirBuildItem> targetDir) throws IOException {
if (targetDir.isEmpty()) {
return null;
}
if (configuredQuinoa != null && configuredQuinoa.resolvedConfig().justBuild()) {
// no need to configure vertx static resources when `just-build` activated
return null;
}

return new BuiltResourcesBuildItem(lookupBuiltResources(targetDir.get().getBuildDirectory()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface PackageManagerCommandConfig {
String DEFAULT_INSTALL_COMMAND = "install";
String DEFAULT_BUILD_COMMAND = "run build";
String DEFAULT_TEST_COMMAND = "run test";
String DEFAULT_PUBLISH_COMMAND = "run publish";

/**
* Custom command for installing all packages.
Expand Down Expand Up @@ -75,6 +76,17 @@ public interface PackageManagerCommandConfig {
*/
Map<String, String> devEnv();

/**
* Custom command for publishing the package.
*/
@WithDefault(DEFAULT_PUBLISH_COMMAND)
Optional<String> publish();

/**
* Environment variables for publish command execution.
*/
Map<String, String> publishEnv();

static boolean isEqual(PackageManagerCommandConfig p1, PackageManagerCommandConfig p2) {
if (!Objects.equals(p1.install(), p2.install())) {
return false;
Expand All @@ -100,6 +112,12 @@ static boolean isEqual(PackageManagerCommandConfig p1, PackageManagerCommandConf
if (!Objects.equals(p1.devEnv(), p2.devEnv())) {
return false;
}
if (!Objects.equals(p1.publish(), p2.publish())) {
return false;
}
if (!Objects.equals(p1.publishEnv(), p2.publishEnv())) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public interface QuinoaConfig {
*/
DevServerConfig devServer();

/**
* Runs additionally package manager publish command.
* This is handy when using together with the justBuild() option to
* publish built web package to a registry.
* The command is executed after the build command and can be customized
* with the {@link PackageManagerCommandConfig#publish()} and {@link PackageManagerCommandConfig#publishEnv()}.
*/
@WithDefault("false")
boolean publish();

static List<String> getNormalizedIgnoredPathPrefixes(QuinoaConfig config,
NonApplicationRootPathBuildItem nonApplicationRootPath) {
return config.ignoredPathPrefixes()
Expand Down Expand Up @@ -268,6 +278,9 @@ static boolean isEqual(QuinoaConfig q1, QuinoaConfig q2) {
if (!DevServerConfig.isEqual(q1.devServer(), q2.devServer())) {
return false;
}
if (!Objects.equals(q1.publish(), q2.publish())) {
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ public Optional<String> dev() {
public Map<String, String> devEnv() {
return delegate.devEnv();
}

@Override
public Optional<String> publish() {
return delegate.publish();
}

@Override
public Map<String, String> publishEnv() {
return delegate.publishEnv();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public DevServerConfig devServer() {
return delegate.devServer();
}

@Override
public boolean publish() {
return delegate.publish();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkiverse.quinoa.deployment.items;

import io.quarkus.builder.item.SimpleBuildItem;

public final class PublishedPackageBuildItem extends SimpleBuildItem {

private final boolean skipped;

public PublishedPackageBuildItem(boolean skipped) {
this.skipped = skipped;
}

public boolean isSkipped() {
return skipped;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ public void test() {
}
}

public void publish() {
final PackageManager.Command publish = packageManager.publish();
LOG.infof("Running Quinoa package manager publish command: %s", publish.commandWithArguments);
if (!exec(publish)) {
throw new RuntimeException(
format("Error in Quinoa while running package manager publish command: %s", publish.commandWithArguments));
}
}

public void stopDev(Process process) {
if (process == null || !process.isAlive()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.quarkiverse.quinoa.deployment.config.PackageManagerCommandConfig.DEFAULT_BUILD_COMMAND;
import static io.quarkiverse.quinoa.deployment.config.PackageManagerCommandConfig.DEFAULT_INSTALL_COMMAND;
import static io.quarkiverse.quinoa.deployment.config.PackageManagerCommandConfig.DEFAULT_PUBLISH_COMMAND;
import static io.quarkiverse.quinoa.deployment.config.PackageManagerCommandConfig.DEFAULT_TEST_COMMAND;

import java.io.File;
Expand Down Expand Up @@ -71,6 +72,13 @@ public Command test() {
prepareCommandWithArguments(commandsConfig.test().orElse(DEFAULT_TEST_COMMAND)));
}

@Override
public Command publish() {
return new Command(
environment(Map.of(), commandsConfig.publishEnv()),
prepareCommandWithArguments(commandsConfig.publish().orElse(DEFAULT_PUBLISH_COMMAND)));
}

@Override
public Command dev() {
return new Command(
Expand Down Expand Up @@ -125,4 +133,4 @@ private String extendPathVariable(final String existingValue, final List<String>
}
return pathBuilder.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface PackageManager {

Command test();

Command publish();

Command dev();

public static PackageManager resolve(PackageManagerType type, String binary,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkiverse.quinoa.test;

import static io.quarkiverse.quinoa.deployment.testing.QuinoaQuarkusUnitTest.getWebUITestDirPath;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.quinoa.deployment.testing.QuinoaQuarkusUnitTest;
import io.quarkus.test.QuarkusUnitTest;

public class QuinoaPackageManagerPublishTest {
private static final String NAME = "package-manager-publish";

@RegisterExtension
static final QuarkusUnitTest config = QuinoaQuarkusUnitTest.create(NAME).toQuarkusUnitTest()
.overrideConfigKey("quarkus.quinoa.just-build", "true")
.overrideConfigKey("quarkus.quinoa.publish", "true")
.assertLogRecords(l -> {
assertThat(l)
.anyMatch(s -> s.getMessage().startsWith("Running Quinoa package manager publish command"));
});

@Test
public void testQuinoa() {
// target/quinoa/build dir shall not exist while just-build activated
assertThat(getWebUITestDirPath(NAME).resolve("quinoa-app-1.0.0.tgz")).exists();
assertThat(getWebUITestDirPath(NAME).resolve("node_modules/installed")).isRegularFile()
.hasContent("hello");
}

}
3 changes: 2 additions & 1 deletion deployment/src/test/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "npm run node-version && mkdirp build && cross-env-shell \"echo $MODE>build/index.html\" && copyfiles -u 1 public/* build/",
"build-something": "mkdirp build && cross-env-shell \"echo $BUILD>build/index.html\" && copyfiles -u 1 public/* build/",
"start": "cross-env MODE=live-coding npm run build && node server.js",
"postinstall": "echo hello> node_modules/installed"
"postinstall": "echo hello> node_modules/installed",
"publish": "npm pack"
},
"keywords": [],
"author": "",
Expand Down
52 changes: 52 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-quinoa.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,23 @@ endif::add-copy-button-to-env-var[]
|`framework detection with fallback to 'start'`


a|icon:lock[title=Fixed at build time] [[quarkus-quinoa_quarkus-quinoa-package-manager-command-publish]]`link:#quarkus-quinoa_quarkus-quinoa-package-manager-command-publish[quarkus.quinoa.package-manager-command.publish]`


[.description]
--
Custom command for publishing the package.

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_QUINOA_PACKAGE_MANAGER_COMMAND_PUBLISH+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_QUINOA_PACKAGE_MANAGER_COMMAND_PUBLISH+++`
endif::add-copy-button-to-env-var[]
--|string
|`run publish`


a|icon:lock[title=Fixed at build time] [[quarkus-quinoa_quarkus-quinoa-run-tests]]`link:#quarkus-quinoa_quarkus-quinoa-run-tests[quarkus.quinoa.run-tests]`


Expand Down Expand Up @@ -707,6 +724,23 @@ endif::add-copy-button-to-env-var[]
|`false`


a|icon:lock[title=Fixed at build time] [[quarkus-quinoa_quarkus-quinoa-publish]]`link:#quarkus-quinoa_quarkus-quinoa-publish[quarkus.quinoa.publish]`


[.description]
--
Runs package manager publish command. This is handy when the using justBuild() option to publish built web module or component to a registry.

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_QUINOA_PUBLISH+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_QUINOA_PUBLISH+++`
endif::add-copy-button-to-env-var[]
--|boolean
|`false`


a|icon:lock[title=Fixed at build time] [[quarkus-quinoa_quarkus-quinoa-package-manager-command-install-env-install-env]]`link:#quarkus-quinoa_quarkus-quinoa-package-manager-command-install-env-install-env[quarkus.quinoa.package-manager-command.install-env."install-env"]`


Expand Down Expand Up @@ -796,4 +830,22 @@ endif::add-copy-button-to-env-var[]

|


a|icon:lock[title=Fixed at build time] [[quarkus-quinoa_quarkus-quinoa-package-manager-command-publish-env-publish-env]]`link:#quarkus-quinoa_quarkus-quinoa-package-manager-command-publish-env-publish-env[quarkus.quinoa.package-manager-command.publish-env."publish-env"]`


[.description]
--
Environment variables for publish command execution.

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_QUINOA_PACKAGE_MANAGER_COMMAND_PUBLISH_ENV__PUBLISH_ENV_+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_QUINOA_PACKAGE_MANAGER_COMMAND_PUBLISH_ENV__PUBLISH_ENV_+++`
endif::add-copy-button-to-env-var[]
--|link:https://docs.oracle.com/javase/8/docs/api/java/lang/String.html[String]

|

|===

0 comments on commit fa365b0

Please sign in to comment.