Saving a run configuration in the run configuration dropdown

Hi - I wrote a small plugin that launches a run configuration programmatically. Code is given below.


What I want to do next is to save that run configuration to the run configuration dropdown. Right now it's not.
I believe having that run configuration saved would allow the user to debug the same run configuration or to run it again with Shift F10. Is that right?


Any other advice on the code is also welcome as this is my first plugin. The full code is on github.

Thanks a lot.
Matt

{
        // .... snipped ....
        Project project = e.getData(PlatformDataKeys.PROJECT);
        String moduleNameOfRunner = component.getModuleNameOfRunner();
        VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE);

        RunManager runManager = RunManager.getInstance(project);
        Module module = ModuleManager.getInstance(project).findModuleByName(moduleNameOfRunner);

        String filename = file.getPath();
        MyRunnerConfigurationType type = new MyRunnerConfigurationType(module, mainClassName, filename);
        RunConfiguration runConfiguration = type.getConfigurationFactories()[0].createTemplateConfiguration(project);

        RunnerAndConfigurationSettings runnerAndConfigurationSettings = runManager.createRunConfiguration("My Runner", type.getConfigurationFactories()[0]);

        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        ProgramRunner runner = RunnerRegistry.getInstance().getRunner(executor.getId(), runConfiguration);
        ExecutionEnvironment environment = new ExecutionEnvironment(runner, runnerAndConfigurationSettings, e.getDataContext());

        try {
            runner.execute(executor, environment);
        } catch (ExecutionException e1) {
            JavaExecutionUtil.showExecutionErrorMessage(e1, "Error", project);
        }
}

3 comments
Comment actions Permalink

By the way, I'm trying to figure out how to save the run configuration into that dropdown. Anyone knows?
Thanks.

0
Comment actions Permalink

Hello,

firstly it is better to obtain instance of MyRunnerConfigurationType from ConfigurationTypeUtil.findConfigurationType method
(MyRunnerConfigurationType is registered as configurationType extension, right?) rather than create a new instance.

Also you don't need to call createTemplateConfiguration() by hand because a RunConfiguration instance is created in
runManager.createRunConfiguration(...) method. You can obtain it by using runnerAndConfigurationSettings.getConfiguration().

Regarding your question: there are no way to add a run configuration to the dropdown via Open API. However you can do it using
RunManagerEx.getInstanceEx(project).addConfiguration(runnerAndConfigurationSettings, false) method call. Source code of RunManagerEx class can be
found in IDEA Community Edition sources.

Hi - I wrote a small plugin that launches a run configuration programmatically. Code is given below.

>
>

What I want to do next is to save that run configuration to the run configuration dropdown. Right now it's not.
I believe having that run configuration saved would allow the user to debug the same run configuration or to run it again with Shift F10. Is that right?

>
>

Any other advice on the code is also welcome as this is my first plugin. The full code is on https://github.com/matstc/intellij-file-runner/blob/master/src/matt/MyRunner.java.

>

Thanks a lot.
Matt

>
>

          // .... snipped ....
>          Project project = e.getData(PlatformDataKeys.PROJECT);
>          String moduleNameOfRunner = component.getModuleNameOfRunner();
>          VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE);
>
>          RunManager runManager = RunManager.getInstance(project);
>          Module module = ModuleManager.getInstance(project).findModuleByName(moduleNameOfRunner);
>
>          String filename = file.getPath();
>          MyRunnerConfigurationType type = new MyRunnerConfigurationType(module, mainClassName, filename);
>          RunConfiguration runConfiguration = type.getConfigurationFactories()[0].createTemplateConfiguration(project);
>
>          RunnerAndConfigurationSettings runnerAndConfigurationSettings = runManager.createRunConfiguration("My Runner", type.getConfigurationFactories()[0]);
>
>          Executor executor = DefaultRunExecutor.getRunExecutorInstance();
>          ProgramRunner runner = RunnerRegistry.getInstance().getRunner(executor.getId(), runConfiguration);
>          ExecutionEnvironment environment = new ExecutionEnvironment(runner, runnerAndConfigurationSettings, e.getDataContext());
>
>          try {
>              runner.execute(executor, environment);
>          } catch (ExecutionException e1) {
>              JavaExecutionUtil.showExecutionErrorMessage(e1, "Error", project);
>          }
> }
>
> ]]>

