Skip to content

Commit

Permalink
Use the value of project/default-codestart from the platform descript…
Browse files Browse the repository at this point in the history
…or as the default codestart instead of a hardcoded value

(cherry picked from commit b5a84eb)
  • Loading branch information
aloubyansky authored and gsmet committed Mar 15, 2024
1 parent ba06007 commit 7b9737f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ protected Collection<Codestart> select(QuarkusCodestartProjectInput projectInput
.collect(Collectors.toCollection(ArrayList::new));

// include default codestarts depending on the versions and the extensions being chosen or not
Optional<ExtensionCodestart> selectedDefaultCodeStart = getSelectedDefaultCodeStart(projectInput);
Optional<String> selectedDefaultCodeStart = getSelectedDefaultCodeStart(projectInput);

if (projectInput.getAppContent().contains(CODE)
&& selectedDefaultCodeStart.isPresent()
&& projectCodestarts.stream()
.noneMatch(c -> c.getType() == CodestartType.CODE && !c.getSpec().isPreselected())) {
final Codestart defaultCodestart = codestarts.stream()
.filter(c -> c.matches(selectedDefaultCodeStart.get().key()))
.filter(c -> c.matches(selectedDefaultCodeStart.get()))
.findFirst().orElseThrow(() -> new CodestartStructureException(
selectedDefaultCodeStart.get().key() + " codestart not found"));
selectedDefaultCodeStart.get() + " codestart not found"));
final String languageName = findLanguageName(projectCodestarts);
if (defaultCodestart.implementsLanguage(languageName)) {
projectCodestarts.add(defaultCodestart);
Expand Down Expand Up @@ -178,7 +178,7 @@ protected Collection<Codestart> select(QuarkusCodestartProjectInput projectInput
return projectCodestarts;
}

private Optional<ExtensionCodestart> getSelectedDefaultCodeStart(QuarkusCodestartProjectInput projectInput) {
private Optional<String> getSelectedDefaultCodeStart(QuarkusCodestartProjectInput projectInput) {
// This is very hackyish, we need a better data structure to do better
Optional<ArtifactCoords> quarkusBom = projectInput.getBoms().stream()
.map(ArtifactCoords::fromString)
Expand All @@ -195,13 +195,17 @@ private Optional<ExtensionCodestart> getSelectedDefaultCodeStart(QuarkusCodestar
if (projectInput.getExtensions().isEmpty() ||
(projectInput.getExtensions().size() == 1
&& isLanguageExtension(projectInput.getExtensions().iterator().next()))) {
return Optional.of(ExtensionCodestart.RESTEASY_REACTIVE);
var defaultCodestart = projectInput.getDefaultCodestart();
if (defaultCodestart == null) {
defaultCodestart = QuarkusCodestartCatalog.ExtensionCodestart.RESTEASY_REACTIVE.key();
}
return Optional.of(defaultCodestart);
}

return Optional.empty();
}

return Optional.of(ExtensionCodestart.RESTEASY);
return Optional.of(ExtensionCodestart.RESTEASY.key());
}

private boolean isCoreBom(ArtifactCoords artifactCoords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public final class QuarkusCodestartProjectInput extends CodestartProjectInput {
private final Collection<ArtifactCoords> extensions;
private final Collection<ArtifactCoords> platforms;
private final String example;
private Set<AppContent> appContent;
private final Set<AppContent> appContent;
private final String defaultCodestart;

public QuarkusCodestartProjectInput(QuarkusCodestartProjectInputBuilder builder) {
super(builder);
Expand All @@ -24,6 +25,7 @@ public QuarkusCodestartProjectInput(QuarkusCodestartProjectInputBuilder builder)
this.example = builder.example;
this.buildTool = requireNonNull(builder.buildTool, "buildTool is required");
this.appContent = builder.appContent;
this.defaultCodestart = builder.defaultCodestart;
}

public static QuarkusCodestartProjectInputBuilder builder() {
Expand All @@ -49,4 +51,8 @@ public Set<AppContent> getAppContent() {
public BuildTool getBuildTool() {
return buildTool;
}

public String getDefaultCodestart() {
return defaultCodestart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class QuarkusCodestartProjectInputBuilder extends CodestartProjectInputBu
Set<AppContent> appContent = new HashSet<>(FULL_CONTENT);
String example;
BuildTool buildTool = BuildTool.MAVEN;
String defaultCodestart;

QuarkusCodestartProjectInputBuilder() {
super();
Expand Down Expand Up @@ -146,6 +147,13 @@ public QuarkusCodestartProjectInputBuilder buildTool(BuildTool buildTool) {
return this;
}

public QuarkusCodestartProjectInputBuilder defaultCodestart(String defaultCodestart) {
if (defaultCodestart != null) {
this.defaultCodestart = defaultCodestart;
}
return this;
}

public QuarkusCodestartProjectInput build() {
return new QuarkusCodestartProjectInput(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
.addData(toCodestartData(invocation.getValues()))
.addData(invocation.getValue(DATA, Collections.emptyMap()))
.messageWriter(invocation.log())
.defaultCodestart(getDefaultCodestart(mainCatalog))
.build();
invocation.log().info("-----------");
if (!extensionsToAdd.isEmpty()) {
Expand Down Expand Up @@ -249,4 +250,20 @@ private void checkMinimumJavaVersion(String javaVersionString, List<Extension> e
.format("Some extensions are not compatible with the selected Java version (%s):\n %s", javaVersion, list));
}
}

private static String getDefaultCodestart(ExtensionCatalog catalog) {
var map = catalog.getMetadata();
if (map != null && !map.isEmpty()) {
var projectMetadata = map.get("project");
if (projectMetadata instanceof Map) {
var defaultCodestart = ((Map<?, ?>) projectMetadata).get("default-codestart");
if (defaultCodestart != null) {
if (defaultCodestart instanceof String) {
return defaultCodestart.toString();
}
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Map;

import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog;
import io.quarkus.registry.catalog.ExtensionCatalog;
Expand Down Expand Up @@ -36,4 +37,17 @@ public static ExtensionCatalog newFakeExtensionCatalog() {
throw new UncheckedIOException(e);
}
}

public static String getDefaultCodestart() {
var map = FAKE_EXTENSION_CATALOG.getMetadata().get("project");
if (map != null) {
if (map instanceof Map) {
var defaultCodestart = ((Map<?, ?>) map).get("default-codestart");
if (defaultCodestart instanceof String) {
return defaultCodestart.toString();
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
],
"metadata": {
"project": {
"default-codestart": "resteasy-reactive",
"properties": {
"doc-root": "https://quarkus.io",
"rest-assured-version": "4.3.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.quarkus.devtools.codestarts.CodestartProjectDefinition;
import io.quarkus.devtools.codestarts.CodestartType;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.testing.FakeExtensionCatalog;
import io.quarkus.devtools.testing.SnapshotTesting;
import io.quarkus.maven.dependency.ArtifactKey;

Expand Down Expand Up @@ -47,7 +48,7 @@ void loadTest() throws IOException {

@Test
void createProjectTestEmpty() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.noCode()
.noBuildToolWrapper()
.noDockerfiles()
Expand All @@ -68,7 +69,7 @@ void createProjectTestEmpty() throws IOException {

@Test
void createProjectTestNoExample() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.noCode()
.build();
final CodestartProjectDefinition projectDefinition = getCatalog().createProject(input);
Expand All @@ -88,7 +89,7 @@ void createProjectTestNoExample() throws IOException {

@Test
void createProjectTestGradle() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.buildTool(BuildTool.GRADLE)
.build();
final CodestartProjectDefinition projectDefinition = getCatalog().createProject(input);
Expand All @@ -98,7 +99,7 @@ void createProjectTestGradle() throws IOException {

@Test
void createProjectTestKotlin() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-kotlin"))
.build();
final CodestartProjectDefinition projectDefinition = getCatalog().createProject(input);
Expand All @@ -108,7 +109,7 @@ void createProjectTestKotlin() throws IOException {

@Test
void prepareProjectTestScala() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-scala"))
.build();
final CodestartProjectDefinition projectDefinition = getCatalog().createProject(input);
Expand All @@ -118,7 +119,7 @@ void prepareProjectTestScala() throws IOException {

@Test
void prepareProjectTestConfigYaml() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-config-yaml"))
.build();
final CodestartProjectDefinition projectDefinition = getCatalog().createProject(input);
Expand All @@ -128,7 +129,7 @@ void prepareProjectTestConfigYaml() throws IOException {

@Test
void prepareProjectTestResteasy() throws IOException {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-resteasy"))
.build();
final CodestartProjectDefinition projectDefinition = getCatalog().createProject(input);
Expand All @@ -141,6 +142,10 @@ void prepareProjectTestResteasy() throws IOException {
"resteasy-codestart");
}

private static QuarkusCodestartProjectInputBuilder newInputBuilder() {
return QuarkusCodestartProjectInput.builder().defaultCodestart(FakeExtensionCatalog.getDefaultCodestart());
}

private QuarkusCodestartCatalog getCatalog() throws IOException {
return FAKE_QUARKUS_CODESTART_CATALOG;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.TestInfo;

import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.testing.FakeExtensionCatalog;
import io.quarkus.devtools.testing.SnapshotTesting;
import io.quarkus.devtools.testing.codestarts.QuarkusCodestartTesting;
import io.quarkus.maven.dependency.ArtifactCoords;
Expand All @@ -38,7 +39,7 @@ private Map<String, Object> getGenerationTestInputData() {

@Test
void generateDefault(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.noCode()
.noDockerfiles()
.noBuildToolWrapper()
Expand All @@ -60,9 +61,13 @@ void generateDefault(TestInfo testInfo) throws Throwable {
assertThat(projectDir.resolve("src/main/java")).exists().isEmptyDirectory();
}

private static QuarkusCodestartProjectInputBuilder newInputBuilder() {
return QuarkusCodestartProjectInput.builder().defaultCodestart(FakeExtensionCatalog.getDefaultCodestart());
}

@Test
void generateRESTEasyJavaCustom(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addData(getGenerationTestInputData())
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-resteasy"))
.putData(PROJECT_PACKAGE_NAME.key(), "com.andy")
Expand Down Expand Up @@ -101,7 +106,7 @@ void verifyIndexExtensionList(TestInfo testInfo) throws Throwable {

@Test
void generateMavenWithCustomDep(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addData(getGenerationTestInputData())
.addBoms(QuarkusCodestartTesting.getPlatformBoms())
.addExtension(ArtifactCoords.fromString("io.quarkus:quarkus-resteasy:1.8"))
Expand All @@ -127,7 +132,7 @@ void generateMavenWithCustomDep(TestInfo testInfo) throws Throwable {

@Test
void generateRESTEasyKotlinCustom(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addData(getGenerationTestInputData())
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-resteasy"))
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-kotlin"))
Expand Down Expand Up @@ -160,7 +165,7 @@ void generateRESTEasyKotlinCustom(TestInfo testInfo) throws Throwable {

@Test
void generateRESTEasyScalaCustom(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addData(getGenerationTestInputData())
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-resteasy"))
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-scala"))
Expand Down Expand Up @@ -193,7 +198,7 @@ void generateRESTEasyScalaCustom(TestInfo testInfo) throws Throwable {

@Test
void generateMavenDefaultJava(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addData(getGenerationTestInputData())
.build();
final Path projectDir = testDirPath.resolve("maven-default-java");
Expand All @@ -211,7 +216,7 @@ void generateMavenDefaultJava(TestInfo testInfo) throws Throwable {

@Test
void generateGradleDefaultJava(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.buildTool(BuildTool.GRADLE)
.addData(getGenerationTestInputData())
.build();
Expand All @@ -230,7 +235,7 @@ void generateGradleDefaultJava(TestInfo testInfo) throws Throwable {

@Test
void generateMavenResteasyJava(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-resteasy"))
.addData(getGenerationTestInputData())
.build();
Expand All @@ -249,7 +254,7 @@ void generateMavenResteasyJava(TestInfo testInfo) throws Throwable {

@Test
void generateMavenConfigYamlJava(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.addExtension(ArtifactKey.fromString("io.quarkus:quarkus-config-yaml"))
.addData(getGenerationTestInputData())
.build();
Expand All @@ -264,7 +269,7 @@ void generateMavenConfigYamlJava(TestInfo testInfo) throws Throwable {

@Test
public void generateGradleWrapperGithubAction(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.buildTool(BuildTool.GRADLE)
.addData(getGenerationTestInputData())
.addCodestarts(Collections.singletonList("tooling-github-action"))
Expand All @@ -280,7 +285,7 @@ public void generateGradleWrapperGithubAction(TestInfo testInfo) throws Throwabl

@Test
public void generateGradleNoWrapperGithubAction(TestInfo testInfo) throws Throwable {
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
final QuarkusCodestartProjectInput input = newInputBuilder()
.buildTool(BuildTool.GRADLE)
.noBuildToolWrapper()
.addData(getGenerationTestInputData())
Expand Down

0 comments on commit 7b9737f

Please sign in to comment.