Search in sources :

Example 81 with GroovyShell

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

the class UnimplementedSyntaxTest method test_StaticImport2.

public void test_StaticImport2() throws Exception {
    //GROOVY-3711: The following call now results in a valid script class node, so foo.Bar needs to get resolved.
    GroovyShell groovyShell = new GroovyShell();
    compile("package foo; class Bar{}", groovyShell);
    assertNotNull(compile("import static foo.Bar.*", groovyShell));
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 82 with GroovyShell

use of groovy.lang.GroovyShell in project intellij-community by JetBrains.

the class GroovyScriptMacro method runIt.

private static Object runIt(Expression[] params, ExpressionContext context) {
    try {
        Result result = params[0].calculateResult(context);
        if (result == null)
            return result;
        String text = result.toString();
        GroovyShell shell = new GroovyShell();
        File possibleFile = new File(text);
        Script script = possibleFile.exists() ? shell.parse(possibleFile) : shell.parse(text);
        Binding binding = new Binding();
        for (int i = 1; i < params.length; ++i) {
            Result paramResult = params[i].calculateResult(context);
            Object value = null;
            if (paramResult instanceof ListResult) {
                value = ContainerUtil.map2List(((ListResult) paramResult).getComponents(), result1 -> result1.toString());
            } else if (paramResult != null) {
                value = paramResult.toString();
            }
            binding.setVariable("_" + i, value);
        }
        binding.setVariable("_editor", context.getEditor());
        script.setBinding(binding);
        Object o = script.run();
        return o != null ? StringUtil.convertLineSeparators(o.toString()) : null;
    } catch (Exception e) {
        return new TextResult(StringUtil.convertLineSeparators(e.getLocalizedMessage()));
    }
}
Also used : Binding(groovy.lang.Binding) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.codeInsight.template(com.intellij.codeInsight.template) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) Script(groovy.lang.Script) GroovyShell(groovy.lang.GroovyShell) File(java.io.File) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) Function(com.intellij.util.Function) Binding(groovy.lang.Binding) NotNull(org.jetbrains.annotations.NotNull) LinkedHashSet(java.util.LinkedHashSet) Script(groovy.lang.Script) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 83 with GroovyShell

use of groovy.lang.GroovyShell in project intellij-community by JetBrains.

the class DependentGroovycRunner method applyConfigurationScript.

// adapted from https://github.com/gradle/gradle/blob/c4fdfb57d336b1a0f1b27354c758c61c0a586942/subprojects/language-groovy/src/main/java/org/gradle/api/internal/tasks/compile/ApiGroovyCompiler.java
private static void applyConfigurationScript(File configScript, CompilerConfiguration configuration) {
    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);
    try {
        new GroovyShell(binding, configuratorConfig).evaluate(configScript);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Binding(groovy.lang.Binding) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell)

Example 84 with GroovyShell

use of groovy.lang.GroovyShell in project uPortal by Jasig.

the class PortalShell method main.

public static void main(String[] args) throws Exception {
    final Options options = getOptions();
    final CommandLineParser parser = new PosixParser();
    final CommandLine commandLine;
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        printHelp(options);
        return;
    }
    final ApplicationContext applicationContext = PortalApplicationContextLocator.getApplicationContext();
    try {
        final Binding binding = new SpringBinding(applicationContext);
        binding.setVariable("logger", LOGGER);
        final CompilerConfiguration conf = new CompilerConfiguration(System.getProperties());
        final GroovyShell shell = new GroovyShell(binding, conf);
        if (commandLine.hasOption("script")) {
            final String scriptName = commandLine.getOptionValue("script");
            final File scriptFile = getAbsoluteFile(scriptName);
            shell.run(scriptFile, args);
        }
    } finally {
        if (applicationContext instanceof DisposableBean) {
            ((DisposableBean) applicationContext).destroy();
        }
    }
}
Also used : Binding(groovy.lang.Binding) Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) GroovyShell(groovy.lang.GroovyShell) CommandLine(org.apache.commons.cli.CommandLine) ApplicationContext(org.springframework.context.ApplicationContext) DisposableBean(org.springframework.beans.factory.DisposableBean) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 85 with GroovyShell

use of groovy.lang.GroovyShell in project bamboobsc by billchen198318.

the class ScriptExpressionUtils method buildGroovyShell.

public static GroovyShell buildGroovyShell(boolean fromThreadLocal) {
    if (fromThreadLocal) {
        GroovyShell groovyShell = null;
        if ((groovyShell = groovyShellTL.get()) == null) {
            groovyShell = new GroovyShell(groovyCompilerConfig);
            groovyShellTL.set(groovyShell);
        }
        return groovyShell;
    }
    return new GroovyShell(groovyCompilerConfig);
}
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