>

---
Original message URL: http://devnet.jetbrains.net/message/5278419#5278419



--
Nikolay Chashnikov
Software Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Thanks Nikolay.
That was good info.

I was able to hook my run configuration to the dropdown unless it is there already. Hopefully my code can help someone else:



package matt;

import com.intellij.execution.*;
import com.intellij.execution.application.ApplicationConfigurationType;
import com.intellij.execution.impl.*;
import com.intellij.execution.application.ApplicationConfiguration;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.ProgramRunner;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.lang.StringUtils;

public class MyRunner extends AnAction {
    private static final String MY_RUNNER = "MyRunner";

    public void actionPerformed(AnActionEvent e) {
        Application application = ApplicationManager.getApplication();
        MyRunnerComponent component = application.getComponent(MyRunnerComponent.class);

        String mainClassName = component.getMainClassName();
        if (mainClassName == null || mainClassName.isEmpty()) {
            say("Set the class of the runner in the My Runner settings.");
            return;
        }


        Project project = e.getData(PlatformDataKeys.PROJECT);
        String moduleNameOfRunner = component.getModuleNameOfRunner();
        if (moduleNameOfRunner == null || moduleNameOfRunner.isEmpty()) {
            say("Specify the name of the module where the runner class can be found. Check the settings.");
        }

        VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE);

        if (file == null) {
            say("Select a file or focus on the file being edited to feed it to My Runner.");
            return;
        }

        RunManagerImpl runManager = (RunManagerImpl) RunManager.getInstance(project);
        Module module = ModuleManager.getInstance(project).findModuleByName(moduleNameOfRunner);

        if (module == null) {
            say("Could not find the module of the runner with name '" + moduleNameOfRunner + "'. Check settings." +
                    "\n\nHere is the list of modules that were found:\n   " + StringUtils.join(ModuleManager.getInstance(project).getModules(), "\n   "));
            return;
        }

        RunnerAndConfigurationSettingsImpl runnerAndConfigurationSettings = findConfigurationByName(MY_RUNNER, runManager);
        ApplicationConfiguration conf = null;

        if (runnerAndConfigurationSettings != null)     {
            conf = (ApplicationConfiguration) runnerAndConfigurationSettings.getConfiguration();
            updateConfiguration(mainClassName, file, module, conf);

        } else {
            MyRunnerConfigurationType type = application.getComponent(MyRunnerConfigurationType.class);
            runnerAndConfigurationSettings = (RunnerAndConfigurationSettingsImpl) runManager.createRunConfiguration(MY_RUNNER, type.getConfigurationFactories()[0]);
            conf = (ApplicationConfiguration) runnerAndConfigurationSettings.getConfiguration();
            updateConfiguration(mainClassName, file, module, conf);
            runManager.addConfiguration(runnerAndConfigurationSettings, true);
        }
        
        runManager.setActiveConfiguration(runnerAndConfigurationSettings);


        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        ProgramRunner runner = RunnerRegistry.getInstance().getRunner(executor.getId(), conf);
        ExecutionEnvironment environment = new ExecutionEnvironment(runner, runnerAndConfigurationSettings, e.getDataContext());

        try {
            runner.execute(executor, environment);
        } catch (ExecutionException e1) {
            JavaExecutionUtil.showExecutionErrorMessage(e1, "Error", project);
        }
    }

    private RunnerAndConfigurationSettingsImpl findConfigurationByName(String name, RunManagerImpl runManager){
        for (RunnerAndConfigurationSettings settings : runManager.getSortedConfigurations()){
            if (settings.getName().equals(name))
                return (RunnerAndConfigurationSettingsImpl) settings;
        }
        return null;

    }

    private void updateConfiguration(String mainClassName, VirtualFile file, Module module, ApplicationConfiguration conf) {
        conf.setMainClassName(mainClassName);
        conf.setProperty(RunJavaConfiguration.PROGRAM_PARAMETERS_PROPERTY, file.getPath());
        conf.setModule(module);
    }

    public void say(String message) {
        Messages.showMessageDialog(message, "Info", Messages.getInformationIcon());
    }
}



I don't fully udnerstand why the whole API is not accessible for plugin developers though. The RunManager interface is really restrictive.
Thanks,
Matt

0

Please sign in to leave a comment.