Skip to content

Commit

Permalink
Refactor and Introduce ExtensionsManager in CodeGen
Browse files Browse the repository at this point in the history
This is part of quarkusio#8178

- Improved general readability and consistency
- Added ExtensionManager to define a high level way of managing (read/write) extensions in any QuarkusProject
- Removed most unsafe Gradle operations which were outside of the Gradle plugin (we need to figure out a way to improve the "generic" gradle support to make it compatible again..)
- Removed existing project support with the create command (throws an error)
- Removed compatibility with project without the Quarkus platform bom defined
  • Loading branch information
ia3andy committed Jun 8, 2020
1 parent 61d80b8 commit e5a0d02
Show file tree
Hide file tree
Showing 62 changed files with 1,390 additions and 1,344 deletions.
8 changes: 4 additions & 4 deletions devtools/aesh/src/main/java/io/quarkus/cli/QuarkusCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
import org.aesh.command.validator.CommandValidatorException;
import org.aesh.command.validator.OptionValidatorException;

import io.quarkus.cli.commands.QuarkusCommand;
import io.quarkus.cli.commands.QuarkusCommandHandler;

public class QuarkusCli {

public static void main(String[] args) throws CommandRegistryException {
CommandRuntime runtime = AeshCommandRuntimeBuilder
.builder()
.commandRegistry(AeshCommandRegistryBuilder.builder().command(QuarkusCommand.class).create())
.commandRegistry(AeshCommandRegistryBuilder.builder().command(QuarkusCommandHandler.class).create())
.build();

if (args.length > 0) {
StringBuilder sb = new StringBuilder(QuarkusCommand.COMMAND_NAME).append(" ");
StringBuilder sb = new StringBuilder(QuarkusCommandHandler.COMMAND_NAME).append(" ");
if (args.length == 1) {
sb.append(args[0]);
} else {
Expand Down Expand Up @@ -54,6 +54,6 @@ private static void showHelpIfNeeded(CommandRuntime runtime, Exception e) {
if (e != null) {
System.err.println(e.getMessage());
}
System.err.println(runtime.commandInfo(QuarkusCommand.COMMAND_NAME));
System.err.println(runtime.commandInfo(QuarkusCommandHandler.COMMAND_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import io.quarkus.cli.commands.data.QuarkusCommandOutcome;
import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author <a href="mailto:[email protected]">Ståle Pedersen</a>
*/
@GroupCommandDefinition(name = QuarkusCommand.COMMAND_NAME, groupCommands = { ListExtensionsCommand.class,
@GroupCommandDefinition(name = QuarkusCommandHandler.COMMAND_NAME, groupCommands = { ListExtensionsCommand.class,
AddExtensionCommand.class,
CreateProjectCommand.class }, description = "<command> [<args>] \n\nThese are the common quarkus commands used in various situations")
public class QuarkusCommand implements Command<CommandInvocation> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import io.quarkus.cli.commands.data.QuarkusCommandOutcome;
import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class AddExtensionToModuleInMultiModuleProjectTest extends QuarkusGradleT
public void testBasicMultiModuleBuild() throws Exception {

final File projectDir = getProjectDir("add-extension-multi-module");

BuildResult build = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import io.quarkus.cli.commands.CreateProject;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.generators.SourceType;
import io.quarkus.devtools.project.codegen.SourceType;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;
import io.quarkus.test.devmode.util.DevModeTestUtils;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.gradle;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -13,33 +14,35 @@
import org.gradle.tooling.model.eclipse.EclipseExternalDependency;
import org.gradle.tooling.model.eclipse.EclipseProject;

import io.quarkus.devtools.buildfile.GradleBuildFile;
import io.quarkus.devtools.writer.ProjectWriter;
import io.quarkus.devtools.buildfile.AbstractGradleBuildFile;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

public class GradleBuildFileFromConnector extends GradleBuildFile {
public class GradleBuildFileFromConnector extends AbstractGradleBuildFile {

private List<Dependency> dependencies = null;

public GradleBuildFileFromConnector(ProjectWriter writer) {
super(writer);
// we need to initialize here since there is no other single point of entry
public GradleBuildFileFromConnector(final Path projectFolderPath, final QuarkusPlatformDescriptor platformDescriptor) {
super(projectFolderPath, platformDescriptor);
}

public GradleBuildFileFromConnector(Path projectFolderPath, QuarkusPlatformDescriptor platformDescriptor,
Path rootProjectPath) {
super(projectFolderPath, platformDescriptor, rootProjectPath);
}

@Override
public List<Dependency> getDependencies() throws IOException {
if (dependencies == null) {
EclipseProject eclipseProject = null;
if (getBuildContent() != null) {
if (getWriter().hasFile()) {
try {
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(getWriter().getProjectFolder())
.connect();
eclipseProject = connection.getModel(EclipseProject.class);
} catch (BuildException e) {
// ignore this error.
e.printStackTrace();
}
try {
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(getProjectFolderPath().toFile())
.connect();
eclipseProject = connection.getModel(EclipseProject.class);
} catch (BuildException e) {
// ignore this error.
e.printStackTrace();
}
}
if (eclipseProject != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import org.gradle.api.tasks.options.Option;

import io.quarkus.cli.commands.ListExtensions;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.writer.FileProjectWriter;
import io.quarkus.gradle.GradleBuildFileFromConnector;

public class QuarkusListExtensions extends QuarkusPlatformTask {

Expand Down Expand Up @@ -58,11 +55,7 @@ public QuarkusListExtensions() {
@TaskAction
public void listExtensions() {
try {
final GradleBuildFileFromConnector buildFileFromConnector = new GradleBuildFileFromConnector(
new FileProjectWriter(getProject().getProjectDir()));
QuarkusProject quarkusProject = QuarkusProject.of(getProject().getProjectDir().toPath(), platformDescriptor(),
buildFileFromConnector);
new ListExtensions(quarkusProject)
new ListExtensions(getQuarkusProject())
.all(isAll())
.format(getFormat())
.search(getSearchPattern())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.Internal;

import io.quarkus.devtools.buildfile.GradleBuildFile;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.writer.FileProjectWriter;
import io.quarkus.devtools.writer.ProjectWriter;
import io.quarkus.devtools.buildfile.BuildFile;
import io.quarkus.gradle.GradleBuildFileFromConnector;
import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.platform.descriptor.resolver.json.QuarkusJsonPlatformDescriptorResolver;
Expand Down Expand Up @@ -66,12 +65,11 @@ protected QuarkusPlatformDescriptor platformDescriptor() {
}

@Internal
protected GradleBuildFile getGradleBuildFile() {
final ProjectWriter writer = new FileProjectWriter(getProject().getProjectDir());
protected BuildFile getGradleBuildFile() {
return getProject().getParent() == null
? new GradleBuildFile(writer)
: new GradleBuildFile(writer,
new FileProjectWriter(getProject().getRootProject().getProjectDir()));
? new GradleBuildFileFromConnector(getProject().getProjectDir().toPath(), platformDescriptor())
: new GradleBuildFileFromConnector(getProject().getProjectDir().toPath(), platformDescriptor(),
getProject().getRootProject().getProjectDir().toPath());
}

@Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.quarkus.devtools.writer.FileProjectWriter;
import io.quarkus.devtools.project.codegen.writer.FileProjectWriter;

class GradleBuildFileTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.apache.maven.plugins.annotations.Parameter;

import io.quarkus.cli.commands.AddExtensions;
import io.quarkus.cli.commands.QuarkusCommandOutcome;
import io.quarkus.cli.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.tools.MessageWriter;

Expand All @@ -22,7 +22,7 @@
* parameters.
*/
@Mojo(name = "add-extension")
public class AddExtensionMojo extends BuildFileMojoBase {
public class AddExtensionMojo extends QuarkusProjectMojoBase {

/**
* The list of extensions to be added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@

import io.quarkus.bootstrap.resolver.AppModelResolverException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.cli.commands.AddExtensions;
import io.quarkus.cli.commands.CreateProject;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.generators.SourceType;
import io.quarkus.devtools.project.codegen.SourceType;
import io.quarkus.maven.components.MavenVersionEnforcer;
import io.quarkus.maven.components.Prompter;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
Expand Down Expand Up @@ -214,11 +212,6 @@ public void execute() throws MojoExecutionException {
success = createProject.execute().isSuccess();

File createdDependenciesBuildFile = new File(projectRoot, buildToolEnum.getDependenciesFile());
if (success) {
success = new AddExtensions(QuarkusProject.of(projectFolderPath, platform, buildToolEnum))
.extensions(extensions).execute()
.isSuccess();
}
if (BuildTool.MAVEN.equals(buildToolEnum)) {
createMavenWrapper(createdDependenciesBuildFile, ToolsUtils.readQuarkusProperties(platform));
} else if (BuildTool.GRADLE.equals(buildToolEnum)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You can list all extension or just installable. Choose between 3 output formats: name, concise and full.
*/
@Mojo(name = "list-extensions", requiresProject = false)
public class ListExtensionsMojo extends BuildFileMojoBase {
public class ListExtensionsMojo extends QuarkusProjectMojoBase {

/**
* List all extensions or just the installable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.quarkus.maven;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
Expand All @@ -19,19 +21,16 @@

import io.quarkus.bootstrap.resolver.BootstrapAppModelResolver;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.buildfile.BuildFile;
import io.quarkus.devtools.buildfile.MavenBuildFile;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.writer.FileProjectWriter;
import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.platform.descriptor.resolver.json.QuarkusJsonPlatformDescriptorResolver;
import io.quarkus.platform.tools.MessageWriter;
import io.quarkus.platform.tools.ToolsConstants;
import io.quarkus.platform.tools.maven.MojoMessageWriter;

public abstract class BuildFileMojoBase extends AbstractMojo {
public abstract class QuarkusProjectMojoBase extends AbstractMojo {

@Parameter(defaultValue = "${project}")
protected MavenProject project;
Expand Down Expand Up @@ -60,37 +59,31 @@ public void execute() throws MojoExecutionException {
// Validate Mojo parameters
validateParameters();

final MessageWriter log = new MojoMessageWriter(getLog());
final Path projectFolderPath = project.getBasedir().toPath();
final BuildTool buildTool = QuarkusProject.resolveExistingProjectBuildTool(projectFolderPath);
final QuarkusPlatformDescriptor platformDescriptor = resolvePlatformDescriptor(log);

doExecute(QuarkusProject.of(project.getBasedir().toPath(), platformDescriptor, buildTool), log);
}

private QuarkusPlatformDescriptor resolvePlatformDescriptor(final MessageWriter log) throws MojoExecutionException {
// Resolve and setup the platform descriptor
final MavenArtifactResolver mvn;
try {
mvn = MavenArtifactResolver.builder().setRepositorySystem(repoSystem).setRepositorySystemSession(repoSession)
final MavenArtifactResolver mvn = MavenArtifactResolver.builder().setRepositorySystem(repoSystem)
.setRepositorySystemSession(repoSession)
.setRemoteRepositories(repos).build();
} catch (Exception e) {
throw new MojoExecutionException("Failed to initialize maven artifact resolver", e);
}

final MessageWriter log = new MojoMessageWriter(getLog());

QuarkusPlatformDescriptor platformDescr = null;
BuildTool buildTool = null;
BuildFile buildFile = null;
try (FileProjectWriter fileProjectWriter = new FileProjectWriter(project.getBasedir())) {
if (project.getFile() != null) {
// Maven project
final MavenBuildFile mvnBuild = new MavenBuildFile(fileProjectWriter);
buildTool = BuildTool.MAVEN;
buildFile = new MavenBuildFile(fileProjectWriter);

final List<Artifact> descrArtifactList = new ArrayList<>(2);
for (Dependency dep : mvnBuild.getManagedDependencies()) {
for (Dependency dep : getManagedDependencies()) {
if ((dep.getScope() == null || !dep.getScope().equals("import"))
&& (dep.getType() == null || !dep.getType().equals("pom"))) {
continue;
}
// We don't know which BOM is the platform one, so we are trying every BOM here
final String bomVersion = resolveValue(dep.getVersion(), buildFile);
final String bomGroupId = resolveValue(dep.getGroupId(), buildFile);
final String bomArtifactId = resolveValue(dep.getArtifactId(), buildFile);
final String bomVersion = resolveValue(dep.getVersion());
final String bomGroupId = resolveValue(dep.getGroupId());
final String bomArtifactId = resolveValue(dep.getArtifactId());
if (bomVersion == null || bomGroupId == null || bomArtifactId == null) {
continue;
}
Expand All @@ -102,28 +95,19 @@ public void execute() throws MojoExecutionException {
}
if (!descrArtifactList.isEmpty()) {
if (descrArtifactList.size() == 1) {
platformDescr = loadPlatformDescriptor(mvn, log, descrArtifactList.get(0));
return loadPlatformDescriptor(mvn, log, descrArtifactList.get(0));
} else {
final CombinedQuarkusPlatformDescriptor.Builder builder = CombinedQuarkusPlatformDescriptor.builder();
for (Artifact descrArtifact : descrArtifactList) {
builder.addPlatform(loadPlatformDescriptor(mvn, log, descrArtifact));
}
platformDescr = builder.build();
return builder.build();
}
}
} else if (new File(project.getBasedir(), "build.gradle").exists()
|| new File(project.getBasedir(), "build.gradle.kts").exists()) {
// Gradle project
buildTool = BuildTool.GRADLE;
}

if (platformDescr == null) {
platformDescr = CreateUtils.resolvePlatformDescriptor(bomGroupId, bomArtifactId, bomVersion, mvn, getLog());
}

doExecute(QuarkusProject.of(project.getBasedir().toPath(), platformDescr, buildTool), log);
} catch (IOException e) {
throw new MojoExecutionException("Failed to initialize project reading tools", e);
return CreateUtils.resolvePlatformDescriptor(bomGroupId, bomArtifactId, bomVersion, mvn, getLog());
} catch (Exception e) {
throw new MojoExecutionException("Failed to initialize maven artifact resolver", e);
}
}

Expand Down Expand Up @@ -166,10 +150,10 @@ protected void validateParameters() throws MojoExecutionException {
protected abstract void doExecute(QuarkusProject quarkusProject, MessageWriter log)
throws MojoExecutionException;

private String resolveValue(String expr, BuildFile buildFile) throws IOException {
private String resolveValue(String expr) throws IOException {
if (expr.startsWith("${") && expr.endsWith("}")) {
final String name = expr.substring(2, expr.length() - 1);
final String v = buildFile.getProperty(name);
final String v = project.getModel().getProperties().getProperty(name);
if (v == null) {
if (getLog().isDebugEnabled()) {
getLog().debug("Failed to resolve property " + name);
Expand All @@ -179,4 +163,10 @@ private String resolveValue(String expr, BuildFile buildFile) throws IOException
}
return expr;
}

private List<Dependency> getManagedDependencies() throws IOException {
final DependencyManagement managed = project.getModel().getDependencyManagement();
return managed != null ? managed.getDependencies()
: Collections.emptyList();
}
}
Loading

0 comments on commit e5a0d02

Please sign in to comment.