Search in sources :

Example 11 with CompilerConfiguration

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);
}
Also used : HashMap(java.util.HashMap) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with CompilerConfiguration

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();
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) List(java.util.List) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) ParseException(org.apache.commons.cli.ParseException) GroovyShell(groovy.lang.GroovyShell)

Example 13 with CompilerConfiguration

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;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) URLClassLoader(java.net.URLClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 14 with CompilerConfiguration

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();
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell)

Example 15 with CompilerConfiguration

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());
}
Also used : Timer(org.gradle.internal.time.Timer) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Aggregations

CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)42 GroovyShell (groovy.lang.GroovyShell)14 Binding (groovy.lang.Binding)10 File (java.io.File)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)8 GroovyClassLoader (groovy.lang.GroovyClassLoader)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 GroovyBugError (org.codehaus.groovy.GroovyBugError)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 GroovyClass (org.codehaus.groovy.tools.GroovyClass)4 Map (java.util.Map)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)3 Closure (groovy.lang.Closure)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2