Skip to content

Commit

Permalink
Fix Maven and Gradle update commands and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky authored and ia3andy committed Feb 15, 2023
1 parent 75204de commit ad950ca
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 58 deletions.
35 changes: 22 additions & 13 deletions devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.commands.ListCategories;
import io.quarkus.devtools.commands.ListExtensions;
import io.quarkus.devtools.commands.ProjectInfo;
import io.quarkus.devtools.commands.RemoveExtensions;
import io.quarkus.devtools.commands.UpdateProject;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
Expand Down Expand Up @@ -133,22 +131,33 @@ public Integer removeExtension(RunModeOption runMode, Set<String> extensions) th

@Override
public Integer projectInfo(boolean perModule) throws Exception {
final ProjectInfo invoker = new ProjectInfo(quarkusProject());
invoker.perModule(perModule);
invoker.appModel(MavenProjectBuildFile.resolveApplicationModel(projectRoot));
return invoker.execute().isSuccess() ? CommandLine.ExitCode.OK : CommandLine.ExitCode.SOFTWARE;
ArrayDeque<String> args = new ArrayDeque<>();
setMavenProperties(args, true);
args.add("quarkus:info");
if (perModule) {
args.add("-DperModule");
}
args.add("-ntp");
return run(prependExecutable(args));
}

@Override
public Integer updateProject(String targetPlatformVersion, String targetPlatformStreamId, boolean perModule)
throws Exception {
final UpdateProject invoker = new UpdateProject(quarkusProject());
invoker.latestCatalog(quarkusProject().getExtensionsCatalog());
// TODO ALEXEY: resolve targetPlatformVersion from targetPlatformStreamId if needed or from latest version
invoker.targetPlatformVersion(targetPlatformVersion);
invoker.perModule(perModule);
invoker.appModel(MavenProjectBuildFile.resolveApplicationModel(projectRoot));
return invoker.execute().isSuccess() ? CommandLine.ExitCode.OK : CommandLine.ExitCode.SOFTWARE;
ArrayDeque<String> args = new ArrayDeque<>();
setMavenProperties(args, true);
args.add("quarkus:update");
if (targetPlatformVersion != null) {
args.add("-DplatformVersion=" + targetPlatformVersion);
}
if (targetPlatformStreamId != null) {
args.add("-DstreamId=" + targetPlatformStreamId);
}
if (perModule) {
args.add("-DperModule");
}
args.add("-ntp");
return run(prependExecutable(args));
}

@Override
Expand Down
12 changes: 5 additions & 7 deletions devtools/cli/src/test/java/io/quarkus/cli/CliVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ public class CliVersionTest {

@Test
public void testCommandVersion() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "version");

CliDriver.Result result = CliDriver.execute(workspaceRoot, "-v");
result.echoSystemOut();
Assertions.assertEquals(result.stdout.trim(), System.getProperty("project.version"),
"Version output for command aliases should be the same.");

CliDriver.Result result2 = CliDriver.execute(workspaceRoot, "-v");
CliDriver.Result result2 = CliDriver.execute(workspaceRoot, "--version");
Assertions.assertEquals(result.stdout, result2.stdout, "Version output for command aliases should be the same.");
CliDriver.println("-- same as above\n\n");

