From 0ac89c0d2ecafaa78d281fb266b01ecaab8493c1 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Sat, 21 Dec 2013 21:44:25 +0400 Subject: [PATCH] don't run groovy shell if there is no full Groovy lib in a module --- .../console/DefaultGroovyShellRunner.java | 41 +++---------------- .../groovy/console/GroovyConsoleRunner.java | 12 +++--- .../groovy/console/GroovyShellAction.java | 32 +++------------ .../groovy/console/GroovyShellActionBase.java | 8 +++- .../runner/DefaultGroovyScriptRunner.java | 11 +++-- 5 files changed, 27 insertions(+), 77 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/DefaultGroovyShellRunner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/DefaultGroovyShellRunner.java index db046d1423c8..26e5cadd890c 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/DefaultGroovyShellRunner.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/DefaultGroovyShellRunner.java @@ -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; - } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyConsoleRunner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyConsoleRunner.java index bf46a21b87e4..1932da4e2e10 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyConsoleRunner.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyConsoleRunner.java @@ -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 + ")"; } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellAction.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellAction.java index 835687a07aad..085cce5a6fdf 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellAction.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellAction.java @@ -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", "
Use 'Groovy Console' action (Tools | Groovy Console...) to run Groovy Console
Use 'Groovy Shell' action (Tools | Groovy Shell...) to invoke Groovy Shell
") - .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); } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellActionBase.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellActionBase.java index 90087a81c2aa..8a80bb209d69 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellActionBase.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/GroovyShellActionBase.java @@ -64,10 +64,10 @@ public abstract class GroovyShellActionBase extends DumbAwareAction { public static final Key GROOVY_SHELL_FILE = Key.create("GROOVY_SHELL_FILE"); private static final String GROOVY_SHELL_LAST_MODULE = "Groovy.Shell.LastModule"; - private static List getGroovyCompatibleModules(Project project) { + private List getGroovyCompatibleModules(Project project) { ArrayList result = new ArrayList(); 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); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/runner/DefaultGroovyScriptRunner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/runner/DefaultGroovyScriptRunner.java index dff59a8df2a7..a725363e93ef 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/runner/DefaultGroovyScriptRunner.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/runner/DefaultGroovyScriptRunner.java @@ -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)));