diff --git a/groovy-impl/src/main/java/org/jetbrains/plugins/groovy/impl/runner/GroovyScriptRunConfiguration.java b/groovy-impl/src/main/java/org/jetbrains/plugins/groovy/impl/runner/GroovyScriptRunConfiguration.java index c23b45bc..d1dee00e 100644 --- a/groovy-impl/src/main/java/org/jetbrains/plugins/groovy/impl/runner/GroovyScriptRunConfiguration.java +++ b/groovy-impl/src/main/java/org/jetbrains/plugins/groovy/impl/runner/GroovyScriptRunConfiguration.java @@ -18,7 +18,8 @@ import com.intellij.java.execution.CommonJavaRunConfigurationParameters; import com.intellij.java.execution.configurations.JavaCommandLineState; import com.intellij.java.language.psi.PsiClass; -import consulo.application.ApplicationManager; +import consulo.annotation.access.RequiredReadAction; +import consulo.application.Application; import consulo.content.bundle.Sdk; import consulo.execution.CantRunException; import consulo.execution.RuntimeConfigurationException; @@ -29,6 +30,7 @@ import consulo.execution.runner.ExecutionEnvironment; import consulo.execution.ui.console.TextConsoleBuilderFactory; import consulo.execution.util.ProgramParametersUtil; +import consulo.groovy.localize.GroovyLocalize; import consulo.java.execution.configurations.OwnJavaParameters; import consulo.java.language.module.extension.JavaModuleExtension; import consulo.language.editor.refactoring.event.RefactoringElementAdapter; @@ -38,6 +40,7 @@ import consulo.language.psi.PsiFile; import consulo.language.psi.PsiManager; import consulo.language.util.ModuleUtilCore; +import consulo.localize.LocalizeValue; import consulo.logging.Logger; import consulo.module.Module; import consulo.module.ModuleManager; @@ -57,388 +60,399 @@ import consulo.virtualFileSystem.VirtualFile; import consulo.virtualFileSystem.VirtualFileManager; import consulo.virtualFileSystem.util.VirtualFilePathUtil; +import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; import org.jdom.Element; -import org.jetbrains.plugins.groovy.GroovyBundle; import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition; import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass; -import jakarta.annotation.Nonnull; - import java.util.*; /** * @author peter */ -public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration implements CommonJavaRunConfigurationParameters, RefactoringListenerProvider { - private static final Logger LOG = Logger.getInstance(GroovyScriptRunConfiguration.class); - private String vmParams; - private String workDir; - private boolean isDebugEnabled; - @Nullable - private String scriptParams; - @Nullable - private String scriptPath; - private final Map envs = new LinkedHashMap(); - public boolean passParentEnv = true; - - public GroovyScriptRunConfiguration(String name, Project project, ConfigurationFactory factory) { - super(name, new RunConfigurationModule(project), factory); - workDir = VirtualFilePathUtil.getLocalPath(project.getBaseDir()); - } - - @Override - protected ModuleBasedConfiguration createInstance() { - return new GroovyScriptRunConfiguration(getName(), getProject(), getFactory()); - } - - public void setWorkDir(String dir) { - workDir = dir; - } - - public String getWorkDir() { - return workDir; - } - - @Nullable - public Module getModule() { - return getConfigurationModule().getModule(); - } - - public Collection getValidModules() { - Module[] modules = ModuleManager.getInstance(getProject()).getModules(); - GroovyScriptRunner scriptRunner = findConfiguration(); - if (scriptRunner == null) { - return Arrays.asList(modules); - } - - - ArrayList res = new ArrayList(); - for (Module module : modules) { - if (scriptRunner.isValidModule(module)) { - res.add(module); - } - } - return res; - } - - @Nullable - private GroovyScriptRunner findConfiguration() { - VirtualFile scriptFile = getScriptFile(); - if (scriptFile == null) { - return null; - } - - PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(scriptFile); - if (!(psiFile instanceof GroovyFile)) { - return null; - } - - GroovyFile groovyFile = (GroovyFile)psiFile; - if (groovyFile.isScript()) { - return GroovyScriptUtil.getScriptType(groovyFile).getRunner(); - } - else { - return new DefaultGroovyScriptRunner(); - } - } - - public void readExternal(Element element) throws InvalidDataException { - ProjectPathMacroManager.getInstance(getProject()).expandPaths(element); - super.readExternal(element); - scriptPath = ExternalizablePath.localPathValue(JDOMExternalizer.readString(element, "path")); - vmParams = JDOMExternalizer.readString(element, "vmparams"); - scriptParams = JDOMExternalizer.readString(element, "params"); - String wrk = JDOMExternalizer.readString(element, "workDir"); - if (!".".equals(wrk)) { - workDir = ExternalizablePath.localPathValue(wrk); - } - isDebugEnabled = Boolean.parseBoolean(JDOMExternalizer.readString(element, "debug")); - envs.clear(); - JDOMExternalizer.readMap(element, envs, null, "env"); - } - - public void writeExternal(Element element) throws WriteExternalException { - super.writeExternal(element); - JDOMExternalizer.write(element, "path", ExternalizablePath.urlValue(scriptPath)); - JDOMExternalizer.write(element, "vmparams", vmParams); - JDOMExternalizer.write(element, "params", scriptParams); - JDOMExternalizer.write(element, "workDir", ExternalizablePath.urlValue(workDir)); - JDOMExternalizer.write(element, "debug", isDebugEnabled); - JDOMExternalizer.writeMap(element, envs, null, "env"); - ProjectPathMacroManager.getInstance(getProject()).collapsePathsRecursively(element); - } - - public RunProfileState getState(@Nonnull Executor executor, @Nonnull ExecutionEnvironment environment) throws ExecutionException { - final VirtualFile script = getScriptFile(); - if (script == null) { - throw new CantRunException("Cannot find script " + scriptPath); - } - - final GroovyScriptRunner scriptRunner = findConfiguration(); - if (scriptRunner == null) { - throw new CantRunException("Unknown script type " + scriptPath); +public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration + implements CommonJavaRunConfigurationParameters, RefactoringListenerProvider { + + private static final Logger LOG = Logger.getInstance(GroovyScriptRunConfiguration.class); + + private String vmParams; + private String workDir; + private boolean isDebugEnabled; + @Nullable + private String scriptParams; + @Nullable + private String scriptPath; + private final Map envs = new LinkedHashMap<>(); + public boolean passParentEnv = true; + + public GroovyScriptRunConfiguration(String name, Project project, ConfigurationFactory factory) { + super(name, new RunConfigurationModule(project), factory); + workDir = VirtualFilePathUtil.getLocalPath(project.getBaseDir()); } - final Module module = getModule(); - if (!scriptRunner.ensureRunnerConfigured(module, this, executor, getProject())) { - return null; + @Override + protected ModuleBasedConfiguration createInstance() { + return new GroovyScriptRunConfiguration(getName(), getProject(), getFactory()); } - final boolean tests = ProjectRootManager.getInstance(getProject()).getFileIndex().isInTestSourceContent(script); + public void setWorkDir(String dir) { + workDir = dir; + } - JavaCommandLineState state = new JavaCommandLineState(environment) { - @Override - protected void buildProcessHandler(@Nonnull ProcessHandlerBuilder builder) throws ExecutionException { - super.buildProcessHandler(builder); - builder.shouldDestroyProcessRecursively(true); - } + public String getWorkDir() { + return workDir; + } - @Override - protected void setupProcessHandler(@Nonnull ProcessHandler handler) { - super.setupProcessHandler(handler); + @Nullable + public Module getModule() { + return getConfigurationModule().getModule(); + } - if (scriptRunner.shouldRefreshAfterFinish()) { - handler.addProcessListener(new ProcessListener() { - @Override - public void processTerminated(ProcessEvent event) { - if (!ApplicationManager.getApplication().isDisposed()) { - VirtualFileManager.getInstance().asyncRefresh(null); - } + @Override + public Collection getValidModules() { + Module[] modules = ModuleManager.getInstance(getProject()).getModules(); + GroovyScriptRunner scriptRunner = findConfiguration(); + if (scriptRunner == null) { + return Arrays.asList(modules); + } + + + List res = new ArrayList<>(); + for (Module module : modules) { + if (scriptRunner.isValidModule(module)) { + res.add(module); } - }); } - } + return res; + } - protected OwnJavaParameters createJavaParameters() throws ExecutionException { - OwnJavaParameters params = createJavaParametersWithSdk(module); - ProgramParametersUtil.configureConfiguration(params, GroovyScriptRunConfiguration.this); - scriptRunner.configureCommandLine(params, module, tests, script, GroovyScriptRunConfiguration.this); + @Nullable + @RequiredReadAction + private GroovyScriptRunner findConfiguration() { + VirtualFile scriptFile = getScriptFile(); + if (scriptFile == null) { + return null; + } - return params; - } - }; + PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(scriptFile); + if (!(psiFile instanceof GroovyFile groovyFile)) { + return null; + } - state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject())); - return state; - } + if (groovyFile.isScript()) { + return GroovyScriptUtil.getScriptType(groovyFile).getRunner(); + } + else { + return new DefaultGroovyScriptRunner(); + } + } - public void setScriptParameters(String scriptParameters) { - scriptParams = scriptParameters; - } + @Override + public void readExternal(Element element) throws InvalidDataException { + ProjectPathMacroManager.getInstance(getProject()).expandPaths(element); + super.readExternal(element); + scriptPath = ExternalizablePath.localPathValue(JDOMExternalizer.readString(element, "path")); + vmParams = JDOMExternalizer.readString(element, "vmparams"); + scriptParams = JDOMExternalizer.readString(element, "params"); + String wrk = JDOMExternalizer.readString(element, "workDir"); + if (!".".equals(wrk)) { + workDir = ExternalizablePath.localPathValue(wrk); + } + isDebugEnabled = Boolean.parseBoolean(JDOMExternalizer.readString(element, "debug")); + envs.clear(); + JDOMExternalizer.readMap(element, envs, null, "env"); + } - @Override - public RefactoringElementListener getRefactoringElementListener(PsiElement element) { - if (scriptPath == null || !scriptPath.equals(getPathByElement(element))) { - return null; + @Override + public void writeExternal(Element element) throws WriteExternalException { + super.writeExternal(element); + JDOMExternalizer.write(element, "path", ExternalizablePath.urlValue(scriptPath)); + JDOMExternalizer.write(element, "vmparams", vmParams); + JDOMExternalizer.write(element, "params", scriptParams); + JDOMExternalizer.write(element, "workDir", ExternalizablePath.urlValue(workDir)); + JDOMExternalizer.write(element, "debug", isDebugEnabled); + JDOMExternalizer.writeMap(element, envs, null, "env"); + ProjectPathMacroManager.getInstance(getProject()).collapsePathsRecursively(element); } - PsiClass classToRun = GroovyRunnerUtil.getRunningClass(element); + @Override + @RequiredReadAction + public RunProfileState getState(@Nonnull Executor executor, @Nonnull ExecutionEnvironment environment) throws ExecutionException { + final VirtualFile script = getScriptFile(); + if (script == null) { + throw new CantRunException(LocalizeValue.localizeTODO("Cannot find script " + scriptPath)); + } - if (element instanceof GroovyFile) { - return new RefactoringElementAdapter() { - @Override - protected void elementRenamedOrMoved(@Nonnull PsiElement newElement) { - if (newElement instanceof GroovyFile) { - GroovyFile file = (GroovyFile)newElement; - setScriptPath(file.getVirtualFile().getPath()); + final GroovyScriptRunner scriptRunner = findConfiguration(); + if (scriptRunner == null) { + throw new CantRunException(LocalizeValue.localizeTODO("Unknown script type " + scriptPath)); + } + + final Module module = getModule(); + if (!scriptRunner.ensureRunnerConfigured(module, this, executor, getProject())) { + return null; + } - PsiClass newClassToRun = GroovyRunnerUtil.getRunningClass(newElement); - if (newClassToRun instanceof GroovyScriptClass) { - setName(GroovyRunnerUtil.getConfigurationName(file.getScriptClass(), getConfigurationModule())); + final boolean tests = ProjectRootManager.getInstance(getProject()).getFileIndex().isInTestSourceContent(script); + + JavaCommandLineState state = new JavaCommandLineState(environment) { + @Override + protected void buildProcessHandler(@Nonnull ProcessHandlerBuilder builder) throws ExecutionException { + super.buildProcessHandler(builder); + builder.shouldDestroyProcessRecursively(true); } - } + + @Override + protected void setupProcessHandler(@Nonnull ProcessHandler handler) { + super.setupProcessHandler(handler); + + if (scriptRunner.shouldRefreshAfterFinish()) { + handler.addProcessListener(new ProcessListener() { + @Override + public void processTerminated(ProcessEvent event) { + if (!Application.get().isDisposed()) { + VirtualFileManager.getInstance().asyncRefresh(null); + } + } + }); + } + } + + @Override + protected OwnJavaParameters createJavaParameters() throws ExecutionException { + OwnJavaParameters params = createJavaParametersWithSdk(module); + ProgramParametersUtil.configureConfiguration(params, GroovyScriptRunConfiguration.this); + scriptRunner.configureCommandLine(params, module, tests, script, GroovyScriptRunConfiguration.this); + + return params; + } + }; + + state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject())); + return state; + } + + public void setScriptParameters(String scriptParameters) { + scriptParams = scriptParameters; + } + + @Override + public RefactoringElementListener getRefactoringElementListener(PsiElement element) { + if (scriptPath == null || !scriptPath.equals(getPathByElement(element))) { + return null; } - @Override - public void undoElementMovedOrRenamed(@Nonnull PsiElement newElement, @Nonnull String oldQualifiedName) { - elementRenamedOrMoved(newElement); + PsiClass classToRun = GroovyRunnerUtil.getRunningClass(element); + + if (element instanceof GroovyFile) { + return new RefactoringElementAdapter() { + @Override + protected void elementRenamedOrMoved(@Nonnull PsiElement newElement) { + if (newElement instanceof GroovyFile file) { + setScriptPath(file.getVirtualFile().getPath()); + + PsiClass newClassToRun = GroovyRunnerUtil.getRunningClass(newElement); + if (newClassToRun instanceof GroovyScriptClass) { + setName(GroovyRunnerUtil.getConfigurationName(file.getScriptClass(), getConfigurationModule())); + } + } + } + + @Override + public void undoElementMovedOrRenamed(@Nonnull PsiElement newElement, @Nonnull String oldQualifiedName) { + elementRenamedOrMoved(newElement); + } + }; + } + else if (element instanceof PsiClass && element.getManager().areElementsEquivalent(element, classToRun)) { + return new RefactoringElementAdapter() { + @Override + @RequiredReadAction + protected void elementRenamedOrMoved(@Nonnull PsiElement newElement) { + setName(((PsiClass) newElement).getName()); + } + + @Override + @RequiredReadAction + public void undoElementMovedOrRenamed(@Nonnull PsiElement newElement, @Nonnull String oldQualifiedName) { + elementRenamedOrMoved(newElement); + } + }; } - }; + return null; } - else if (element instanceof PsiClass && element.getManager().areElementsEquivalent(element, classToRun)) { - return new RefactoringElementAdapter() { - @Override - protected void elementRenamedOrMoved(@Nonnull PsiElement newElement) { - setName(((PsiClass)newElement).getName()); + + @Nullable + private static String getPathByElement(@Nonnull PsiElement element) { + PsiFile file = element.getContainingFile(); + if (file == null) { + return null; } + VirtualFile vFile = file.getVirtualFile(); + if (vFile == null) { + return null; + } + return vFile.getPath(); + } + + public static OwnJavaParameters createJavaParametersWithSdk(@Nullable Module module) { + OwnJavaParameters params = new OwnJavaParameters(); + params.setCharset(null); + + if (module != null) { + Sdk sdk = ModuleUtilCore.getSdk(module, JavaModuleExtension.class); + if (sdk != null) { + params.setJdk(sdk); + } + } + return params; + } + + @Nullable + private VirtualFile getScriptFile() { + if (scriptPath == null) { + return null; + } + return LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(scriptPath)); + } - @Override - public void undoElementMovedOrRenamed(@Nonnull PsiElement newElement, @Nonnull String oldQualifiedName) { - elementRenamedOrMoved(newElement); + @Nullable + @RequiredReadAction + private PsiClass getScriptClass() { + VirtualFile scriptFile = getScriptFile(); + if (scriptFile == null) { + return null; } - }; - } - return null; - } - - @Nullable - private static String getPathByElement(@Nonnull PsiElement element) { - PsiFile file = element.getContainingFile(); - if (file == null) { - return null; - } - VirtualFile vfile = file.getVirtualFile(); - if (vfile == null) { - return null; - } - return vfile.getPath(); - } - - public static OwnJavaParameters createJavaParametersWithSdk(@Nullable Module module) { - OwnJavaParameters params = new OwnJavaParameters(); - params.setCharset(null); - - if (module != null) { - Sdk sdk = ModuleUtilCore.getSdk(module, JavaModuleExtension.class); - if (sdk != null) { - params.setJdk(sdk); - } - } - return params; - } - - @Nullable - private VirtualFile getScriptFile() { - if (scriptPath == null) { - return null; - } - return LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(scriptPath)); - } - - @Nullable - private PsiClass getScriptClass() { - VirtualFile scriptFile = getScriptFile(); - if (scriptFile == null) { - return null; - } - PsiFile file = PsiManager.getInstance(getProject()).findFile(scriptFile); - return GroovyRunnerUtil.getRunningClass(file); - } - - public SettingsEditor getConfigurationEditor() { - return new GroovyRunConfigurationEditor(getProject()); - } - - @Override - public void checkConfiguration() throws RuntimeConfigurationException { - super.checkConfiguration(); - PsiClass toRun = getScriptClass(); - if (toRun == null) { - throw new RuntimeConfigurationWarning(GroovyBundle.message("class.does.not.exist")); - } - if (toRun instanceof GrTypeDefinition) { - if (!GroovyRunnerUtil.canBeRunByGroovy(toRun)) { - throw new RuntimeConfigurationWarning(GroovyBundle.message("class.can't be executed")); - } - } - else if (!(toRun instanceof GroovyScriptClass)) { - throw new RuntimeConfigurationWarning(GroovyBundle.message("script.file.is.not.groovy.file")); - } - } - - @Override - public void setVMParameters(String value) { - vmParams = value; - } - - @Override - public String getVMParameters() { - return vmParams; - } - - @Override - public boolean isAlternativeJrePathEnabled() { - throw new UnsupportedOperationException(); - } - - @Override - public void setAlternativeJrePathEnabled(boolean enabled) { - throw new UnsupportedOperationException(); - } - - @Override - public String getAlternativeJrePath() { - throw new UnsupportedOperationException(); - } - - @Override - public void setAlternativeJrePath(String path) { - throw new UnsupportedOperationException(); - } - - @Override - public String getRunClass() { - return null; - } - - @Override - public String getPackage() { - return null; - } - - @Override - public void setProgramParameters(@Nullable String value) { - LOG.error("Don't add program parameters to Groovy script run configuration. Use Script parameters instead"); - } - - @Override - public String getProgramParameters() { - return null; - } - - @Nullable - public String getScriptParameters() { - return scriptParams; - } - - @Override - public void setWorkingDirectory(@Nullable String value) { - workDir = value; - } - - @Override - public String getWorkingDirectory() { - return workDir; - } - - @Override - public void setEnvs(@Nonnull Map envs) { - this.envs.clear(); - this.envs.putAll(envs); - } - - @Nonnull - @Override - public Map getEnvs() { - return envs; - } - - @Override - public void setPassParentEnvs(boolean passParentEnvs) { - this.passParentEnv = passParentEnvs; - } - - @Override - public boolean isPassParentEnvs() { - return passParentEnv; - } - - public boolean isDebugEnabled() { - return isDebugEnabled; - } - - public void setDebugEnabled(boolean debugEnabled) { - isDebugEnabled = debugEnabled; - } - - @Nullable - public String getScriptPath() { - return scriptPath; - } - - public void setScriptPath(@Nullable String scriptPath) { - this.scriptPath = scriptPath; - } + PsiFile file = PsiManager.getInstance(getProject()).findFile(scriptFile); + return GroovyRunnerUtil.getRunningClass(file); + } + + @Override + public SettingsEditor getConfigurationEditor() { + return new GroovyRunConfigurationEditor(getProject()); + } + + @Override + @RequiredReadAction + public void checkConfiguration() throws RuntimeConfigurationException { + super.checkConfiguration(); + PsiClass toRun = getScriptClass(); + if (toRun == null) { + throw new RuntimeConfigurationWarning(GroovyLocalize.classDoesNotExist()); + } + if (toRun instanceof GrTypeDefinition) { + if (!GroovyRunnerUtil.canBeRunByGroovy(toRun)) { + throw new RuntimeConfigurationWarning(GroovyLocalize.classCanTBeExecuted()); + } + } + else if (!(toRun instanceof GroovyScriptClass)) { + throw new RuntimeConfigurationWarning(GroovyLocalize.scriptFileIsNotGroovyFile()); + } + } + + @Override + public void setVMParameters(String value) { + vmParams = value; + } + + @Override + public String getVMParameters() { + return vmParams; + } + + @Override + public boolean isAlternativeJrePathEnabled() { + throw new UnsupportedOperationException(); + } + + @Override + public void setAlternativeJrePathEnabled(boolean enabled) { + throw new UnsupportedOperationException(); + } + + @Override + public String getAlternativeJrePath() { + throw new UnsupportedOperationException(); + } + + @Override + public void setAlternativeJrePath(String path) { + throw new UnsupportedOperationException(); + } + + @Override + public String getRunClass() { + return null; + } + + @Override + public String getPackage() { + return null; + } + + @Override + public void setProgramParameters(@Nullable String value) { + LOG.error("Don't add program parameters to Groovy script run configuration. Use Script parameters instead"); + } + + @Override + public String getProgramParameters() { + return null; + } + + @Nullable + public String getScriptParameters() { + return scriptParams; + } + + @Override + public void setWorkingDirectory(@Nullable String value) { + workDir = value; + } + + @Override + public String getWorkingDirectory() { + return workDir; + } + + @Override + public void setEnvs(@Nonnull Map envs) { + this.envs.clear(); + this.envs.putAll(envs); + } + + @Nonnull + @Override + public Map getEnvs() { + return envs; + } + + @Override + public void setPassParentEnvs(boolean passParentEnvs) { + this.passParentEnv = passParentEnvs; + } + + @Override + public boolean isPassParentEnvs() { + return passParentEnv; + } + + public boolean isDebugEnabled() { + return isDebugEnabled; + } + + public void setDebugEnabled(boolean debugEnabled) { + isDebugEnabled = debugEnabled; + } + + @Nullable + public String getScriptPath() { + return scriptPath; + } + + public void setScriptPath(@Nullable String scriptPath) { + this.scriptPath = scriptPath; + } }