From 1af4c01c2bb0d8a60eaa0ed016c7a5465ba3b042 Mon Sep 17 00:00:00 2001 From: Andy Damevin Date: Tue, 26 May 2020 16:13:39 +0200 Subject: [PATCH] Improve consistency in codegen This is part of #8178 The result is just a tiny bit better, this is just a step in my journey. Having more consistency and readability will help in the refactoring process.. - Use only the values in `QuarkusCommandInvocation`, because having the Properties and the Values made it confusing - Make `QuarkusCommandInvocation` more consistent by making it partially immutable (only values are mutable) and enforcing non null params in the constructor - Remove legacy @Deprecated stuff --- .../cli/commands/CreateProjectCommand.java | 12 +- .../cli/commands/ListExtensionsCommand.java | 19 ++-- .../gradle/tasks/QuarkusAddExtension.java | 2 +- .../gradle/tasks/QuarkusListExtensions.java | 3 +- .../gradle/tasks/QuarkusPlatformTask.java | 9 +- .../gradle/tasks/QuarkusRemoveExtension.java | 2 +- .../io/quarkus/maven/AddExtensionMojo.java | 9 +- .../io/quarkus/maven/BuildFileMojoBase.java | 6 +- .../io/quarkus/maven/CreateProjectMojo.java | 2 +- .../io/quarkus/maven/ListExtensionsMojo.java | 5 +- .../io/quarkus/maven/RemoveExtensionMojo.java | 8 +- .../cli/commands/AddExtensionResult.java | 27 ----- .../quarkus/cli/commands/AddExtensions.java | 57 ++-------- .../commands/AddExtensionsCommandHandler.java | 2 +- .../quarkus/cli/commands/CreateProject.java | 103 ++++++++++-------- .../commands/CreateProjectCommandHandler.java | 38 +++---- .../quarkus/cli/commands/ListExtensions.java | 19 +--- .../ListExtensionsCommandHandler.java | 8 +- .../commands/QuarkusCommandInvocation.java | 95 +++------------- .../cli/commands/RemoveExtensions.java | 11 +- .../io/quarkus/cli/commands/ValueMap.java | 15 +++ .../quarkus/cli/commands/file/BuildFile.java | 5 +- .../LegacyQuarkusCommandInvocation.java | 32 ------ .../java/io/quarkus/generators/BuildTool.java | 3 + .../quarkus/generators/ProjectGenerator.java | 11 -- .../rest/BasicRestProjectGenerator.java | 38 +++---- .../cli/commands/ListExtensionsTest.java | 16 ++- .../rest/BasicRestProjectGeneratorTest.java | 37 +++++-- 28 files changed, 218 insertions(+), 376 deletions(-) delete mode 100644 independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionResult.java delete mode 100644 independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/legacy/LegacyQuarkusCommandInvocation.java diff --git a/devtools/aesh/src/main/java/io/quarkus/cli/commands/CreateProjectCommand.java b/devtools/aesh/src/main/java/io/quarkus/cli/commands/CreateProjectCommand.java index 34a77f314e917b..265cfba9e86708 100644 --- a/devtools/aesh/src/main/java/io/quarkus/cli/commands/CreateProjectCommand.java +++ b/devtools/aesh/src/main/java/io/quarkus/cli/commands/CreateProjectCommand.java @@ -12,6 +12,7 @@ import org.aesh.io.Resource; import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.platform.tools.config.QuarkusPlatformConfig; /** * @author Ståle Pedersen @@ -42,11 +43,12 @@ public CommandResult execute(CommandInvocation commandInvocation) { if (path != null) { try { - boolean status = new CreateProject(new FileProjectWriter(new File(path.getAbsolutePath()))) - .groupId(groupid) - .artifactId(artifactid) - .version(this.version) - .doCreateProject(new HashMap<>()); + boolean status = new CreateProject(new FileProjectWriter(new File(path.getAbsolutePath())), + QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()) + .groupId(groupid) + .artifactId(artifactid) + .version(this.version) + .doCreateProject(new HashMap<>()); if (status) { commandInvocation.println("Project " + artifactid + " created successfully."); } else { diff --git a/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java b/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java index 7e0b1b2ebd3541..7f8163d1b8a433 100644 --- a/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java +++ b/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java @@ -15,6 +15,7 @@ import io.quarkus.cli.commands.file.GradleBuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.platform.tools.config.QuarkusPlatformConfig; /** @@ -44,19 +45,19 @@ public CommandResult execute(CommandInvocation commandInvocation) throws Command } else { try { BuildFile buildFile = null; + ProjectWriter writer = null; if (path != null) { File projectDirectory = new File(path.getAbsolutePath()); - try (FileProjectWriter writer = new FileProjectWriter(projectDirectory)) { - if (new File(projectDirectory, "build.gradle").exists() - || new File(projectDirectory, "build.gradle.kts").exists()) { - buildFile = new GradleBuildFile(writer); - } else { - buildFile = new MavenBuildFile(writer); - } + writer = new FileProjectWriter(projectDirectory); + if (new File(projectDirectory, "build.gradle").exists() + || new File(projectDirectory, "build.gradle.kts").exists()) { + buildFile = new GradleBuildFile(writer); + } else { + buildFile = new MavenBuildFile(writer); } } - new ListExtensions(buildFile, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()) - .listExtensions(all, format, searchPattern); + new ListExtensions(writer, buildFile, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()) + .all(all).format(format).search(searchPattern); } catch (IOException e) { throw new CommandException("Unable to list extensions", e); } diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusAddExtension.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusAddExtension.java index 6d5d7a0a794b1a..96a65f61d4a394 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusAddExtension.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusAddExtension.java @@ -40,7 +40,7 @@ public void addExtension() { .collect(toSet()); try { - new AddExtensions(getGradleBuildFile(), platformDescriptor()) + new AddExtensions(getWriter(), getGradleBuildFile(), platformDescriptor()) .extensions(extensionsSet) .execute(); } catch (Exception e) { diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java index 1644d9f8b5969c..05645ede916bf1 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java @@ -7,7 +7,6 @@ import org.gradle.api.tasks.options.Option; import io.quarkus.cli.commands.ListExtensions; -import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.gradle.GradleBuildFileFromConnector; public class QuarkusListExtensions extends QuarkusPlatformTask { @@ -57,7 +56,7 @@ public QuarkusListExtensions() { @TaskAction public void listExtensions() { try { - new ListExtensions(new GradleBuildFileFromConnector(new FileProjectWriter(getProject().getProjectDir())), + new ListExtensions(getWriter(), new GradleBuildFileFromConnector(getWriter()), platformDescriptor()) .all(isAll()) .format(getFormat()) diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java index 82087c556b4fcf..ac76d1aa9b0846 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java @@ -66,8 +66,13 @@ protected QuarkusPlatformDescriptor platformDescriptor() { @Internal protected GradleBuildFile getGradleBuildFile() { return getProject().getParent() == null - ? new GradleBuildFile(new FileProjectWriter(getProject().getProjectDir())) - : new GradleBuildFile(new FileProjectWriter(getProject().getProjectDir()), + ? new GradleBuildFile(getWriter()) + : new GradleBuildFile(getWriter(), new FileProjectWriter(getProject().getRootProject().getProjectDir())); } + + @Internal + protected FileProjectWriter getWriter() { + return new FileProjectWriter(getProject().getProjectDir()); + } } diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusRemoveExtension.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusRemoveExtension.java index 427b3f9fd95551..2c646851a4e62f 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusRemoveExtension.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusRemoveExtension.java @@ -39,7 +39,7 @@ public void removeExtension() { .map(String::trim) .collect(toSet()); try { - new RemoveExtensions(getGradleBuildFile(), platformDescriptor()) + new RemoveExtensions(getWriter(), getGradleBuildFile(), platformDescriptor()) .extensions(extensionsSet) .execute(); } catch (Exception e) { diff --git a/devtools/maven/src/main/java/io/quarkus/maven/AddExtensionMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/AddExtensionMojo.java index ba2f1ff21372df..f0edefb59652e3 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/AddExtensionMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/AddExtensionMojo.java @@ -14,7 +14,7 @@ import io.quarkus.cli.commands.AddExtensions; import io.quarkus.cli.commands.QuarkusCommandOutcome; import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.generators.BuildTool; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.tools.MessageWriter; @@ -49,12 +49,11 @@ protected void validateParameters() throws MojoExecutionException { } @Override - public void doExecute(BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) + public void doExecute(ProjectWriter writer, BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) throws MojoExecutionException { - if (buildFile == null) { try { - buildFile = BuildTool.MAVEN.createBuildFile(new FileProjectWriter(project.getBasedir())); + buildFile = BuildTool.MAVEN.createBuildFile(writer); } catch (IOException e) { throw new MojoExecutionException("Failed to initialize the project's build descriptor", e); } @@ -69,7 +68,7 @@ public void doExecute(BuildFile buildFile, QuarkusPlatformDescriptor platformDes } try { - final QuarkusCommandOutcome outcome = new AddExtensions(buildFile, platformDescr) + final QuarkusCommandOutcome outcome = new AddExtensions(writer, buildFile, platformDescr) .extensions(ext.stream().map(String::trim).collect(Collectors.toSet())) .execute(); if (!outcome.isSuccess()) { diff --git a/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java b/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java index 940c4e75bc1329..8c79d2104cb577 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java @@ -23,6 +23,7 @@ import io.quarkus.cli.commands.file.GradleBuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.descriptor.resolver.json.QuarkusJsonPlatformDescriptorResolver; @@ -118,7 +119,7 @@ public void execute() throws MojoExecutionException { platformDescr = CreateUtils.resolvePlatformDescriptor(bomGroupId, bomArtifactId, bomVersion, mvn, getLog()); } - doExecute(buildFile, platformDescr, log); + doExecute(fileProjectWriter, buildFile, platformDescr, log); } catch (IOException e) { throw new MojoExecutionException("Failed to initialize project reading tools", e); } finally { @@ -168,7 +169,8 @@ private Artifact resolveJsonOrNull(MavenArtifactResolver mvn, String bomGroupId, protected void validateParameters() throws MojoExecutionException { } - protected abstract void doExecute(BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) + protected abstract void doExecute(ProjectWriter writer, BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, + MessageWriter log) throws MojoExecutionException; private String resolveValue(String expr, BuildFile buildFile) throws IOException { diff --git a/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java index bd26c6867a537b..75c3cf391d5ca9 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java @@ -210,7 +210,7 @@ public void execute() throws MojoExecutionException { .className(className) .extensions(extensions); if (path != null) { - createProject.setProperty("path", path); + createProject.setValue("path", path); } success = createProject.execute().isSuccess(); diff --git a/devtools/maven/src/main/java/io/quarkus/maven/ListExtensionsMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/ListExtensionsMojo.java index 68d6fa733d1dc9..6eb444087e1fb6 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/ListExtensionsMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/ListExtensionsMojo.java @@ -6,6 +6,7 @@ import io.quarkus.cli.commands.ListExtensions; import io.quarkus.cli.commands.file.BuildFile; +import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.tools.MessageWriter; @@ -38,10 +39,10 @@ public class ListExtensionsMojo extends BuildFileMojoBase { protected String searchPattern; @Override - public void doExecute(BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) + public void doExecute(ProjectWriter writer, BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) throws MojoExecutionException { try { - new ListExtensions(buildFile, platformDescr) + new ListExtensions(writer, buildFile, platformDescr) .all(all) .format(format) .search(searchPattern) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/RemoveExtensionMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/RemoveExtensionMojo.java index 5b2595e983a2f6..bd4482b1a2aa0e 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/RemoveExtensionMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/RemoveExtensionMojo.java @@ -14,7 +14,7 @@ import io.quarkus.cli.commands.QuarkusCommandOutcome; import io.quarkus.cli.commands.RemoveExtensions; import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.generators.BuildTool; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.tools.MessageWriter; @@ -49,12 +49,12 @@ protected void validateParameters() throws MojoExecutionException { } @Override - public void doExecute(BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) + public void doExecute(ProjectWriter writer, BuildFile buildFile, QuarkusPlatformDescriptor platformDescr, MessageWriter log) throws MojoExecutionException { if (buildFile == null) { try { - buildFile = BuildTool.MAVEN.createBuildFile(new FileProjectWriter(project.getBasedir())); + buildFile = BuildTool.MAVEN.createBuildFile(writer); } catch (IOException e) { throw new MojoExecutionException("Failed to initialize the project's build descriptor", e); } @@ -69,7 +69,7 @@ public void doExecute(BuildFile buildFile, QuarkusPlatformDescriptor platformDes } try { - final QuarkusCommandOutcome outcome = new RemoveExtensions(buildFile, platformDescr) + final QuarkusCommandOutcome outcome = new RemoveExtensions(writer, buildFile, platformDescr) .extensions(ext.stream().map(String::trim).collect(Collectors.toSet())) .execute(); if (!outcome.isSuccess()) { diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionResult.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionResult.java deleted file mode 100644 index 056b03fecfc02f..00000000000000 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionResult.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.quarkus.cli.commands; - -/** - * @deprecated in 1.3.0.CR1 - * This class was replaced with {@link QuarkusCommandOutcome} as the generic outcome of a project manipulating - * command. - * @see QuarkusCommand - */ -@Deprecated -public class AddExtensionResult { - - private final boolean updated; - private final boolean succeeded; - - public AddExtensionResult(boolean updated, boolean succeeded) { - this.updated = updated; - this.succeeded = succeeded; - } - - public boolean isUpdated() { - return updated; - } - - public boolean succeeded() { - return succeeded; - } -} diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java index c61f94a2859cdd..1d4b941cdb5974 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java @@ -1,5 +1,7 @@ package io.quarkus.cli.commands; +import static com.google.common.base.Preconditions.checkNotNull; + import io.quarkus.cli.commands.file.BuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; @@ -7,7 +9,6 @@ import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.tools.ToolsConstants; import io.quarkus.platform.tools.ToolsUtils; -import io.quarkus.platform.tools.config.QuarkusPlatformConfig; import java.io.IOException; import java.util.Set; @@ -22,46 +23,18 @@ public class AddExtensions { private final QuarkusCommandInvocation invocation; - /** - * @deprecated in 1.3.0.CR1 - * Please use the variant that accepts {@link QuarkusPlatformDescriptor} as an argument. - */ - @Deprecated - public AddExtensions(ProjectWriter writer) throws IOException { - this(writer, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()); - } - - /** - * @deprecated in 1.3.0.CR1 - * Please use the variant that accepts {@link QuarkusPlatformDescriptor} as an argument. - */ - @Deprecated - public AddExtensions(BuildFile buildFile) throws IOException { - this(buildFile, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()); - } - - /** - * @deprecated in 1.3.0.CR1 - * Please use the variant that accepts {@link QuarkusPlatformDescriptor} as an argument. - */ - @Deprecated - public AddExtensions(final ProjectWriter writer, final BuildTool buildTool) - throws IOException { - this(writer, buildTool, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()); - } - public AddExtensions(final ProjectWriter writer, QuarkusPlatformDescriptor platformDescr) throws IOException { - this(new MavenBuildFile(writer), platformDescr); + this(writer, new MavenBuildFile(writer), platformDescr); } public AddExtensions(final ProjectWriter writer, final BuildTool buildTool, QuarkusPlatformDescriptor platformDescr) throws IOException { - this(buildTool.createBuildFile(writer), platformDescr); + this(writer, buildTool.createBuildFile(writer), platformDescr); } - public AddExtensions(final BuildFile buildFile, QuarkusPlatformDescriptor platformDescr) { - invocation = new QuarkusCommandInvocation(platformDescr); - invocation.setBuildFile(buildFile); + public AddExtensions(final ProjectWriter writer, final BuildFile buildFile, QuarkusPlatformDescriptor platformDescr) { + checkNotNull(buildFile, "buildFile is required"); + invocation = new QuarkusCommandInvocation(platformDescr, writer, buildFile); } public AddExtensions extensions(Set extensions) { @@ -69,22 +42,6 @@ public AddExtensions extensions(Set extensions) { return this; } - /** - * @deprecated in 1.3.0.CR1 - * Please call {@link #extensions(Set)} and then {@link #execute()} - */ - @Deprecated - public AddExtensionResult addExtensions(final Set extensions) throws IOException { - final QuarkusCommandOutcome outcome; - try { - outcome = extensions(extensions).execute(); - } catch (QuarkusCommandException e) { - throw new IOException("Failed to list extensions", e); - } - return new AddExtensionResult(outcome.getValue(OUTCOME_UPDATED, false), outcome.isSuccess()); - - } - public QuarkusCommandOutcome execute() throws QuarkusCommandException { return new AddExtensionsCommandHandler().execute(invocation); } diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionsCommandHandler.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionsCommandHandler.java index 350619c683eda3..34c1637acc9bd4 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionsCommandHandler.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/AddExtensionsCommandHandler.java @@ -72,7 +72,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws throw new QuarkusCommandException("Failed to add extensions", e); } - if (updated) { + if (buildFile != null && updated) { try { buildFile.close(); } catch (IOException e) { diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java index fcc0e19e8c9658..6f2617955354b6 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java @@ -1,5 +1,6 @@ package io.quarkus.cli.commands; +import static com.google.common.base.Preconditions.checkNotNull; import static io.quarkus.generators.ProjectGenerator.CLASS_NAME; import static io.quarkus.generators.ProjectGenerator.IS_SPRING; import static io.quarkus.generators.ProjectGenerator.JAVA_TARGET; @@ -13,9 +14,9 @@ import io.quarkus.generators.BuildTool; import io.quarkus.generators.SourceType; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; -import io.quarkus.platform.tools.config.QuarkusPlatformConfig; import java.io.IOException; import java.util.Collection; +import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -32,54 +33,36 @@ public class CreateProject { private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile("(?:1\\.)?(\\d+)(?:\\..*)?"); - public static SourceType determineSourceType(Set extensions) { - Optional sourceType = extensions.stream() - .map(SourceType::parse) - .filter(Optional::isPresent) - .map(e -> e.orElse(SourceType.JAVA)) - .findAny(); - return sourceType.orElse(SourceType.JAVA); - } - - private static boolean isSpringStyle(Collection extensions) { - return extensions != null && extensions.stream().anyMatch(e -> e.toLowerCase().contains("spring-web")); - } - - private QuarkusCommandInvocation invocation; - + private final ProjectWriter writer; + private final QuarkusPlatformDescriptor platformDescr; private String javaTarget; + private BuildFile buildFile; + private BuildTool buildTool = BuildTool.MAVEN; - /** - * @deprecated since 1.3.0.CR1 - * Please use {@link #CreateProject(ProjectWriter, QuarkusPlatformDescriptor)} instead. - */ - @Deprecated - public CreateProject(ProjectWriter writer) { - this(writer, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()); - } + private Map values = new HashMap<>(); public CreateProject(final ProjectWriter writer, QuarkusPlatformDescriptor platformDescr) { - invocation = new QuarkusCommandInvocation(platformDescr); - invocation.setProjectWriter(writer); + this.writer = checkNotNull(writer, "writer is required"); + this.platformDescr = checkNotNull(platformDescr, "platformDescr is required"); } public CreateProject groupId(String groupId) { - setProperty(PROJECT_GROUP_ID, groupId); + setValue(PROJECT_GROUP_ID, groupId); return this; } public CreateProject artifactId(String artifactId) { - setProperty(PROJECT_ARTIFACT_ID, artifactId); + setValue(PROJECT_ARTIFACT_ID, artifactId); return this; } public CreateProject version(String version) { - setProperty(PROJECT_VERSION, version); + setValue(PROJECT_VERSION, version); return this; } public CreateProject sourceType(SourceType sourceType) { - invocation.setValue(SOURCE_TYPE, sourceType); + setValue(SOURCE_TYPE, sourceType); return this; } @@ -95,7 +78,13 @@ public CreateProject className(String className) { if (!(SourceVersion.isName(className) && !SourceVersion.isKeyword(className))) { throw new IllegalArgumentException(className + " is not a valid class name"); } - setProperty(CLASS_NAME, className); + setValue(CLASS_NAME, className); + return this; + } + + public CreateProject buildFile(BuildFile buildFile) { + this.buildFile = buildFile; + this.buildTool(buildFile.getBuildTool()); return this; } @@ -105,28 +94,18 @@ public CreateProject className(String className) { @Deprecated public CreateProject extensions(Set extensions) { if (isSpringStyle(extensions)) { - invocation.setValue(IS_SPRING, true); + setValue(IS_SPRING, true); } return this; } - public CreateProject setProperty(String name, String value) { - invocation.setProperty(name, value); - return this; - } - public CreateProject setValue(String name, Object value) { - invocation.setValue(name, value); - return this; - } - - public CreateProject buildFile(BuildFile buildFile) { - invocation.setBuildFile(buildFile); + values.put(name, value); return this; } public CreateProject buildTool(BuildTool buildTool) { - invocation.setBuildTool(buildTool); + this.buildTool = checkNotNull(buildTool, "buildTool is required"); return this; } @@ -134,7 +113,7 @@ public boolean doCreateProject(final Map context) throws IOExcep if (context != null && !context.isEmpty()) { for (Map.Entry entry : context.entrySet()) { if (entry.getValue() != null) { - invocation.setProperty(entry.getKey(), entry.getValue().toString()); + setValue(entry.getKey(), entry.getValue()); } } } @@ -151,11 +130,41 @@ public QuarkusCommandOutcome execute() throws QuarkusCommandException { Matcher matcher = JAVA_VERSION_PATTERN .matcher(this.javaTarget != null ? this.javaTarget : System.getProperty("java.version", "")); if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 11) { - invocation.setProperty(JAVA_TARGET, "8"); + setValue(JAVA_TARGET, "8"); } else { - invocation.setProperty(JAVA_TARGET, "11"); + setValue(JAVA_TARGET, "11"); } + final BuildFile computeBuildFile = computeBuildFile(); + final QuarkusCommandInvocation invocation = new QuarkusCommandInvocation(values, platformDescr, writer, + computeBuildFile); return new CreateProjectCommandHandler().execute(invocation); } + + private BuildFile computeBuildFile() throws QuarkusCommandException { + if (buildFile != null) { + return buildFile; + } + if (buildTool != null) { + try { + return buildTool.createBuildFile(writer); + } catch (final IOException e) { + throw new QuarkusCommandException("Failed to create project", e); + } + } + throw new QuarkusCommandException("Either BuildTool or BuildFile must be defined"); + } + + public static SourceType determineSourceType(Set extensions) { + Optional sourceType = extensions.stream() + .map(SourceType::parse) + .filter(Optional::isPresent) + .map(e -> e.orElse(SourceType.JAVA)) + .findAny(); + return sourceType.orElse(SourceType.JAVA); + } + + private static boolean isSpringStyle(Collection extensions) { + return extensions != null && extensions.stream().anyMatch(e -> e.toLowerCase().contains("spring-web")); + } } diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProjectCommandHandler.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProjectCommandHandler.java index d04cb36129f08e..52b3caec87fdff 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProjectCommandHandler.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProjectCommandHandler.java @@ -1,16 +1,6 @@ package io.quarkus.cli.commands; -import static io.quarkus.generators.ProjectGenerator.BOM_ARTIFACT_ID; -import static io.quarkus.generators.ProjectGenerator.BOM_GROUP_ID; -import static io.quarkus.generators.ProjectGenerator.BOM_VERSION; -import static io.quarkus.generators.ProjectGenerator.BUILD_FILE; -import static io.quarkus.generators.ProjectGenerator.CLASS_NAME; -import static io.quarkus.generators.ProjectGenerator.PACKAGE_NAME; -import static io.quarkus.generators.ProjectGenerator.PROJECT_ARTIFACT_ID; -import static io.quarkus.generators.ProjectGenerator.PROJECT_GROUP_ID; -import static io.quarkus.generators.ProjectGenerator.PROJECT_VERSION; -import static io.quarkus.generators.ProjectGenerator.QUARKUS_VERSION; -import static io.quarkus.generators.ProjectGenerator.SOURCE_TYPE; +import static io.quarkus.generators.ProjectGenerator.*; import io.quarkus.cli.commands.file.BuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; @@ -39,36 +29,34 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws } final QuarkusPlatformDescriptor platformDescr = invocation.getPlatformDescriptor(); - invocation.setProperty(BOM_GROUP_ID, platformDescr.getBomGroupId()); - invocation.setProperty(BOM_ARTIFACT_ID, platformDescr.getBomArtifactId()); - invocation.setProperty(QUARKUS_VERSION, platformDescr.getQuarkusVersion()); - invocation.setProperty(BOM_VERSION, platformDescr.getBomVersion()); + invocation.setValue(BOM_GROUP_ID, platformDescr.getBomGroupId()); + invocation.setValue(BOM_ARTIFACT_ID, platformDescr.getBomArtifactId()); + invocation.setValue(QUARKUS_VERSION, platformDescr.getQuarkusVersion()); + invocation.setValue(BOM_VERSION, platformDescr.getBomVersion()); final Properties quarkusProps = ToolsUtils.readQuarkusProperties(platformDescr); - quarkusProps.forEach((k, v) -> invocation.setProperty(k.toString().replace("-", "_"), v.toString())); + quarkusProps.forEach((k, v) -> invocation.setValue(k.toString().replace("-", "_"), v.toString())); try (BuildFile buildFile = invocation.getBuildFile()) { - invocation.setValue(BUILD_FILE, buildFile); - - String className = invocation.getProperty(CLASS_NAME); + String className = invocation.getStringValue(CLASS_NAME); if (className != null) { className = invocation.getValue(SOURCE_TYPE, SourceType.JAVA).stripExtensionFrom(className); int idx = className.lastIndexOf('.'); if (idx >= 0) { - String pkgName = invocation.getProperty(PACKAGE_NAME); + String pkgName = invocation.getStringValue(PACKAGE_NAME); if (pkgName == null) { - invocation.setProperty(PACKAGE_NAME, className.substring(0, idx)); + invocation.setValue(PACKAGE_NAME, className.substring(0, idx)); } className = className.substring(idx + 1); } - invocation.setProperty(CLASS_NAME, className); + invocation.setValue(CLASS_NAME, className); } ProjectGeneratorRegistry.get(BasicRestProjectGenerator.NAME).generate(projectWriter, invocation); // call close at the end to save file - buildFile.completeFile(invocation.getProperty(PROJECT_GROUP_ID), - invocation.getProperty(PROJECT_ARTIFACT_ID), - invocation.getProperty(PROJECT_VERSION), + buildFile.completeFile(invocation.getStringValue(PROJECT_GROUP_ID), + invocation.getStringValue(PROJECT_ARTIFACT_ID), + invocation.getStringValue(PROJECT_VERSION), platformDescr, quarkusProps); } catch (IOException e) { throw new QuarkusCommandException("Failed to create project", e); diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensions.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensions.java index 6f7daf0e3a4016..c1d8f98eb00de0 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensions.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensions.java @@ -1,10 +1,10 @@ package io.quarkus.cli.commands; import io.quarkus.cli.commands.file.BuildFile; +import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.tools.ToolsConstants; import io.quarkus.platform.tools.ToolsUtils; -import io.quarkus.platform.tools.config.QuarkusPlatformConfig; import java.io.IOException; import java.util.Map; import org.apache.maven.model.Dependency; @@ -22,20 +22,9 @@ public class ListExtensions { private final QuarkusCommandInvocation invocation; private final ListExtensionsCommandHandler handler = new ListExtensionsCommandHandler(); - /** - * @deprecated since 1.3.0.CR1 - * Please use {@link #ListExtensions(BuildFile, QuarkusPlatformDescriptor)} instead. - */ - @Deprecated - public ListExtensions(final BuildFile buildFile) throws IOException { - this(buildFile, QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()); - } - - public ListExtensions(final BuildFile buildFile, QuarkusPlatformDescriptor platformDescr) throws IOException { - this.invocation = new QuarkusCommandInvocation(platformDescr); - if (buildFile != null) { - invocation.setBuildFile(buildFile); - } + public ListExtensions(final ProjectWriter writer, final BuildFile buildFile, QuarkusPlatformDescriptor platformDescr) + throws IOException { + this.invocation = new QuarkusCommandInvocation(platformDescr, writer, buildFile); } public ListExtensions all(boolean all) { diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensionsCommandHandler.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensionsCommandHandler.java index 6fe372b9eaea04..4e7995ba29f616 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensionsCommandHandler.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ListExtensionsCommandHandler.java @@ -4,7 +4,6 @@ import io.quarkus.cli.commands.file.GradleBuildFile; import io.quarkus.dependencies.Extension; import java.io.IOException; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -102,12 +101,7 @@ private BuildFile getBuildFileSafe(QuarkusCommandInvocation invocation) { } Map findInstalled(QuarkusCommandInvocation invocation) throws IOException { - final BuildFile buildFile = invocation.getBuildFile(false); - if (buildFile != null) { - return buildFile.findInstalled(); - } else { - return Collections.emptyMap(); - } + return invocation.getBuildFile().findInstalled(); } private boolean filterUnlisted(Extension e) { diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/QuarkusCommandInvocation.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/QuarkusCommandInvocation.java index b83636cd7dfc99..6853e34405dffe 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/QuarkusCommandInvocation.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/QuarkusCommandInvocation.java @@ -1,49 +1,45 @@ package io.quarkus.cli.commands; +import static com.google.common.base.Preconditions.checkNotNull; + import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; -import io.quarkus.generators.BuildTool; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; import io.quarkus.platform.tools.DefaultMessageWriter; import io.quarkus.platform.tools.MessageWriter; -import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Properties; public class QuarkusCommandInvocation extends ValueMap { protected final QuarkusPlatformDescriptor platformDescr; protected final MessageWriter log; - protected final Properties props; - private BuildTool buildTool; - private BuildFile buildFile; - private ProjectWriter writer; + private final BuildFile buildFile; + private final ProjectWriter writer; - public QuarkusCommandInvocation(QuarkusPlatformDescriptor platformDescr) { - this(platformDescr, new DefaultMessageWriter()); + public QuarkusCommandInvocation(QuarkusPlatformDescriptor platformDescr, ProjectWriter writer, + BuildFile buildFile) { + this(new HashMap<>(), platformDescr, writer, buildFile, new DefaultMessageWriter()); } - public QuarkusCommandInvocation(QuarkusPlatformDescriptor platformDescr, MessageWriter log) { - this(platformDescr, log, new HashMap<>(), new Properties(System.getProperties())); + public QuarkusCommandInvocation(Map values, QuarkusPlatformDescriptor platformDescr, ProjectWriter writer, + BuildFile buildFile) { + this(values, platformDescr, writer, buildFile, new DefaultMessageWriter()); } - public QuarkusCommandInvocation(QuarkusPlatformDescriptor platformDescr, MessageWriter log, Map values, - Properties props) { + public QuarkusCommandInvocation(Map values, QuarkusPlatformDescriptor platformDescr, ProjectWriter writer, + BuildFile buildFile, MessageWriter log) { super(values); - this.platformDescr = platformDescr; - this.log = log; - this.props = props; + this.platformDescr = checkNotNull(platformDescr, "platformDescr is required"); + this.log = checkNotNull(log, "log is required"); + this.writer = checkNotNull(writer, "writer is required"); + this.buildFile = checkNotNull(buildFile, "buildFile is required"); } public QuarkusCommandInvocation(QuarkusCommandInvocation original) { super(original.values); this.platformDescr = original.platformDescr; this.log = original.log; - this.props = new Properties(); - this.props.putAll(original.props); - this.buildTool = original.buildTool; this.buildFile = original.buildFile; this.writer = original.writer; } @@ -56,71 +52,12 @@ public QuarkusPlatformDescriptor getPlatformDescriptor() { return platformDescr; } - public String getProperty(String name) { - final String value = props.getProperty(name, NOT_SET); - return value == NOT_SET ? System.getProperty(name) : value; - } - - public boolean hasProperty(String name) { - return props.getOrDefault(name, NOT_SET) != NOT_SET; - } - - public QuarkusCommandInvocation setProperty(String name, String value) { - props.setProperty(name, value); - return this; - } - - public Properties getProperties() { - return props; - } - - public BuildTool getBuildTool() { - return buildTool; - } - - public void setBuildTool(BuildTool buildTool) { - this.buildTool = buildTool; - } - public BuildFile getBuildFile() { - return getBuildFile(true); - } - - BuildFile getBuildFile(boolean required) { - if (buildFile == null) { - if (writer == null) { - if (required) { - throw new IllegalStateException( - "Neither project's build file handler nor the project writer has been provided"); - } - return null; - } - if (buildTool == null) { - try { - buildFile = new MavenBuildFile(writer); - } catch (IOException e) { - throw new IllegalStateException("Failed to initialize project's build file handler", e); - } - } else { - try { - buildFile = buildTool.createBuildFile(writer); - } catch (IOException e) { - throw new IllegalStateException("Failed to initialize project's build file handler", e); - } - } - } return buildFile; } - public void setBuildFile(BuildFile buildFile) { - this.buildFile = buildFile; - } - public ProjectWriter getProjectWriter() { return writer; } - public void setProjectWriter(ProjectWriter writer) { - this.writer = writer; - } } diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/RemoveExtensions.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/RemoveExtensions.java index dfc5669e396e05..e23b8edcfff01c 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/RemoveExtensions.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/RemoveExtensions.java @@ -1,7 +1,6 @@ package io.quarkus.cli.commands; import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.generators.BuildTool; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; @@ -22,17 +21,17 @@ public class RemoveExtensions { private final QuarkusCommandInvocation invocation; public RemoveExtensions(final ProjectWriter writer, QuarkusPlatformDescriptor platformDescr) throws IOException { - this(new MavenBuildFile(writer), platformDescr); + this(writer, BuildTool.MAVEN, platformDescr); } public RemoveExtensions(final ProjectWriter writer, final BuildTool buildTool, QuarkusPlatformDescriptor platformDescr) throws IOException { - this(buildTool.createBuildFile(writer), platformDescr); + this(writer, buildTool.createBuildFile(writer), platformDescr); } - public RemoveExtensions(final BuildFile buildFile, QuarkusPlatformDescriptor platformDescr) { - invocation = new QuarkusCommandInvocation(platformDescr); - invocation.setBuildFile(buildFile); + public RemoveExtensions(final ProjectWriter writer, final BuildFile buildFile, QuarkusPlatformDescriptor platformDescr) + throws IOException { + invocation = new QuarkusCommandInvocation(platformDescr, writer, buildFile); } public RemoveExtensions extensions(Set extensions) { diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ValueMap.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ValueMap.java index 607a357f6610ea..7636d9a88b5c6e 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ValueMap.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/ValueMap.java @@ -37,6 +37,17 @@ public T getValue(String name, T defaultValue) { return (T) value; } + public String getStringValue(String name) { + final Object value = getValue(name, null); + if (value == null) { + return null; + } + if (value instanceof String) { + return ((String) value); + } + throw new IllegalStateException("value for '" + name + "' must be a String"); + } + public boolean getValue(String name, boolean defaultValue) { final Object value = getValue(name, null); if (value == null) { @@ -48,6 +59,10 @@ public boolean getValue(String name, boolean defaultValue) { return Boolean.parseBoolean(value.toString()); } + public Map getValues() { + return values; + } + public boolean valueIs(String name, Object o) { final Object value = values.get(name); return o == null ? value == null : o.equals(value); diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/BuildFile.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/BuildFile.java index 2602ca12fb525a..8c1ce02ee92741 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/BuildFile.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/BuildFile.java @@ -1,5 +1,6 @@ package io.quarkus.cli.commands.file; +import static com.google.common.base.Preconditions.checkNotNull; import static io.quarkus.maven.utilities.MojoUtils.credentials; import static java.util.stream.Collectors.toList; @@ -28,8 +29,8 @@ public abstract class BuildFile implements Closeable { private final BuildTool buildTool; public BuildFile(final ProjectWriter writer, BuildTool buildTool) { - this.writer = writer; - this.buildTool = buildTool; + this.writer = checkNotNull(writer, "writer is required"); + this.buildTool = checkNotNull(buildTool, "buildTool is required"); } protected void write(String fileName, String content) throws IOException { diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/legacy/LegacyQuarkusCommandInvocation.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/legacy/LegacyQuarkusCommandInvocation.java deleted file mode 100644 index 98e3c9afde66ca..00000000000000 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/legacy/LegacyQuarkusCommandInvocation.java +++ /dev/null @@ -1,32 +0,0 @@ -package io.quarkus.cli.commands.legacy; - -import io.quarkus.cli.commands.QuarkusCommandInvocation; -import io.quarkus.platform.tools.config.QuarkusPlatformConfig; -import java.util.Map; -import java.util.Properties; - -/** - * @deprecated since 1.3.0.CR1 - * Please use {@link QuarkusCommandInvocation} instead - */ -@Deprecated -public class LegacyQuarkusCommandInvocation extends QuarkusCommandInvocation { - - public LegacyQuarkusCommandInvocation() { - this(null); - } - - public LegacyQuarkusCommandInvocation(Map params) { - super(QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor(), - QuarkusPlatformConfig.getGlobalDefault().getMessageWriter(), - params, - new Properties(System.getProperties())); - if (params != null && !params.isEmpty()) { - for (Map.Entry entry : params.entrySet()) { - if (entry.getValue() != null) { - setProperty(entry.getKey(), entry.getValue().toString()); - } - } - } - } -} diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java index 2f07a880ed7c69..1440f30ad1f9c4 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java @@ -1,5 +1,7 @@ package io.quarkus.generators; +import static com.google.common.base.Preconditions.checkNotNull; + import io.quarkus.cli.commands.file.BuildFile; import io.quarkus.cli.commands.file.GradleBuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; @@ -53,6 +55,7 @@ public String getBuildDirectory() { } public BuildFile createBuildFile(final ProjectWriter writer) throws IOException { + checkNotNull(writer, "writer is required"); switch (this) { case GRADLE: return new GradleBuildFile(writer); diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java index ef9a66f4a60cc3..1579d6c9a1971b 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java @@ -1,10 +1,8 @@ package io.quarkus.generators; import io.quarkus.cli.commands.QuarkusCommandInvocation; -import io.quarkus.cli.commands.legacy.LegacyQuarkusCommandInvocation; import io.quarkus.cli.commands.writer.ProjectWriter; import java.io.IOException; -import java.util.Map; public interface ProjectGenerator { String BOM_GROUP_ID = "bom_groupId"; @@ -26,14 +24,5 @@ public interface ProjectGenerator { String getName(); - /** - * @deprecated since 1.3.0.CR1 - * Please use {@link #generate(ProjectWriter, QuarkusCommandInvocation)} instead. - */ - @Deprecated - default void generate(ProjectWriter writer, Map parameters) throws IOException { - generate(writer, new LegacyQuarkusCommandInvocation(parameters)); - } - void generate(ProjectWriter writer, QuarkusCommandInvocation invocation) throws IOException; } diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java index ad20146203a150..557c7f8a26a69e 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java @@ -4,7 +4,6 @@ import static java.lang.String.format; import io.quarkus.cli.commands.QuarkusCommandInvocation; -import io.quarkus.cli.commands.file.BuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; import io.quarkus.generators.BuildTool; import io.quarkus.generators.ProjectGenerator; @@ -61,11 +60,7 @@ private BasicRestProject(final ProjectWriter writer, QuarkusCommandInvocation in @SuppressWarnings("unchecked") private T get(final String key, final T defaultValue) { - Object value = invocation.getValue(key); - if (value == null) { - value = invocation.getProperty(key); - } - return value == null ? defaultValue : (T) value; + return invocation.getValue(key, defaultValue); } private boolean initProject() throws IOException { @@ -88,11 +83,11 @@ private boolean initProject() throws IOException { private boolean initBuildTool() throws IOException { BuildTool buildTool = getBuildTool(); - if (!invocation.hasProperty(ADDITIONAL_GITIGNORE_ENTRIES)) { - invocation.setProperty(ADDITIONAL_GITIGNORE_ENTRIES, buildTool.getGitIgnoreEntries()); + if (!invocation.hasValue(ADDITIONAL_GITIGNORE_ENTRIES)) { + invocation.setValue(ADDITIONAL_GITIGNORE_ENTRIES, buildTool.getGitIgnoreEntries()); } - if (!invocation.hasProperty(BUILD_DIRECTORY)) { - invocation.setProperty(BUILD_DIRECTORY, buildTool.getBuildDirectory()); + if (!invocation.hasValue(BUILD_DIRECTORY)) { + invocation.setValue(BUILD_DIRECTORY, buildTool.getBuildDirectory()); } boolean newProject = !writer.exists(buildTool.getDependenciesFile()); @@ -118,18 +113,17 @@ private boolean initBuildTool() throws IOException { } } if (gav[0] != null) { - invocation.setProperty(PROJECT_GROUP_ID, gav[0]); + invocation.setValue(PROJECT_GROUP_ID, gav[0]); } if (gav[1] != null) { - invocation.setProperty(PROJECT_ARTIFACT_ID, gav[1]); + invocation.setValue(PROJECT_ARTIFACT_ID, gav[1]); } } return newProject; } private BuildTool getBuildTool() { - BuildFile buildFileManager = get(BUILD_FILE, null); - return buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool(); + return invocation.getBuildFile().getBuildTool(); } private void generate(final String templateName, QuarkusCommandInvocation invocation, final String outputFilePath, @@ -140,15 +134,15 @@ private void generate(final String templateName, QuarkusCommandInvocation invoca if (template == null) { throw new IOException("Template resource is missing: " + templateName); } - for (Entry e : invocation.getProperties().entrySet()) { - if (e.getValue() != null) { // Exclude null values (classname and path can be null) - template = template.replace(format("${%s}", e.getKey().toString()), e.getValue().toString()); + for (Entry e : invocation.getValues().entrySet()) { + if (e.getValue() instanceof String) { // Exclude null values (classname and path can be null) + template = template.replace(format("${%s}", e.getKey()), (String) e.getValue()); } } // do some nasty replacements for Java target if we want to generate Java 11 projects - if ("11".equals(invocation.getProperty(JAVA_TARGET))) { - if (BuildTool.GRADLE.equals(invocation.getBuildTool())) { + if ("11".equals(invocation.getValue(JAVA_TARGET))) { + if (BuildTool.GRADLE.equals(invocation.getBuildFile().getBuildTool())) { template = template.replace("JavaVersion.VERSION_1_8", "JavaVersion.VERSION_11"); } else { template = template.replace("1.8", @@ -219,8 +213,8 @@ private void createApplicationConfig() throws IOException { } private void setupContext() throws IOException { - if (invocation.getProperty(CLASS_NAME) != null) { - String packageName = invocation.getProperty(PACKAGE_NAME); + if (invocation.getValue(CLASS_NAME) != null) { + String packageName = invocation.getStringValue(PACKAGE_NAME); if (packageName != null) { String packageDir = srcMainPath + '/' + packageName.replace('.', '/'); @@ -243,7 +237,7 @@ private void setupContext() throws IOException { } private void createClasses() throws IOException { - Object className = invocation.getProperty(CLASS_NAME); + Object className = invocation.getValue(CLASS_NAME); // If className is null we disable the generation of the JAX-RS resource. if (className != null) { String extension = type.getExtension(); diff --git a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/ListExtensionsTest.java b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/ListExtensionsTest.java index d73e92b0d3b970..f360a406a33dfa 100644 --- a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/ListExtensionsTest.java +++ b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/ListExtensionsTest.java @@ -7,12 +7,14 @@ import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.generators.BuildTool; import io.quarkus.maven.utilities.MojoUtils; import io.quarkus.maven.utilities.QuarkusDependencyPredicate; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Files; import java.util.HashSet; import java.util.Map; import org.apache.maven.model.Dependency; @@ -27,7 +29,7 @@ public void listWithBom() throws Exception { final FileProjectWriter writer = createNewProject(new File("target/list-extensions-test", "pom.xml")); addExtensions(writer, "commons-io:commons-io:2.5", "Agroal"); - final ListExtensions listExtensions = new ListExtensions(new MavenBuildFile(writer), getPlatformDescriptor()); + final ListExtensions listExtensions = new ListExtensions(writer, new MavenBuildFile(writer), getPlatformDescriptor()); final Map installed = listExtensions.findInstalled(); @@ -45,7 +47,7 @@ public void listWithBomExtensionWithSpaces() throws Exception { final FileProjectWriter writer = createNewProject(new File("target/list-extensions-test", "pom.xml")); addExtensions(writer, "resteasy", " hibernate-validator "); - final ListExtensions listExtensions = new ListExtensions(new MavenBuildFile(writer), getPlatformDescriptor()); + final ListExtensions listExtensions = new ListExtensions(writer, new MavenBuildFile(writer), getPlatformDescriptor()); final Map installed = listExtensions.findInstalled(); @@ -75,7 +77,7 @@ public void listWithoutBom() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final PrintStream printStream = new PrintStream(baos, false, "UTF-8")) { System.setOut(printStream); - new ListExtensions(new MavenBuildFile(writer), getPlatformDescriptor()) + new ListExtensions(writer, new MavenBuildFile(writer), getPlatformDescriptor()) .all(true) .format("full") .execute(); @@ -123,7 +125,7 @@ public void searchUnexpected() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final PrintStream printStream = new PrintStream(baos, false, "UTF-8")) { System.setOut(printStream); - new ListExtensions(new MavenBuildFile(writer), getPlatformDescriptor()) + new ListExtensions(writer, new MavenBuildFile(writer), getPlatformDescriptor()) .all(true) .format("full") .search("unexpectedSearch") @@ -145,7 +147,7 @@ public void searchRest() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final PrintStream printStream = new PrintStream(baos, false, "UTF-8")) { System.setOut(printStream); - new ListExtensions(new MavenBuildFile(writer), getPlatformDescriptor()) + new ListExtensions(writer, new MavenBuildFile(writer), getPlatformDescriptor()) .all(true) .format("full") .search("Rest") @@ -159,7 +161,9 @@ public void searchRest() throws Exception { @Test void testListExtensionsWithoutAPomFile() throws IOException { - ListExtensions listExtensions = new ListExtensions(null, getPlatformDescriptor()); + final FileProjectWriter writer = new FileProjectWriter(Files.createTempDirectory("proj").toFile()); + ListExtensions listExtensions = new ListExtensions(writer, + BuildTool.MAVEN.createBuildFile(writer), getPlatformDescriptor()); assertThat(listExtensions.findInstalled()).isEmpty(); } diff --git a/independent-projects/tools/common/src/test/java/io/quarkus/generators/rest/BasicRestProjectGeneratorTest.java b/independent-projects/tools/common/src/test/java/io/quarkus/generators/rest/BasicRestProjectGeneratorTest.java index a939761585735d..07023678de5e3a 100644 --- a/independent-projects/tools/common/src/test/java/io/quarkus/generators/rest/BasicRestProjectGeneratorTest.java +++ b/independent-projects/tools/common/src/test/java/io/quarkus/generators/rest/BasicRestProjectGeneratorTest.java @@ -16,16 +16,21 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; import io.quarkus.bootstrap.util.IoUtils; import io.quarkus.cli.commands.PlatformAwareTestBase; import io.quarkus.cli.commands.QuarkusCommandInvocation; import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.cli.commands.writer.ProjectWriter; +import io.quarkus.generators.BuildTool; import io.quarkus.generators.SourceType; import io.quarkus.maven.utilities.MojoUtils; import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; @@ -38,15 +43,16 @@ class BasicRestProjectGeneratorTest extends PlatformAwareTestBase { - private final QuarkusCommandInvocation BASIC_PROJECT_CONTEXT = new QuarkusCommandInvocation(getPlatformDescriptor()) - .setProperty(PROJECT_GROUP_ID, "org.example") - .setProperty(PROJECT_ARTIFACT_ID, "quarkus-app") - .setProperty(PROJECT_VERSION, "0.0.1-SNAPSHOT") - .setProperty(BOM_VERSION, getBomVersion()) - .setProperty(PACKAGE_NAME, "org.example") - .setProperty(CLASS_NAME, "ExampleResource") - .setProperty("path", "/hello") - .setValue(SOURCE_TYPE, SourceType.JAVA); + private final Map BASIC_PROJECT_CONTEXT = Maps.newHashMap(ImmutableMap. builder() + .put(PROJECT_GROUP_ID, "org.example") + .put(PROJECT_ARTIFACT_ID, "quarkus-app") + .put(PROJECT_VERSION, "0.0.1-SNAPSHOT") + .put(BOM_VERSION, getBomVersion()) + .put(PACKAGE_NAME, "org.example") + .put(CLASS_NAME, "ExampleResource") + .put("path", "/hello") + .put(SOURCE_TYPE, SourceType.JAVA) + .build()); @Test @Timeout(2) @@ -58,7 +64,8 @@ void generateMultipleTimes() throws InterruptedException { List> collect = IntStream.range(0, 20).boxed().map(i -> (Callable) () -> { final File file = Files.createTempDirectory("test").toFile(); try (FileProjectWriter writer = new FileProjectWriter(file)) { - basicRestProjectGenerator.generate(writer, BASIC_PROJECT_CONTEXT); + basicRestProjectGenerator.generate(writer, + createQuarkusCommandInvocation(writer)); } finally { IoUtils.recursiveDelete(file.toPath()); } @@ -77,7 +84,8 @@ void generateFilesWithJaxRsResource() throws Exception { when(mockWriter.mkdirs(anyString())).thenAnswer(invocationOnMock -> invocationOnMock.getArgument(0, String.class)); - basicRestProjectGenerator.generate(mockWriter, BASIC_PROJECT_CONTEXT); + basicRestProjectGenerator.generate(mockWriter, + createQuarkusCommandInvocation(mockWriter)); verify(mockWriter, times(10)).mkdirs(anyString()); verify(mockWriter, times(3)).mkdirs(""); @@ -116,7 +124,7 @@ void generateFilesWithSpringControllerResource() throws Exception { when(mockWriter.mkdirs(anyString())).thenAnswer(invocationOnMock -> invocationOnMock.getArgument(0, String.class)); - QuarkusCommandInvocation springContext = new QuarkusCommandInvocation(BASIC_PROJECT_CONTEXT); + QuarkusCommandInvocation springContext = createQuarkusCommandInvocation(mockWriter); springContext.setValue(IS_SPRING, Boolean.TRUE); basicRestProjectGenerator.generate(mockWriter, springContext); @@ -127,4 +135,9 @@ void generateFilesWithSpringControllerResource() throws Exception { } + private QuarkusCommandInvocation createQuarkusCommandInvocation(ProjectWriter writer) throws IOException { + return new QuarkusCommandInvocation(BASIC_PROJECT_CONTEXT, getPlatformDescriptor(), + writer, BuildTool.MAVEN.createBuildFile(writer)); + } + }