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 packages names and content
- 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) -> quarkusio#9875
- Removed compatibility with project without the Quarkus platform bom defined
- Remove SetupIT which mostly duplicate CreateProjectMojoIT
  • Loading branch information
ia3andy committed Jun 9, 2020
1 parent 4dc464a commit 036f5cd
Show file tree
Hide file tree
Showing 77 changed files with 1,918 additions and 2,021 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.aesh.io.Resource;

import io.quarkus.dependencies.Extension;
import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.aesh.command.option.Option;
import org.aesh.io.Resource;

import io.quarkus.devtools.commands.CreateProject;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.aesh.command.option.Option;
import org.aesh.io.Resource;

import io.quarkus.devtools.commands.ListExtensions;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.aesh.io.Resource;

import io.quarkus.dependencies.Extension;
import io.quarkus.devtools.commands.RemoveExtensions;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import io.quarkus.platform.tools.config.QuarkusPlatformConfig;
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 @@ -21,9 +21,9 @@

import com.google.common.collect.ImmutableMap;

import io.quarkus.cli.commands.CreateProject;
import io.quarkus.devtools.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.project.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 @@ -11,7 +11,7 @@
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import io.quarkus.cli.commands.AddExtensions;
import io.quarkus.devtools.commands.AddExtensions;

public class QuarkusAddExtension extends QuarkusPlatformTask {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.gradle.api.tasks.TaskAction;
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;
import io.quarkus.devtools.commands.ListExtensions;

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
@@ -1,5 +1,6 @@
package io.quarkus.gradle.tasks;

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

Expand All @@ -10,10 +11,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.project.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 +66,12 @@ protected QuarkusPlatformDescriptor platformDescriptor() {
}

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

@Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import io.quarkus.cli.commands.RemoveExtensions;
import io.quarkus.devtools.commands.RemoveExtensions;

public class QuarkusRemoveExtension extends QuarkusPlatformTask {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
Expand All @@ -14,13 +13,13 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.maven.model.Dependency;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.devtools.writer.FileProjectWriter;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;

class GradleBuildFileTest {

Expand All @@ -31,7 +30,8 @@ public static void beforeAll() throws URISyntaxException {
URL url = GradleBuildFileTest.class.getClassLoader().getResource("gradle-project");
URI uri = url.toURI();
Path gradleProjectPath = Paths.get(uri);
buildFile = new GradleBuildFileFromConnector(new FileProjectWriter(gradleProjectPath.toFile()));
final QuarkusPlatformDescriptor descriptor = Mockito.mock(QuarkusPlatformDescriptor.class);
buildFile = new GradleBuildFileFromConnector(gradleProjectPath, descriptor);
}

@Test
Expand Down Expand Up @@ -62,20 +62,4 @@ void testGetTestClosure() throws IOException {
assertNull(buildFile.getProperty("test"));
}

@Test
void testFindInstalled() throws IOException {
Map<String, Dependency> installed = buildFile.findInstalled();
assertNotNull(installed);
assertThat(installed).isNotEmpty();

Dependency jsonb = installed.get("io.quarkus:quarkus-jsonb");
assertThat(jsonb).extracting("version").isEqualTo("0.23.2");

Dependency jsonp = installed.get("io.quarkus:quarkus-jsonp");
assertThat(jsonp).extracting("version").isEqualTo("0.23.2");

Dependency resteasy = installed.get("io.quarkus:quarkus-resteasy");
assertThat(resteasy).extracting("version").isEqualTo("0.23.2");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import io.quarkus.cli.commands.AddExtensions;
import io.quarkus.cli.commands.QuarkusCommandOutcome;
import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.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.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 @@ -162,19 +160,7 @@ public void execute() throws MojoExecutionException {
File pom = new File(projectRoot, "pom.xml");

if (pom.isFile()) {
// Enforce that the GAV are not set
if (!StringUtils.isBlank(projectGroupId) || !StringUtils.isBlank(projectArtifactId)
|| !StringUtils.isBlank(projectVersion)) {
throw new MojoExecutionException("Unable to generate the project, the `projectGroupId`, " +
"`projectArtifactId` and `projectVersion` parameters are not supported when applied to an " +
"existing `pom.xml` file");
}

// Load the GAV from the existing project
projectGroupId = project.getGroupId();
projectArtifactId = project.getArtifactId();
projectVersion = project.getVersion();

throw new MojoExecutionException("Unable to generate the project in a directory that already contains a pom.xml");
} else {
askTheUserForMissingValues();
projectRoot = new File(outputDirectory, projectArtifactId);
Expand Down Expand Up @@ -214,11 +200,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 @@ -4,7 +4,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import io.quarkus.cli.commands.ListExtensions;
import io.quarkus.devtools.commands.ListExtensions;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.tools.MessageWriter;

Expand All @@ -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
Loading

0 comments on commit 036f5cd

Please sign in to comment.