Skip to content

Commit

Permalink
Merge pull request #17564 from aloubyansky/new-platform-model
Browse files Browse the repository at this point in the history
Support for resolving extension catalog given platform stream, align member BOM imports using quarkus.platform.group-id and quarkus.platform.version
  • Loading branch information
aloubyansky authored Jun 15, 2021
2 parents 99fbfab + 254166d commit b39ecfa
Show file tree
Hide file tree
Showing 24 changed files with 347 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,29 @@ public ExtensionCatalog getExtensionCatalog(TargetQuarkusVersionGroup targetVers
log.debug("Resolving Quarkus extension catalog for " + targetVersion);
QuarkusProjectHelper.setMessageWriter(log);

if (targetVersion.isPlatformSpecified()) {
ArtifactCoords coords = targetVersion.getPlatformBom();
return ToolsUtils.resolvePlatformDescriptorDirectly(coords.getGroupId(), coords.getArtifactId(),
coords.getVersion(), QuarkusProjectHelper.artifactResolver(), log);
}

ExtensionCatalogResolver catalogResolver = QuarkusProjectHelper.getCatalogResolver(enableRegistryClient, log);

try {
if (!catalogResolver.hasRegistries()) {
log.debug("Falling back to direct resolution of the platform bom");

// Fall back to previous methods of finding registries (e.g. client has been disabled)
if (targetVersion.isPlatformSpecified()) {
ArtifactCoords coords = targetVersion.getPlatformBom();
return ToolsUtils.resolvePlatformDescriptorDirectly(coords.getGroupId(), coords.getArtifactId(),
coords.getVersion(), QuarkusProjectHelper.artifactResolver(), log);
} else {
return ToolsUtils.resolvePlatformDescriptorDirectly(null, null, Version.clientVersion(),
QuarkusProjectHelper.artifactResolver(), log);
}
return ToolsUtils.resolvePlatformDescriptorDirectly(null, null, Version.clientVersion(),
QuarkusProjectHelper.artifactResolver(), log);
}

// if (targetVersion.isPlatformSpecified()) {
// } else {
// }
if (targetVersion.isStream()) {
final String stream = targetVersion.getStream();
final int colon = stream.indexOf(':');
final String platformKey = colon <= 0 ? null : stream.substring(0, colon);
final String streamId = colon < 0 ? stream : stream.substring(colon + 1);
return catalogResolver.resolveExtensionCatalog(platformKey, streamId);
}

return catalogResolver.resolveExtensionCatalog();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TargetBuildToolGroup {
@CommandLine.Option(names = { "--gradle" }, description = "Use Gradle")
boolean gradle = false;

@CommandLine.Option(names = { "--grade-kotlin-dsl" }, description = "Use Gradle with Kotlin DSL")
@CommandLine.Option(names = { "--gradle-kotlin-dsl" }, description = "Use Gradle with Kotlin DSL")
boolean gradleKotlinDsl = false;

public boolean isBuildless() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TargetQuarkusVersionGroup {
CommandSpec spec;

@CommandLine.Option(paramLabel = "STREAM", names = { "-S",
"--stream" }, description = "A target stream, e.g. default, snapshot", defaultValue = "default", hidden = true)
"--stream" }, description = "A target stream, e.g. default, snapshot", hidden = true)
String stream;

@CommandLine.Option(paramLabel = "groupId:artifactId:version", names = { "-p",
Expand All @@ -38,7 +38,7 @@ public ArtifactCoords getPlatformBom() {
}

public boolean isStream() {
return platformBom == null;
return stream != null;
}

public String getStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ val quarkusPlatformArtifactId: String by project
val quarkusPlatformVersion: String by project

dependencies {
{#each boms}
{#if it.groupId == quarkus.platform.group-id && it.version == quarkus.platform.version}
{#if it.artifactId == quarkus.platform.artifact-id}
implementation(enforcedPlatform("$\{quarkusPlatformGroupId}:$\{quarkusPlatformArtifactId}:$\{quarkusPlatformVersion}"))
{#for bom in boms}
implementation(enforcedPlatform("{bom}"))
{/for}
{#else}
implementation(enforcedPlatform("$\{quarkusPlatformGroupId}:{it.artifactId}:$\{quarkusPlatformVersion}"))
{/if}
{#else}
implementation(enforcedPlatform("{it.groupId}:{it.artifactId}:{it.version}"))
{/if}
{/each}
{#for dep in dependencies}
implementation("{dep}")
{/for}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ repositories {

{#insert dependencies}
dependencies {
{#each boms}
{#if it.groupId == quarkus.platform.group-id && it.version == quarkus.platform.version}
{#if it.artifactId == quarkus.platform.artifact-id}
implementation enforcedPlatform("$\{quarkusPlatformGroupId}:$\{quarkusPlatformArtifactId}:$\{quarkusPlatformVersion}")
{#for bom in boms}
implementation enforcedPlatform('{bom}')
{/for}
{#else}
implementation enforcedPlatform("$\{quarkusPlatformGroupId}:{it.artifactId}:$\{quarkusPlatformVersion}")
{/if}
{#else}
implementation enforcedPlatform('{it.groupId}:{it.artifactId}:{it.version}')
{/if}
{/each}
{#for dep in dependencies}
implementation '{dep}'
{/for}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<quarkus.platform.group-id>{quarkus.platform.group-id}</quarkus.platform.group-id>
<quarkus.platform.artifact-id>{quarkus.platform.artifact-id}</quarkus.platform.artifact-id>
<quarkus.platform.version>{quarkus.platform.version}</quarkus.platform.version>
{#if quarkus.maven-plugin.version != quarkus.platform.version || quarkus.maven-plugin.group-id != quarkus.platform.group-id}
<quarkus-plugin.version>{quarkus.maven-plugin.version}</quarkus-plugin.version>
{/if}
<compiler-plugin.version>{maven-compiler-plugin.version}</compiler-plugin.version>
<surefire-plugin.version>{maven-surefire-plugin.version}</surefire-plugin.version>
{#if uberjar}
Expand All @@ -25,18 +27,23 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>$\{quarkus.platform.group-id}</groupId>
<artifactId>$\{quarkus.platform.artifact-id}</artifactId>
<version>$\{quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
{#each boms}
<dependency>
{#if it.groupId == quarkus.platform.group-id && it.version == quarkus.platform.version}
<groupId>$\{quarkus.platform.group-id}</groupId>
{#else}
<groupId>{it.groupId}</groupId>
{/if}
{#if it.artifactId == quarkus.platform.artifact-id}
<artifactId>$\{quarkus.platform.artifact-id}</artifactId>
{#else}
<artifactId>{it.artifactId}</artifactId>
{/if}
{#if it.groupId == quarkus.platform.group-id && it.version == quarkus.platform.version}
<version>$\{quarkus.platform.version}</version>
{#else}
<version>{it.version}</version>
{/if}
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -110,9 +117,17 @@
<plugins>
<!-- Quarkus build plugin - also provides the quarkus:dev live-reload feature -->
<plugin>
{#if quarkus.maven-plugin.version != quarkus.platform.version || quarkus.maven-plugin.group-id != quarkus.platform.group-id}
<groupId>{quarkus.maven-plugin.group-id}</groupId>
{#else}
<groupId>$\{quarkus.platform.group-id}</groupId>
{/if}
<artifactId>{quarkus.maven-plugin.artifact-id}</artifactId>
{#if quarkus.maven-plugin.version != quarkus.platform.version || quarkus.maven-plugin.group-id != quarkus.platform.group-id}
<version>$\{quarkus-plugin.version}</version>
{#else}
<version>$\{quarkus.platform.version}</version>
{/if}
<extensions>true</extensions>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public QuarkusCodestartProjectInputBuilder addData(Map<String, Object> data) {
return this;
}

@Override
public QuarkusCodestartProjectInputBuilder addBoms(Collection<String> boms) {
super.addBoms(boms);
return this;
}

@Override
public QuarkusCodestartProjectInputBuilder putData(String key, Object value) {
super.putData(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static io.quarkus.devtools.commands.CreateProject.NO_BUILDTOOL_WRAPPER;
import static io.quarkus.devtools.commands.CreateProject.NO_CODE;
import static io.quarkus.devtools.commands.CreateProject.NO_DOCKERFILES;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeCoordsFromQuery;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeExtensionsFromQuery;
import static io.quarkus.devtools.project.codegen.ProjectGenerator.APP_CONFIG;
import static io.quarkus.devtools.project.codegen.ProjectGenerator.BOM_ARTIFACT_ID;
import static io.quarkus.devtools.project.codegen.ProjectGenerator.BOM_GROUP_ID;
Expand All @@ -25,10 +25,12 @@
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.messagewriter.MessageIcons;
import io.quarkus.devtools.project.codegen.ProjectGenerator;
import io.quarkus.devtools.project.extensions.Extensions;
import io.quarkus.maven.ArtifactCoords;
import io.quarkus.platform.tools.ToolsUtils;
import io.quarkus.registry.catalog.Extension;
import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.catalog.ExtensionOrigin;
import io.quarkus.registry.union.ElementCatalog;
import io.quarkus.registry.union.ElementCatalogBuilder;
import java.io.IOException;
Expand All @@ -47,6 +49,9 @@
*/
public class CreateProjectCommandHandler implements QuarkusCommandHandler {

private static final String QUARKUS_PLATFORM_GROUP_ID_EXPR = "${quarkus.platform.group-id}";
private static final String QUARKUS_PLATFORM_VERSION_EXPR = "${quarkus.platform.version}";

@Override
public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws QuarkusCommandException {
final Set<String> extensionsQuery = invocation.getValue(ProjectGenerator.EXTENSIONS, Collections.emptySet());
Expand All @@ -65,27 +70,36 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
}
}

final List<ArtifactCoords> extensionsToAdd = computeCoordsFromQuery(invocation, extensionsQuery);
final List<Extension> extensionsToAdd = computeExtensionsFromQuery(invocation, extensionsQuery);
if (extensionsToAdd == null) {
throw new QuarkusCommandException("Failed to create project because of invalid extensions");
}

ExtensionCatalog mainPlatform = invocation.getExtensionsCatalog(); // legacy platform initialization
final List<ExtensionCatalog> platformsToImport = getPlatformsToImport(mainPlatform, extensionsToAdd);
List<ArtifactCoords> extraBoms = Collections.emptyList();
if (platformsToImport.size() == 1) {
final List<ArtifactCoords> platformBoms = new ArrayList<>(Math.max(platformsToImport.size(), 1));
if (platformsToImport.size() > 0) {
mainPlatform = platformsToImport.get(0);
} else if (platformsToImport.size() > 1) {
extraBoms = new ArrayList<>(platformsToImport.size() - 1);
for (ExtensionCatalog platform : platformsToImport) {
// TODO once we adjust the templates to use a single version property for all the members
// this shouldn't be necessary
if (platform.getBom().getArtifactId().equals("quarkus-bom")) {
mainPlatform = platform;
} else {
extraBoms.add(platform.getBom());
}
platformBoms.add(platform.getBom());
}
} else {
platformBoms.add(mainPlatform.getBom());
}

final List<ArtifactCoords> extensionCoords = new ArrayList<>(extensionsToAdd.size());
for (Extension e : extensionsToAdd) {
ArtifactCoords coords = e.getArtifact();
for (ExtensionOrigin origin : e.getOrigins()) {
if (origin.getBom() != null && platformBoms.contains(origin.getBom())) {
coords = Extensions.stripVersion(coords);
break;
}
}
extensionCoords.add(coords);
}

invocation.setValue(BOM_GROUP_ID, mainPlatform.getBom().getGroupId());
Expand All @@ -94,9 +108,9 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
invocation.setValue(QUARKUS_VERSION, mainPlatform.getQuarkusCoreVersion());
final Properties quarkusProps = ToolsUtils.readQuarkusProperties(mainPlatform);
quarkusProps.forEach((k, v) -> {
String name = k.toString().replace("-", "_");
final String name = k.toString().replace("-", "_");
if (!invocation.hasValue(name)) {
invocation.setValue(k.toString().replace("-", "_"), v.toString());
invocation.setValue(name, v.toString());
}
});

Expand All @@ -109,8 +123,8 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
platformData.put("gradle", mainPlatform.getMetadata().get("gradle"));
}
final QuarkusCodestartProjectInput input = QuarkusCodestartProjectInput.builder()
.addPlatforms(extraBoms)
.addExtensions(extensionsToAdd)
.addPlatforms(platformBoms)
.addExtensions(extensionCoords)
.buildTool(invocation.getQuarkusProject().getBuildTool())
.example(invocation.getValue(EXAMPLE))
.noCode(invocation.getValue(NO_CODE, false))
Expand All @@ -125,7 +139,8 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
invocation.log().info("-----------");
if (!extensionsToAdd.isEmpty()) {
invocation.log().info("selected extensions: \n"
+ extensionsToAdd.stream().map(e -> "- " + e.getGroupId() + ":" + e.getArtifactId() + "\n")
+ extensionsToAdd.stream()
.map(e -> "- " + e.getArtifact().getGroupId() + ":" + e.getArtifact().getArtifactId() + "\n")
.collect(Collectors.joining()));
}

Expand All @@ -148,7 +163,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
}

@SuppressWarnings("unchecked")
private List<ExtensionCatalog> getPlatformsToImport(ExtensionCatalog extensionCatalog, List<ArtifactCoords> extensionsToAdd)
private List<ExtensionCatalog> getPlatformsToImport(ExtensionCatalog extensionCatalog, List<Extension> extensionsToAdd)
throws QuarkusCommandException {
final ElementCatalog<ExtensionCatalog> ec = (ElementCatalog<ExtensionCatalog>) extensionCatalog.getMetadata()
.get("element-catalog");
Expand All @@ -167,10 +182,25 @@ private List<ExtensionCatalog> getPlatformsToImport(ExtensionCatalog extensionCa
eKeys = Collections.singletonList(
quarkusCore.getArtifact().getGroupId() + ":" + quarkusCore.getArtifact().getArtifactId());
} else {
eKeys = extensionsToAdd.stream().map(e -> e.getGroupId() + ":" + e.getArtifactId())
eKeys = extensionsToAdd.stream().map(e -> e.getArtifact().getGroupId() + ":" + e.getArtifact().getArtifactId())
.collect(Collectors.toList());
eKeys.add(quarkusCore.getArtifact().getGroupId() + ":" + quarkusCore.getArtifact().getArtifactId());
}
return ElementCatalogBuilder.getMembersForElements(ec, eKeys);
List<ExtensionCatalog> catalogs = ElementCatalogBuilder.getMembersForElements(ec, eKeys);
List<ExtensionCatalog> filtered = null;
for (int i = 0; i < catalogs.size(); ++i) {
final ExtensionCatalog c = catalogs.get(i);
if (c.isPlatform()) {
if (filtered != null) {
filtered.add(c);
}
} else if (filtered == null) {
filtered = new ArrayList<>(catalogs.size());
for (int j = 0; j < i; ++j) {
filtered.add(catalogs.get(j));
}
}
}
return filtered == null ? catalogs : filtered;
}
}
Loading

0 comments on commit b39ecfa

Please sign in to comment.