Skip to content

Commit

Permalink
Support for dev-mode-only conditional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Loubyansky committed Nov 15, 2024
1 parent efcd612 commit de4a697
Show file tree
Hide file tree
Showing 27 changed files with 717 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ protected QuarkusBootstrap.Builder initBootstrapBuilder() throws Exception {
final QuarkusBootstrap.Builder bootstrap = QuarkusBootstrap.builder()
.setApplicationRoot(applicationRoot)
.setProjectRoot(applicationRoot)
.setAppModelResolver(resolver)
.setTest(isBootstrapForTestMode());
.setAppModelResolver(resolver);

switch (getBootstrapMode()) {
case PROD:
break;
case TEST:
bootstrap.setTest(true);
break;
default:
throw new IllegalArgumentException("Not supported bootstrap mode " + getBootstrapMode());
}

if (createWorkspace() || !wsModules.isEmpty()) {
System.setProperty("basedir", ws.toAbsolutePath().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ protected QuarkusBootstrap.Builder initBootstrapBuilder()
.setApplicationRoot(applicationRoot)
.setProjectRoot(applicationRoot)
.setTargetDirectory(appModel.getAppArtifact().getWorkspaceModule().getBuildDir().toPath())
.setAppModelResolver(resolver)
.setTest(isBootstrapForTestMode());
.setAppModelResolver(resolver);
switch (getBootstrapMode()) {
case PROD:
break;
case TEST:
bootstrap.setTest(true);
break;
default:
throw new IllegalArgumentException("Not supported bootstrap mode " + getBootstrapMode());
}
return bootstrap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashSet;
import java.util.Set;

import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.resolver.TsArtifact;
import io.quarkus.bootstrap.resolver.TsDependency;
Expand All @@ -17,8 +18,8 @@
public class ProvidedExtensionDepsInTestModeTest extends BootstrapFromOriginalJarTestBase {

@Override
protected boolean isBootstrapForTestMode() {
return true;
protected QuarkusBootstrap.Mode getBootstrapMode() {
return QuarkusBootstrap.Mode.TEST;
}

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

import org.eclipse.aether.util.artifact.JavaScopes;

import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.resolver.TsArtifact;
import io.quarkus.bootstrap.resolver.TsDependency;
Expand All @@ -19,8 +20,8 @@
public class ProvidedExtensionDepsTestModeTest extends BootstrapFromOriginalJarTestBase {

@Override
protected boolean isBootstrapForTestMode() {
return true;
protected QuarkusBootstrap.Mode getBootstrapMode() {
return QuarkusBootstrap.Mode.TEST;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class QuarkusExtensionConfiguration {
private ListProperty<String> runnerParentFirstArtifacts;
private ListProperty<String> lesserPriorityArtifacts;
private ListProperty<String> conditionalDependencies;
private ListProperty<String> conditionalDevDependencies;
private ListProperty<String> dependencyCondition;
private RemovedResources removedResources = new RemovedResources();
private Capabilities capabilities = new Capabilities();
Expand All @@ -41,6 +42,7 @@ public QuarkusExtensionConfiguration(Project project) {
runnerParentFirstArtifacts = project.getObjects().listProperty(String.class);
lesserPriorityArtifacts = project.getObjects().listProperty(String.class);
conditionalDependencies = project.getObjects().listProperty(String.class);
conditionalDevDependencies = project.getObjects().listProperty(String.class);
dependencyCondition = project.getObjects().listProperty(String.class);
}

Expand Down Expand Up @@ -108,6 +110,14 @@ public void setConditionalDependencies(List<String> conditionalDependencies) {
this.conditionalDependencies.addAll(conditionalDependencies);
}

public ListProperty<String> getConditionalDevDependencies() {
return conditionalDevDependencies;
}

public void setConditionalDevDependencies(List<String> conditionalDependencies) {
this.conditionalDevDependencies.addAll(conditionalDependencies);
}

public ListProperty<String> getDependencyConditions() {
return dependencyCondition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.quarkus.extension.gradle.dsl.Capability;
import io.quarkus.extension.gradle.dsl.RemovedResource;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.GACT;

Expand Down Expand Up @@ -109,16 +110,10 @@ private void generateQuarkusExtensionProperties(Path metaInfDir) {

props.setProperty(BootstrapConstants.PROP_DEPLOYMENT_ARTIFACT, deploymentArtifact);

List<String> conditionalDependencies = quarkusExtensionConfiguration.getConditionalDependencies().get();
if (conditionalDependencies != null && !conditionalDependencies.isEmpty()) {
final StringBuilder buf = new StringBuilder();
int i = 0;
buf.append(AppArtifactCoords.fromString(conditionalDependencies.get(i++)).toString());
while (i < conditionalDependencies.size()) {
buf.append(' ').append(AppArtifactCoords.fromString(conditionalDependencies.get(i++)).toString());
}
props.setProperty(BootstrapConstants.CONDITIONAL_DEPENDENCIES, buf.toString());
}
setConditionalDepsProperty(BootstrapConstants.CONDITIONAL_DEPENDENCIES,
quarkusExtensionConfiguration.getConditionalDependencies().get(), props);
setConditionalDepsProperty(BootstrapConstants.CONDITIONAL_DEV_DEPENDENCIES,
quarkusExtensionConfiguration.getConditionalDevDependencies().get(), props);

List<String> dependencyConditions = quarkusExtensionConfiguration.getDependencyConditions().get();
if (dependencyConditions != null && !dependencyConditions.isEmpty()) {
Expand Down Expand Up @@ -222,6 +217,18 @@ private void generateQuarkusExtensionProperties(Path metaInfDir) {
}
}

private static void setConditionalDepsProperty(String propName, List<String> conditionalDependencies, Properties props) {
if (conditionalDependencies != null && !conditionalDependencies.isEmpty()) {
final StringBuilder buf = new StringBuilder();
int i = 0;
buf.append(ArtifactCoords.fromString(conditionalDependencies.get(i++)));
while (i < conditionalDependencies.size()) {
buf.append(' ').append(ArtifactCoords.fromString(conditionalDependencies.get(i++)));
}
props.setProperty(propName, buf.toString());
}
}

private void generateQuarkusExtensionDescriptor(Path inputMetaInfDirectory, Path outputMetaInfDirectory)
throws IOException {
File extensionFile = new File(inputMetaInfDirectory.toFile(), BootstrapConstants.QUARKUS_EXTENSION_FILE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ConditionalDependenciesEnabler(Project project, LaunchMode mode,

if (!baseRuntimeConfig.getIncoming().getDependencies().isEmpty()) {
// Gather all extensions from the full resolved dependency tree
collectConditionalDependencies(baseRuntimeConfig.getResolvedConfiguration().getResolvedArtifacts());
collectConditionalDependencies(baseRuntimeConfig.getResolvedConfiguration().getResolvedArtifacts(), mode);
// If there are any extensions which had unresolved conditional dependencies:
while (!unsatisfiedConditionalDeps.isEmpty()) {
boolean satisfiedConditionalDeps = false;
Expand All @@ -58,7 +58,7 @@ public ConditionalDependenciesEnabler(Project project, LaunchMode mode,
while (i < unsatisfiedConditionalDeps.size()) {
final Dependency conditionalDep = unsatisfiedConditionalDeps.get(i);
// Try to resolve it with the latest evolved graph available
if (resolveConditionalDependency(conditionalDep)) {
if (resolveConditionalDependency(conditionalDep, mode)) {
// Mark the resolution as a success so we know the graph evolved
satisfiedConditionalDeps = true;
unsatisfiedConditionalDeps.remove(i);
Expand Down Expand Up @@ -86,7 +86,7 @@ private void reset() {
unsatisfiedConditionalDeps.clear();
}

private void collectConditionalDependencies(Set<ResolvedArtifact> runtimeArtifacts) {
private void collectConditionalDependencies(Set<ResolvedArtifact> runtimeArtifacts, LaunchMode mode) {
// For every artifact in the dependency graph:
for (ResolvedArtifact artifact : runtimeArtifacts) {
// Add to master list of artifacts:
Expand All @@ -96,17 +96,28 @@ private void collectConditionalDependencies(Set<ResolvedArtifact> runtimeArtifac
if (extension != null) {
// Add to master list of accepted extensions:
allExtensions.put(extension.getExtensionId(), extension);
for (Dependency conditionalDep : extension.getConditionalDependencies()) {
// If the dependency is not present yet in the graph, queue it for resolution later
if (!exists(conditionalDep)) {
queueConditionalDependency(extension, conditionalDep);
}
}
queueConditionalDependencies(extension, mode);
}
}
}

private boolean resolveConditionalDependency(Dependency conditionalDep) {
private void queueConditionalDependencies(ExtensionDependency<?> extension, LaunchMode mode) {
queueConditionalDependencies(extension, extension.getConditionalDependencies());
if (LaunchMode.DEVELOPMENT.equals(mode)) {
queueConditionalDependencies(extension, extension.getConditionalDevDependencies());
}
}

private void queueConditionalDependencies(ExtensionDependency<?> extension, Collection<Dependency> conditionalDeps) {
for (Dependency conditionalDep : conditionalDeps) {
// If the dependency is not present yet in the graph, queue it for resolution later
if (!exists(conditionalDep)) {
queueConditionalDependency(extension, conditionalDep);
}
}
}

private boolean resolveConditionalDependency(Dependency conditionalDep, LaunchMode mode) {

final Configuration conditionalDeps = createConditionalDependenciesConfiguration(project, conditionalDep);
Set<ResolvedArtifact> resolvedArtifacts = conditionalDeps.getResolvedConfiguration().getResolvedArtifacts();
Expand Down Expand Up @@ -148,12 +159,7 @@ private boolean resolveConditionalDependency(Dependency conditionalDep) {
extensionDependency.setConditional(true);
// Add to the master list of accepted extensions
allExtensions.put(extensionDependency.getExtensionId(), extensionDependency);
for (Dependency cd : extensionDependency.getConditionalDependencies()) {
// Add any unsatisfied/unresolved conditional dependencies of this dependency to the queue
if (!exists(cd)) {
queueConditionalDependency(extensionDependency, cd);
}
}
queueConditionalDependencies(extensionDependency, mode);
}
return satisfied;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static ListProperty<String> getConditionalDependencies(@NotNull Object ex
return (ListProperty<String>) callGetter(extensionConfiguration, "getConditionalDependencies");
}

@SuppressWarnings("unchecked")
public static ListProperty<String> getConditionalDevDependencies(@NotNull Object extensionConfiguration) {
return (ListProperty<String>) callGetter(extensionConfiguration, "getConditionalDevDependencies");
}

@SuppressWarnings("unchecked")
public static ListProperty<String> getDependencyConditions(@NotNull Object extensionConfiguration) {
return (ListProperty<String>) callGetter(extensionConfiguration, "getDependencyConditions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class ArtifactExtensionDependency extends ExtensionDependency<ArtifactCoo
public ArtifactExtensionDependency(ModuleVersionIdentifier extensionId,
ArtifactCoords deploymentModule,
List<Dependency> conditionalDependencies,
List<Dependency> conditionalDevDeps,
List<ArtifactKey> dependencyConditions) {
super(extensionId, deploymentModule, conditionalDependencies, dependencyConditions);
super(extensionId, deploymentModule, conditionalDependencies, conditionalDevDeps, dependencyConditions);
}
}
Loading

0 comments on commit de4a697

Please sign in to comment.