Search in sources :

Example 26 with CompilerConfiguration

use of org.codehaus.groovy.control.CompilerConfiguration in project gradle by gradle.

the class ApiGroovyCompiler method applyConfigurationScript.

private void applyConfigurationScript(File configScript, CompilerConfiguration configuration) {
    VersionNumber version = parseGroovyVersion();
    if (version.compareTo(VersionNumber.parse("2.1")) < 0) {
        throw new GradleException("Using a Groovy compiler configuration script requires Groovy 2.1+ but found Groovy " + version + "");
    }
    Binding binding = new Binding();
    binding.setVariable("configuration", configuration);
    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);
    try {
        shell.evaluate(configScript);
    } catch (Exception e) {
        throw new GradleException("Could not execute Groovy compiler configuration script: " + configScript.getAbsolutePath(), e);
    }
}
Also used : Binding(groovy.lang.Binding) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) VersionNumber(org.gradle.util.VersionNumber) GroovyShell(groovy.lang.GroovyShell) GradleException(org.gradle.api.GradleException)

Example 27 with CompilerConfiguration

use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.

the class GroovyShellTest method testScriptWithCustomBodyMethod.

/**
     * Test for GROOVY-6615
     * @throws Exception
     */
public void testScriptWithCustomBodyMethod() throws Exception {
    Binding context = new Binding();
    CompilerConfiguration config = new CompilerConfiguration();
    config.setScriptBaseClass(BaseScriptCustomBodyMethod.class.getName());
    GroovyShell shell = new GroovyShell(context, config);
    Object result = shell.evaluate("'I like ' + cheese");
    assertEquals("I like Cheddar", result);
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 28 with CompilerConfiguration

use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.

the class GroovyShellTest method testScriptWithDerivedBaseClass.

public void testScriptWithDerivedBaseClass() throws Exception {
    Binding context = new Binding();
    CompilerConfiguration config = new CompilerConfiguration();
    config.setScriptBaseClass(DerivedScript.class.getName());
    GroovyShell shell = new GroovyShell(context, config);
    Object result = shell.evaluate("x = 'abc'; doSomething(cheese)");
    assertEquals("I like Cheddar", result);
    assertEquals("abc", context.getVariable("x"));
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 29 with CompilerConfiguration

use of org.codehaus.groovy.control.CompilerConfiguration in project groovy-core by groovy.

the class FileSystemCompiler method commandLineCompile.

/**
     * Same as main(args) except that exceptions are thrown out instead of causing
     * the VM to exit and the lookup for .groovy files can be controlled
     */
public static void commandLineCompile(String[] args, boolean lookupUnnamedFiles) throws Exception {
    Options options = createCompilationOptions();
    CommandLineParser cliParser = new GroovyInternalPosixParser();
    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, lookupUnnamedFiles);
    }
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 30 with CompilerConfiguration

use of org.codehaus.groovy.control.CompilerConfiguration in project spock by spockframework.

the class ConfigurationScriptLoader method createShell.

private GroovyShell createShell() {
    CompilerConfiguration compilerSettings = new CompilerConfiguration();
    compilerSettings.setScriptBaseClass(DelegatingScript.class.getName());
    return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings);
}
Also used : DelegatingScript(org.spockframework.builder.DelegatingScript) 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