Permalink
Browse files

Rewrote command line parsing and removed dependency on commons CLI

  • Loading branch information...
1 parent 188a244 commit fe9a4f9819ed349308005dc050ce7aebd0567692 Graeme Rocher committed Jun 17, 2011
View
@@ -25,7 +25,7 @@ ivyVersion = "2.2.0"
gantVersion = "1.9.5"
groovyVersion = "1.8.0"
jansiVersion = "1.2.1"
-jlineVersion = "0.9.94"
+jlineVersion = "1.0"
jnaVersion = "3.2.3"
commonsCliVersion = "1.2"
commonsCollectionsVersion = "3.2.1"
@@ -91,7 +91,10 @@ subprojects { project ->
dependencies {
// Groovy
- groovy groovyDependency
+ groovy( groovyDependency ) {
+ exclude module:"commons-cli"
+ exclude module:"ant"
+ }
compile groovyDependency
// Logging
@@ -1,11 +1,21 @@
import org.apache.tools.ant.filters.ReplaceTokens
dependencies {
- compile "org.springframework:spring-core:${springVersion}",
- "org.springframework:spring-beans:${springVersion}",
- "org.springframework:spring-test:${springVersion}",
- "org.springframework:spring-context:${springVersion}",
- "org.springframework:spring-context-support:${springVersion}"
+ compile( "org.springframework:spring-core:${springVersion}") {
+ exclude module:"commons-logging"
+ }
+ compile("org.springframework:spring-beans:${springVersion}") {
+ exclude module:"commons-logging"
+ }
+ compile("org.springframework:spring-test:${springVersion}") {
+ exclude module:"commons-logging"
+ }
+ compile("org.springframework:spring-context:${springVersion}") {
+ exclude module:"commons-logging"
+ }
+ compile("org.springframework:spring-context-support:${springVersion}") {
+ exclude module:"commons-logging"
+ }
compile("org.springframework.uaa:org.springframework.uaa.client:1.0.1.RELEASE") {
@@ -22,19 +32,22 @@ dependencies {
"org.bouncycastle:bcpg-jdk15:1.45"
compile "commons-lang:commons-lang:$commonsLangVersion"
- compile 'commons-cli:commons-cli:1.2'
// Ant
- compile 'org.fusesource.jansi:jansi:1.2.1',
- 'jline:jline:1.0',
- 'net.java.dev.jna:jna:3.2.3'
+ compile "org.fusesource.jansi:jansi:$jansiVersion",
+ "jline:jline:$jlineVersion",
+ "net.java.dev.jna:jna:$jnaVersion"
compile "org.apache.ant:ant:${antVersion}",
"org.apache.ant:ant-launcher:${antVersion}",
"org.apache.ant:ant-junit:${antVersion}",
"org.apache.ant:ant-trax:${antTraxVersion}",
- "org.apache.ivy:ivy:${ivyVersion}",
- "org.codehaus.gant:gant_groovy1.8:${gantVersion}"
+ "org.apache.ivy:ivy:${ivyVersion}"
+
+ compile("org.codehaus.gant:gant_groovy1.8:${gantVersion}") {
+ exclude module:'commons-cli'
+ }
+
}
processResources {
@@ -15,7 +15,7 @@
*/
package org.codehaus.groovy.grails.cli;
-import static org.apache.commons.cli.OptionBuilder.withArgName;
+
import gant.Gant;
import grails.build.logging.GrailsConsole;
import grails.util.BuildSettings;
@@ -50,17 +50,15 @@
import java.util.Set;
import java.util.regex.Pattern;
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GnuParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
+
import org.apache.tools.ant.Project;
import org.codehaus.gant.GantBinding;
import org.codehaus.gant.GantMetaClass;
import org.codehaus.groovy.grails.cli.interactive.InteractiveMode;
+import org.codehaus.groovy.grails.cli.parsing.CommandLine;
+import org.codehaus.groovy.grails.cli.parsing.CommandLineParser;
+import org.codehaus.groovy.grails.cli.parsing.DefaultCommandLine;
+import org.codehaus.groovy.grails.cli.parsing.ParseException;
import org.codehaus.groovy.grails.cli.support.ClasspathConfigurer;
import org.codehaus.groovy.grails.cli.support.PluginPathDiscoverySupport;
import org.codehaus.groovy.grails.cli.support.ScriptBindingInitializer;
@@ -80,37 +78,21 @@
*/
public class GrailsScriptRunner {
- private static Map<String, String> ENV_ARGS = new HashMap<String, String>();
- // this map contains default environments for several scripts in form 'script-name':'env-code'
- private static Map<String, String> DEFAULT_ENVS = new HashMap<String, String>();
-
- static {
- ENV_ARGS.put("dev", Environment.DEVELOPMENT.getName());
- ENV_ARGS.put("prod", Environment.PRODUCTION.getName());
- ENV_ARGS.put("test", Environment.TEST.getName());
- DEFAULT_ENVS.put("War", Environment.PRODUCTION.getName());
- DEFAULT_ENVS.put("TestApp", Environment.TEST.getName());
- DEFAULT_ENVS.put("RunWebtest", Environment.TEST.getName());
- ExpandoMetaClass.enableGlobally();
- // disable annoying ehcache up-to-date check
- System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
- }
-
private static final Pattern scriptFilePattern = Pattern.compile("^[^_]\\w+\\.groovy$");
public static final String VERBOSE_ARGUMENT = "verbose";
public static final String AGENT_ARGUMENT = "reloading";
public static final String VERSION_ARGUMENT = "version";
public static final String HELP_ARGUMENT = "help";
- public static final String NON_INTERACTIVE_ARGUMENT = "nonInteractive";
+ public static final String NON_INTERACTIVE_ARGUMENT = "non-interactive";
@SuppressWarnings("rawtypes")
public static final Closure DO_NOTHING_CLOSURE = new Closure(GrailsScriptRunner.class) {
private static final long serialVersionUID = 1L;
@Override public Object call(Object arguments) { return null; }
@Override public Object call() { return null; }
@Override public Object call(Object... args) { return null; }
};
- public static final String NOANSI_ARGUMENT = "plainOutput";
+ public static final String NOANSI_ARGUMENT = "plain-output";
private static InputStream originalIn;
private static PrintStream originalOut;
@@ -124,6 +106,7 @@
private File scriptCacheDir;
private final List<Resource> scriptsAllowedOutsideOfProject = new ArrayList<Resource>();
+ private boolean useDefaultEnv = true;
public GrailsScriptRunner() {
this(new BuildSettings());
@@ -158,30 +141,21 @@ public static void main(String[] args) {
originalIn = System.in;
originalOut = System.out;
- CommandLineParser parser = new GnuParser();
- args = splitAndTrimArgs(args);
+ CommandLineParser parser = getCommandLineParser();
-
- Options options = new Options();
- options.addOption(new Option(VERBOSE_ARGUMENT, "Enable verbose output"));
- options.addOption(new Option(AGENT_ARGUMENT, "Enable the reloading agent"));
- options.addOption(new Option(NON_INTERACTIVE_ARGUMENT, "Whether to allow the command line to request input"));
- options.addOption(new Option(HELP_ARGUMENT, "Command line help"));
- options.addOption(new Option(VERSION_ARGUMENT, "Current Grails version"));
- options.addOption(new Option(NOANSI_ARGUMENT, "Disables ANSI output"));
-
- options.addOption(withArgName("property=value")
- .hasArgs(2)
- .withValueSeparator()
- .withDescription("Used to specify System properties")
- .create("D"));
GrailsConsole console = GrailsConsole.getInstance();
CommandLine commandLine;
try {
- commandLine = parser.parse(options, args);
- if (commandLine.hasOption(NOANSI_ARGUMENT)) {
- console.setAnsiEnabled(false);
+ if(args.length == 0) {
+ commandLine = new DefaultCommandLine();
+ }
+ else {
+ commandLine = parser.parseString(args[0]);
+ if (commandLine.hasOption(NOANSI_ARGUMENT)) {
+ console.setAnsiEnabled(false);
+ }
+
}
} catch (ParseException e) {
console.error("Error processing command line arguments: " + e.getMessage());
@@ -221,8 +195,7 @@ public static void main(String[] args) {
}
if (commandLine.hasOption(HELP_ARGUMENT)) {
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp("grails [options] [command]",options);
+ console.log(parser.getHelpMessage());
System.exit(0);
}
@@ -262,22 +235,20 @@ public static void main(String[] args) {
}
}
- private static String[] splitAndTrimArgs(String[] args) {
- StringBuilder allArgs = new StringBuilder("");
- for (String arg : args) {
- arg = arg.trim();
- if (arg.length()>0) {
- allArgs.append(" ").append(arg);
- }
- }
+ public static CommandLineParser getCommandLineParser() {
+ CommandLineParser parser = new CommandLineParser();
- String allArgsString = allArgs.toString().trim();
- if (allArgsString.length() == 0) {
- return new String[0];
- }
- return allArgsString.split(" ");
+
+ parser.addOption(VERBOSE_ARGUMENT, "Enable verbose output");
+ parser.addOption(AGENT_ARGUMENT, "Enable the reloading agent");
+ parser.addOption(NON_INTERACTIVE_ARGUMENT, "Whether to allow the command line to request input");
+ parser.addOption(HELP_ARGUMENT, "Command line help");
+ parser.addOption(VERSION_ARGUMENT, "Current Grails version");
+ parser.addOption(NOANSI_ARGUMENT, "Disables ANSI output");
+ return parser;
}
+
private static void exitWithError(String error) {
GrailsConsole.getInstance().error(error);
System.exit(1);
@@ -290,45 +261,21 @@ private static ScriptAndArgs processArgumentsAndReturnScriptName(CommandLine com
}
processSystemArguments(commandLine);
- String[] arguments = commandLine.getArgs();
-
- if (arguments.length > 0) {
- return processAndReturnArguments(arguments);
- }
- return new ScriptAndArgs();
+ return processAndReturnArguments(commandLine);
}
- private static ScriptAndArgs processAndReturnArguments(String[] arguments) {
+ private static ScriptAndArgs processAndReturnArguments(CommandLine commandLine) {
ScriptAndArgs info = new ScriptAndArgs();
- int currentParamIndex = 0;
if (Environment.isSystemSet()) {
info.env = Environment.getCurrent().getName();
}
- else if (isEnvironmentArgs(arguments[currentParamIndex])) {
- // use first argument as environment name and step further
- String env = arguments[currentParamIndex++];
- info.env = ENV_ARGS.get(env);
+ else if (commandLine.getEnvironment() != null) {
+ info.env = commandLine.getEnvironment();
}
- abortIfOutOfBounds(arguments, currentParamIndex);
- // use current argument as script name and step further
- String paramName = arguments[currentParamIndex++];
-
-
- if (paramName.charAt(0) == '-') {
- paramName = paramName.substring(1);
- }
- info.inputName = paramName;
- info.name = GrailsNameUtils.getNameFromScript(paramName);
-
- if (currentParamIndex < arguments.length) {
- // if we have additional params provided - store it in system property
- StringBuilder b = new StringBuilder(arguments[currentParamIndex]);
- for (int i = currentParamIndex + 1; i < arguments.length; i++) {
- b.append(' ').append(arguments[i]);
- }
- info.args = b.toString();
- }
+ info.inputName = commandLine.getCommandName();
+ info.name = GrailsNameUtils.getNameFromScript(commandLine.getCommandName());
+ info.args = commandLine.getRemainingArgsString();
return info;
}
@@ -340,18 +287,14 @@ private static void abortIfOutOfBounds(String[] splitArgs, int currentParamIndex
}
private static void processSystemArguments(CommandLine allArgs) {
- Properties systemProps = allArgs.getOptionProperties("D");
+ Properties systemProps = allArgs.getSystemProperties();
if (systemProps != null) {
for (Map.Entry<Object, Object> entry : systemProps.entrySet()) {
System.setProperty(entry.getKey().toString(), entry.getValue().toString());
}
}
}
- private static boolean isEnvironmentArgs(String env) {
- return ENV_ARGS.containsKey(env);
- }
-
public PrintStream getOut() {
return out;
}
@@ -408,12 +351,6 @@ public int executeCommand(String scriptName, String args, String env) {
private void setRunningEnvironment(String scriptName, String env) {
// Get the default environment if one hasn't been set.
- boolean useDefaultEnv = env == null;
- if (useDefaultEnv) {
- env = DEFAULT_ENVS.get(scriptName);
- env = env != null ? env : Environment.DEVELOPMENT.getName();
- }
-
System.setProperty("base.dir", settings.getBaseDir().getPath());
System.setProperty(Environment.KEY, env);
System.setProperty(Environment.DEFAULT, "true");
@@ -428,15 +365,15 @@ private int callPluginOrGrailsScript(String scriptName, String env) {
return executeScriptWithCaching(scriptName, env);
}
- public int executeScriptWithCaching(String scriptName, String env, String args) {
- if (args != null) {
- System.setProperty("grails.cli.args", args.replace(' ', '\n'));
- }
- return executeScriptWithCaching(scriptName, env);
+ public int executeScriptWithCaching(CommandLine commandLine) {
+ processSystemArguments(commandLine);
+
+ System.setProperty("grails.cli.args", commandLine.getRemainingArgsLineSeparated());
+ return executeScriptWithCaching(GrailsNameUtils.getNameFromScript(commandLine.getCommandName()), commandLine.getEnvironment());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- public int executeScriptWithCaching(String scriptName, String env) {
+ private int executeScriptWithCaching(String scriptName, String env) {
List<Resource> potentialScripts;
List<Resource> allScripts = getAvailableScripts();
GantBinding binding = new GantBinding();
@@ -23,6 +23,7 @@ import grails.util.GrailsNameUtils
import org.codehaus.groovy.grails.cli.GrailsScriptRunner
import org.codehaus.groovy.grails.cli.ScriptNotFoundException
import org.codehaus.groovy.grails.cli.support.MetaClassRegistryCleaner
+import org.codehaus.groovy.grails.cli.parsing.ParseException
/**
* Provides the implementation of interactive mode in Grails.
@@ -98,17 +99,16 @@ class InteractiveMode {
error "Error occurred executing process: ${e.message}"
}
}
- else if (scriptName.contains(" ")) {
- def i = scriptName.indexOf(" ")
- def args = scriptName[i..-1]
- scriptName = scriptName[0..i]
- scriptRunner.executeScriptWithCaching(GrailsNameUtils.getNameFromScript(scriptName),
- Environment.current.name, args)
- }
else {
- scriptRunner.executeScriptWithCaching(GrailsNameUtils.getNameFromScript(scriptName),
- Environment.current.name)
+ def parser = GrailsScriptRunner.getCommandLineParser()
+ try {
+ def commandLine = parser.parseString(scriptName)
+ scriptRunner.executeScriptWithCaching(commandLine)
+ } catch (ParseException e) {
+ error "Invalid command: ${e.message}"
+ }
}
+
}
else {
error "Not script name specified"
Oops, something went wrong.

0 comments on commit fe9a4f9

Please sign in to comment.