From 3604392e6f17e28ba9a3b14b8081a836a7f4fd26 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Mon, 19 Feb 2024 12:28:46 -0700 Subject: [PATCH 1/2] Add exception on implicit empty config --- .../build.gradle.kts | 13 +++++++ .../model/main.smithy | 9 +++++ .../settings.gradle.kts | 8 +++++ .../custom-trait/consumer/build.gradle.kts | 6 +++- .../multi-project/consumer/build.gradle.kts | 4 +++ .../multiple-sources/smithy-build.json | 3 ++ .../ForbidImplicitNoBuildConfigTest.java | 24 +++++++++++++ .../smithy/gradle/SmithyBasePlugin.java | 1 + .../amazon/smithy/gradle/SmithyExtension.java | 12 +++++++ .../smithy/gradle/tasks/SmithyBuildTask.java | 36 ++++++++++++++++++- 10 files changed, 114 insertions(+), 2 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/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..9ce776e 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 { + noBuildConfig.set(true) +} diff --git a/examples/jar-plugin/multi-project/consumer/build.gradle.kts b/examples/jar-plugin/multi-project/consumer/build.gradle.kts index 06a5a1f..f433f53 100644 --- a/examples/jar-plugin/multi-project/consumer/build.gradle.kts +++ b/examples/jar-plugin/multi-project/consumer/build.gradle.kts @@ -6,3 +6,7 @@ dependencies { implementation(project(":producer1")) implementation(project(":producer2")) } + +smithy { + noBuildConfig.set(true) +} 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..6692406 --- /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 `noBuildConfigs` flag to `true`.")); + 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/SmithyBasePlugin.java b/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyBasePlugin.java index a653865..b66e03f 100644 --- a/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyBasePlugin.java +++ b/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyBasePlugin.java @@ -195,6 +195,7 @@ private TaskProvider addBuildTaskForSourceSet(SourceSet sourceS build.getSmithyBuildConfigs().set(extension.getSmithyBuildConfigs()); build.getSourceProjection().set(extension.getSourceProjection()); build.getProjectionSourceTags().set(extension.getProjectionSourceTags()); + build.getNoBuildConfig().set(extension.getNoBuildConfig()); build.getOutputDir().set(extension.getOutputDirectory()); // Add smithy configurations as classpaths for build task 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..68c12e0 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 @@ -54,6 +54,7 @@ private void configureDefaults(Project project) { getSourceProjection().convention(SMITHY_SOURCE_PROJECTION_DEFAULT); getFork().convention(false); getFormat().convention(true); + getNoBuildConfig().convention(false); getAllowUnknownTraits().convention(false); getOutputDirectory().convention(getDefaultOutputDirectory(project)); @@ -143,6 +144,17 @@ public NamedDomainObjectContainer getSourceSets() { */ public abstract Property getFork(); + /** + * Gets whether unknown traits in the model should be ignored. + * + *

