Skip to content

Commit

Permalink
Merge pull request #34065 from ia3andy/update-info
Browse files Browse the repository at this point in the history
Add support for project Java version update based on extensions
  • Loading branch information
gsmet authored Aug 3, 2023
2 parents f55273b + 0d28057 commit 7a72b87
Show file tree
Hide file tree
Showing 44 changed files with 1,091 additions and 650 deletions.
2 changes: 1 addition & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/CreateApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import io.quarkus.cli.create.TargetGAVGroup;
import io.quarkus.cli.create.TargetLanguageGroup;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.handlers.CreateJBangProjectCommandHandler;
import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.SourceType;
import picocli.CommandLine;

@CommandLine.Command(name = "app", header = "Create a Quarkus application project.", description = "%n"
Expand Down
2 changes: 1 addition & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/CreateCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import io.quarkus.cli.create.TargetGAVGroup;
import io.quarkus.cli.create.TargetLanguageGroup;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.handlers.CreateJBangProjectCommandHandler;
import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.SourceType;
import picocli.CommandLine;

@CommandLine.Command(name = "cli", header = "Create a Quarkus command-line project.", description = "%n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.cli.create;

import static io.quarkus.devtools.commands.CreateProjectHelper.computeJavaVersion;
import static io.quarkus.devtools.project.JavaVersion.computeJavaVersion;

import java.nio.file.Path;
import java.util.Collection;
Expand All @@ -17,10 +17,10 @@
import io.quarkus.cli.registry.ToggleRegistryClientMixin;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.registry.RegistryResolutionException;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.stream.Collectors;

import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.SourceType;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.ParameterException;
Expand All @@ -17,13 +17,13 @@ public class TargetLanguageGroup {

static class VersionCandidates extends ArrayList<String> {
VersionCandidates() {
super(CreateProjectHelper.JAVA_VERSIONS_LTS.stream().map(String::valueOf).collect(Collectors.toList()));
super(JavaVersion.JAVA_VERSIONS_LTS.stream().map(String::valueOf).collect(Collectors.toList()));
}
}

@CommandLine.Option(names = {
"--java" }, description = "Target Java version.\n Valid values: ${COMPLETION-CANDIDATES}", completionCandidates = VersionCandidates.class, defaultValue = CreateProjectHelper.DETECT_JAVA_RUNTIME_VERSION)
String javaVersion = CreateProjectHelper.DETECT_JAVA_RUNTIME_VERSION;
"--java" }, description = "Target Java version.\n Valid values: ${COMPLETION-CANDIDATES}", completionCandidates = VersionCandidates.class, defaultValue = JavaVersion.DETECT_JAVA_RUNTIME_VERSION)
String javaVersion = JavaVersion.DETECT_JAVA_RUNTIME_VERSION;

@CommandLine.Option(names = { "--kotlin" }, description = "Use Kotlin")
boolean kotlin = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.quarkus.cli.common.TargetQuarkusPlatformGroup;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.maven.dependency.ArtifactCoords;
Expand Down Expand Up @@ -62,7 +63,7 @@ public QuarkusProject createQuarkusProject(Path projectRoot, TargetQuarkusPlatfo
+ "Use the maven/gradle plugins when working with Quarkus 1.x projects.");
}
catalog = CreateProjectHelper.completeCatalog(catalog, extensions, QuarkusProjectHelper.artifactResolver());
return QuarkusProjectHelper.getProject(projectRoot, catalog, buildTool, log);
return QuarkusProjectHelper.getProject(projectRoot, catalog, buildTool, JavaVersion.NA, log);
}

ExtensionCatalog getExtensionCatalog(TargetQuarkusPlatformGroup targetVersion, OutputOptionMixin log)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import org.gradle.api.GradleException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.compile.JavaCompile;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.buildfile.BuildFile;
Expand Down Expand Up @@ -118,7 +121,6 @@ protected String quarkusCoreVersion() {
}

