예제 #1
0
  /** Returns a list of all the executable Gant scripts available to this application. */
  public List<File> getAvailableScripts() {
    List<File> scripts = new ArrayList<File>();
    if (settings.getGrailsHome() != null) {
      addCommandScripts(new File(settings.getGrailsHome(), "scripts"), scripts);
      addCommandScripts(
          new File(settings.getGrailsHome(), "grails-scripts/src/main/scripts"), scripts);
    }
    addCommandScripts(new File(settings.getBaseDir(), "scripts"), scripts);
    addCommandScripts(new File(settings.getUserHome(), ".grails/scripts"), scripts);

    for (File dir : pluginPathSupport.listKnownPluginDirs()) {
      addPluginScripts(dir, scripts);
    }

    PathMatchingResourcePatternResolver resolver =
        new PathMatchingResourcePatternResolver(settings.getRootLoader());
    try {
      final Resource[] resources = resolver.getResources("classpath*:META-INF/scripts/*.groovy");
      for (Resource resource : resources) {
        scripts.add(resource.getFile());
      }
    } catch (IOException e) {
      // ignore
    }
    return scripts;
  }
예제 #2
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  private int executeScriptWithCaching(CommandLine commandLine, String scriptName, String env) {
    List<File> potentialScripts;
    List<File> allScripts = getAvailableScripts();
    GantBinding binding = new GantBinding();
    binding.setVariable("scriptName", scriptName);

    setDefaultInputStream(binding);

    // Now find what scripts match the one requested by the user.
    potentialScripts = getPotentialScripts(scriptName, allScripts);

    if (potentialScripts.size() == 0) {
      try {
        File aliasFile = new File(settings.getUserHome(), ".grails/.aliases");
        if (aliasFile.exists()) {
          Properties aliasProperties = new Properties();
          aliasProperties.load(new FileReader(aliasFile));
          if (aliasProperties.containsKey(commandLine.getCommandName())) {
            String aliasValue = (String) aliasProperties.get(commandLine.getCommandName());
            String[] aliasPieces = aliasValue.split(" ");
            String commandName = aliasPieces[0];
            String correspondingScriptName = GrailsNameUtils.getNameFromScript(commandName);
            potentialScripts = getPotentialScripts(correspondingScriptName, allScripts);

            if (potentialScripts.size() > 0) {
              String[] additionalArgs = new String[aliasPieces.length - 1];
              System.arraycopy(aliasPieces, 1, additionalArgs, 0, additionalArgs.length);
              insertArgumentsInFrontOfExistingArguments(commandLine, additionalArgs);
            }
          }
        }
      } catch (Exception e) {
        console.error(e);
      }
    }

    // First try to load the script from its file. If there is no
    // file, then attempt to load it as a pre-compiled script. If
    // that fails, then let the user know and then exit.
    if (potentialScripts.size() > 0) {
      potentialScripts = (List) DefaultGroovyMethods.unique(potentialScripts);
      final File scriptFile = potentialScripts.get(0);
      if (!isGrailsProject() && !isExternalScript(scriptFile)) {
        return handleScriptExecutedOutsideProjectError();
      }
      return executeScriptFile(commandLine, scriptName, env, binding, scriptFile);
    }

    return attemptPrecompiledScriptExecute(commandLine, scriptName, env, binding, allScripts);
  }
  protected void loadEventHooks(@SuppressWarnings("hiding") BuildSettings buildSettings) {
    if (buildSettings == null) {
      return;
    }

    loadEventsScript(findEventsScript(new File(buildSettings.getUserHome(), ".grails/scripts")));
    loadEventsScript(findEventsScript(new File(buildSettings.getBaseDir(), "scripts")));

    PluginBuildSettings pluginSettings =
        (PluginBuildSettings) binding.getVariable("pluginSettings");
    for (Resource pluginBase : pluginSettings.getPluginDirectories()) {
      try {
        loadEventsScript(findEventsScript(new File(pluginBase.getFile(), "scripts")));
      } catch (IOException ex) {
        throw new RuntimeException(ex);
      }
    }
  }