don't run groovy shell if there is no full Groovy lib in a module

This commit is contained in:
Max Medvedev
2013-12-21 21:44:25 +04:00
parent 53ea37558d
commit 0ac89c0d2e
5 changed files with 27 additions and 77 deletions
@@ -19,7 +19,6 @@ import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.search.GlobalSearchScope;
@@ -28,12 +27,8 @@ import org.jetbrains.plugins.groovy.config.AbstractConfigUtils;
import org.jetbrains.plugins.groovy.config.GroovyConfigUtils;
import org.jetbrains.plugins.groovy.runner.DefaultGroovyScriptRunner;
import org.jetbrains.plugins.groovy.runner.GroovyScriptRunConfiguration;
import org.jetbrains.plugins.groovy.runner.GroovyScriptRunner;
import org.jetbrains.plugins.groovy.util.GroovyUtils;
import org.jetbrains.plugins.groovy.util.LibrariesUtil;
import java.io.File;
/**
* @author Sergey Evdokimov
*/
@@ -49,58 +44,32 @@ public class DefaultGroovyShellRunner extends GroovyShellRunner {
@Override
public JavaParameters createJavaParameters(@NotNull Module module) throws ExecutionException {
JavaParameters res = GroovyScriptRunConfiguration.createJavaParametersWithSdk(module);
boolean useBundled = !hasGroovyWithNeededJars(module);
DefaultGroovyScriptRunner.configureGenericGroovyRunner(res, module, "org.codehaus.groovy.tools.shell.Main", false, true);
if (useBundled) {
String libRoot = GroovyUtils.getBundledGroovyJar().getParent();
File libDir = new File(libRoot + "/groovy/lib");
assert libDir.isDirectory();
for (File file : libDir.listFiles()) {
res.getClassPath().add(file);
}
GroovyScriptRunner.setGroovyHome(res, FileUtil.toCanonicalPath(libRoot + "/groovy"));
}
res.setWorkingDirectory(getWorkingDirectory(module));
return res;
}
@Override
public boolean canRun(@NotNull Module module) {
VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots();
return contentRoots.length > 0;
return contentRoots.length > 0 && hasGroovyWithNeededJars(module);
}
@NotNull
@Override
public String getTitle(@NotNull Module module) {
String homePath = LibrariesUtil.getGroovyHomePath(module);
boolean bundled = !hasGroovyWithNeededJars(module);
if (bundled) {
homePath = GroovyUtils.getBundledGroovyJar().getParentFile().getParent();
}
else {
assert homePath != null;
}
assert homePath != null;
String version = GroovyConfigUtils.getInstance().getSDKVersion(homePath);
return version == AbstractConfigUtils.UNDEFINED_VERSION ? "" : " (" + (bundled ? "Bundled " : "") + "Groovy " + version + ")";
return version == AbstractConfigUtils.UNDEFINED_VERSION ? "" : " (Groovy " + version + ")";
}
private static boolean hasGroovyWithNeededJars(Module module) {
static boolean hasGroovyWithNeededJars(Module module) {
GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
JavaPsiFacade facade = JavaPsiFacade.getInstance(module.getProject());
return (facade.findClass("org.apache.commons.cli.CommandLineParser", scope) != null ||
facade.findClass("groovyjarjarcommonscli.CommandLineParser", scope) != null) &&
return (facade.findClass("org.apache.commons.cli.CommandLineParser", scope) != null || facade.findClass("groovyjarjarcommonscli.CommandLineParser", scope) != null) &&
facade.findClass("groovy.ui.GroovyMain", scope) != null &&
facade.findClass("org.fusesource.jansi.AnsiConsole", scope) != null;
}
@NotNull
@Override
public String transformUserInput(@NotNull String userInput) {
//return StringUtil.replace(userInput, "\n", "###\\n");
return userInput;
}
}
@@ -69,13 +69,11 @@ public class GroovyConsoleRunner extends GroovyShellRunner {
@NotNull
@Override
public String getTitle(@NotNull Module module) {
String homePath = LibrariesUtil.getGroovyHomePath(module);
boolean bundled = false;
if (homePath == null || !hasGroovyAll(module)) {
homePath = GroovyUtils.getBundledGroovyJar().getParentFile().getParent();
bundled = true;
}
String version = GroovyConfigUtils.getInstance().getSDKVersion(homePath);
String moduleGroovyHomePath = LibrariesUtil.getGroovyHomePath(module);
boolean bundled = moduleGroovyHomePath == null || !hasGroovyAll(module);
String homePathToUse = bundled ? GroovyUtils.getBundledGroovyJar().getParentFile().getParent() : moduleGroovyHomePath;
String version = GroovyConfigUtils.getInstance().getSDKVersion(homePathToUse);
return version == AbstractConfigUtils.UNDEFINED_VERSION ? "" : " (" + (bundled ? "Bundled " : "") + "Groovy " + version + ")";
}
@@ -22,6 +22,11 @@ import com.intellij.openapi.project.Project;
* @author peter
*/
public class GroovyShellAction extends GroovyShellActionBase {
@Override
protected boolean isSuitableModule(Module module) {
return super.isSuitableModule(module) && DefaultGroovyShellRunner.hasGroovyWithNeededJars(module);
}
protected GroovyShellRunner getRunner(Module module) {
return new DefaultGroovyShellRunner();
}
@@ -33,31 +38,6 @@ public class GroovyShellAction extends GroovyShellActionBase {
@Override
protected GroovyShellConsoleImpl createConsole(Project project, String title) {
final GroovyShellConsoleImpl console = new GroovyShellConsoleImpl(project, title);
/*UiNotifyConnector.doWhenFirstShown(console.getComponent(), new Runnable() {
@Override
public void run() {
final String key = "groovy.shell.is.really.groovy.shell";
if (!PropertiesComponent.getInstance().isTrueValue(key)) {
final Alarm alarm = new Alarm();
alarm.addRequest(new Runnable() {
@Override
public void run() {
GotItMessage.createMessage("Groovy Shell & Groovy Console", "<html><div align='left'>Use 'Groovy Console' action (Tools | Groovy Console...) to run <a href='http://'>Groovy Console</a><br>Use 'Groovy Shell' action (Tools | Groovy Shell...) to invoke <a href=\"http://groovy.codehaus.org/Groovy+Shell\">Groovy Shell</a></div></html>")
.setDisposable(console)
.show(new RelativePoint(console.getComponent(), new Point(10, 0)), Balloon.Position.above);
PropertiesComponent.getInstance().setValue(key, String.valueOf(true));
Disposer.dispose(alarm);
}
}, 2000);
}
}
})*/;
return console;
return new GroovyShellConsoleImpl(project, title);
}
}
@@ -64,10 +64,10 @@ public abstract class GroovyShellActionBase extends DumbAwareAction {
public static final Key<Boolean> GROOVY_SHELL_FILE = Key.create("GROOVY_SHELL_FILE");
private static final String GROOVY_SHELL_LAST_MODULE = "Groovy.Shell.LastModule";
private static List<Module> getGroovyCompatibleModules(Project project) {
private List<Module> getGroovyCompatibleModules(Project project) {
ArrayList<Module> result = new ArrayList<Module>();
for (Module module : ModuleManager.getInstance(project).getModules()) {
if (GroovyUtils.isSuitableModule(module)) {
if (isSuitableModule(module)) {
Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
if (sdk != null && sdk.getSdkType() instanceof JavaSdkType) {
result.add(module);
@@ -77,6 +77,10 @@ public abstract class GroovyShellActionBase extends DumbAwareAction {
return result;
}
protected boolean isSuitableModule(Module module) {
return GroovyUtils.isSuitableModule(module);
}
@Override
public void update(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
@@ -100,14 +100,13 @@ public class DefaultGroovyScriptRunner extends GroovyScriptRunner {
setToolsJar(params);
String groovyHome = useBundled ? FileUtil.toCanonicalPath(GroovyUtils.getBundledGroovyJar().getParentFile().getParent()) : LibrariesUtil.getGroovyHomePath(module);
if (groovyHome != null) {
groovyHome = FileUtil.toSystemDependentName(groovyHome);
}
if (groovyHome != null) {
setGroovyHome(params, groovyHome);
String groovyHomeDependentName = groovyHome != null ? FileUtil.toSystemDependentName(groovyHome) : null;
if (groovyHomeDependentName != null) {
setGroovyHome(params, groovyHomeDependentName);
}
final String confPath = getConfPath(groovyHome);
final String confPath = getConfPath(groovyHomeDependentName);
params.getVMParametersList().add("-Dgroovy.starter.conf=" + confPath);
params.getVMParametersList().addAll(HttpConfigurable.convertArguments(HttpConfigurable.getJvmPropertiesList(false, null)));