Skip to content

Commit

Permalink
Add Smithy select task
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema committed Mar 20, 2024
1 parent 45eda40 commit aaf9d71
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
});
});
}
Expand Down Expand Up @@ -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<SmithyBuildTask> addBuildTaskForSourceSet(SourceSet sourceSet,
SmithySourceDirectorySet sds,
SmithyExtension extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public NamedDomainObjectContainer<SmithySourceDirectorySet> getSourceSets() {
return this.sourceSets;
}


/**
* Gets whether to execute the format task on files in the Smithy source set
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.
*
* <p>WARNING: This task will mutate input source files and change their formatting in-place.
*
* <p>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<String> getSelector();


@Input
@Optional
@Option(option = "show", description = "The Smithy selector to execute")
abstract Property<String> getShow();

@Input
@Optional
@Option(option = "show-traits", description = "The Smithy selector to execute")
abstract Property<String> getShowTraits();

@TaskAction
public void execute() {
if (!getSelector().isPresent()) {
throw new GradleException("Select task requires that the command line option `--select` be set.");
}
List<String> 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
);
}
}

0 comments on commit aaf9d71

Please sign in to comment.