예제 #1
0
파일: Groovyc.java 프로젝트: endSly/groovy
  private void runCompiler(String[] commandLine) {
    // hand crank it so we can add our own compiler configuration
    try {
      Options options = FileSystemCompiler.createCompilationOptions();

      CommandLineParser cliParser = new DefaultParser();

      CommandLine cli;
      cli = cliParser.parse(options, commandLine);

      configuration = FileSystemCompiler.generateCompilerConfigurationFromOptions(cli);
      configuration.setScriptExtensions(getScriptExtensions());
      String tmpExtension = getScriptExtension();
      if (tmpExtension.startsWith("*.")) tmpExtension = tmpExtension.substring(1);
      configuration.setDefaultScriptExtension(tmpExtension);

      // Load the file name list
      String[] filenames = FileSystemCompiler.generateFileNamesFromOptions(cli);
      boolean fileNameErrors = filenames == null;

      fileNameErrors = fileNameErrors || !FileSystemCompiler.validateFiles(filenames);

      if (targetBytecode != null) {
        configuration.setTargetBytecode(targetBytecode);
      }

      if (!fileNameErrors) {
        FileSystemCompiler.doCompilation(
            configuration, makeCompileUnit(), filenames, forceLookupUnnamedFiles);
      }

    } catch (Exception re) {
      Throwable t = re;
      if ((re.getClass() == RuntimeException.class) && (re.getCause() != null)) {
        // unwrap to the real exception
        t = re.getCause();
      }
      StringWriter writer = new StringWriter();
      new ErrorReporter(t, false).write(new PrintWriter(writer));
      String message = writer.toString();

      taskSuccess = false;
      if (errorProperty != null) {
        getProject().setNewProperty(errorProperty, "true");
      }

      if (failOnError) {
        log.error(message);
        throw new BuildException("Compilation Failed", t, getLocation());
      } else {
        log.error(message);
      }
    }
  }
예제 #2
0
  /** Sets the Flags to defaults. */
  public CompilerConfiguration() {
    //
    // Set in safe defaults

    setWarningLevel(WarningMessage.LIKELY_ERRORS);
    setOutput(null);
    setTargetDirectory((File) null);
    setClasspath("");
    setVerbose(false);
    setDebug(false);
    setTolerance(10);
    setScriptBaseClass(null);
    setRecompileGroovySource(false);
    setMinimumRecompilationInterval(100);
    setTargetBytecode(getVMVersion());
    setDefaultScriptExtension(".groovy");

    //
    // Source file encoding
    String encoding = null;
    try {
      encoding = System.getProperty("file.encoding", "US-ASCII");
    } catch (Exception e) {
      // IGNORE
    }
    try {
      encoding = System.getProperty("groovy.source.encoding", encoding);
    } catch (Exception e) {
      // IGNORE
    }
    setSourceEncoding(encoding);

    try {
      setOutput(new PrintWriter(System.err));
    } catch (Exception e) {
      // IGNORE
    }

    try {
      String target = System.getProperty("groovy.target.directory");
      if (target != null) {
        setTargetDirectory(target);
      }
    } catch (Exception e) {
      // IGNORE
    }
  }
예제 #3
0
 /**
  * Copy constructor. Use this if you have a mostly correct configuration for your compilation but
  * you want to make a some changes programmatically. An important reason to prefer this approach
  * is that your code will most likely be forward compatible with future changes to this
  * configuration API.<br>
  * An example of this copy constructor at work:<br>
  *
  * <pre>
  *    // In all likelihood there is already a configuration in your code's context
  *    // for you to copy, but for the sake of this example we'll use the global default.
  *    CompilerConfiguration myConfiguration = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
  *    myConfiguration.setDebug(true);
  * </pre>
  *
  * @param configuration The configuration to copy.
  */
 public CompilerConfiguration(CompilerConfiguration configuration) {
   setWarningLevel(configuration.getWarningLevel());
   setOutput(configuration.getOutput());
   setTargetDirectory(configuration.getTargetDirectory());
   setClasspathList(new LinkedList(configuration.getClasspath()));
   setVerbose(configuration.getVerbose());
   setDebug(configuration.getDebug());
   setTolerance(configuration.getTolerance());
   setScriptBaseClass(configuration.getScriptBaseClass());
   setRecompileGroovySource(configuration.getRecompileGroovySource());
   setMinimumRecompilationInterval(configuration.getMinimumRecompilationInterval());
   setTargetBytecode(configuration.getTargetBytecode());
   setDefaultScriptExtension(configuration.getDefaultScriptExtension());
   setSourceEncoding(configuration.getSourceEncoding());
   setOutput(configuration.getOutput());
   setTargetDirectory(configuration.getTargetDirectory());
   Map jointCompilationOptions = configuration.getJointCompilationOptions();
   if (jointCompilationOptions != null) {
     jointCompilationOptions = new HashMap(jointCompilationOptions);
   }
   setJointCompilationOptions(jointCompilationOptions);
   setPluginFactory(configuration.getPluginFactory());
 }