Skip to content

Commit

Permalink
Compile and run the script only once.
Browse files Browse the repository at this point in the history
This should fix issue #116
  • Loading branch information
uschindler committed Jan 31, 2017
1 parent 1a145c3 commit 1fc0a16
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import groovy.lang.Binding;
import groovy.lang.GroovyCodeSource;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import groovy.util.DelegatingScript;

import java.net.URL;
Expand All @@ -45,22 +46,30 @@ public class ForbiddenApisPlugin implements Plugin<Project> {
/** Name of the extension to define defaults for all tasks of this module. */
public static final String FORBIDDEN_APIS_EXTENSION_NAME = "forbiddenApis";

@Override
public void apply(final Project project) {
private final Script script;

public ForbiddenApisPlugin() {
final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName());
final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer);
configuration.setScriptBaseClass(DelegatingScript.class.getName());
configuration.setSourceEncoding("UTF-8");
final Binding binding = new Binding(Collections.singletonMap("project", project));
final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), binding, configuration);
final GroovyShell shell = new GroovyShell(ForbiddenApisPlugin.class.getClassLoader(), new Binding(), configuration);
final URL scriptUrl = ForbiddenApisPlugin.class.getResource(PLUGIN_INIT_SCRIPT);
if (scriptUrl == null) {
throw new PluginInstantiationException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT);
}
final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl);
final DelegatingScript script = (DelegatingScript) shell.parse(csrc);
script.setDelegate(this);
this.script = script;
}

// synchronized because we change the property "project" in the binding
@Override
public synchronized void apply(Project project) {
script.setProperty("project", project);
script.run();
script.setProperty("project", null);
}

}

0 comments on commit 1fc0a16

Please sign in to comment.