|
Groovy example source code file (FileSystemCompiler.java)
The Groovy FileSystemCompiler.java source code/* * Copyright 2003-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.codehaus.groovy.tools; import groovy.lang.GroovyResourceLoader; import org.apache.commons.cli.*; import org.codehaus.groovy.control.CompilationUnit; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.ConfigurationException; import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit; import groovy.lang.GroovySystem; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.*; /** * Command-line compiler (aka. <tt>groovyc). * * @version $Id: FileSystemCompiler.java 21459 2011-02-03 13:01:54Z ait $ */ public class FileSystemCompiler { private final CompilationUnit unit; public FileSystemCompiler(CompilerConfiguration configuration) throws ConfigurationException { this(configuration, null); } public FileSystemCompiler(CompilerConfiguration configuration, CompilationUnit cu) throws ConfigurationException { if (cu != null) { unit = cu; } else if (configuration.getJointCompilationOptions() != null) { unit = new JavaAwareCompilationUnit(configuration); } else { unit = new CompilationUnit(configuration); } // in command line we don't need to do script lookups unit.getClassLoader().setResourceLoader(new GroovyResourceLoader() { public URL loadGroovySource(String filename) throws MalformedURLException { return null; } }); } public void compile(String[] paths) throws Exception { unit.addSources(paths); unit.compile(); } public void compile(File[] files) throws Exception { unit.addSources(files); unit.compile(); } public static void displayHelp(final Options options) { final HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(80, "groovyc [options] <source-files>", "options:", options, ""); } public static void displayVersion() { String version = GroovySystem.getVersion(); System.err.println("Groovy compiler version " + version); System.err.println("Copyright 2003-2010 The Codehaus. http://groovy.codehaus.org/"); System.err.println(""); } public static int checkFiles(String[] filenames) { int errors = 0; for (String filename : filenames) { File file = new File(filename); if (!file.exists()) { System.err.println("error: file not found: " + file); ++errors; } else if (!file.canRead()) { System.err.println("error: file not readable: " + file); ++errors; } } return errors; } public static boolean validateFiles(String[] filenames) { return checkFiles(filenames) == 0; } private static boolean displayStackTraceOnError = false; /** * Same as main(args) except that exceptions are thrown out instead of causing * the VM to exit. */ public static void commandLineCompile(String[] args) throws Exception { Options options = createCompilationOptions(); PosixParser cliParser = new PosixParser(); CommandLine cli; cli = cliParser.parse(options, args); if (cli.hasOption('h')) { displayHelp(options); return; } if (cli.hasOption('v')) { displayVersion(); return; } displayStackTraceOnError = cli.hasOption('e'); CompilerConfiguration configuration = generateCompilerConfigurationFromOptions(cli); // // Load the file name list String[] filenames = generateFileNamesFromOptions(cli); boolean fileNameErrors = filenames == null; if (!fileNameErrors && (filenames.length == 0)) { displayHelp(options); return; } fileNameErrors = fileNameErrors && !validateFiles(filenames); if (!fileNameErrors) { doCompilation(configuration, null, filenames); } } /** * Primary entry point for compiling from the command line * (using the groovyc script). * * If calling inside a process and you don't want the JVM to exit on an * error call commandLineCompile(String[]), which this method simply wraps */ public static void main(String[] args) { try { commandLineCompile(args); } catch( Throwable e ) { new ErrorReporter( e, displayStackTraceOnError).write( System.err ); System.exit(1); } } public static void doCompilation(CompilerConfiguration configuration, CompilationUnit unit, String[] filenames) throws Exception { File tmpDir = null; // if there are any joint compilation options set stubDir if not set try { if ((configuration.getJointCompilationOptions() != null) && !configuration.getJointCompilationOptions().containsKey("stubDir")) { tmpDir = createTempDir(); configuration.getJointCompilationOptions().put("stubDir", tmpDir); } FileSystemCompiler compiler = new FileSystemCompiler(configuration, unit); compiler.compile(filenames); } finally { try { if (tmpDir != null) deleteRecursive(tmpDir); } catch (Throwable t) { System.err.println("error: could not delete temp files - " + tmpDir.getPath()); } } } public static String[] generateFileNamesFromOptions(CommandLine cli) { String[] filenames = cli.getArgs(); List<String> fileList = new ArrayList Other Groovy examples (source code examples)Here is a short list of links related to this Groovy FileSystemCompiler.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.