Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception on implicit empty config #123

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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[")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace smithy.example

structure Foo {
foo: String
}

@aws.auth#unsignedPayload
operation Bar {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rootProject.name = "forbid-dependency-resolution"

pluginManagement {
repositories {
mavenLocal()
mavenCentral()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ repositories {

dependencies {
implementation(project(":custom-string-trait"))
}
}

smithy {
noBuildConfig.set(true)
}
4 changes: 4 additions & 0 deletions examples/jar-plugin/multi-project/consumer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ dependencies {
implementation(project(":producer1"))
implementation(project(":producer2"))
}

smithy {
noBuildConfig.set(true)
}
3 changes: 3 additions & 0 deletions examples/jar-plugin/multiple-sources/smithy-build.json
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,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");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ private TaskProvider<SmithyBuildTask> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -143,6 +144,17 @@ public NamedDomainObjectContainer<SmithySourceDirectorySet> getSourceSets() {
*/
public abstract Property<Boolean> getFork();

/**
* Gets whether unknown traits in the model should be ignored.
*
* <p>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<Boolean> getNoBuildConfig();

/**
* Gets the output directory for running Smithy build.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +39,7 @@ public SmithyBuildTask(ObjectFactory objectFactory) {
super(objectFactory);

getSourceProjection().convention("source");
getNoBuildConfig().convention(false);
getOutputDir().convention(SmithyUtils.getProjectionOutputDirProperty(getProject()));
}

Expand All @@ -57,13 +60,24 @@ public SmithyBuildTask(ObjectFactory objectFactory) {
public abstract SetProperty<String> 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<FileCollection> getSmithyBuildConfigs();

/**
* Sets whether to allow the build to continue if no build config is set.
*
* <p> Defaults to false.
*
* @return flag indicating if build should continue if no build configs are found.
*/
@Input
@Optional
public abstract Property<Boolean> getNoBuildConfig();

/**
* Sets whether to fail a {@link SmithyBuildTask} if an unknown trait is encountered.
Expand Down Expand Up @@ -104,9 +118,20 @@ List<String> getModelAbsolutePaths() {
.collect(Collectors.toList());
}

/**
* Read-only property.
*
* @return Returns true if at least one of the specified build configs exists.
*/
@Internal
Provider<Boolean> getSmithyBuildConfigsExist() {
return getSmithyBuildConfigs().map(files -> !files.filter(File::exists).isEmpty());
hpmellema marked this conversation as resolved.
Show resolved Hide resolved
}

@TaskAction
public void execute() {
writeHeading("Running smithy build");
validateBuildConfigs();

BuildParameterBuilder builder = new BuildParameterBuilder();

Expand Down Expand Up @@ -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`.");
}
}
}
Loading