protected QuarkusProject getQuarkusProject(boolean limitExtensionsToImportedPlatforms) {

final GradleMessageWriter log = messageWriter();
final ExtensionCatalog catalog = extensionsCatalog(limitExtensionsToImportedPlatforms, log);

Expand All @@ -136,7 +138,17 @@ protected QuarkusProject getQuarkusProject(boolean limitExtensionsToImportedPlat
throw new GradleException(
"Mixed DSL is not supported. Both build and settings file need to use either Kotlin or Groovy DSL");
}
return QuarkusProjectHelper.getProject(getProject().getProjectDir().toPath(), catalog, buildFile, log);
final JavaVersion javaVersion = resolveProjectJavaVersion();
return QuarkusProjectHelper.getProject(getProject().getProjectDir().toPath(), catalog, buildFile, javaVersion, log);
}

private JavaVersion resolveProjectJavaVersion() {
TaskCollection<JavaCompile> compileTasks = getProject().getTasks().withType(JavaCompile.class);
if (compileTasks.isEmpty()) {
return JavaVersion.NA;
}
final JavaCompile task = compileTasks.iterator().next();
return new JavaVersion(task.getTargetCompatibility());
}

protected GradleMessageWriter messageWriter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.maven.utilities.MojoUtils;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void execute() throws MojoExecutionException {
.artifactResolver(mvn)
.build();
final CreateJBangProject createJBangProject = new CreateJBangProject(QuarkusProject.of(projectDirPath, catalog,
codestartsResourceLoader, log, BuildTool.MAVEN))
codestartsResourceLoader, log, BuildTool.MAVEN, new JavaVersion(javaVersion)))
.extensions(extensions)
.javaVersion(javaVersion)
.setValue(NO_JBANG_WRAPPER, noJBangWrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.maven.components.MavenVersionEnforcer;
Expand Down Expand Up @@ -297,7 +298,7 @@ public void execute() throws MojoExecutionException {
.artifactResolver(mvn)
.build();
QuarkusProject newProject = QuarkusProject.of(projectDirPath, catalog,
codestartsResourceLoader, log, buildToolEnum);
codestartsResourceLoader, log, buildToolEnum, new JavaVersion(javaVersion));
final CreateProject createProject = new CreateProject(newProject)
.groupId(projectGroupId)
.artifactId(projectArtifactId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.buildfile.MavenProjectBuildFile;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void execute() throws MojoExecutionException {
final List<ResourceLoader> codestartsResourceLoader = getCodestartResourceLoaders(resolveExtensionCatalog());
quarkusProject = QuarkusProject.of(baseDir(), resolveExtensionCatalog(),
codestartsResourceLoader,
log, buildTool);
log, buildTool, JavaVersion.NA);
}

doExecute(quarkusProject, getMessageWriter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import io.quarkus.devtools.codestarts.CodestartProjectInputBuilder;
import io.quarkus.devtools.codestarts.DataKey;
import io.quarkus.devtools.codestarts.utils.NestedMaps;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.extensions.Extensions;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
Expand Down Expand Up @@ -86,7 +86,7 @@ public boolean noJBangWrapper() {
public QuarkusJBangCodestartProjectInput build() {
if (!this.containsData("java")) {
this.addData(NestedMaps
.unflatten(Map.of("java.version", String.valueOf(CreateProjectHelper.determineBestJavaLtsVersion()))));
.unflatten(Map.of("java.version", String.valueOf(JavaVersion.determineBestJavaLtsVersion()))));
}
return new QuarkusJBangCodestartProjectInput(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.devtools.commands;

import static io.quarkus.devtools.commands.CreateProjectHelper.computeJavaVersion;
import static io.quarkus.devtools.project.JavaVersion.computeJavaVersion;
import static java.util.Objects.requireNonNull;

import java.util.HashMap;
Expand All @@ -14,6 +14,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.commands.handlers.CreateJBangProjectCommandHandler;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.SourceType;

public class CreateJBangProject {
public interface CreateJBangProjectKey {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.quarkus.devtools.commands;

import static io.quarkus.devtools.commands.CreateProject.CreateProjectKey.*;
import static io.quarkus.devtools.commands.CreateProjectHelper.computeJavaVersion;
import static io.quarkus.devtools.commands.handlers.CreateProjectCodestartDataConverter.PlatformPropertiesKey.QUARKUS_GRADLE_PLUGIN_VERSION;
import static io.quarkus.devtools.commands.handlers.CreateProjectCodestartDataConverter.PlatformPropertiesKey.QUARKUS_MAVEN_PLUGIN_VERSION;
import static io.quarkus.devtools.project.JavaVersion.computeJavaVersion;
import static java.util.Objects.requireNonNull;

import java.util.Collections;
Expand All @@ -20,6 +20,7 @@
import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.platform.tools.ToolsUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.lang.model.SourceVersion;
Expand All @@ -25,20 +21,14 @@
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.paths.PathTree;
import io.quarkus.registry.catalog.Extension;
import io.quarkus.registry.catalog.ExtensionCatalog;

public class CreateProjectHelper {

// ordering is important here, so let's keep them ordered
public static final SortedSet<Integer> JAVA_VERSIONS_LTS = new TreeSet<>(List.of(11, 17));
public static final int DEFAULT_JAVA_VERSION = 11;
private static final int MAX_LTS_SUPPORTED_BY_KOTLIN = 17;
public static final String DETECT_JAVA_RUNTIME_VERSION = "<<detect java runtime version>>";
private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile("(\\d+)(?:\\..*)?");

public static final String DEFAULT_GROUP_ID = "org.acme";
public static final String DEFAULT_ARTIFACT_ID = "code-with-quarkus";
public static final String DEFAULT_VERSION = "1.0.0-SNAPSHOT";
Expand Down Expand Up @@ -177,45 +167,6 @@ public static Path createOutputDirectory(String targetDirectory) {
return outputPath;
}

public static String computeJavaVersion(SourceType sourceType, String inputJavaVersion) {
Integer javaFeatureVersionTarget = null;

if (inputJavaVersion != null && !DETECT_JAVA_RUNTIME_VERSION.equals(inputJavaVersion)) {
// Probably too much as we should push only the feature version but let's be as thorough as we used to be
Matcher matcher = JAVA_VERSION_PATTERN.matcher(inputJavaVersion);
if (matcher.matches()) {
javaFeatureVersionTarget = Integer.valueOf(matcher.group(1));
}
}

if (javaFeatureVersionTarget == null) {
javaFeatureVersionTarget = Runtime.version().feature();
}

int bestJavaLtsVersion = determineBestJavaLtsVersion(javaFeatureVersionTarget);

if (SourceType.KOTLIN.equals(sourceType)
&& bestJavaLtsVersion > MAX_LTS_SUPPORTED_BY_KOTLIN) {
bestJavaLtsVersion = MAX_LTS_SUPPORTED_BY_KOTLIN;
}
return String.valueOf(bestJavaLtsVersion);
}

public static int determineBestJavaLtsVersion() {
return determineBestJavaLtsVersion(Runtime.version().feature());
}

public static int determineBestJavaLtsVersion(int runtimeVersion) {
int bestLtsVersion = DEFAULT_JAVA_VERSION;
for (int ltsVersion : JAVA_VERSIONS_LTS) {
if (ltsVersion > runtimeVersion) {
break;
}
bestLtsVersion = ltsVersion;
}
return bestLtsVersion;
}

public static Set<String> sanitizeExtensions(Set<String> extensions) {
if (extensions == null) {
return extensions = Set.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import io.quarkus.devtools.codestarts.CodestartType;
import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog;
import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartProjectInput;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.commands.handlers.CreateProjectCodestartDataConverter.CatalogKey;
import io.quarkus.devtools.messagewriter.MessageIcons;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.extensions.Extensions;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.platform.tools.ToolsUtils;
Expand Down Expand Up @@ -232,7 +232,7 @@ private static void addOrigins(final List<ExtensionOrigins> extOrigins, Extensio

private void checkMinimumJavaVersion(String javaVersionString, List<Extension> extensions) throws QuarkusCommandException {
final List<Extension> incompatibleExtensions = new ArrayList<>();
final int javaVersion = javaVersionString == null ? CreateProjectHelper.DEFAULT_JAVA_VERSION
final int javaVersion = javaVersionString == null ? JavaVersion.DEFAULT_JAVA_VERSION
: Integer.parseInt(javaVersionString);
for (Extension extension : extensions) {
Integer extMinJavaVersion = getMinimumJavaVersion(extension);
Expand Down
Loading

0 comments on commit 7a72b87

Please sign in to comment.