Search in sources :

Example 26 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class SeansBug method testMarkupBug.

public void testMarkupBug() throws Exception {
    String[] lines = { "package groovy.xml", "", "b = new MarkupBuilder()", "", "b.root1(a:5, b:7) { ", "    elem1('hello1') ", "    elem2('hello2') ", "    elem3(x:7) ", "}" };
    String code = asCode(lines);
    GroovyShell shell = new GroovyShell();
    shell.evaluate(code);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 27 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

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 28 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

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.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 29 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

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)

Example 30 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class GroovyMain method processFiles.

/**
     * Process the input files.
     */
private void processFiles() throws CompilationFailedException, IOException, URISyntaxException {
    GroovyShell groovy = new GroovyShell(conf);
    setupContextClassLoader(groovy);
    Script s = groovy.parse(getScriptSource(isScriptFile, script));
    if (args.isEmpty()) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer = new PrintWriter(System.out);
        try {
            processReader(s, reader, writer);
        } finally {
            reader.close();
            writer.close();
        }
    } else {
        Iterator i = args.iterator();
        while (i.hasNext()) {
            String filename = (String) i.next();
            //TODO: These are the arguments for -p and -i.  Why are we searching using Groovy script extensions?
            // Where is this documented?
            File file = huntForTheScriptFile(filename);
            processFile(s, file);
        }
    }
}
Also used : Script(groovy.lang.Script) Iterator(java.util.Iterator) 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