Skip to content

Commit

Permalink
feat: image cli forced deps/ext in gradle projects
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Nov 23, 2022
1 parent 5e5d057 commit fe77423
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import io.quarkus.cli.common.HelpOption;
import io.quarkus.cli.common.OutputOptionMixin;
Expand All @@ -28,6 +30,7 @@ public class BaseBuildCommand {
protected PropertiesOptions propertiesOptions = new PropertiesOptions();

Path projectRoot;
protected List<String> forcedExtensions = new ArrayList<>();

public Path projectRoot() {
if (projectRoot == null) {
Expand All @@ -43,4 +46,20 @@ public BuildSystemRunner getRunner() {
BuildTool buildTool = QuarkusProjectHelper.detectExistingBuildTool(projectRoot()); // nullable
return BuildSystemRunner.getRunner(output, propertiesOptions, registryClient, projectRoot(), buildTool);
}

public List<String> getForcedExtensions() {
return forcedExtensions;
}

/**
* Commands using `@ParentCommand` need to set the ouput.
* This is needed for testing purposes.
* More specifically --cli-test-dir relies on this.
*
* @param output The command ouput
*/
public void setOutput(OutputOptionMixin output) {
this.output = output;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
import java.util.concurrent.Callable;

import io.quarkus.cli.build.BaseBuildCommand;
import io.quarkus.cli.build.BuildSystemRunner;
import io.quarkus.cli.common.BuildOptions;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.utils.GradleInitScript;
import io.quarkus.devtools.project.BuildTool;
import picocli.CommandLine;
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Parameters;

public class BaseImageCommand extends BaseBuildCommand implements Callable<Integer> {

protected static final String QUARKUS_FORCED_EXTENSIONS = "quarkus.application.forced-extensions";
protected static final String QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX = "io.quarkus:quarkus-container-image-";

protected static final String QUARKUS_CONTAINER_IMAGE_BUILDER = "quarkus.container-image.builder";
private static final String QUARKUS_CONTAINER_IMAGE_GROUP = "quarkus.container-image.group";
private static final String QUARKUS_CONTAINER_IMAGE_NAME = "quarkus.container-image.name";
private static final String QUARKUS_CONTAINER_IMAGE_TAG = "quarkus.container-image.tag";
protected static final String QUARKUS_CONTAINER_IMAGE_REGISTRY = "quarkus.container-image.registry";
protected static final String QUARKUS_CONTAINER_IMAGE_DRY_RUN = "quarkus.container-image.dry-run";

protected static final Map<BuildTool, String> ACTION_MAPPING = Map.of(BuildTool.MAVEN, "quarkus:image-build",
BuildTool.GRADLE, "imageBuild");
Expand All @@ -46,8 +47,54 @@ public void populateImageConfiguration(Map<String, String> properties) {
imageOptions.name.ifPresent(name -> properties.put(QUARKUS_CONTAINER_IMAGE_NAME, name));
imageOptions.tag.ifPresent(tag -> properties.put(QUARKUS_CONTAINER_IMAGE_TAG, tag));
imageOptions.registry.ifPresent(registry -> properties.put(QUARKUS_CONTAINER_IMAGE_REGISTRY, registry));
}

@Override
public Integer call() throws Exception {
try {
populateImageConfiguration(propertiesOptions.properties);
BuildSystemRunner runner = getRunner();

String action = getAction()
.orElseThrow(() -> new IllegalStateException("Unknown image action for " + runner.getBuildTool().name()));

if (runner.getBuildTool() == BuildTool.GRADLE) {
prepareGradle();
}

if (runner.getBuildTool() == BuildTool.MAVEN) {
prepareMaven();
}
BuildSystemRunner.BuildCommandArgs commandArgs = runner.prepareAction(action, buildOptions, runMode, params);
if (runMode.isDryRun()) {
System.out.println("Dry run option detected. Target command:");
System.out.println(" " + commandArgs.showCommand());
return ExitCode.OK;
}
return runner.run(commandArgs);
} catch (Exception e) {
return output.handleCommandException(e, "Unable to build image: " + e.getMessage());
}
}

public void prepareMaven() {
if (runMode.isDryRun()) {
properties.put(QUARKUS_CONTAINER_IMAGE_DRY_RUN, "true");
return;
}
BuildSystemRunner runner = getRunner();
BuildSystemRunner.BuildCommandArgs compileArgs = runner.prepareAction("compiler:compile", buildOptions, runMode,
params);
int compileExitCode = runner.run(compileArgs);
if (compileExitCode != ExitCode.OK) {
throw new RuntimeException("Failed to compile. Compilation exited with exit code:" + compileExitCode);
}
}

public void prepareGradle() {
if (!getForcedExtensions().isEmpty()) {
// Ensure that params is modifiable
params = new ArrayList<>(this.params);
GradleInitScript.populateForExtensions(getForcedExtensions(), params);
}
}

Expand All @@ -59,8 +106,4 @@ public PropertiesOptions getPropertiesOptions() {
return this.propertiesOptions;
}

@Override
public Integer call() throws Exception {
return 0;
}
}
43 changes: 11 additions & 32 deletions devtools/cli/src/main/java/io/quarkus/cli/image/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.util.Map;
import java.util.Optional;

import io.quarkus.cli.build.BuildSystemRunner;
import io.quarkus.devtools.project.BuildTool;
import picocli.CommandLine;
import picocli.CommandLine.ExitCode;

@CommandLine.Command(name = "build", sortOptions = false, showDefaultValues = true, mixinStandardHelpOptions = false, header = "Build a container image.", description = "%n"
+ "This command will build a container image for the project.", subcommands = { Docker.class, Buildpack.class,
Expand All @@ -20,43 +18,24 @@ public class Build extends BaseImageCommand {
BuildTool.GRADLE, "imageBuild");

@Override
public void populateImageConfiguration(Map<String, String> properties) {
super.populateImageConfiguration(properties);
public void prepareGradle() {
//For gradle the builder options is meaningless so let's sepcicy the builder using `--builder`
//This is done only for `--builder` as this is the only option that is processed from the gradle task.
//The rest of the options are passed to the container image processors.
Optional<String> optionalBuilder = Optional
.ofNullable(propertiesOptions.properties.remove(QUARKUS_CONTAINER_IMAGE_BUILDER));
String builder = optionalBuilder.orElse("docker");
params.add("--builder=" + builder);
forcedExtensions.add(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + builder);
// Always call super.prepareGralde after adding forcedExtension or else forcedExtensions will be ignored.
super.prepareGradle();
}

@Override
public Optional<String> getAction() {
return Optional.ofNullable(ACTION_MAPPING.get(getRunner().getBuildTool()));
}

@Override
public Integer call() throws Exception {
try {
populateImageConfiguration(propertiesOptions.properties);
BuildSystemRunner runner = getRunner();

String action = getAction().orElseThrow(
() -> new IllegalStateException("Unknown image build action for " + runner.getBuildTool().name()));
BuildSystemRunner.BuildCommandArgs commandArgs = runner.prepareAction(action, buildOptions, runMode, params);
if (runMode.isDryRun()) {
System.out.println("Dry run option detected. Target command:");
System.out.println(" " + commandArgs.showCommand());
return ExitCode.OK;
}
if (getRunner().getBuildTool() == BuildTool.MAVEN) {
BuildSystemRunner.BuildCommandArgs compileArgs = runner.prepareAction("compiler:compile", buildOptions, runMode,
params);
int compileExitCode = runner.run(compileArgs);
if (compileExitCode != ExitCode.OK) {
return compileExitCode;
}
}
return runner.run(commandArgs);
} catch (Exception e) {
return output.handleCommandException(e, "Unable to build image: " + e.getMessage());
}
}

@Override
public String toString() {
return "Build {imageOptions='" + imageOptions + "'}";
Expand Down
23 changes: 13 additions & 10 deletions devtools/cli/src/main/java/io/quarkus/cli/image/Buildpack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.cli.image;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -51,10 +53,9 @@ public class Buildpack extends BaseImageCommand {
BaseImageCommand parent;

@Override
public void populateImageConfiguration(Map<String, String> properties) {
super.populateImageConfiguration(properties);
public Integer call() throws Exception {
Map<String, String> properties = parent.getPropertiesOptions().properties;
properties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, BUILDPACK);
properties.put(QUARKUS_FORCED_EXTENSIONS, QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + BUILDPACK);

builderImage.ifPresent(i -> properties
.put(BUILDPACK_CONFIG_PREFIX + (buildOptions.buildNative ? NATIVE_BUILDER_IMAGE : JVM_BUILDER_IMAGE), i));
Expand All @@ -64,16 +65,18 @@ public void populateImageConfiguration(Map<String, String> properties) {
dockerHost.ifPresent(h -> properties.put(BUILDPACK_CONFIG_PREFIX + DOCKER_HOST, h));
runImage.ifPresent(i -> properties.put(BUILDPACK_CONFIG_PREFIX + RUN_IMAGE, i));
buildEnv.forEach((k, v) -> properties.put(BUILDPACK_CONFIG_PREFIX + BUILDER_ENV + "." + k, v));

parent.getForcedExtensions().add(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + BUILDPACK);
parent.runMode = runMode;
parent.buildOptions = buildOptions;
parent.imageOptions = imageOptions;
parent.setOutput(output);
return parent.call();
}

@Override
public Integer call() throws Exception {
try {
populateImageConfiguration(parent.getPropertiesOptions().properties);
return parent.call();
} catch (Exception e) {
return output.handleCommandException(e, "Unable to build image: " + e.getMessage());
}
public List<String> getForcedExtensions() {
return Arrays.asList(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + BUILDPACK);
}

@Override
Expand Down
26 changes: 11 additions & 15 deletions devtools/cli/src/main/java/io/quarkus/cli/image/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ public class Docker extends BaseImageCommand {
@ParentCommand
BaseImageCommand parent;

@Override
public void populateImageConfiguration(Map<String, String> properties) {
super.populateImageConfiguration(properties);
properties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, DOCKER);
properties.put(QUARKUS_FORCED_EXTENSIONS, QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + DOCKER);
dockerFile.ifPresent(d -> properties.put(DOCKER_CONFIG_PREFIX
+ (buildOptions.buildNative ? DOCKERFILE_NATIVE_PATH : DOCKERFILE_JVM_PATH), d));
}

@Override
public Integer call() throws Exception {
try {
populateImageConfiguration(parent.getPropertiesOptions().properties);
return parent.call();
} catch (Exception e) {
return output.handleCommandException(e, "Unable to build image: " + e.getMessage());
}
Map<String, String> properties = parent.getPropertiesOptions().properties;
properties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, DOCKER);
dockerFile.ifPresent(d -> properties
.put(DOCKER_CONFIG_PREFIX + (buildOptions.buildNative ? DOCKERFILE_NATIVE_PATH : DOCKERFILE_JVM_PATH), d));

parent.getForcedExtensions().add(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + DOCKER);
parent.runMode = runMode;
parent.buildOptions = buildOptions;
parent.imageOptions = imageOptions;
parent.setOutput(output);
return parent.call();
}

@Override
Expand Down
23 changes: 12 additions & 11 deletions devtools/cli/src/main/java/io/quarkus/cli/image/Jib.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.cli.image;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -77,11 +78,9 @@ public class Jib extends BaseImageCommand {
BaseImageCommand parent;

@Override
public void populateImageConfiguration(Map<String, String> properties) {
super.populateImageConfiguration(properties);
public Integer call() throws Exception {
Map<String, String> properties = parent.getPropertiesOptions().properties;
properties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, JIB);
properties.put(QUARKUS_FORCED_EXTENSIONS, QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + JIB);

baseImage.ifPresent(
d -> properties.put(JIB_CONFIG_PREFIX + (buildOptions.buildNative ? BASE_NATIVE_IMAGE : BASE_JVM_IMAGE), d));

Expand Down Expand Up @@ -133,16 +132,18 @@ public void populateImageConfiguration(Map<String, String> properties) {
if (buildOptions.offline) {
properties.put(JIB_CONFIG_PREFIX + OFFLINE_MODE, "true");
}

parent.getForcedExtensions().add(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + JIB);
parent.runMode = runMode;
parent.buildOptions = buildOptions;
parent.imageOptions = imageOptions;
parent.setOutput(output);
return parent.call();
}

@Override
public Integer call() throws Exception {
try {
populateImageConfiguration(parent.getPropertiesOptions().properties);
return parent.call();
} catch (Exception e) {
return output.handleCommandException(e, "Unable to build image: " + e.getMessage());
}
public List<String> getForcedExtensions() {
return Arrays.asList(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + JIB);
}

@Override
Expand Down
22 changes: 12 additions & 10 deletions devtools/cli/src/main/java/io/quarkus/cli/image/Openshift.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.cli.image;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -59,10 +60,9 @@ public class Openshift extends BaseImageCommand implements Callable<Integer> {
BaseImageCommand parent;

@Override
public void populateImageConfiguration(Map<String, String> properties) {
super.populateImageConfiguration(properties);
public Integer call() throws Exception {
Map<String, String> properties = parent.getPropertiesOptions().properties;
properties.put(QUARKUS_CONTAINER_IMAGE_BUILDER, OPENSHIFT);
properties.put(QUARKUS_FORCED_EXTENSIONS, QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + OPENSHIFT);
properties.put(OPENSHIFT_CONFIG_PREFIX + BUILD_STRATEGY, buildStrategy.orElse("docker"));

baseImage.ifPresent(d -> properties
Expand All @@ -87,16 +87,18 @@ public void populateImageConfiguration(Map<String, String> properties) {
properties.put(OPENSHIFT_CONFIG_PREFIX + (buildOptions.buildNative ? NATIVE_FILE_NAME : JAR_FILE_NAME),
artifactFilename);
}

parent.getForcedExtensions().add(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + OPENSHIFT);
parent.runMode = runMode;
parent.buildOptions = buildOptions;
parent.imageOptions = imageOptions;
parent.setOutput(output);
return parent.call();
}

@Override
public Integer call() throws Exception {
try {
populateImageConfiguration(parent.getPropertiesOptions().properties);
return parent.call();
} catch (Exception e) {
return output.handleCommandException(e, "Unable to build image: " + e.getMessage());
}
public List<String> getForcedExtensions() {
return Arrays.asList(QUARKUS_CONTAINER_IMAGE_EXTENSION_KEY_PREFIX + OPENSHIFT);
}

@Override
Expand Down
Loading

0 comments on commit fe77423

Please sign in to comment.