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

Remove unecessary indirection added by SmithyBaseTask class #134

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -7,32 +7,67 @@

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.gradle.StartParameter;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.configuration.ShowStacktrace;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.internal.logging.text.StyledTextOutput;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import org.gradle.work.DisableCachingByDefault;
import org.gradle.workers.WorkerExecutor;
import software.amazon.smithy.gradle.SmithyUtils;

/**
* Abstract class used to share functionality across Smithy CLI tasks
* (that is, tasks that are meant to be run ad-hoc).
*/
@DisableCachingByDefault(because = "Abstract super-class, not to be instantiated directly")
abstract class AbstractSmithyCliTask extends BaseSmithyTask {
abstract class AbstractSmithyCliTask extends DefaultTask {

/**
* Object factory used to create new gradle domain objects such as {@code FileCollection}s.
*/
protected final ObjectFactory objectFactory;
private final StartParameter startParameter;

AbstractSmithyCliTask(ObjectFactory objectFactory) {
AbstractSmithyCliTask(ObjectFactory objectFactory, StartParameter startParameter) {
this.objectFactory = objectFactory;
this.startParameter = startParameter;

getFork().convention(false);
getShowStackTrace().convention(ShowStacktrace.INTERNAL_EXCEPTIONS);
getAllowUnknownTraits().convention(false);

// By default, the build classpath and model discovery classpaths are empty file collections.
getBuildClasspath().set(getProject().files());
getModelDiscoveryClasspath().set(getProject().files());

// if the smithyCli configuration exists use it by default
if (getProject().getConfigurations().findByName(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME) != null) {
getCliClasspath().convention(getProject().getConfigurations()
.getByName(SmithyUtils.SMITHY_CLI_CONFIGURATION_NAME));
}
}

/** Sets whether to fail a Smithy CLI task if an unknown trait is encountered.
/**
* Base classpath used for executing the smithy cli.
*
* <p>Note: this classpath must contain the smithy-cli jar.
*/
@Classpath
public abstract Property<FileCollection> getCliClasspath();

/**
* Sets whether to fail a Smithy CLI task if an unknown trait is encountered.
*
* <p> Defaults to {@code false}
*
Expand All @@ -42,6 +77,85 @@ abstract class AbstractSmithyCliTask extends BaseSmithyTask {
@Optional
public abstract Property<Boolean> getAllowUnknownTraits();

/**
* Classpath used for discovery of additional Smithy models during cli execution.
*
* <p>Defaults to an empty collection.
*/
@Classpath
@Optional
public abstract Property<FileCollection> getModelDiscoveryClasspath();

/**
* Classpath to use for build dependencies.
*/
@Classpath
@Optional
public abstract Property<FileCollection> getBuildClasspath();

/**
* Gets the list of models to execute a CLI command on.
*
* <p>These models are also considered "sources" when building a JAR
* for a project. A source model is a model that appears in the
* {@code META-INF/smithy} directory of a JAR.
*
* <p>This method will return an empty {@code FileCollection} and
* never {@code null}.
*
* @return Returns the models to validate.
*/
@InputFiles
@Optional
public abstract Property<FileCollection> getModels();

/**
* Whether to fork a new process for executing the smithy cli.
*
* <p> If false, the smithy cli will be executed in a new thread instead of a
* new process
*
* <p> Defaults to {@code false}
*
* @return flag indicating fork setting.
*/
@Input
@Optional
public abstract Property<Boolean> getFork();

/**
* Sets the detail to include in stack traces.
*
* <p>Defaults to {@code ShowStacktrace.INTERNAL_EXCEPTIONS}
*
* @return stack trace setting.
*/
@Input
@Optional
public abstract Property<ShowStacktrace> getShowStackTrace();

/**
* Read-only property that returns the classpath used to determine the
* classpath used when executing the cli.
*
* @return classpath to use when executing cli command.
*/
@Internal
Provider<FileCollection> getCliExecutionClasspath() {
return getCliClasspath().zip(getBuildClasspath(), FileCollection::plus);
}

/**
* Read-only property that returns a worker executor to use for executing a CLI
* command.
*
* @return executor to use for CLI command.
*/
@Internal
WorkerExecutor getExecutor() {
return getServices().get(WorkerExecutor.class);
}

/**
* Executes the given CLI command.
*
Expand All @@ -52,7 +166,7 @@ abstract class AbstractSmithyCliTask extends BaseSmithyTask {
* @param additionalArgs Custom arguments that aren't one of the shared args.
* @param sources Source files to execute the command on
*/
final void executeCliProcess(String command,
protected void executeCliProcess(String command,
List<String> additionalArgs,
FileCollection sources,
boolean disableModelDiscovery
Expand Down Expand Up @@ -81,4 +195,51 @@ final void executeCliProcess(String command,

SmithyUtils.executeCli(getExecutor(), args, getCliExecutionClasspath().get(), getFork().get());
}

/**
* Writes header-formatted text to the build output.
*
* @param text text to write as a header.
*/
protected void writeHeading(String text) {
StyledTextOutput output = getServices().get(StyledTextOutputFactory.class)
.create("smithy")
.style(StyledTextOutput.Style.Header);
output.println(text);
}

/**
* Add --stacktrace and --logging settings based on Gradle's settings.
*/
protected void configureLoggingOptions(final List<String> args) {
ShowStacktrace showStacktrace = getShowStackTrace().get();
if (showStacktrace == ShowStacktrace.ALWAYS || showStacktrace == ShowStacktrace.ALWAYS_FULL) {
args.add("--stacktrace");
}

switch (startParameter.getLogLevel()) {
case DEBUG:
args.add("--debug");
break;
case LIFECYCLE: // The default setting in Gradle, so use Smithy's default of WARNING.
case WARN:
args.add("--logging");
args.add(Level.WARNING.toString());
break;
case QUIET:
args.add("--logging");
args.add(Level.OFF.toString());
args.add("--quiet");
break;
case ERROR:
args.add("--logging");
args.add(Level.SEVERE.toString());
break;
case INFO:
default:
args.add("--logging");
args.add(Level.INFO.toString());
break;
}
}
}
Loading
Loading