Search in sources :

Example 11 with GroovyShell

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

the class PlatformLineWriterTest method testPlatformLineWriter.

public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
    String LS = System.getProperty("line.separator");
    Binding binding = new Binding();
    binding.setVariable("first", "Tom");
    binding.setVariable("last", "Adams");
    StringWriter stringWriter = new StringWriter();
    Writer platformWriter = new PlatformLineWriter(stringWriter);
    GroovyShell shell = new GroovyShell(binding);
    platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
    platformWriter.flush();
    assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
    stringWriter = new StringWriter();
    platformWriter = new PlatformLineWriter(stringWriter);
    platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
    platformWriter.flush();
    assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
Also used : Binding(groovy.lang.Binding) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) GroovyShell(groovy.lang.GroovyShell)

Example 12 with GroovyShell

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

the class Eval method me.

/**
     * Evaluates the specified String expression and makes the parameter available inside
     * the script, returning the result. For example, this code binds the 'x' variable: 
     * <pre class="groovyTestCase">
     * assert Eval.me('x', 2, ' x * 4 + 2') == 10
     * </pre>
     * @param expression the Groovy expression to evaluate
     * @return the result of the expression
     * @throws CompilationFailedException if expression is not valid Groovy
     */
public static Object me(final String symbol, final Object object, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable(symbol, object);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 13 with GroovyShell

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

the class Eval method xy.

/**
     * Evaluates the specified String expression and makes the first two parameters available inside
     * the script bound to variables named 'x' and 'y' respectively, returning the result. For example, 
     * this code executes without failure: 
     * <pre class="groovyTestCase">
     * assert Eval.xy(2, 4, ' x * y + 2') == 10
     * </pre>
     * @param expression the Groovy expression to evaluate
     * @return the result of the expression
     * @throws CompilationFailedException if expression is not valid Groovy
     */
public static Object xy(final Object x, final Object y, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable("x", x);
    b.setVariable("y", y);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 14 with GroovyShell

use of groovy.lang.GroovyShell 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 15 with GroovyShell

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

the class GroovyMain method processOnce.

/**
     * Process the standard, single script with args.
     */
private void processOnce() throws CompilationFailedException, IOException, URISyntaxException {
    GroovyShell groovy = new GroovyShell(conf);
    setupContextClassLoader(groovy);
    groovy.run(getScriptSource(isScriptFile, script), args);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

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