Skip to content

Commit

Permalink
Allow users access to smithyCli configuration so they can explicitly …
Browse files Browse the repository at this point in the history
…set cli version
  • Loading branch information
hpmellema committed Mar 14, 2024
1 parent 4826625 commit dc0d714
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 11 deletions.
14 changes: 14 additions & 0 deletions examples/base-plugin/uses-explicitly-set-cli-version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Example Project - Build Script Dependency

This is an example Gradle Smithy project. In addition to serving as documentation,
this project is run as an integration test for the plugin.

This example demonstrates using the version of Smithy found in the build script
dependencies to build the project.

## Using the example as a starting point

Since this sample is run as an integration test, by default it is only configured
to use a locally published version of the plugin. To use this as a starting point
for your own project, uncomment the lines in `settings.gradle.kts` that configure
Gradle to use public sources.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This example is an integration test to ensure that the smithy-cli version can be
// found by scanning buildScript dependencies.

plugins {
id("java-library")
id("software.amazon.smithy.gradle.smithy-base").version("0.10.1")
}

dependencies {
smithyCli("software.amazon.smithy:smithy-cli:1.45.0")
}

repositories {
mavenLocal()
mavenCentral()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace smithy.example

structure Foo {
bar: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rootProject.name = "scans-for-cli-version"

pluginManagement {
repositories {
mavenLocal()
mavenCentral()
// Uncomment these to use the published version of the plugin from your preferred source.
// gradlePluginPortal()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package software.amazon.smithy.gradle;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class UsesExplicitlySetCLIVersionTest {
@Test
public void usesExplicitCliVersion() {
Utils.withCopy("base-plugin/uses-explicitly-set-cli-version", buildDir -> {
BuildResult result = GradleRunner.create()
.forwardOutput()
.withProjectDir(buildDir)
.withArguments("clean", "build", "-i", "--stacktrace")
.build();

Utils.assertSmithyBuildTaskRan(result);
Utils.assertValidationRan(result);
Assertions.assertTrue(result.getOutput().contains("(using explicitly configured Smithy CLI)"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public final class SmithyBasePlugin implements Plugin<Project> {
* Default name to use for the build task created by this plugin.
*/
public static final String SMITHY_BUILD_TASK_NAME = "smithyBuild";
public static final String SMITHY_CLI_CONFIG = "smithyCli";

private static final GradleVersion MINIMUM_GRADLE_VERSION = GradleVersion.version("8.2.0");

Expand Down Expand Up @@ -68,7 +67,7 @@ private static void configureSmithyCliConfig(Project project) {
// Set up Smithy-specific configurations
Configuration smithyCliConfiguration = project.getConfigurations()
.maybeCreate(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME);
smithyCliConfiguration.setVisible(false);
smithyCliConfiguration.setVisible(true);
smithyCliConfiguration.setDescription("Configuration for Smithy CLI and associated dependencies.");
}

Expand All @@ -90,7 +89,7 @@ private void configureSourceSetDefaults(Project project, SmithyExtension extensi

project.afterEvaluate(p -> {
// Resolve the Smithy CLI artifact
CliDependencyResolver.resolve(project);
CliDependencyResolver.resolve(p);

p.getExtensions().getByType(SourceSetContainer.class).all(sourceSet -> {
// Add format task for source set if enabled
Expand Down Expand Up @@ -179,8 +178,10 @@ private TaskProvider<SmithyBuildTask> addBuildTaskForSourceSet(SourceSet sourceS
build.getOutputDir().set(extension.getOutputDirectory());

// Add smithy configurations as classpaths for build task
build.getCliClasspath().set(project.getConfigurations().getByName(SMITHY_CLI_CONFIG));
build.getBuildClasspath().set(project.getConfigurations().getByName(buildConfigName));
build.getCliClasspath().set(project.getConfigurations()
.getByName(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME));
build.getBuildClasspath().set(project.getConfigurations()
.getByName(buildConfigName));
build.getModelDiscoveryClasspath().set(
project.getConfigurations().getByName(runtimeConfigName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import org.gradle.work.DisableCachingByDefault;
import org.gradle.workers.WorkerExecutor;
import software.amazon.smithy.gradle.SmithyUtils;

/**
* Base class for all Smithy tasks.
Expand All @@ -40,9 +41,9 @@ abstract class BaseSmithyTask extends DefaultTask {
getModelDiscoveryClasspath().set(getProject().files());

// if the smithyCli configuration exists use it by default
if (getProject().getConfigurations().findByName("smithyCli") != null) {
if (getProject().getConfigurations().findByName(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME) != null) {
getCliClasspath().convention(getProject().getConfigurations()
.getByName("smithyCli"));
.getByName(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME));
}

startParameter = getProject().getGradle().getStartParameter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.gradle.SmithyUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -15,7 +16,7 @@ public class SmithyBuildTaskTest {
@BeforeEach
public void init() {
testProject = ProjectBuilder.builder().build();
testProject.getConfigurations().create("smithyCli");
testProject.getConfigurations().create(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME);
testProject.getConfigurations().create("smithyBuild");
testProject.getConfigurations().create("runtimeClasspath");
}
Expand All @@ -28,4 +29,4 @@ public void validateDefaults() {
assertEquals(buildTask.getShowStackTrace().get(), ShowStacktrace.INTERNAL_EXCEPTIONS);
assertEquals(buildTask.getSourceProjection().get(), "source");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.gradle.SmithyUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -15,7 +16,8 @@ public class SmithyFormatTaskTest {
@BeforeEach
public void init() {
testProject = ProjectBuilder.builder().build();
testProject.getConfigurations().create("smithyCli");
testProject.getConfigurations().create(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME);

testProject.getConfigurations().create("smithyBuild");
testProject.getConfigurations().create("runtimeClasspath");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.gradle.SmithyUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -16,7 +17,7 @@ public class SmithyValidateTaskTest {
@BeforeEach
public void init() {
testProject = ProjectBuilder.builder().build();
testProject.getConfigurations().create("smithyCli");
testProject.getConfigurations().create(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME);
testProject.getConfigurations().create("smithyBuild");
testProject.getConfigurations().create("runtimeClasspath");
}
Expand Down

0 comments on commit dc0d714

Please sign in to comment.