Search in sources :

Example 1 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer 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 2 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project workflow-cps-plugin by jenkinsci.

the class CpsGroovyShellFactory method makeImportCustomizer.

private ImportCustomizer makeImportCustomizer() {
    ImportCustomizer ic = new ImportCustomizer();
    ic.addStarImports(NonCPS.class.getPackage().getName());
    ic.addStarImports("hudson.model", "jenkins.model");
    for (GroovyShellDecorator d : decorators) {
        d.customizeImports(execution, ic);
    }
    return ic;
}
Also used : ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer)

Example 3 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project groovity by disney.

the class Groovity method createCompilerConfiguration.

protected CompilerConfiguration createCompilerConfiguration(Map<Integer, Integer> sourceLineNumbers, Collection<String> initDependencies) {
    CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    if (scriptBaseClass != null) {
        compilerConfiguration.setScriptBaseClass(scriptBaseClass);
        compilerConfiguration.addCompilationCustomizers(new ImportCustomizer().addImports(scriptBaseClass));
    }
    compilerConfiguration.addCompilationCustomizers(new ImportCustomizer().addStarImports("java.util.concurrent", "groovy.transform", "java.util.function", "com.disney.groovity.model").addImports(ClosureWritable.class.getName(), AsyncChannel.class.getName(), GroovityStatistics.class.getName(), Taggable.class.getName(), Tag.class.getName(), Attr.class.getName(), Function.class.getName(), Arg.class.getName(), SkipStatistics.class.getName()));
    compilerConfiguration.addCompilationCustomizers(new ASTTransformationCustomizer(new GroovityASTTransformation(this, sourceLineNumbers, initDependencies)));
    compilerConfiguration.addCompilationCustomizers(new ASTTransformationCustomizer(new StatsASTTransformation()));
    Map<String, Boolean> options = compilerConfiguration.getOptimizationOptions();
    options.put(CompilerConfiguration.JDK8, true);
    compilerConfigurationDecorators.forEach(decorator -> {
        decorator.decorate(compilerConfiguration);
    });
    return compilerConfiguration;
}
Also used : ASTTransformationCustomizer(org.codehaus.groovy.control.customizers.ASTTransformationCustomizer) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) StatsASTTransformation(com.disney.groovity.compile.StatsASTTransformation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GroovityASTTransformation(com.disney.groovity.compile.GroovityASTTransformation)

Example 4 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project sponge by softelnet.

the class GroovyKnowledgeBaseInterpreter method prepareInterpreter.

@Override
protected void prepareInterpreter() {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    PROCESSOR_CLASSES.forEach((interfaceClass, scriptClass) -> addImport(importCustomizer, scriptClass, interfaceClass.getSimpleName()));
    addImport(importCustomizer, GroovyPlugin.class, Plugin.class.getSimpleName());
    getStandardImportClasses().forEach(cls -> addImport(importCustomizer, cls));
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    binding = createBinding();
    shell = new GroovyShell(binding, configuration);
    scripts = Collections.synchronizedList(new ArrayList<>());
    setVariable(KnowledgeBaseConstants.VAR_ENGINE_OPERATIONS, getEngineOperations());
    setClasspath(getEngineOperations() != null ? getEngineOperations().getEngine() : null);
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ArrayList(java.util.ArrayList) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell) GroovyPlugin(org.openksavi.sponge.groovy.GroovyPlugin) Plugin(org.openksavi.sponge.plugin.Plugin)

Example 5 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project beakerx by twosigma.

the class GroovyEvaluator method reloadClassloader.

private void reloadClassloader() {
    this.beakerxUrlClassLoader = newParentClassLoader(getClasspath());
    this.icz = new ImportCustomizer();
    this.groovyClassLoader = newEvaluator(getImports(), getClasspath(), getOutDir(), icz, beakerxUrlClassLoader);
    this.scriptBinding = new Binding();
}
Also used : Binding(groovy.lang.Binding) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyClassLoaderFactory.addImportPathToImportCustomizer(com.twosigma.beakerx.groovy.evaluator.GroovyClassLoaderFactory.addImportPathToImportCustomizer)

Aggregations

ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)25 GroovyShell (groovy.lang.GroovyShell)14 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Binding (groovy.lang.Binding)12 File (java.io.File)5 Closure (groovy.lang.Closure)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 List (java.util.List)2 ParseException (org.apache.commons.cli.ParseException)2 BuildException (org.apache.tools.ant.BuildException)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)2 Test (org.junit.Test)2