From 59f1454a9242a096ee068c2f6c25440e83f4931a Mon Sep 17 00:00:00 2001 From: Hunter Mellema <124718352+hpmellema@users.noreply.github.com> Date: Mon, 19 Feb 2024 13:27:38 -0700 Subject: [PATCH] Add exception on implicit empty config (#123) Add exception on implicit empty config and require that an empty list be provided in order for an implicit smithy-build.json to be used. --- .../build.gradle.kts | 13 ++++++++++ .../model/main.smithy | 9 +++++++ .../settings.gradle.kts | 8 +++++++ .../custom-trait/consumer/build.gradle.kts | 6 ++++- .../multi-project/consumer/smithy-build.json | 3 +++ .../multiple-sources/smithy-build.json | 3 +++ .../ForbidImplicitNoBuildConfigTest.java | 24 +++++++++++++++++++ .../amazon/smithy/gradle/SmithyExtension.java | 1 - .../smithy/gradle/tasks/SmithyBuildTask.java | 23 ++++++++++++++++-- 9 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 examples/base-plugin/failure-cases/forbid-implicit-no-build-config/build.gradle.kts create mode 100644 examples/base-plugin/failure-cases/forbid-implicit-no-build-config/model/main.smithy create mode 100644 examples/base-plugin/failure-cases/forbid-implicit-no-build-config/settings.gradle.kts create mode 100644 examples/jar-plugin/multi-project/consumer/smithy-build.json create mode 100644 examples/jar-plugin/multiple-sources/smithy-build.json create mode 100644 smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java diff --git a/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/build.gradle.kts b/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/build.gradle.kts new file mode 100644 index 0000000..09552b0 --- /dev/null +++ b/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + `java-library` + id("software.amazon.smithy.gradle.smithy-base").version("0.10.0") +} + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + implementation("software.amazon.smithy:smithy-aws-traits:[1.0, 2.0[") +} diff --git a/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/model/main.smithy b/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/model/main.smithy new file mode 100644 index 0000000..364b6d8 --- /dev/null +++ b/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/model/main.smithy @@ -0,0 +1,9 @@ +namespace smithy.example + +structure Foo { + foo: String +} + +@aws.auth#unsignedPayload +operation Bar { +} diff --git a/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/settings.gradle.kts b/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/settings.gradle.kts new file mode 100644 index 0000000..e622edd --- /dev/null +++ b/examples/base-plugin/failure-cases/forbid-implicit-no-build-config/settings.gradle.kts @@ -0,0 +1,8 @@ +rootProject.name = "forbid-dependency-resolution" + +pluginManagement { + repositories { + mavenLocal() + mavenCentral() + } +} diff --git a/examples/jar-plugin/custom-trait/consumer/build.gradle.kts b/examples/jar-plugin/custom-trait/consumer/build.gradle.kts index 0933f40..df652aa 100644 --- a/examples/jar-plugin/custom-trait/consumer/build.gradle.kts +++ b/examples/jar-plugin/custom-trait/consumer/build.gradle.kts @@ -14,4 +14,8 @@ repositories { dependencies { implementation(project(":custom-string-trait")) -} \ No newline at end of file +} + +smithy { + smithyBuildConfigs.set(project.files()) +} diff --git a/examples/jar-plugin/multi-project/consumer/smithy-build.json b/examples/jar-plugin/multi-project/consumer/smithy-build.json new file mode 100644 index 0000000..703ffb7 --- /dev/null +++ b/examples/jar-plugin/multi-project/consumer/smithy-build.json @@ -0,0 +1,3 @@ +{ + "version": "1.0" +} diff --git a/examples/jar-plugin/multiple-sources/smithy-build.json b/examples/jar-plugin/multiple-sources/smithy-build.json new file mode 100644 index 0000000..703ffb7 --- /dev/null +++ b/examples/jar-plugin/multiple-sources/smithy-build.json @@ -0,0 +1,3 @@ +{ + "version": "1.0" +} diff --git a/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java b/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java new file mode 100644 index 0000000..a343acc --- /dev/null +++ b/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java @@ -0,0 +1,24 @@ +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 ForbidImplicitNoBuildConfigTest { + @Test + public void testExceptionThrows() { + Utils.withCopy("base-plugin/failure-cases/forbid-implicit-no-build-config", buildDir -> { + BuildResult result = GradleRunner.create() + .forwardOutput() + .withProjectDir(buildDir) + .withArguments("clean", "build", "--stacktrace") + .buildAndFail(); + + Assertions.assertTrue(result.getOutput() + .contains("No smithy-build configs found. If this was intentional, set the `smithyBuildConfigs` property to an empty list.")); + Utils.assertArtifactsNotCreated(buildDir, + "build/smithyprojections/forbid-implicit-no-build-config/source/build-info/smithy-build-info.json"); + }); + } +} diff --git a/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyExtension.java b/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyExtension.java index 5ceacd0..594c945 100644 --- a/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyExtension.java +++ b/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyExtension.java @@ -56,7 +56,6 @@ private void configureDefaults(Project project) { getFormat().convention(true); getAllowUnknownTraits().convention(false); getOutputDirectory().convention(getDefaultOutputDirectory(project)); - } /** diff --git a/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithyBuildTask.java b/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithyBuildTask.java index c2b432b..77352c9 100644 --- a/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithyBuildTask.java +++ b/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithyBuildTask.java @@ -10,10 +10,12 @@ import java.util.List; import java.util.stream.Collectors; import javax.inject.Inject; +import org.gradle.api.GradleException; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileCollection; import org.gradle.api.model.ObjectFactory; import org.gradle.api.provider.Property; +import org.gradle.api.provider.Provider; import org.gradle.api.provider.SetProperty; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputFiles; @@ -57,14 +59,14 @@ public SmithyBuildTask(ObjectFactory objectFactory) { public abstract SetProperty getProjectionSourceTags(); - /** Smithy build configs to use for building models. + /** + * Smithy build configs to use for building models. * * @return list of smithy-build config json files */ @InputFiles public abstract Property getSmithyBuildConfigs(); - /** * Sets whether to fail a {@link SmithyBuildTask} if an unknown trait is encountered. * @@ -104,10 +106,27 @@ List getModelAbsolutePaths() { .collect(Collectors.toList()); } + /** + * Read-only property. + * + * @return Returns true if at least one of the specified build configs exists. + */ + @Internal + Provider getSmithyBuildConfigsMissing() { + return getSmithyBuildConfigs().map( + files -> !files.isEmpty() && files.filter(File::exists).isEmpty() + ); + } + @TaskAction public void execute() { writeHeading("Running smithy build"); + if (getSmithyBuildConfigsMissing().get()) { + throw new GradleException("No smithy-build configs found. " + + "If this was intentional, set the `smithyBuildConfigs` property to an empty list."); + } + BuildParameterBuilder builder = new BuildParameterBuilder(); // Model discovery classpath