Skip to content

Commit

Permalink
Re-add "Add exception on implicit empty config (#123)" (#128)
Browse files Browse the repository at this point in the history
Re-add changes from "Add exception on implicit empty config (#123)" that were temporarily removed
  • Loading branch information
hpmellema committed Mar 18, 2024
1 parent 118fdeb commit 1927b0d
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 6 deletions.
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.1")
}

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()
}
}
6 changes: 5 additions & 1 deletion examples/jar-plugin/custom-trait/consumer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ repositories {

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

smithy {
smithyBuildConfigs.set(project.files())
}
3 changes: 3 additions & 0 deletions examples/jar-plugin/multi-project/consumer/smithy-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.0"
}
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 `smithyBuildConfigs` property to an empty list."));
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 @@ -56,7 +56,6 @@ private void configureDefaults(Project project) {
getFormat().convention(true);
getAllowUnknownTraits().convention(false);
getOutputDirectory().convention(getDefaultOutputDirectory(project));

}

/**
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 @@ -23,6 +25,7 @@
import org.gradle.api.tasks.TaskAction;
import software.amazon.smithy.cli.BuildParameterBuilder;
import software.amazon.smithy.gradle.SmithyUtils;
import software.amazon.smithy.model.validation.Severity;

/**
* Executes the Smithy CLI {@code build} command.
Expand All @@ -37,6 +40,7 @@ public SmithyBuildTask(ObjectFactory objectFactory) {
super(objectFactory);

getSourceProjection().convention("source");
getSeverity().convention(Severity.WARNING.toString());
getOutputDir().convention(SmithyUtils.getProjectionOutputDirProperty(getProject()));
}

Expand All @@ -57,14 +61,14 @@ 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 fail a {@link SmithyBuildTask} if an unknown trait is encountered.
*
Expand All @@ -84,6 +88,16 @@ public SmithyBuildTask(ObjectFactory objectFactory) {
@Optional
public abstract Property<String> getSourceProjection();

/**
* Set the minimum reported validation severity.
*
* <p>This value should be one of: NOTE, WARNING [default setting], DANGER, ERROR.
*
* @return minimum validator severity
*/
@Input
@Optional
public abstract Property<String> getSeverity();

/**
* Output directory for Smithy build artifacts.
Expand All @@ -104,10 +118,28 @@ List<String> getModelAbsolutePaths() {
.collect(Collectors.toList());
}

/**
* Read-only property.
*
* @return Returns false if the Smithy build config property is set to an explicit empty list
* or if least one of the specified build configs exists
*/
@Internal
Provider<Boolean> 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
Expand All @@ -129,6 +161,11 @@ public void execute() {
// Add extra configuration options for build command
List<String> extraArgs = new ArrayList<>();
configureLoggingOptions(extraArgs);

// Add validator severity option if it exists
extraArgs.add("--severity");
extraArgs.add(getSeverity().get());

builder.addExtraArgs(extraArgs.toArray(new String[0]));

BuildParameterBuilder.Result result = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package software.amazon.smithy.gradle.tasks;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
Expand All @@ -15,7 +17,7 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.model.validation.Severity;


/**
Expand All @@ -35,6 +37,7 @@ public SmithyValidateTask(ObjectFactory objectFactory) {
super(objectFactory);
getAllowUnknownTraits().convention(false);
getDisableModelDiscovery().convention(false);
getSeverity().convention(Severity.ERROR.toString());
setDescription(DESCRIPTION);
}

Expand Down Expand Up @@ -67,6 +70,17 @@ public SmithyValidateTask(ObjectFactory objectFactory) {
@Optional
public abstract Property<Boolean> getDisableModelDiscovery();

/**
* Set the minimum reported validation severity.
*
* <p>This value should be one of: NOTE, WARNING, DANGER, ERROR [default].
*
* @return minimum validator severity
*/
@Input
@Optional
public abstract Property<String> getSeverity();

/**
* Gets the classpath to use when executing the Smithy CLI.
*
Expand All @@ -86,8 +100,13 @@ Provider<FileCollection> getCliExecutionClasspath() {
public void execute() {
writeHeading("Running smithy validate");

// Add validator severity settings
List<String> extraArgs = new ArrayList<>();
extraArgs.add("--severity");
extraArgs.add(getSeverity().get());

// Set models to an empty collection so source models are not included in validation path.
executeCliProcess("validate", ListUtils.of(),
executeCliProcess("validate", extraArgs,
getJarToValidate().get(),
getDisableModelDiscovery().get()
);
Expand Down

0 comments on commit 1927b0d

Please sign in to comment.