Skip to content

Commit

Permalink
Switch back to Detached configuration for conditional dependencies in…
Browse files Browse the repository at this point in the history
… Gradle
  • Loading branch information
aloubyansky committed Aug 12, 2024
1 parent b859d7f commit 1e1e6e5
Showing 1 changed file with 9 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,44 +168,15 @@ private void queueConditionalDependency(ExtensionDependency<?> extension, Depend
}

private Configuration createConditionalDependenciesConfiguration(Project project, Dependency conditionalDep) {
// previously we used a detached configuration here but apparently extendsFrom(enforcedPlatforms)
// wouldn't actually enforce platforms on a detached configuration
var name = getConditionalConfigurationName(conditionalDep);
var config = project.getConfigurations().findByName(name);
if (config == null) {
project.getConfigurations().register(name, configuration -> {
configuration.setCanBeConsumed(false);
configuration.extendsFrom(enforcedPlatforms);
configuration.getDependencies().add(conditionalDep);
});
config = project.getConfigurations().getByName(name);
}
return config;
}

private static String getConditionalConfigurationName(Dependency conditionalDep) {
var name = new StringBuilder().append("quarkusConditional");
appendCapitalized(name, conditionalDep.getGroup());
appendCapitalized(name, conditionalDep.getName());
appendCapitalized(name, conditionalDep.getVersion());
return name.append("Configuration").toString();
}

private static void appendCapitalized(StringBuilder sb, String part) {
if (part != null && !part.isEmpty()) {
boolean toUpperCase = true;
for (int i = 0; i < part.length(); ++i) {
var c = part.charAt(i);
if (toUpperCase) {
sb.append(Character.toUpperCase(c));
toUpperCase = false;
} else if (c == '.' || c == '-') {
toUpperCase = true;
} else {
sb.append(c);
}
}
}
// Ideally, we want a named configuration here to make sure platform version constraints are enforced
// on conditional dependencies (https://github.com/gradle/gradle/issues/6881)
// However, if we use a named configuration we run into issues preventing IDEs to import projects
// (https://github.com/quarkusio/quarkus/issues/41825) and ./gradlew refreshVersions seems to break as well.
Configuration conditionalDepConfiguration = project.getConfigurations()
.detachedConfiguration()
.extendsFrom(enforcedPlatforms);
conditionalDepConfiguration.getDependencies().add(conditionalDep);
return conditionalDepConfiguration;
}

private void enableConditionalDependency(ModuleVersionIdentifier dependency) {
Expand Down

0 comments on commit 1e1e6e5

Please sign in to comment.