Browse files

Removed redundant arg parsing logic since this is now handled by Grai…

…lsScriptRunner and CommandLineParser
  • Loading branch information...
1 parent 5aea3ba commit e6a887c483a3290957b726cdd3fcb82e7d6ddaa2 Graeme Rocher committed Jul 13, 2011
View
33 grails-bootstrap/src/main/groovy/org/codehaus/groovy/grails/cli/GrailsScriptRunner.java
@@ -218,8 +218,8 @@ public static void main(String[] args) {
console.verbose("Base Directory: " + build.getBaseDir().getPath());
try {
- int exitCode = scriptRunner.executeCommand(
- script.name, script.args, script.env);
+ int exitCode = scriptRunner.executeCommand(commandLine,
+ script.name, script.env);
System.exit(exitCode);
}
catch (ScriptNotFoundException ex) {
@@ -314,6 +314,13 @@ public int executeCommand(String scriptName, String args, String env) {
System.setProperty("grails.cli.args", "");
}
+ CommandLineParser parser = getCommandLineParser();
+ CommandLine commandLine = parser.parseString(args);
+
+ return executeCommand(commandLine, scriptName, env);
+ }
+
+ private int executeCommand(CommandLine commandLine, String scriptName, String env) {
@SuppressWarnings("hiding") GrailsConsole console = GrailsConsole.getInstance();
// Load the BuildSettings file for this project if it exists. Note
// that this does not load any environment-specific settings.
@@ -341,7 +348,7 @@ public int executeCommand(String scriptName, String args, String env) {
BuildSettingsHolder.setSettings(settings);
- return callPluginOrGrailsScript(scriptName, env);
+ return callPluginOrGrailsScript(commandLine, scriptName, env);
}
private void setRunningEnvironment(String env) {
@@ -355,20 +362,20 @@ private void setRunningEnvironment(String env) {
settings.setDefaultEnv(useDefaultEnv);
}
- private int callPluginOrGrailsScript(String scriptName, String env) {
+ private int callPluginOrGrailsScript(CommandLine commandLine, String scriptName, String env) {
initializeState(scriptName);
- return executeScriptWithCaching(scriptName, env);
+ return executeScriptWithCaching(commandLine,scriptName, env);
}
public int executeScriptWithCaching(CommandLine commandLine) {
processSystemArguments(commandLine);
System.setProperty("grails.cli.args", commandLine.getRemainingArgsLineSeparated());
- return executeScriptWithCaching(GrailsNameUtils.getNameFromScript(commandLine.getCommandName()), commandLine.getEnvironment());
+ return executeScriptWithCaching(commandLine, GrailsNameUtils.getNameFromScript(commandLine.getCommandName()), commandLine.getEnvironment());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- private int executeScriptWithCaching(String scriptName, String env) {
+ private int executeScriptWithCaching(CommandLine commandLine, String scriptName, String env) {
List<Resource> potentialScripts;
List<Resource> allScripts = getAvailableScripts();
GantBinding binding = new GantBinding();
@@ -404,20 +411,20 @@ private int executeScriptWithCaching(String scriptName, String env) {
if (!isGrailsProject() && !isExternalScript(scriptFile)) {
return handleScriptExecutedOutsideProjectError();
}
- return executeScriptFile(scriptName, env, binding, scriptFile);
+ return executeScriptFile(commandLine, scriptName, env, binding, scriptFile);
}
- return attemptPrecompiledScriptExecute(scriptName, env, binding, allScripts);
+ return attemptPrecompiledScriptExecute(commandLine, scriptName, env, binding, allScripts);
}
- private int attemptPrecompiledScriptExecute(String scriptName, String env, GantBinding binding, List<Resource> allScripts) {
+ private int attemptPrecompiledScriptExecute(CommandLine commandLine, String scriptName, String env, GantBinding binding, List<Resource> allScripts) {
console.updateStatus("Running pre-compiled script");
// Must be called before the binding is initialised.
setRunningEnvironment(env);
// Get Gant to load the class by name using our class loader.
- ScriptBindingInitializer bindingInitializer = new ScriptBindingInitializer(
+ ScriptBindingInitializer bindingInitializer = new ScriptBindingInitializer(commandLine,
settings, pluginPathSupport, isInteractive);
Gant gant = new Gant(bindingInitializer.initBinding(binding, scriptName), classLoader);
@@ -440,15 +447,15 @@ private int attemptPrecompiledScriptExecute(String scriptName, String env, GantB
return executeWithGantInstance(gant, DO_NOTHING_CLOSURE).exitCode;
}
- private int executeScriptFile(String scriptName, String env, GantBinding binding, Resource scriptFile) {
+ private int executeScriptFile(CommandLine commandLine, String scriptName, String env, GantBinding binding, Resource scriptFile) {
// We can now safely set the default environment
String scriptFileName = getScriptNameFromFile(scriptFile);
setRunningEnvironment(env);
binding.setVariable("scriptName", scriptFileName);
// Setup the script to call.
ScriptBindingInitializer bindingInitializer = new ScriptBindingInitializer(
- settings, pluginPathSupport, isInteractive);
+ commandLine, settings, pluginPathSupport, isInteractive);
Gant gant = new Gant(bindingInitializer.initBinding(binding, scriptName), classLoader);
gant.setUseCache(true);
gant.setCacheDirectory(scriptCacheDir);
View
4 grails-bootstrap/src/main/groovy/org/codehaus/groovy/grails/cli/parsing/CommandLine.java
@@ -16,6 +16,7 @@
package org.codehaus.groovy.grails.cli.parsing;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
/**
@@ -83,4 +84,7 @@
* @return The remaining args separated by the line separator char
*/
String getRemainingArgsLineSeparated();
+
+
+ Map<String, Object> getUndeclaredOptions();
}
View
4 ...-bootstrap/src/main/groovy/org/codehaus/groovy/grails/cli/parsing/DefaultCommandLine.java
@@ -128,6 +128,10 @@ private String remainingArgsToString(String separator) {
return sb.toString();
}
+ public Map<String, Object> getUndeclaredOptions() {
+ return undeclaredOptions;
+ }
+
public void addSystemProperty(String name, String value) {
systemProperties.put(name, value);
}
View
10 ...trap/src/main/groovy/org/codehaus/groovy/grails/cli/support/ScriptBindingInitializer.java
@@ -28,7 +28,9 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -39,6 +41,7 @@
import org.codehaus.groovy.grails.cli.api.BaseSettingsApi;
import org.codehaus.groovy.grails.cli.logging.GrailsConsoleAntBuilder;
import org.codehaus.groovy.grails.cli.logging.GrailsConsoleBuildListener;
+import org.codehaus.groovy.grails.cli.parsing.CommandLine;
import org.codehaus.groovy.runtime.MethodClosure;
import org.springframework.util.ReflectionUtils;
@@ -56,8 +59,10 @@
private BuildSettings settings;
private PluginPathDiscoverySupport pluginPathSupport;
private boolean isInteractive;
+ private CommandLine commandLine;
- public ScriptBindingInitializer(BuildSettings settings, PluginPathDiscoverySupport pluginPathSupport, boolean interactive) {
+ public ScriptBindingInitializer(CommandLine commandLine, BuildSettings settings, PluginPathDiscoverySupport pluginPathSupport, boolean interactive) {
+ this.commandLine = commandLine;
this.settings = settings;
this.pluginPathSupport = pluginPathSupport;
isInteractive = interactive;
@@ -80,6 +85,9 @@ public ScriptBindingInitializer(BuildSettings settings, PluginPathDiscoverySuppo
public GantBinding initBinding(final GantBinding binding, String scriptName) {
Closure<?> c = settings.getGrailsScriptClosure();
c.setDelegate(binding);
+ Map argsMap = new LinkedHashMap(commandLine.getUndeclaredOptions());
+ argsMap.put("params", commandLine.getRemainingArgs());
+ binding.setVariable("argsMap", argsMap);
binding.setVariable("grailsScript", c);
binding.setVariable("grailsConsole", GrailsConsole.getInstance());
binding.setVariable("grailsSettings", settings);
View
22 scripts/_GrailsArgParsing.groovy
@@ -28,26 +28,6 @@ _grails_arg_parsing_called = true
includeTargets << grailsScript("_GrailsEvents")
-argsMap = [params: []]
-
target(parseArguments: "Parse the arguments passed on the command line") {
- // Only ever parse the arguments once. We also don't bother parsing
- // the arguments if the "args" string is empty.
- if (argsMap.size() > 1 || argsMap["params"] || !args) return
-
- args?.tokenize().each {token ->
- def nameValueSwitch = token =~ "--?(.*)=(.*)"
- if (nameValueSwitch.matches()) { // this token is a name/value pair (ex: --foo=bar or -z=qux)
- argsMap[nameValueSwitch[0][1]] = nameValueSwitch[0][2]
- }
- else {
- def nameOnlySwitch = token =~ "--?(.*)"
- if (nameOnlySwitch.matches()) { // this token is just a switch (ex: -force or --help)
- argsMap[nameOnlySwitch[0][1]] = true
- }
- else { // single item tokens, append in order to an array of params
- argsMap["params"] << token
- }
- }
- }
+ // Do nothing, here for compatibility. This is now handled by the CommandLineParser class
}

0 comments on commit e6a887c

Please sign in to comment.