Search in sources :

Example 36 with GroovyShell

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

the class GrapeRunner method main.

public static void main(String[] args) {
    final GroovyShell shell = new GroovyShell();
    try {
        shell.parse(args[0] + " import java.lang.*");
    } catch (MultipleCompilationErrorsException e) {
        List errors = e.getErrorCollector().getErrors();
        for (Iterator iterator = errors.iterator(); iterator.hasNext(); ) {
            Object o = iterator.next();
            if (o instanceof ExceptionMessage) {
                Exception cause = ((ExceptionMessage) o).getCause();
                String message = cause.getMessage();
                if (message != null && message.startsWith("Error grabbing Grapes")) {
                    System.out.println(message);
                    return;
                }
            }
        }
        e.printStackTrace();
        return;
    } catch (Throwable e) {
        e.printStackTrace();
        return;
    }
    URL[] urls = shell.getClassLoader().getURLs();
    for (int i = 0; i < urls.length; i++) {
        System.out.println(URL_PREFIX + urls[i]);
    }
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) Iterator(java.util.Iterator) List(java.util.List) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) GroovyShell(groovy.lang.GroovyShell) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) URL(java.net.URL)

Example 37 with GroovyShell

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

the class GroovyEditor method actionProcess.

//  actionPerformed
/**
	 *  Process Script
	 */
private void actionProcess() {
    MUser user = MUser.get(Env.getCtx());
    if (!user.isAdministrator()) {
        fResult.setText("Not Administrator");
        return;
    }
    //
    GroovyShell sh = new GroovyShell();
    Exception e = null;
    try {
        sh.parse(editor.getTextEditor().getText());
    } catch (Exception e1) {
        e = e1;
    }
    if (e != null) {
        ADialog.error(m_WindowNo, this, "ScriptError", e.toString());
        fResult.setText("Syntax errors detected.");
    } else
        fResult.setText("No syntax errors detected.");
}
Also used : MUser(org.compiere.model.MUser) GroovyShell(groovy.lang.GroovyShell)

Example 38 with GroovyShell

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

the class GroovyModelInitializer method run.

@Override
public void run(JpsModel model) {
    Map<String, Object> variables = new HashMap<>();
    variables.put("project", model.getProject());
    variables.put("global", model.getGlobal());
    try {
        new GroovyShell(new Binding(variables)).evaluate(myScriptFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) IOException(java.io.IOException) GroovyShell(groovy.lang.GroovyShell)

Example 39 with GroovyShell

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

the class ScriptSupport method checkValidScript.

public static String checkValidScript(String scriptText) {
    try {
        final File scriptFile = new File(scriptText);
        final GroovyShell shell = new GroovyShell();
        final Script script = scriptFile.exists() ? shell.parse(scriptFile) : shell.parse(scriptText);
        return null;
    } catch (IOException e) {
        return e.getMessage();
    } catch (MultipleCompilationErrorsException e) {
        final ErrorCollector errorCollector = e.getErrorCollector();
        final List<Message> errors = errorCollector.getErrors();
        for (Message error : errors) {
            if (error instanceof SyntaxErrorMessage) {
                final SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) error;
                final SyntaxException cause = errorMessage.getCause();
                return cause.getMessage();
            }
        }
        return e.getMessage();
    } catch (CompilationFailedException ex) {
        return ex.getLocalizedMessage();
    }
}
Also used : Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) ArrayList(java.util.ArrayList) List(java.util.List) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 40 with GroovyShell

use of groovy.lang.GroovyShell in project fess by codelibs.

the class GroovyUtil method evaluate.

public static Object evaluate(final String template, final Map<String, Object> paramMap) {
    final Map<String, Object> bindingMap = new HashMap<>(paramMap);
    bindingMap.put("container", SingletonLaContainerFactory.getContainer());
    final GroovyShell groovyShell = new GroovyShell(new Binding(bindingMap));
    try {
        return groovyShell.evaluate(template);
    } catch (final Exception e) {
        logger.warn("Failed to evalue groovy script: " + template + " => " + paramMap, e);
        return null;
    } finally {
        final GroovyClassLoader loader = groovyShell.getClassLoader();
        //            StreamUtil.of(loader.getLoadedClasses()).forEach(c -> {
        //                try {
        //                    GroovySystem.getMetaClassRegistry().removeMetaClass(c);
        //                } catch (Throwable t) {
        //                    logger.warn("Failed to delete " + c, t);
        //                }
        //            });
        loader.clearCache();
    }
}
Also used : Binding(groovy.lang.Binding) GroovyClassLoader(groovy.lang.GroovyClassLoader) HashMap(java.util.HashMap) 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