private GrailsConsole getConsole(CommandLine commandLine) { GrailsConsole console = GrailsConsole.getInstance(); // Set the console display properties console.setAnsiEnabled(!commandLine.hasOption(CommandLine.NOANSI_ARGUMENT)); console.setStacktrace(commandLine.hasOption(CommandLine.STACKTRACE_ARGUMENT)); console.setVerbose(commandLine.hasOption(CommandLine.VERBOSE_ARGUMENT)); return console; }
/** * Evaluate the arguments to get the name of the script to execute, which environment to run it * in, and the arguments to pass to the script. This also evaluates arguments of the form * "-Dprop=value" and creates system properties from each one. * * @param args Command line arguments */ public static void main(String[] args) { originalIn = System.in; originalOut = System.out; CommandLineParser parser = getCommandLineParser(); GrailsConsole console = GrailsConsole.getInstance(); CommandLine commandLine; try { if (args.length == 0) { commandLine = new DefaultCommandLine(); } else { commandLine = parser.parseString(args[0]); if (commandLine.hasOption(CommandLine.NOANSI_ARGUMENT)) { console.setAnsiEnabled(false); } } } catch (ParseException e) { console.error("Error processing command line arguments: " + e.getMessage()); System.exit(1); return; } String version = System.getProperty("grails.version"); ScriptAndArgs script = processArgumentsAndReturnScriptName(commandLine); // Get hold of the GRAILS_HOME environment variable if it is available. String grailsHome = System.getProperty("grails.home"); // Now we can pick up the Grails version from the Ant project properties. BuildSettings build = null; try { build = new BuildSettings(new File(grailsHome)); build.setModified(commandLine.hasOption(CommandLine.REFRESH_DEPENDENCIES_ARGUMENT)); build.setOffline(commandLine.hasOption(CommandLine.OFFLINE_ARGUMENT)); if (build.getRootLoader() == null) { build.setRootLoader((URLClassLoader) GrailsScriptRunner.class.getClassLoader()); } } catch (Exception e) { exitWithError( "An error occurred loading the grails-app/conf/BuildConfig.groovy file: " + e.getMessage(), null); } // Check that Grails' home actually exists. final File grailsHomeInSettings = build.getGrailsHome(); if (grailsHomeInSettings == null || !grailsHomeInSettings.exists()) { exitWithError("Grails' installation directory not found: " + build.getGrailsHome(), null); } if (commandLine.hasOption(CommandLine.VERSION_ARGUMENT)) { console.log("Grails version: " + build.getGrailsVersion()); System.exit(0); } if (commandLine.hasOption(CommandLine.HELP_ARGUMENT)) { console.log(parser.getHelpMessage()); System.exit(0); } // If there aren't any arguments, then we don't have a command // to execute, so enter "interactive mode" boolean resolveDeps = commandLine.hasOption(CommandLine.REFRESH_DEPENDENCIES_ARGUMENT); if (resolveDeps) { if (commandLine.hasOption("include-source")) { build.setIncludeSource(true); } if (commandLine.hasOption("include-javadoc")) { build.setIncludeJavadoc(true); } } GrailsScriptRunner scriptRunner = new GrailsScriptRunner(build); scriptRunner.setInteractive(!commandLine.hasOption(CommandLine.NON_INTERACTIVE_ARGUMENT)); if ("Interactive".equals(script.name)) { console.error( "The 'interactive' script is deprecated; to run in interactive mode just omit the script name"); script.name = null; } if (script.name == null) { console.updateStatus( "Loading Grails " + (version != null ? version : build.getGrailsVersion())); build.loadConfig(); if (resolveDeps) { ClasspathConfigurer.cleanResolveCache(build); } scriptRunner.initializeState(); try { new InteractiveMode(build, scriptRunner).run(); } catch (Throwable e) { console.error("Interactive mode exited with error: " + e.getMessage(), e); } } else { console.getCategory().push(script.inputName); console.verbose("Base Directory: " + build.getBaseDir().getPath()); try { int exitCode = scriptRunner.executeCommand(commandLine, script.name, script.env); System.exit(exitCode); } catch (ScriptNotFoundException ex) { console.error("Script not found: " + ex.getScriptName()); } catch (Throwable t) { String msg = "Error executing script " + script.name + ": " + t.getMessage(); exitWithError(msg, t); } } }