By default, the build will fail if no config is provided. + * This can be set to true to allow the build task to execute + * with an empty config when no smithy-build config is provided. + * + * @return Returns true if no config should be provided. + */ + public abstract Property getNoBuildConfig(); + /** * Gets the output directory for running Smithy build. * 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..e44544e 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; @@ -37,6 +39,7 @@ public SmithyBuildTask(ObjectFactory objectFactory) { super(objectFactory); getSourceProjection().convention("source"); + getNoBuildConfig().convention(false); getOutputDir().convention(SmithyUtils.getProjectionOutputDirProperty(getProject())); } @@ -57,13 +60,24 @@ 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 allow the build to continue if no build config is set. + * + *

Defaults to false. + * + * @return flag indicating if build should continue if no build configs are found. + */ + @Input + @Optional + public abstract Property getNoBuildConfig(); /** * Sets whether to fail a {@link SmithyBuildTask} if an unknown trait is encountered. @@ -104,9 +118,20 @@ List getModelAbsolutePaths() { .collect(Collectors.toList()); } + /** + * Read-only property. + * + * @return Returns true if at least one of the specified build configs exists. + */ + @Internal + Provider getSmithyBuildConfigsExist() { + return getSmithyBuildConfigs().map(files -> !files.filter(File::exists).isEmpty()); + } + @TaskAction public void execute() { writeHeading("Running smithy build"); + validateBuildConfigs(); BuildParameterBuilder builder = new BuildParameterBuilder(); @@ -139,4 +164,13 @@ public void execute() { getFork().get() ); } + + private void validateBuildConfigs() { + // If none of the specified Smithy build configs exist and + // the `noBuildConfigs` flag is false then throw an error + if (!getSmithyBuildConfigsExist().get() && !getNoBuildConfig().get()) { + throw new GradleException("No smithy-build configs found. " + + "If this was intentional, set the `noBuildConfigs` flag to `true`."); + } + } } From 07455d68c6b7383f871e577c2fadc94f8ab4b480 Mon Sep 17 00:00:00 2001 From: Hunter Mellema Date: Mon, 19 Feb 2024 13:14:29 -0700 Subject: [PATCH 2/2] Update to use empty list instead of new property --- .../custom-trait/consumer/build.gradle.kts | 2 +- .../multi-project/consumer/build.gradle.kts | 4 --- .../multi-project/consumer/smithy-build.json | 3 ++ .../ForbidImplicitNoBuildConfigTest.java | 2 +- .../smithy/gradle/SmithyBasePlugin.java | 1 - .../amazon/smithy/gradle/SmithyExtension.java | 13 -------- .../smithy/gradle/tasks/SmithyBuildTask.java | 33 +++++-------------- 7 files changed, 14 insertions(+), 44 deletions(-) create mode 100644 examples/jar-plugin/multi-project/consumer/smithy-build.json diff --git a/examples/jar-plugin/custom-trait/consumer/build.gradle.kts b/examples/jar-plugin/custom-trait/consumer/build.gradle.kts index 9ce776e..df652aa 100644 --- a/examples/jar-plugin/custom-trait/consumer/build.gradle.kts +++ b/examples/jar-plugin/custom-trait/consumer/build.gradle.kts @@ -17,5 +17,5 @@ dependencies { } smithy { - noBuildConfig.set(true) + smithyBuildConfigs.set(project.files()) } diff --git a/examples/jar-plugin/multi-project/consumer/build.gradle.kts b/examples/jar-plugin/multi-project/consumer/build.gradle.kts index f433f53..06a5a1f 100644 --- a/examples/jar-plugin/multi-project/consumer/build.gradle.kts +++ b/examples/jar-plugin/multi-project/consumer/build.gradle.kts @@ -6,7 +6,3 @@ dependencies { implementation(project(":producer1")) implementation(project(":producer2")) } - -smithy { - noBuildConfig.set(true) -} 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/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java b/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java index 6692406..a343acc 100644 --- a/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java +++ b/smithy-base/src/it/java/software/amazon/smithy/gradle/ForbidImplicitNoBuildConfigTest.java @@ -16,7 +16,7 @@ public void testExceptionThrows() { .buildAndFail(); Assertions.assertTrue(result.getOutput() - .contains("No smithy-build configs found. If this was intentional, set the `noBuildConfigs` flag to `true`.")); + .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/SmithyBasePlugin.java b/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyBasePlugin.java index b66e03f..a653865 100644 --- a/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyBasePlugin.java +++ b/smithy-base/src/main/java/software/amazon/smithy/gradle/SmithyBasePlugin.java @@ -195,7 +195,6 @@ private TaskProvider addBuildTaskForSourceSet(SourceSet sourceS build.getSmithyBuildConfigs().set(extension.getSmithyBuildConfigs()); build.getSourceProjection().set(extension.getSourceProjection()); build.getProjectionSourceTags().set(extension.getProjectionSourceTags()); - build.getNoBuildConfig().set(extension.getNoBuildConfig()); build.getOutputDir().set(extension.getOutputDirectory()); // Add smithy configurations as classpaths for build task 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 68c12e0..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 @@ -54,10 +54,8 @@ private void configureDefaults(Project project) { getSourceProjection().convention(SMITHY_SOURCE_PROJECTION_DEFAULT); getFork().convention(false); getFormat().convention(true); - getNoBuildConfig().convention(false); getAllowUnknownTraits().convention(false); getOutputDirectory().convention(getDefaultOutputDirectory(project)); - } /** @@ -144,17 +142,6 @@ public NamedDomainObjectContainer getSourceSets() { */ public abstract Property getFork(); - /** - * Gets whether unknown traits in the model should be ignored. - * - *

By default, the build will fail if no config is provided. - * This can be set to true to allow the build task to execute - * with an empty config when no smithy-build config is provided. - * - * @return Returns true if no config should be provided. - */ - public abstract Property getNoBuildConfig(); - /** * Gets the output directory for running Smithy build. * 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 e44544e..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 @@ -39,7 +39,6 @@ public SmithyBuildTask(ObjectFactory objectFactory) { super(objectFactory); getSourceProjection().convention("source"); - getNoBuildConfig().convention(false); getOutputDir().convention(SmithyUtils.getProjectionOutputDirProperty(getProject())); } @@ -68,17 +67,6 @@ public SmithyBuildTask(ObjectFactory objectFactory) { @InputFiles public abstract Property getSmithyBuildConfigs(); - /** - * Sets whether to allow the build to continue if no build config is set. - * - *

Defaults to false. - * - * @return flag indicating if build should continue if no build configs are found. - */ - @Input - @Optional - public abstract Property getNoBuildConfig(); - /** * Sets whether to fail a {@link SmithyBuildTask} if an unknown trait is encountered. * @@ -124,14 +112,20 @@ List getModelAbsolutePaths() { * @return Returns true if at least one of the specified build configs exists. */ @Internal - Provider getSmithyBuildConfigsExist() { - return getSmithyBuildConfigs().map(files -> !files.filter(File::exists).isEmpty()); + Provider getSmithyBuildConfigsMissing() { + return getSmithyBuildConfigs().map( + files -> !files.isEmpty() && files.filter(File::exists).isEmpty() + ); } @TaskAction public void execute() { writeHeading("Running smithy build"); - validateBuildConfigs(); + + 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(); @@ -164,13 +158,4 @@ public void execute() { getFork().get() ); } - - private void validateBuildConfigs() { - // If none of the specified Smithy build configs exist and - // the `noBuildConfigs` flag is false then throw an error - if (!getSmithyBuildConfigsExist().get() && !getNoBuildConfig().get()) { - throw new GradleException("No smithy-build configs found. " - + "If this was intentional, set the `noBuildConfigs` flag to `true`."); - } - } }