Skip to content

Commit

Permalink
Determine cli version from resolved runtime dependencies (#105)
Browse files Browse the repository at this point in the history
Updates the behavior of CLI dependency resolution to use the resolved version of the smithy-model dependency from the runtimeClasspath configuration if the dependency is present.
  • Loading branch information
hpmellema committed Oct 16, 2023
1 parent 85cd5af commit 3217682
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,26 @@ private void configureSourceSetDefaults(Project project, SmithyExtension extensi
project.getTasks().getByName("build").dependsOn(buildTaskTaskProvider);
}

// Add format task for source set if enabled and the CLI version supports it
String cliVersion = CliDependencyResolver.resolve(project, sourceSet);
if (extension.getFormat().get() && cliVersionSupportsFormat(cliVersion)) {
addFormatTaskForSourceSet(sourceSet, sds, extension);
}
project.afterEvaluate(p -> {
// Resolve the Smithy CLI artifact to use
String cliVersion = CliDependencyResolver.resolve(project, sourceSet);

// Add format task for source set if enabled and the CLI version supports it
if (extension.getFormat().get() && cliVersionSupportsFormat(cliVersion, sourceSet)) {
addFormatTaskForSourceSet(sourceSet, sds, extension);
}
});
});
}


/**
* Smithy-format was added in version 1.33.0. It is not supported in earlier CLI versions.
*/
private boolean cliVersionSupportsFormat(String cliVersion) {
private boolean cliVersionSupportsFormat(String cliVersion, SourceSet sourceSet) {
boolean supported = GradleVersion.version(cliVersion).compareTo(MIN_SMITHY_FORMAT_VERSION) >= 0;

if (!supported) {
if (!supported && SourceSet.isMain(sourceSet)) {
project.getLogger().warn("Formatting task is not supported Smithy CLI version: "
+ "(" + cliVersion + "). Skipping."
+ "Minimum supported Smithy CLI version for formatting is "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private CliDependencyResolver() {}
*
* @param project Project to add dependencies to.
*
* @return the version of the CLI in use
*/
public static String resolve(Project project, SourceSet sourceSet) {
Configuration cli = SmithyUtils.getCliConfiguration(project, sourceSet);
Expand All @@ -62,18 +61,15 @@ public static String resolve(Project project, SourceSet sourceSet) {
// Force projects in the main smithy repo to use an explicit smithy cli dependency
failIfRunningInMainSmithyRepo(project);

// If no explicit dependency was found, find the CLI version by scanning
// and set this as a dependency
String cliVersion = getCliVersion(project, cli);
// If no explicit dependency was found, find the CLI version by scanning and set this as a dependency
String cliVersion = getCliVersion(project, sourceSet);
project.getDependencies().add(cli.getName(), String.format(DEPENDENCY_NOTATION, cliVersion));

return cliVersion;
}


private static String getCliVersion(Project project, Configuration cliConfiguration) {
String cliVersion = detectCliVersionInDependencies(cliConfiguration);

private static String getCliVersion(Project project, SourceSet sourceSet) {
String cliVersion = detectCliVersionInRuntimeDependencies(project, sourceSet);
if (cliVersion != null) {
project.getLogger().info("(detected Smithy CLI version {})", cliVersion);
} else {
Expand All @@ -87,15 +83,17 @@ private static String getCliVersion(Project project, Configuration cliConfigurat
/**
* Check if there's a dependency on smithy-model somewhere, and assume that version.
*
* @param configuration configuration to search for CLI version
* @return version of cli available in configuration
* @param project configuration to search for CLI version
* @param sourceSet SourceSet to get runtime configuration for
*
* @return version of cli available in configuration
*/
public static String detectCliVersionInDependencies(Configuration configuration) {
return configuration.getAllDependencies().stream()
.filter(d -> isMatchingDependency(d, "smithy-model"))
.map(Dependency::getVersion)
.filter(Objects::nonNull)
public static String detectCliVersionInRuntimeDependencies(Project project, SourceSet sourceSet) {
Configuration runtimeClasspath = project.getConfigurations().getByName(
sourceSet.getRuntimeClasspathConfigurationName());
return runtimeClasspath.getResolvedConfiguration().getResolvedArtifacts().stream()
.filter(ra -> ra.getName().equals("smithy-model"))
.map(ra -> ra.getModuleVersion().getId().getVersion())
.findFirst()
.orElse(null);
}
Expand Down

0 comments on commit 3217682

Please sign in to comment.