org.gradle.launcher.cli.CommandLineActionFactory Maven / Gradle / Ivy
Show all versions of gradle-api Show documentation
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.launcher.cli;
import groovy.lang.GroovySystem;
import org.apache.tools.ant.Main;
import org.gradle.api.Action;
import org.gradle.api.internal.file.IdentityFileResolver;
import org.gradle.api.logging.configuration.LoggingConfiguration;
import org.gradle.cli.CommandLineArgumentException;
import org.gradle.cli.CommandLineConverter;
import org.gradle.cli.CommandLineParser;
import org.gradle.cli.ParsedCommandLine;
import org.gradle.cli.SystemPropertiesCommandLineConverter;
import org.gradle.configuration.GradleLauncherMetaData;
import org.gradle.initialization.BuildLayoutParameters;
import org.gradle.initialization.DefaultParallelismConfiguration;
import org.gradle.initialization.LayoutCommandLineConverter;
import org.gradle.initialization.ParallelismConfiguration;
import org.gradle.initialization.ParallelismConfigurationCommandLineConverter;
import org.gradle.internal.Actions;
import org.gradle.internal.buildevents.BuildExceptionReporter;
import org.gradle.internal.jvm.Jvm;
import org.gradle.internal.jvm.inspection.CachingJvmVersionDetector;
import org.gradle.internal.jvm.inspection.DefaultJvmVersionDetector;
import org.gradle.internal.logging.DefaultLoggingConfiguration;
import org.gradle.internal.logging.LoggingCommandLineConverter;
import org.gradle.internal.logging.LoggingManagerInternal;
import org.gradle.internal.logging.services.LoggingServiceRegistry;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import org.gradle.internal.nativeintegration.services.NativeServices;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.internal.service.ServiceRegistry;
import org.gradle.launcher.bootstrap.ExecutionListener;
import org.gradle.launcher.cli.converter.LayoutToPropertiesConverter;
import org.gradle.launcher.cli.converter.PropertiesToParallelismConfigurationConverter;
import org.gradle.process.internal.DefaultExecActionFactory;
import org.gradle.util.GradleVersion;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Responsible for converting a set of command-line arguments into a {@link Runnable} action.
*/
public class CommandLineActionFactory {
private static final String HELP = "h";
private static final String VERSION = "v";
/**
* Converts the given command-line arguments to an {@link Action} which performs the action requested by the
* command-line args.
*
* @param args The command-line arguments.
* @return The action to execute.
*/
public Action convert(List args) {
ServiceRegistry loggingServices = createLoggingServices();
LoggingConfiguration loggingConfiguration = new DefaultLoggingConfiguration();
return new WithLogging(loggingServices,
args,
loggingConfiguration,
new ExceptionReportingAction(
new JavaRuntimeValidationAction(
new ParseAndBuildAction(loggingServices, args)),
new BuildExceptionReporter(loggingServices.get(StyledTextOutputFactory.class), loggingConfiguration, clientMetaData())));
}
protected void createActionFactories(ServiceRegistry loggingServices, Collection actions) {
actions.add(new GuiActionsFactory());
actions.add(new BuildActionsFactory(loggingServices, new ParametersConverter(), new CachingJvmVersionDetector(new DefaultJvmVersionDetector(new DefaultExecActionFactory(new IdentityFileResolver())))));
}
private static GradleLauncherMetaData clientMetaData() {
return new GradleLauncherMetaData();
}
public ServiceRegistry createLoggingServices() {
return LoggingServiceRegistry.newCommandLineProcessLogging();
}
private static void showUsage(PrintStream out, CommandLineParser parser) {
out.println();
out.print("USAGE: ");
clientMetaData().describeCommand(out, "[option...]", "[task...]");
out.println();
out.println();
parser.printUsage(out);
out.println();
}
private static class BuiltInActions implements CommandLineAction {
public void configureCommandLineParser(CommandLineParser parser) {
parser.option(HELP, "?", "help").hasDescription("Shows this help message.");
parser.option(VERSION, "version").hasDescription("Print version info.");
}
public Runnable createAction(CommandLineParser parser, ParsedCommandLine commandLine) {
if (commandLine.hasOption(HELP)) {
return new ShowUsageAction(parser);
}
if (commandLine.hasOption(VERSION)) {
return new ShowVersionAction();
}
return null;
}
}
private static class CommandLineParseFailureAction implements Action {
private final Exception e;
private final CommandLineParser parser;
public CommandLineParseFailureAction(CommandLineParser parser, Exception e) {
this.parser = parser;
this.e = e;
}
public void execute(ExecutionListener executionListener) {
System.err.println();
System.err.println(e.getMessage());
showUsage(System.err, parser);
executionListener.onFailure(e);
}
}
private static class ShowUsageAction implements Runnable {
private final CommandLineParser parser;
public ShowUsageAction(CommandLineParser parser) {
this.parser = parser;
}
public void run() {
showUsage(System.out, parser);
}
}
private static class ShowVersionAction implements Runnable {
public void run() {
GradleVersion currentVersion = GradleVersion.current();
final StringBuilder sb = new StringBuilder();
sb.append("%n------------------------------------------------------------%nGradle ");
sb.append(currentVersion.getVersion());
sb.append("%n------------------------------------------------------------%n%nBuild time: ");
sb.append(currentVersion.getBuildTime());
sb.append("%nRevision: ");
sb.append(currentVersion.getRevision());
sb.append("%n%nGroovy: ");
sb.append(GroovySystem.getVersion());
sb.append("%nAnt: ");
sb.append(Main.getAntVersion());
sb.append("%nJVM: ");
sb.append(Jvm.current());
sb.append("%nOS: ");
sb.append(OperatingSystem.current());
sb.append("%n");
System.out.println(String.format(sb.toString()));
}
}
private static class WithLogging implements Action {
private final ServiceRegistry loggingServices;
private final List args;
private final LoggingConfiguration loggingConfiguration;
private final Action action;
WithLogging(ServiceRegistry loggingServices, List args, LoggingConfiguration loggingConfiguration, Action action) {
this.loggingServices = loggingServices;
this.args = args;
this.loggingConfiguration = loggingConfiguration;
this.action = action;
}
public void execute(ExecutionListener executionListener) {
CommandLineConverter loggingConfigurationConverter = new LoggingCommandLineConverter();
CommandLineConverter buildLayoutConverter = new LayoutCommandLineConverter();
CommandLineConverter parallelConverter = new ParallelismConfigurationCommandLineConverter();
CommandLineConverter