Skip to content

Commit

Permalink
Merge pull request quarkusio#14681 from ia3andy/override-examples
Browse files Browse the repository at this point in the history
Add new "examples" parameter to Create & CreateProjectMojo
  • Loading branch information
ia3andy authored Feb 9, 2021
2 parents 669336a + 4411b09 commit c2ae110
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 15 deletions.
9 changes: 7 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/Create.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ public class Create extends BaseSubCommand implements Callable<Integer> {

@CommandLine.Option(names = { "-a",
"--artifact-id" }, order = 2, paramLabel = "ARTIFACT-ID", description = "The artifactId for project")
String artifactId = "my-project";
String artifactId = "code-with-quarkus";

@CommandLine.Option(names = { "-v",
"--version" }, order = 3, paramLabel = "VERSION", description = "The version for project")
String version = "1.0-SNAPSHOT";
String version = "1.0.0-SNAPSHOT";

@CommandLine.Option(names = { "-0",
"--no-examples" }, order = 4, description = "Generate without example code.")
boolean noExamples = false;

@CommandLine.Option(names = { "-e",
"--examples" }, order = 4, description = "Choose which example(s) you want in the generated Quarkus application.")
Set<String> examples;

@CommandLine.ArgGroup()
TargetBuildTool targetBuildTool = new TargetBuildTool();

Expand Down Expand Up @@ -114,6 +118,7 @@ else if (targetBuildTool.gradleKotlinDsl)
.artifactId(artifactId)
.version(version)
.sourceType(sourceType)
.overrideExamples(examples)
.extensions(extensions)
.noExamples(noExamples)
.execute().isSuccess();
Expand Down
24 changes: 12 additions & 12 deletions devtools/cli/src/test/java/io/quarkus/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ public void revertCWD() {

@Test
public void testCreateMavenDefaults() throws Exception {
Path project = workspace.resolve("my-project");
Path project = workspace.resolve("code-with-quarkus");

execute("create");

Assertions.assertEquals(CommandLine.ExitCode.OK, exitCode);
Assertions.assertTrue(screen.contains("Project my-project created"));
Assertions.assertTrue(screen.contains("Project code-with-quarkus created"));

Assertions.assertTrue(project.resolve("pom.xml").toFile().exists());
String pom = readString(project.resolve("pom.xml"));
Assertions.assertTrue(pom.contains("<groupId>org.acme</groupId>"));
Assertions.assertTrue(pom.contains("<artifactId>my-project</artifactId>"));
Assertions.assertTrue(pom.contains("<version>1.0-SNAPSHOT</version>"));
Assertions.assertTrue(pom.contains("<artifactId>code-with-quarkus</artifactId>"));
Assertions.assertTrue(pom.contains("<version>1.0.0-SNAPSHOT</version>"));
}

@Test
public void testAddListRemove() throws Exception {
Path project = workspace.resolve("my-project");
Path project = workspace.resolve("code-with-quarkus");
testCreateMavenDefaults();

execute("add");
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testGradleAddListRemove() throws Exception {
// Gradle list command cannot be screen captured with the current implementation
// so I will just test good return values
//
Path project = workspace.resolve("my-project");
Path project = workspace.resolve("code-with-quarkus");
testCreateGradleDefaults();

execute("add");
Expand Down Expand Up @@ -251,20 +251,20 @@ public void testCreateMaven() throws Exception {

@Test
public void testCreateGradleDefaults() throws Exception {
Path project = workspace.resolve("my-project");
Path project = workspace.resolve("code-with-quarkus");

execute("create", "--gradle");

Assertions.assertEquals(CommandLine.ExitCode.OK, exitCode);
Assertions.assertTrue(screen.contains("Project my-project created"));
Assertions.assertTrue(screen.contains("Project code-with-quarkus created"));

Assertions.assertTrue(project.resolve("build.gradle").toFile().exists());
String pom = readString(project.resolve("build.gradle"));
Assertions.assertTrue(pom.contains("group 'org.acme'"));
Assertions.assertTrue(pom.contains("version '1.0-SNAPSHOT'"));
Assertions.assertTrue(pom.contains("version '1.0.0-SNAPSHOT'"));
Assertions.assertTrue(project.resolve("settings.gradle").toFile().exists());
String settings = readString(project.resolve("settings.gradle"));
Assertions.assertTrue(settings.contains("my-project"));
Assertions.assertTrue(settings.contains("code-with-quarkus"));

}

Expand All @@ -273,7 +273,7 @@ public void testGradleBuild() throws Exception {

execute("create", "--gradle", "resteasy");

Path project = workspace.resolve("my-project");
Path project = workspace.resolve("code-with-quarkus");
System.setProperty("user.dir", project.toFile().getAbsolutePath());
Assertions.assertEquals(CommandLine.ExitCode.OK, exitCode);

Expand All @@ -290,7 +290,7 @@ public void testMavenBuild() throws Exception {

execute("create", "resteasy");

Path project = workspace.resolve("my-project");
Path project = workspace.resolve("code-with-quarkus");
System.setProperty("user.dir", project.toFile().getAbsolutePath());
Assertions.assertEquals(CommandLine.ExitCode.OK, exitCode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,18 @@ public class CreateProjectMojo extends AbstractMojo {
@Parameter(property = "legacyCodegen", defaultValue = "false")
private boolean legacyCodegen;

/**
* When true, do not include any example code in the generated Quarkus project.
*/
@Parameter(property = "noExamples", defaultValue = "false")
private boolean noExamples;

/**
* Choose which example(s) you want in the generated Quarkus application.
*/
@Parameter(property = "examples")
private Set<String> examples;

/**
* Group ID of the target platform BOM
*/
Expand Down Expand Up @@ -272,6 +281,7 @@ public void execute() throws MojoExecutionException {
.className(className)
.packageName(packageName)
.extensions(extensions)
.overrideExamples(examples)
.legacyCodegen(legacyCodegen)
.noExamples(noExamples);
if (path != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ protected Collection<Codestart> select(QuarkusCodestartProjectInput projectInput
// Add codestarts from extension and for tooling
projectInput.getSelection().addNames(getExtensionCodestarts(projectInput));
projectInput.getSelection().addNames(getToolingCodestarts(projectInput));
projectInput.getSelection().addNames(projectInput.getOverrideExamples());

projectInput.getData().putAll(generateSelectedExtensionsData(projectInput));

// Filter out examples if noExamples
final List<Codestart> projectCodestarts = super.select(projectInput).stream()
.filter(c -> !isExample(c) || !projectInput.noExamples())
.filter(c -> !isExample(c) || projectInput.getOverrideExamples().isEmpty()
|| c.isSelected(projectInput.getOverrideExamples()))
.collect(Collectors.toCollection(ArrayList::new));

// include default example codestarts if none selected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
import io.quarkus.devtools.codestarts.CodestartProjectInput;
import io.quarkus.devtools.project.BuildTool;
import java.util.Collection;
import java.util.Set;

public final class QuarkusCodestartProjectInput extends CodestartProjectInput {
private final BuildTool buildTool;
private final Collection<AppArtifactCoords> extensions;
private final Set<String> overrideExamples;
private final boolean noExamples;
private final boolean noDockerfiles;
private final boolean noBuildToolWrapper;

public QuarkusCodestartProjectInput(QuarkusCodestartProjectInputBuilder builder) {
super(builder);
this.extensions = builder.extensions;
this.overrideExamples = builder.overrideExamples;
this.buildTool = requireNonNull(builder.buildTool, "buildTool is required");
this.noExamples = builder.noExamples;
this.noDockerfiles = builder.noDockerfiles;
Expand All @@ -31,6 +34,10 @@ public Collection<AppArtifactCoords> getExtensions() {
return extensions;
}

public Set<String> getOverrideExamples() {
return overrideExamples;
}

public boolean noExamples() {
return noExamples;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class QuarkusCodestartProjectInputBuilder extends CodestartProjectInputBuilder {
public Collection<AppArtifactCoords> extensions = new ArrayList<>();
Collection<AppArtifactCoords> extensions = new ArrayList<>();
Set<String> overrideExamples = new HashSet<>();
boolean noExamples;
boolean noDockerfiles;
boolean noBuildToolWrapper;
Expand All @@ -39,6 +42,16 @@ public QuarkusCodestartProjectInputBuilder addExtension(AppArtifactKey extension
return this.addExtension(Extensions.toCoords(extension, null));
}

public QuarkusCodestartProjectInputBuilder addOverrideExamples(Collection<String> overrideExamples) {
this.overrideExamples.addAll(overrideExamples);
return this;
}

public QuarkusCodestartProjectInputBuilder addExample(String overrideExample) {
this.overrideExamples.add(overrideExample);
return this;
}

@Override
public QuarkusCodestartProjectInputBuilder addCodestarts(Collection<String> codestarts) {
super.addCodestarts(codestarts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class CreateProject {
public static final String NO_BUILDTOOL_WRAPPER = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "no-buildtool-wrapper");
public static final String NO_EXAMPLES = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "no-examples");
public static final String CODESTARTS = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "codestarts");
public static final String OVERRIDE_EXAMPLES = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "examples");

private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile("(?:1\\.)?(\\d+)(?:\\..*)?");

Expand Down Expand Up @@ -134,6 +135,11 @@ public CreateProject codestarts(Set<String> codestarts) {
return this;
}

public CreateProject overrideExamples(Set<String> overrideExamples) {
setValue(OVERRIDE_EXAMPLES, overrideExamples);
return this;
}

/**
* @deprecated As of release 1.10, codestarts are default. Legacy codegen is scheduled to be removed:
* https://github.com/quarkusio/quarkus/issues/12897
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.quarkus.devtools.commands.CreateProject.NO_BUILDTOOL_WRAPPER;
import static io.quarkus.devtools.commands.CreateProject.NO_DOCKERFILES;
import static io.quarkus.devtools.commands.CreateProject.NO_EXAMPLES;
import static io.quarkus.devtools.commands.CreateProject.OVERRIDE_EXAMPLES;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeCoordsFromQuery;
import static io.quarkus.devtools.project.codegen.ProjectGenerator.BOM_ARTIFACT_ID;
import static io.quarkus.devtools.project.codegen.ProjectGenerator.BOM_GROUP_ID;
Expand Down Expand Up @@ -88,6 +89,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
.addExtensions(extensionsToAdd)
.buildTool(invocation.getQuarkusProject().getBuildTool())
.addOverrideExamples(invocation.getValue(OVERRIDE_EXAMPLES, new HashSet<>()))
.addCodestarts(invocation.getValue(CODESTARTS, new HashSet<>()))
.noExamples(invocation.getValue(NO_EXAMPLES, false))
.noBuildToolWrapper(invocation.getValue(NO_BUILDTOOL_WRAPPER, false))
Expand Down

0 comments on commit c2ae110

Please sign in to comment.