Exemplo n.º 1
0
  public int executeCommand(String scriptName, String args, String env) {
    // Populate the root loader with all libraries that this app
    // depends on. If a root loader doesn't exist yet, create it now.

    if (args != null) {
      System.setProperty("grails.cli.args", args.replace(' ', '\n'));
    } else {
      // If GrailsScriptRunner is executed more than once in a
      // single JVM, we have to make sure that the CLI args are reset.
      System.setProperty("grails.cli.args", "");
    }

    CommandLineParser parser = getCommandLineParser();
    CommandLine commandLine = parser.parseString(scriptName, args);

    return executeCommand(commandLine, scriptName, env);
  }
Exemplo n.º 2
0
 public static CommandLineParser getCommandLineParser() {
   CommandLineParser parser = new CommandLineParser();
   parser.addOption(
       CommandLine.REFRESH_DEPENDENCIES_ARGUMENT,
       "Whether to force a resolve of dependencies (skipping any caching)");
   parser.addOption(CommandLine.VERBOSE_ARGUMENT, "Enable verbose output");
   parser.addOption(
       CommandLine.OFFLINE_ARGUMENT,
       "Indicates that Grails should not connect to any remote servers during processing of the build");
   parser.addOption(CommandLine.STACKTRACE_ARGUMENT, "Enable stack traces in output");
   parser.addOption(CommandLine.AGENT_ARGUMENT, "Enable the reloading agent");
   parser.addOption(
       CommandLine.NON_INTERACTIVE_ARGUMENT, "Whether to allow the command line to request input");
   parser.addOption(CommandLine.HELP_ARGUMENT, "Command line help");
   parser.addOption(CommandLine.VERSION_ARGUMENT, "Current Grails version");
   parser.addOption(CommandLine.NOANSI_ARGUMENT, "Disables ANSI output");
   return parser;
 }
Exemplo n.º 3
0
  public int executeCommand(String scriptName, String args, String env) {
    // Populate the root loader with all libraries that this app
    // depends on. If a root loader doesn't exist yet, create it now.

    if (args != null) {
      System.setProperty("grails.cli.args", args.replace(' ', '\n'));
    } else {
      // If GrailsScriptRunner is executed more than once in a
      // single JVM, we have to make sure that the CLI args are reset.
      System.setProperty("grails.cli.args", "");
    }

    CommandLineParser parser = getCommandLineParser();
    DefaultCommandLine commandLine = (DefaultCommandLine) parser.parseString(scriptName, args);
    setInteractive(!commandLine.hasOption(CommandLine.NON_INTERACTIVE_ARGUMENT));
    if (env != null) {
      commandLine.setEnvironment(env);
    }

    return executeCommand(commandLine, scriptName, env);
  }
Exemplo n.º 4
0
  /**
   * 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);
      }
    }
  }