Search in sources :

Example 46 with GroovyShell

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

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

the class DefaultUrlMappingEvaluatorTests method testResourceMappingsWithVersionAndNamespace.

public void testResourceMappingsWithVersionAndNamespace() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    // Resource Entry: "/api/foo"(resources:"foo", version:'1.0', namespace:'v1')
    Script script = shell.parse("mappings = {\n" + "\"/api/foo\"(resources: 'foo', version: '1.0', namespace: 'v1')\n" + "}");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
    assertTrue(mappings.size() > 0);
    //Check that version and namespace are correct for each mapping
    for (UrlMapping mapping : mappings) {
        assertEquals("1.0", mapping.getVersion());
        assertEquals("v1", mapping.getNamespace());
    }
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) GroovyShell(groovy.lang.GroovyShell)

Example 48 with GroovyShell

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

the class DefaultUrlMappingEvaluatorTests method testNewMethod.

public void testNewMethod() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    Script script = shell.parse("mappings = {\n" + "    \"/$controller/$action?/$id?\" { \n" + "        constraints {\n" + "            id(matches:/\\d+/)\n" + "        }\n" + "    }\n" + "}\n");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List mappings = evaluator.evaluateMappings(closure);
    assertEquals(1, mappings.size());
    UrlMapping mapping = (UrlMapping) mappings.get(0);
    assertNull(mapping.getActionName());
    assertNull(mapping.getControllerName());
    assertEquals("(*)", mapping.getUrlData().getTokens()[0]);
    assertEquals("(*)?", mapping.getUrlData().getTokens()[1]);
    assertEquals("(*)?", mapping.getUrlData().getTokens()[2]);
    assertNotNull(mapping.getConstraints());
    assertTrue(makeSureMatchesConstraintExistsOnId(mapping));
    GrailsWebRequest r = GrailsWebMockUtil.bindMockWebRequest();
    UrlMappingInfo info = mapping.match("/mycontroller");
    info.configure(r);
    assertEquals("mycontroller", info.getControllerName());
    assertNull(mapping.match("/mycontroller").getActionName());
    assertNull(mapping.match("/mycontroller").getId());
    UrlMappingInfo info2 = mapping.match("/mycontroller/test");
    info2.configure(r);
    assertEquals("test", info2.getActionName());
    assertNull(mapping.match("/mycontroller/test").getId());
    assertEquals("234", mapping.match("/blog/test/234").getId());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) List(java.util.List) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) GroovyShell(groovy.lang.GroovyShell) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Example 49 with GroovyShell

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

the class DefaultUrlMappingEvaluatorTests method testNamedMappings.

public void testNamedMappings() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    Script script = shell.parse("mappings = {\n" + "name firstMapping: \"/first/one\" {\n" + "}\n" + "\"/second/one\" {" + "}\n" + "name thirdMapping: \"/third/one\" {\n" + "}\n" + "}");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List mappings = evaluator.evaluateMappings(closure);
    assertEquals(3, mappings.size());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Closure(groovy.lang.Closure) List(java.util.List) GroovyShell(groovy.lang.GroovyShell)

Example 50 with GroovyShell

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

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