CliDriver.Result result3 = CliDriver.execute(workspaceRoot, "--version");
Assertions.assertEquals(result.stdout, result3.stdout, "Version output for command aliases should be the same.");
CliDriver.println("-- same as above\n\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void testMissalignedPlatformExtensionVersion() throws Exception {
assertRegistryExtensions(infoResult.stdout, "registry.acme.org",
ArtifactCoords.jar("org.acme", "acme-quarkiverse-extension", "1.0"));

final CliDriver.Result rectifyResult = run(projectDir, "update", "--rectify");
final CliDriver.Result rectifyResult = run(projectDir, "update", "--platform-version=1.0.0");
assertThat(rectifyResult.stdout)
.contains("[INFO] Update: org.acme.quarkus.platform:acme-quarkus-subatomic:1.0.0 -> remove version (managed)");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.quarkus.gradle.tasks;

import java.util.List;

import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import io.quarkus.devtools.commands.UpdateProject;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.catalog.PlatformStreamCoords;

public class QuarkusUpdate extends QuarkusPlatformTask {

Expand All @@ -26,6 +32,7 @@ public void setPerModule(boolean perModule) {
}

@Input
@Optional
public String getTargetStreamId() {
return targetStreamId;
}
Expand All @@ -36,6 +43,7 @@ public void setStreamId(String targetStreamId) {
}

@Input
@Optional
public String getTargetPlatformVersion() {
return targetPlatformVersion;
}
Expand All @@ -55,15 +63,29 @@ public void logUpdates() {
getProject().getLogger().warn(getName() + " is experimental, its options and output might change in future versions");

final QuarkusProject quarkusProject = getQuarkusProject(false);
final UpdateProject invoker = new UpdateProject(quarkusProject);
final ExtensionCatalog targetCatalog;
try {
invoker.latestCatalog(getExtensionCatalogResolver(quarkusProject.log()).resolveExtensionCatalog());
if (targetPlatformVersion != null) {
var targetPrimaryBom = getPrimaryBom(quarkusProject.getExtensionsCatalog());
targetPrimaryBom = ArtifactCoords.pom(targetPrimaryBom.getGroupId(), targetPrimaryBom.getArtifactId(),
targetPlatformVersion);
targetCatalog = getExtensionCatalogResolver(quarkusProject.log())
.resolveExtensionCatalog(List.of(targetPrimaryBom));
} else if (targetStreamId != null) {
var platformStream = PlatformStreamCoords.fromString(targetStreamId);
targetCatalog = getExtensionCatalogResolver(quarkusProject.log()).resolveExtensionCatalog(platformStream);
targetPlatformVersion = getPrimaryBom(targetCatalog).getVersion();
} else {
targetCatalog = getExtensionCatalogResolver(quarkusProject.log()).resolveExtensionCatalog();
targetPlatformVersion = getPrimaryBom(targetCatalog).getVersion();
}
} catch (RegistryResolutionException e) {
throw new GradleException(
"Failed to resolve the latest Quarkus extension catalog from the configured extension registries", e);
throw new RuntimeException(
"Failed to resolve the recommended Quarkus extension catalog from the configured extension registries", e);
}

// TODO ALEXEY: resolve targetPlatformVersion from targetPlatformStreamId if needed or from latest version
final UpdateProject invoker = new UpdateProject(quarkusProject);
invoker.latestCatalog(targetCatalog);
invoker.targetPlatformVersion(targetPlatformVersion);
invoker.perModule(perModule);
invoker.appModel(extension().getApplicationModel());
Expand All @@ -73,4 +95,8 @@ public void logUpdates() {
throw new GradleException("Failed to resolve recommended updates", e);
}
}

private static ArtifactCoords getPrimaryBom(ExtensionCatalog c) {
return c.getDerivedFrom().isEmpty() ? c.getBom() : c.getDerivedFrom().get(0).getBom();
}
}
31 changes: 26 additions & 5 deletions devtools/maven/src/main/java/io/quarkus/maven/UpdateMojo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.quarkus.maven;

import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import io.quarkus.devtools.commands.UpdateProject;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.catalog.PlatformStreamCoords;

/**
* Log Quarkus-related recommended updates, such as new Quarkus platform BOM versions and
Expand Down Expand Up @@ -49,15 +54,27 @@ protected void validateParameters() throws MojoExecutionException {
@Override
protected void processProjectState(QuarkusProject quarkusProject) throws MojoExecutionException {

final UpdateProject invoker = new UpdateProject(quarkusProject);
final ExtensionCatalog targetCatalog;
try {
invoker.latestCatalog(getExtensionCatalogResolver().resolveExtensionCatalog());
if (platformVersion != null) {
var targetPrimaryBom = getPrimaryBom(quarkusProject.getExtensionsCatalog());
targetPrimaryBom = ArtifactCoords.pom(targetPrimaryBom.getGroupId(), targetPrimaryBom.getArtifactId(),
platformVersion);
targetCatalog = getExtensionCatalogResolver().resolveExtensionCatalog(List.of(targetPrimaryBom));
} else if (streamId != null) {
var platformStream = PlatformStreamCoords.fromString(streamId);
targetCatalog = getExtensionCatalogResolver().resolveExtensionCatalog(platformStream);
platformVersion = getPrimaryBom(targetCatalog).getVersion();
} else {
targetCatalog = getExtensionCatalogResolver().resolveExtensionCatalog();
platformVersion = getPrimaryBom(targetCatalog).getVersion();
}
} catch (RegistryResolutionException e) {
throw new MojoExecutionException(
"Failed to resolve the latest Quarkus extension catalog from the configured extension registries", e);
"Failed to resolve the recommended Quarkus extension catalog from the configured extension registries", e);
}

// TODO ALEXEY: resolve targetPlatformVersion from streamId if needed or from latest version
final UpdateProject invoker = new UpdateProject(quarkusProject);
invoker.latestCatalog(targetCatalog);
invoker.targetPlatformVersion(platformVersion);
invoker.perModule(perModule);
invoker.appModel(resolveApplicationModel());
Expand All @@ -68,4 +85,8 @@ protected void processProjectState(QuarkusProject quarkusProject) throws MojoExe
throw new MojoExecutionException("Failed to resolve the available updates", e);
}
}

private static ArtifactCoords getPrimaryBom(ExtensionCatalog c) {
return c.getDerivedFrom().isEmpty() ? c.getBom() : c.getDerivedFrom().get(0).getBom();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class ProjectInfoCommandHandler implements QuarkusCommandHandler {
@Override
public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws QuarkusCommandException {

// TODO ALEXEY: info about the project (versions and extensions) and hint on how to repair if broken (or to update if available - nice to have)

final ApplicationModel appModel = invocation.getValue(ProjectInfo.APP_MODEL);
final boolean logStatePerModule = invocation.getValue(ProjectInfo.PER_MODULE, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
return QuarkusCommandOutcome.failure();
}
if (Objects.equals(projectQuarkusPlatformBom.getVersion(), targetPlatformVersion)) {
invocation.log().info("Instructions to repair this project:");
// TODO ALEXEY: if the targetPlatformVersion is equal to the project platform version, we should give instructions repair the project
ProjectInfoCommandHandler.logState(currentState, perModule, true, invocation.getQuarkusProject().log());

} else {
invocation.log().info("Instructions to update this project from '%s' to '%s':",
projectQuarkusPlatformBom.getVersion(), targetPlatformVersion);
// TODO ALEXEY: instructions to update the project
logUpdates(currentState, latestCatalog, false, perModule,
invocation.getQuarkusProject().log());
logUpdates(currentState, latestCatalog, false, perModule, invocation.getQuarkusProject().log());
}

if (generateRewriteConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;

import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.resolver.AppModelResolverException;
import io.quarkus.bootstrap.resolver.BootstrapAppModelResolver;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
Expand Down Expand Up @@ -71,21 +67,6 @@ public static QuarkusProject getProject(Path projectDir, MessageWriter log, Supp
defaultQuarkusVersion);
}

public static ApplicationModel resolveApplicationModel(Path projectDir) throws MojoExecutionException {
final MavenArtifactResolver mvnResolver = getMavenResolver(projectDir);
final LocalProject currentProject = mvnResolver.getMavenContext().getCurrentProject();
try {
return new BootstrapAppModelResolver(mvnResolver)
.resolveModel(ArtifactCoords.pom(currentProject.getGroupId(), currentProject.getArtifactId(),
currentProject.getVersion()));
} catch (AppModelResolverException e) {
throw new MojoExecutionException("Failed to resolve the Quarkus application model for project "
+ ArtifactCoords.pom(currentProject.getGroupId(), currentProject.getArtifactId(),
currentProject.getVersion()),
e);
}
}

public static QuarkusProject getProject(Artifact projectPom, Model projectModel, Path projectDir,
Properties projectProps, MavenArtifactResolver artifactResolver, MessageWriter log,
Supplier<String> defaultQuarkusVersion) throws RegistryResolutionException {
Expand Down

0 comments on commit ad950ca

Please sign in to comment.