Permalink
Browse files

refactored to use commons-cli for command line argument processing, a…

…dded -verbose, -help and -version options
  • Loading branch information...
1 parent f0a8c2e commit de9c675516effa1cf1184f3e147d3eb7152fb45f @graemerocher graemerocher committed Jun 6, 2011
View
6 build.gradle
@@ -24,6 +24,9 @@ springWebflowVersion= "2.0.8.RELEASE"
ivyVersion = "2.2.0"
gantVersion = "1.9.5"
groovyVersion = "1.8.0"
+jansiVersion = "1.2.1"
+jlineVersion = "0.9.94"
+commonsCliVersion = "1.2"
commonsCollectionsVersion = "3.2.1"
commonsBeanUtilsVersion = "1.8.0"
commonsIOVersion = "1.4"
@@ -232,6 +235,9 @@ task installResources << {
into "conf"
filter(ReplaceTokens, tokens: ['grails.version': grailsVersion])
filter(ReplaceTokens, tokens: ['groovy.version': groovyVersion])
+ filter(ReplaceTokens, tokens: ['jline.version': jlineVersion])
+ filter(ReplaceTokens, tokens: ['jansi.version': jansiVersion])
+ filter(ReplaceTokens, tokens: ['commons.cli.version': commonsCliVersion])
filter(ReplaceTokens, tokens: ['ant.version': antVersion])
filter(ReplaceTokens, tokens: ['gant.version': gantVersion])
filter(ReplaceTokens, tokens: ['ivy.version': ivyVersion])
View
150 grails-bootstrap/src/main/groovy/org/codehaus/groovy/grails/cli/GrailsScriptRunner.java
@@ -20,6 +20,7 @@
import groovy.lang.Closure;
import groovy.lang.ExpandoMetaClass;
import groovy.util.AntBuilder;
+import org.apache.commons.cli.*;
import org.apache.tools.ant.BuildLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.LogLevel;
@@ -35,6 +36,7 @@
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.Log4jConfigurer;
import org.springframework.util.ReflectionUtils;
+import org.springframework.util.StringUtils;
import java.beans.IntrospectionException;
import java.beans.Introspector;
@@ -49,6 +51,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import static org.apache.commons.cli.OptionBuilder.*;
+
/**
* Handles Grails command line interface for running scripts.
*
@@ -76,7 +80,10 @@
private static final Pattern scriptFilePattern = Pattern.compile("^[^_]\\w+\\.groovy$");
private static final Pattern pluginDescriptorPattern = Pattern.compile("^(\\S+)GrailsPlugin.groovy$");
- public static final String AGENT_FLAG = "-agent";
+ public static final String VERBOSE_ARGUMENT = "verbose";
+ public static final String AGENT_ARGUMENT = "agent";
+ public static final String VERSION_ARGUMENT = "version";
+ public static final String HELP_ARGUMENT = "help";
private InputStream orignalIn;
/**
@@ -86,17 +93,37 @@
* @param args
*/
public static void main(String[] args) {
- StringBuilder allArgs = new StringBuilder("");
- for (String arg : args) {
- allArgs.append(" ").append(arg);
- }
+ CommandLineParser parser = new GnuParser();
+ args = splitAndTrimArgs(args);
+
+ 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(HELP_ARGUMENT, "Command line help"));
+ options.addOption( new Option(VERSION_ARGUMENT, "Current Grails version"));
+
+ 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);
+ } catch (ParseException e) {
+ console.error("Error processing command line arguments: " + e.getMessage());
+ System.exit(1);
+ return;
+ }
+
console.updateStatus("Initializing");
- ScriptAndArgs script = processArgumentsAndReturnScriptName(allArgs.toString().trim());
+ ScriptAndArgs script = processArgumentsAndReturnScriptName(commandLine);
+
- console.getCategory().push(script.inputName);
// Get hold of the GRAILS_HOME environment variable if it is available.
String grailsHome = System.getProperty("grails.home");
@@ -117,6 +144,18 @@ public static void main(String[] args) {
exitWithError("Grails' installation directory not found: " + build.getGrailsHome());
}
+ if(commandLine.hasOption(VERSION_ARGUMENT)) {
+ console.log("Grails version: " + build.getGrailsVersion());
+ System.exit(0);
+ }
+
+ if(commandLine.hasOption(HELP_ARGUMENT)) {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp("grails [options] [command]",options);
+ System.exit(0);
+ }
+
+
// If there aren't any arguments, then we don't have a command
// to execute. So we have to exit.
@@ -125,6 +164,7 @@ public static void main(String[] args) {
System.exit(0);
}
+ console.getCategory().push(script.inputName);
console.updateStatus("Base Directory: " + build.getBaseDir().getPath());
try {
@@ -143,40 +183,52 @@ 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);
+ }
+ }
+
+ return allArgs.toString().trim().split(" ");
+ }
+
private static void exitWithError(String error) {
GrailsConsole.getInstance().error(error);
System.exit(1);
}
- private static ScriptAndArgs processArgumentsAndReturnScriptName(String allArgs) {
- ScriptAndArgs info = new ScriptAndArgs();
+ private static ScriptAndArgs processArgumentsAndReturnScriptName(CommandLine commandLine) {
+
+ if(commandLine.hasOption(VERBOSE_ARGUMENT)) {
+ GrailsConsole.getInstance().setVerbose(true);
+ }
+ processSystemArguments(commandLine);
+ String[] arguments = commandLine.getArgs();
- // Check that we actually have some arguments to process.
- if (allArgs == null || allArgs.length() == 0) return info;
+ if(arguments.length > 0) {
+ return processAndReturnArguments(arguments);
+ }
+ return new ScriptAndArgs();
+ }
- String[] splitArgs = processSystemArguments(allArgs).trim().split(" ");
+ private static ScriptAndArgs processAndReturnArguments(String[] arguments) {
+ ScriptAndArgs info = new ScriptAndArgs();
int currentParamIndex = 0;
if (Environment.isSystemSet()) {
info.env = Environment.getCurrent().getName();
}
- else if (isEnvironmentArgs(splitArgs[currentParamIndex])) {
+ else if (isEnvironmentArgs(arguments[currentParamIndex])) {
// use first argument as environment name and step further
- String env = splitArgs[currentParamIndex++];
+ String env = arguments[currentParamIndex++];
info.env = ENV_ARGS.get(env);
}
- abortIfOutOfBounds(splitArgs, currentParamIndex);
-
+ abortIfOutOfBounds(arguments, currentParamIndex);
// use current argument as script name and step further
- String paramName = splitArgs[currentParamIndex++];
-
- if(paramName.equals("-verbose")) {
- GrailsConsole.getInstance().setVerbose(true);
- abortIfOutOfBounds(splitArgs, currentParamIndex);
- paramName = splitArgs[currentParamIndex++];
-
- }
-
+ String paramName = arguments[currentParamIndex++];
if (paramName.charAt(0) == '-') {
@@ -185,11 +237,11 @@ else if (isEnvironmentArgs(splitArgs[currentParamIndex])) {
info.inputName = paramName;
info.name = GrailsNameUtils.getNameFromScript(paramName);
- if (currentParamIndex < splitArgs.length) {
+ if (currentParamIndex < arguments.length) {
// if we have additional params provided - store it in system property
- StringBuilder b = new StringBuilder(splitArgs[currentParamIndex]);
- for (int i = currentParamIndex + 1; i < splitArgs.length; i++) {
- b.append(' ').append(splitArgs[i]);
+ StringBuilder b = new StringBuilder(arguments[currentParamIndex]);
+ for (int i = currentParamIndex + 1; i < arguments.length; i++) {
+ b.append(' ').append(arguments[i]);
}
info.args = b.toString();
}
@@ -203,24 +255,13 @@ private static void abortIfOutOfBounds(String[] splitArgs, int currentParamIndex
}
}
- private static String processSystemArguments(String allArgs) {
- String lastMatch = null;
- Pattern sysPropPattern = Pattern.compile("-D(.+?)=(.+?)\\s+?");
-
- Matcher m = sysPropPattern.matcher(allArgs);
- while (m.find()) {
- System.setProperty(m.group(1).trim(), m.group(2).trim());
- lastMatch = m.group();
- }
-
- if (lastMatch != null) {
- int i = allArgs.lastIndexOf(lastMatch) + lastMatch.length();
- allArgs = allArgs.substring(i);
- }
- if (allArgs.contains(AGENT_FLAG)) {
- allArgs = allArgs.replace(AGENT_FLAG, "");
+ private static void processSystemArguments(CommandLine allArgs) {
+ Properties systemProps = allArgs.getOptionProperties("D");
+ if(systemProps != null) {
+ for (Map.Entry<Object, Object> entry : systemProps.entrySet()) {
+ System.setProperty(entry.getKey().toString(), entry.getValue().toString());
+ }
}
- return allArgs;
}
private static boolean isEnvironmentArgs(String env) {
@@ -366,7 +407,7 @@ private void runInteractive() {
String enteredName = helper.userInput(prompt);
if (enteredName != null && enteredName.trim().length() > 0) {
- script = processArgumentsAndReturnScriptName(enteredName);
+ script = processAndReturnArguments(enteredName.split(" "));
// Update the relevant system property, otherwise the
// arguments will be "remembered" from the previous run.
@@ -847,13 +888,18 @@ private void setUIListener(GantBinding binding) {
AntBuilder ant = (AntBuilder) binding.getVariable("ant");
binding.setVariable("Ant", ant);
Project project = ant.getProject();
- project.addBuildListener(new GrailsConsoleBuildListener(GrailsConsole.getInstance()));
- Vector buildListeners = project.getBuildListeners();
- for (Object buildListener : buildListeners) {
- if(buildListener instanceof BuildLogger) {
- ((BuildLogger)buildListener).setMessageOutputLevel(LogLevel.ERR.getLevel());
+ GrailsConsole instance = GrailsConsole.getInstance();
+ project.addBuildListener(new GrailsConsoleBuildListener(instance));
+
+ if(!instance.isVerbose()) {
+ Vector buildListeners = project.getBuildListeners();
+ for (Object buildListener : buildListeners) {
+ if(buildListener instanceof BuildLogger) {
+ ((BuildLogger)buildListener).setMessageOutputLevel(LogLevel.ERR.getLevel());
+ }
}
}
+
}
protected void makeApiAvailableToScripts(final GantBinding binding, final Object cla) {
View
4 grails-bootstrap/src/main/groovy/org/codehaus/groovy/grails/cli/logging/GrailsConsole.java
@@ -114,6 +114,10 @@ public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
+ public boolean isVerbose() {
+ return verbose;
+ }
+
public InputStream getInput() {
return reader.getInput();
}
View
2 grails-core/src/main/groovy/org/codehaus/groovy/grails/plugins/CoreGrailsPlugin.groovy
@@ -96,7 +96,7 @@ class CoreGrailsPlugin {
}
// add shutdown hook if not running in war deployed mode
- if (!Metadata.getCurrent().isWarDeployed() || Environment.currentEnvironment == Environment.DEVELOPMENT) {
+ if (!Metadata.getCurrent().isWarDeployed() && Environment.currentEnvironment == Environment.DEVELOPMENT) {
shutdownHook(DevelopmentShutdownHook)
}
View
7 grails-resources/src/grails/home/conf/groovy-starter.conf
@@ -9,9 +9,10 @@
## Note: do not add classes from java.lang here. No rt.jar and no tools.jar
##
# Load required libraries
- load ${grails.home}/dist/grails-bootstrap-${grails.version}.jar
- load ${grails.home}/lib/jline/jline/jars/jline-0.9.94.jar
- load ${grails.home}/lib/org.fusesource.jansi/jansi/jars/jansi-1.2.1.jar
+ load ${grails.home}/dist/grails-bootstrap-@grails.version@.jar
+ load ${grails.home}/lib/jline/jline/jars/jline-@jline.version@.jar
+ load ${grails.home}/lib/org.fusesource.jansi/jansi/jars/jansi-@jansi.version@.jar
+ load ${grails.home}/lib/commons-cli/commons-cli/jars/commons-cli-@commons.cli.version@.jar
load ${grails.home}/lib/org.codehaus.groovy/groovy-all/jars/groovy-all-@groovy.version@.jar
load ${grails.home}/lib/org.apache.ivy/ivy/jars/ivy-@ivy.version@.jar
load ${grails.home}/lib/org.apache.ant/ant/jars/ant-@ant.version@.jar
View
4 grails-test/src/main/groovy/org/codehaus/groovy/grails/test/event/GrailsTestRunNotifier.java
@@ -15,8 +15,8 @@
*/
package org.codehaus.groovy.grails.test.event;
+import grails.util.GrailsUtil;
import org.codehaus.groovy.grails.cli.logging.GrailsConsole;
-import org.codehaus.groovy.runtime.StackTraceUtils;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -61,7 +61,7 @@ public void fireTestFailure(Failure failure) {
console.error("FAILURE: " + failure.getDescription().getDisplayName());
Throwable exception = failure.getException();
if(exception != null) {
- StackTraceUtils.deepSanitize(exception);
+ GrailsUtil.deepSanitize(exception);
deepSanitize(exception);
StringWriter sw = new StringWriter();
PrintWriter ps = new PrintWriter(sw);

0 comments on commit de9c675

Please sign in to comment.