Editing Run Configuration settings programatically and/or creating from context
First time trying to make a plugin/use this sdk, so apologies for anything obviously missed or confusingly worded.
What I want to do, in short, is create an instance of a Run Configuration (not of my own creation, specifically Mocha) programmatically based on an EditorPopupMenu action. (For context, later on I want to be able to add a before run action, and trigger it to run).
I have created an empty configuration via
ConfigurationType type = ConfigurationTypeUtil.findConfigurationType("mocha-javascript-test-runner");
ConfigurationFactory[] factories = type.getConfigurationFactories();
RunnerAndConfigurationSettings racs = runManager.createConfiguration("testConfig", factories[0]);
but I'm unsure how to fill out the settings fields that you normally do via the UI. I have seen the SettingsEditor page referenced by the sdk docs, but that seems to be UI only.
I can see in the debugger that a MochaRunSettings object exists with all the fields I'm after but I don't see a method to access it. I also don't know how to create one manually, or turn it into a RunnerAndConfigurationSettings object if I could make it.
Similarly almost all of what I want to set up I think could be done using ConfigurationContext similar to clicking the run button in the gutter. I've done
ConfigurationContext cc = new ConfigurationContext(e.getData(LangDataKeys.PSI_ELEMENT));
but that doesn't seem to have much info when I check it in the debugger, and I'm not sure what method I need to turn it into an actual RunnerAndConfigurationSettings object.
Maybe I'm also just taking a completely wrong approach?
Thanks for any tips anyone has.
Bonus question: How do you programmatically trigger a RunnerAndConfigurationSettings config to actually run as if you clicked it/did Shift+F10?
Please sign in to leave a comment.
Okay I kind of got it, here was generally what was needed:
ConfigurationType type = ConfigurationTypeUtil.findConfigurationType("mocha-javascript-test-runner");
MapDataContext dataContext = new MapDataContext();
dataContext.put(CommonDataKeys.PROJECT, project);
dataContext.put(Location.DATA_KEY, PsiLocation.fromPsiElement(e.getData(LangDataKeys.PSI_ELEMENT)));
List<RunConfigurationProducer<?>> runConfProds = RunConfigurationProducer.getProducers(project);
RunConfigurationProducer runConfProd = null;
// I got the producer above by checking entries in 'runConfProds' against 'type'
// Unfortunately it gets two matches, so it's a bit scuffed right now. Probably better way to do this.
ConfigurationContext configurationContext = ConfigurationContext.getFromContext(dataContext);
ConfigurationFromContext configurationFromContext = runConfProd.createConfigurationFromContext(configurationContext);
RunnerAndConfigurationSettings runnerAndConfigurationSettings = configurationFromContext.getConfigurationSettings();
Executor executor = DefaultRunExecutor.getRunExecutorInstance();
ExecutionUtil.runConfiguration(runnerAndConfigurationSettings, executor);