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 185fa8d..89bf1f2 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 @@ -21,6 +21,7 @@ import software.amazon.smithy.gradle.internal.CliDependencyResolver; import software.amazon.smithy.gradle.tasks.SmithyBuildTask; import software.amazon.smithy.gradle.tasks.SmithyFormatTask; +import software.amazon.smithy.gradle.tasks.SmithySelectTask; /** * A {@link org.gradle.api.Plugin} that builds and validates Smithy models. @@ -93,10 +94,11 @@ private void configureSourceSetDefaults(Project project, SmithyExtension extensi p.getExtensions().getByType(SourceSetContainer.class).all(sourceSet -> { // Add format task for source set if enabled + SmithySourceDirectorySet sds = sourceSet.getExtensions().getByType(SmithySourceDirectorySet.class); if (extension.getFormat().get()) { - SmithySourceDirectorySet sds = sourceSet.getExtensions().getByType(SmithySourceDirectorySet.class); addFormatTaskForSourceSet(sourceSet, sds, extension); } + addSelectTaskForSourceSet(sourceSet, sds, extension); }); }); } @@ -156,6 +158,28 @@ private void addFormatTaskForSourceSet(SourceSet sourceSet, SmithySourceDirector } } + private void addSelectTaskForSourceSet(SourceSet sourceSet, SmithySourceDirectorySet sds, + SmithyExtension extension + ) { + String taskName = SmithyUtils.getRelativeSourceSetName(sourceSet, "select"); + String buildConfigName = SmithyUtils.getSmithyBuildConfigurationName(sourceSet); + String runtimeConfigName = sourceSet.getRuntimeClasspathConfigurationName(); + + project.getTasks().register(taskName, SmithySelectTask.class, selectTask -> { + selectTask.setDescription("Selects smithy models in " + sourceSet.getName() + " source set."); + selectTask.getAllowUnknownTraits().set(extension.getAllowUnknownTraits()); + selectTask.getModels().set(sds.getSourceDirectories()); + selectTask.getFork().set(extension.getFork()); + + // Add smithy configurations as classpaths for select task so select + // task can correctly resolve dependencies. + selectTask.getCliClasspath().set(project.getConfigurations() + .getByName(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME)); + selectTask.getBuildClasspath().set(project.getConfigurations().getByName(buildConfigName)); + selectTask.getModelDiscoveryClasspath().set(project.getConfigurations().getByName(runtimeConfigName)); + }); + } + private TaskProvider addBuildTaskForSourceSet(SourceSet sourceSet, SmithySourceDirectorySet sds, SmithyExtension extension 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 594c945..fb85a12 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 @@ -67,7 +67,6 @@ public NamedDomainObjectContainer getSourceSets() { return this.sourceSets; } - /** * Gets whether to execute the format task on files in the Smithy source set * diff --git a/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithySelectTask.java b/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithySelectTask.java new file mode 100644 index 0000000..e330f3e --- /dev/null +++ b/smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithySelectTask.java @@ -0,0 +1,82 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.gradle.tasks; + +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; +import org.gradle.StartParameter; +import org.gradle.api.GradleException; +import org.gradle.api.model.ObjectFactory; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Optional; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.options.Option; + + +/** + * Executes the Smithy CLI {@code format} tool on a set of source files. + * + *

The smithy format tool is an opinionated formatter that can be used to maintain + * a consistent, readable style for your smithy files. This task can be used to quickly + * reformat smithy files as part of your gradle build process. + * + *

WARNING: This task will mutate input source files and change their formatting in-place. + * + *

Note: Smithy format was introduced to the CLI in version 1.33.0 so earlier + * versions will be unable to use this tool. + */ +public abstract class SmithySelectTask extends AbstractSmithyCliTask { + private static final String DESCRIPTION = "Selects smithy models."; + + @Inject + public SmithySelectTask(ObjectFactory objectFactory, StartParameter startParameter) { + super(objectFactory, startParameter); + setDescription(DESCRIPTION); + } + + @Input + @Option(option = "selector", description = "The Smithy selector to execute") + abstract Property getSelector(); + + + @Input + @Optional + @Option(option = "show", description = "The Smithy selector to execute") + abstract Property getShow(); + + @Input + @Optional + @Option(option = "show-traits", description = "The Smithy selector to execute") + abstract Property getShowTraits(); + + @TaskAction + public void execute() { + if (!getSelector().isPresent()) { + throw new GradleException("Select task requires that the command line option `--select` be set."); + } + List extraArgs = new ArrayList<>(); + extraArgs.add("--selector"); + extraArgs.add(getSelector().get()); + + if (getShow().isPresent()) { + extraArgs.add("--show"); + extraArgs.add(getShow().get()); + } + + if (getShowTraits().isPresent()) { + extraArgs.add("--show-traits"); + extraArgs.add(getShowTraits().get()); + } + + executeCliProcess("select", + extraArgs, + getModels().get(), + true + ); + } +}