use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class FileSystemCompilerTest method setUp.
protected void setUp() throws Exception {
File dir = new File("target/test-generated-classes");
dir.mkdirs();
Map options = new HashMap();
options.put("stubDir", dir);
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.setTargetDirectory(dir);
configuration.setVerbose(dumpClass);
configuration.setJointCompilationOptions(options);
compiler = new FileSystemCompiler(configuration);
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class GroovyMain method process.
/**
* Process the users request.
*
* @param line the parsed command line.
* @throws ParseException if invalid options are chosen
*/
private static boolean process(CommandLine line) throws ParseException, IOException {
List args = line.getArgList();
if (line.hasOption('D')) {
String[] values = line.getOptionValues('D');
for (int i = 0; i < values.length; i++) {
setSystemPropertyFrom(values[i]);
}
}
GroovyMain main = new GroovyMain();
// add the ability to parse scripts with a specified encoding
main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));
main.isScriptFile = !line.hasOption('e');
main.debug = line.hasOption('d');
main.conf.setDebug(main.debug);
main.conf.setParameters(line.hasOption("pa"));
main.processFiles = line.hasOption('p') || line.hasOption('n');
main.autoOutput = line.hasOption('p');
main.editFiles = line.hasOption('i');
if (main.editFiles) {
main.backupExtension = line.getOptionValue('i');
}
main.autoSplit = line.hasOption('a');
String sp = line.getOptionValue('a');
if (sp != null)
main.splitPattern = sp;
if (main.isScriptFile) {
if (args.isEmpty())
throw new ParseException("neither -e or filename provided");
main.script = (String) args.remove(0);
if (main.script.endsWith(".java"))
throw new ParseException("error: cannot compile file with .java extension: " + main.script);
} else {
main.script = line.getOptionValue('e');
}
main.processSockets = line.hasOption('l');
if (main.processSockets) {
// default port to listen to
String p = line.getOptionValue('l', "1960");
main.port = Integer.parseInt(p);
}
// we use "," as default, because then split will create
// an empty array if no option is set
String disabled = line.getOptionValue("disableopt", ",");
String[] deopts = disabled.split(",");
for (String deopt_i : deopts) {
main.conf.getOptimizationOptions().put(deopt_i, false);
}
if (line.hasOption("indy")) {
CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
main.conf.getOptimizationOptions().put("indy", true);
}
if (line.hasOption("basescript")) {
main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
}
if (line.hasOption("configscript")) {
String path = line.getOptionValue("configscript");
File groovyConfigurator = new File(path);
Binding binding = new Binding();
binding.setVariable("configuration", main.conf);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
shell.evaluate(groovyConfigurator);
}
main.args = args;
return main.run();
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class ScriptCompilationExecuter method execute.
public long execute() throws Exception {
ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent());
GroovyClassLoader gcl = new GroovyClassLoader(cl);
CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl, new GroovyClassLoader(this.getClass().getClassLoader()));
for (File source : sources) {
cu.addSource(source);
}
long sd = System.nanoTime();
cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber());
long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd);
return dur;
}
use of org.codehaus.groovy.control.CompilerConfiguration in project hudson-2.x by hudson.
the class BeanBuilder method parse.
/**
* Parses the bean definition groovy script by first exporting the given {@link Binding}.
*/
public void parse(InputStream script, Binding binding) {
setBinding(binding);
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(ClosureScript.class.getName());
GroovyShell shell = new GroovyShell(classLoader, binding, cc);
ClosureScript s = (ClosureScript) shell.parse(script);
s.setDelegate(this);
s.run();
}
use of org.codehaus.groovy.control.CompilerConfiguration in project gradle by gradle.
the class DefaultScriptCompilationHandler method compileToDir.
@Override
public void compileToDir(ScriptSource source, ClassLoader classLoader, File classesDir, File metadataDir, CompileOperation<?> extractingTransformer, Class<? extends Script> scriptBaseClass, Action<? super ClassNode> verifier) {
Timer clock = Time.startTimer();
GFileUtils.deleteDirectory(classesDir);
GFileUtils.mkdirs(classesDir);
CompilerConfiguration configuration = createBaseCompilerConfiguration(scriptBaseClass);
configuration.setTargetDirectory(classesDir);
try {
compileScript(source, classLoader, configuration, metadataDir, extractingTransformer, verifier);
} catch (GradleException e) {
GFileUtils.deleteDirectory(classesDir);
GFileUtils.deleteDirectory(metadataDir);
throw e;
}
logger.debug("Timing: Writing script to cache at {} took: {}", classesDir.getAbsolutePath(), clock.getElapsed());
}
Aggregations