Search in sources :

Example 71 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class FileSystemCompiler method generateCompilerConfigurationFromOptions.

public static CompilerConfiguration generateCompilerConfigurationFromOptions(CommandLine cli) throws IOException {
    // Setup the configuration data
    CompilerConfiguration configuration = new CompilerConfiguration();
    if (cli.hasOption("classpath")) {
        configuration.setClasspath(cli.getOptionValue("classpath"));
    }
    if (cli.hasOption('d')) {
        configuration.setTargetDirectory(cli.getOptionValue('d'));
    }
    configuration.setParameters(cli.hasOption("pa"));
    if (cli.hasOption("encoding")) {
        configuration.setSourceEncoding(cli.getOptionValue("encoding"));
    }
    if (cli.hasOption("basescript")) {
        configuration.setScriptBaseClass(cli.getOptionValue("basescript"));
    }
    // joint compilation parameters
    if (cli.hasOption('j')) {
        Map<String, Object> compilerOptions = new HashMap<String, Object>();
        String[] namedValues = cli.getOptionValues("J");
        compilerOptions.put("namedValues", namedValues);
        String[] flags = cli.getOptionValues("F");
        if (flags != null && cli.hasOption("pa")) {
            flags = Arrays.copyOf(flags, flags.length + 1);
            flags[flags.length - 1] = "parameters";
        }
        compilerOptions.put("flags", flags);
        configuration.setJointCompilationOptions(compilerOptions);
    }
    if (cli.hasOption("indy")) {
        configuration.getOptimizationOptions().put("int", false);
        configuration.getOptimizationOptions().put("indy", true);
    }
    if (cli.hasOption("configscript")) {
        String path = cli.getOptionValue("configscript");
        File groovyConfigurator = new File(path);
        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);
        shell.evaluate(groovyConfigurator);
    }
    return configuration;
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 72 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class Groovy1567_Bug method testGroovyScriptEngineVsGroovyShell.

public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException {
    // @todo refactor this path
    File currentDir = new File("./src/test/groovy/bugs");
    String file = "bug1567_script.groovy";
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    String[] test = null;
    Object result = shell.run(new File(currentDir, file), test);
    String[] roots = new String[] { currentDir.getAbsolutePath() };
    GroovyScriptEngine gse = new GroovyScriptEngine(roots);
    binding = new Binding();
    // a MME was ensued here stating no 't.start()' was available
    // in the script
    gse.run(file, binding);
}
Also used : Binding(groovy.lang.Binding) GroovyScriptEngine(groovy.util.GroovyScriptEngine) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 73 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class SeansBug method testBug.

public void testBug() throws Exception {
    String code = "for (i in 1..10) \n{\n  println(i)\n}";
    GroovyShell shell = new GroovyShell();
    shell.evaluate(code);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 74 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class ClassicGroovyTestGeneratorHelper method evaluate.

/** evaluate the source text against the classic AST with the JSR parser implementation*/
public Object evaluate(String theSrcText, String testName) throws Exception {
    // fail early with a direct message if possible')
    parse(theSrcText, testName);
    GroovyShell groovy = new GroovyShell(new CompilerConfiguration());
    return groovy.run(theSrcText, "main", new ArrayList());
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ArrayList(java.util.ArrayList) GroovyShell(groovy.lang.GroovyShell)

Example 75 with GroovyShell

use of groovy.lang.GroovyShell in project groovy by apache.

the class J2eeConsole method main.

public static void main(String[] args) {
    if (args.length <= 0) {
        System.out.println("Usage: home [configuaration] [localcopy]");
        return;
    }
    String home = args[0];
    Properties p = new Properties();
    System.setProperty("openejb.home", home);
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory");
    p.put("openejb.loader", "embed");
    p.put("openejb.home", home);
    if (args.length > 1) {
        String conf = args[1];
        System.setProperty("openejb.configuration", conf);
        p.put("openejb.configuration", conf);
    }
    if (args.length > 2) {
        String copy = args[2];
        System.setProperty("openejb.localcopy", copy);
        p.put("openejb.localcopy", copy);
    }
    try {
        InitialContext ctx = new InitialContext(p);
        GroovyShell shell = new GroovyShell();
        shell.setVariable("context", ctx);
        //shell.evaluate("src/test/groovy/j2ee/CreateData.groovy");
        //shell.evaluate("src/main/groovy/ui/Console.groovy");
        GroovyObject console = (GroovyObject) InvokerHelper.invokeConstructorOf("groovy.ui.Console", null);
        console.setProperty("shell", shell);
        console.invokeMethod("run", null);
    /*
            */
    } catch (Exception e) {
        System.out.println("Caught: " + e);
    }
}
Also used : Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) GroovyShell(groovy.lang.GroovyShell) GroovyObject(groovy.lang.GroovyObject)

Aggregations

GroovyShell (groovy.lang.GroovyShell)86 Binding (groovy.lang.Binding)43 File (java.io.File)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Script (groovy.lang.Script)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)10 Closure (groovy.lang.Closure)9 IOException (java.io.IOException)8 List (java.util.List)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 AntBuilder (groovy.util.AntBuilder)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 BuildException (org.apache.tools.ant.BuildException)4 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)4 UrlMapping (grails.web.mapping.UrlMapping)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3