Skip to content

Commit

Permalink
Improve consistency in codegen
Browse files Browse the repository at this point in the history
This is part of quarkusio#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
  • Loading branch information
ia3andy authored and johnaohara committed Jun 29, 2020
1 parent 8b7cfe6 commit 1af4c01
Show file tree
Hide file tree
Showing 28 changed files with 218 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.aesh.io.Resource;

import io.quarkus.cli.commands.writer.FileProjectWriter;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;

/**
* @author <a href="mailto:[email protected]">Ståle Pedersen</a>
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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()) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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.ToolsConstants;
import io.quarkus.platform.tools.ToolsUtils;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;
import java.io.IOException;
import java.util.Set;

Expand All @@ -22,69 +23,25 @@ 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<String> extensions) {
invocation.setValue(EXTENSIONS, extensions);
return this;
}

/**
* @deprecated in 1.3.0.CR1
* Please call {@link #extensions(Set)} and then {@link #execute()}
*/
@Deprecated
public AddExtensionResult addExtensions(final Set<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 1af4c01

Please sign in to comment.