-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Smithy select task that allows users to execute `smithy select` within their gradle projects.
- Loading branch information
Showing
4 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
examples/base-plugin/uses-explicitly-set-cli-version/model/main.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
namespace smithy.example | ||
|
||
structure Foo { | ||
/// a string member | ||
bar: String | ||
} |
63 changes: 63 additions & 0 deletions
63
smithy-base/src/it/java/software/amazon/smithy/gradle/SelectTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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; | ||
import software.amazon.smithy.model.node.Node; | ||
|
||
public class SelectTaskTest { | ||
@Test | ||
public void selectsString() { | ||
Utils.withCopy("base-plugin/uses-explicitly-set-cli-version", buildDir -> { | ||
BuildResult result = GradleRunner.create() | ||
.forwardOutput() | ||
.withProjectDir(buildDir) | ||
.withArguments("select", "--selector", "member") | ||
.build(); | ||
|
||
Utils.assertSmithyBuildDidNotRun(result); | ||
|
||
Utils.assertArtifactsNotCreated(buildDir, | ||
"build/smithyprojections/uses-explicitly-set-cli-version/source/build-info/smithy-build-info.json"); | ||
|
||
Assertions.assertTrue(result.getOutput().contains("smithy.example#Foo$bar")); | ||
}); | ||
} | ||
|
||
@Test | ||
public void selectsWithShowString() { | ||
Utils.withCopy("base-plugin/uses-explicitly-set-cli-version", buildDir -> { | ||
BuildResult result = GradleRunner.create() | ||
.forwardOutput() | ||
.withProjectDir(buildDir) | ||
.withArguments("select", "--selector", "member", "--show", "type,vars") | ||
.build(); | ||
|
||
Utils.assertSmithyBuildDidNotRun(result); | ||
|
||
Utils.assertArtifactsNotCreated(buildDir, | ||
"build/smithyprojections/uses-explicitly-set-cli-version/source/build-info/smithy-build-info.json"); | ||
|
||
Assertions.assertTrue(result.getOutput().contains("\"shape\": \"smithy.example#Foo$bar\"")); | ||
}); | ||
} | ||
|
||
@Test | ||
public void selectsWithShowTraitsString() { | ||
Utils.withCopy("base-plugin/uses-explicitly-set-cli-version", buildDir -> { | ||
BuildResult result = GradleRunner.create() | ||
.forwardOutput() | ||
.withProjectDir(buildDir) | ||
.withArguments("select", "--selector", "structure > member", "--show-traits", "documentation", "--stacktrace") | ||
.build(); | ||
|
||
Utils.assertSmithyBuildDidNotRun(result); | ||
|
||
Utils.assertArtifactsNotCreated(buildDir, | ||
"build/smithyprojections/uses-explicitly-set-cli-version/source/build-info/smithy-build-info.json"); | ||
|
||
Assertions.assertTrue(result.getOutput().contains("\"smithy.api#documentation\": \"a string member\"")); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
smithy-base/src/main/java/software/amazon/smithy/gradle/tasks/SmithySelectTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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; | ||
import org.gradle.work.DisableCachingByDefault; | ||
|
||
|
||
/** | ||
* Executes the Smithy CLI {@code select} command on a set of source files. | ||
* | ||
* <p>This task queries a set of models from the provided sources using a selector. | ||
* | ||
* <p>NOTE: this task must be executed with the command line option `--selector` set. | ||
* | ||
* @see <a href="https://smithy.io/2.0/spec/selectors.html#selectors">Smithy Selectors</a> | ||
*/ | ||
@DisableCachingByDefault(because = "Select task should only be called manually.") | ||
public abstract class SmithySelectTask extends AbstractSmithyCliTask { | ||
private static final String DESCRIPTION = "Queries Smithy models with a selector."; | ||
|
||
@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 | ||
); | ||
} | ||
} |