These series of steps show how to register and implement a simple Run Configuration. Run Configurations are used to run internal and external processes from within IntelliJ Platform based products.
Consider the runConfiguration sample plugin available in the code samples. See Code Samples on how to set up and run the plugin.
// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.sdk.runConfiguration;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationType;
import com.intellij.icons.AllIcons;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
public class DemoRunConfigurationType implements ConfigurationType {
static final String ID = "DemoRunConfiguration";
@NotNull
@Override
public String getDisplayName() {
return "Demo";
}
@Override
public String getConfigurationTypeDescription() {
return "Demo run configuration type";
}
@Override
public Icon getIcon() {
return AllIcons.General.Information;
}
@NotNull
@Override
public String getId() {
return ID;
}
@Override
public ConfigurationFactory[] getConfigurationFactories() {
return new ConfigurationFactory[]{new DemoConfigurationFactory(this)};
}
}
Implement a ConfigurationFactory
Implement a new ConfigurationFactory through which custom run configurations will be created.
// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.sdk.runConfiguration;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationType;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.openapi.components.BaseState;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class DemoConfigurationFactory extends ConfigurationFactory {
protected DemoConfigurationFactory(ConfigurationType type) {
super(type);
}
@Override
public @NotNull String getId() {
return DemoRunConfigurationType.ID;
}
@NotNull
@Override
public RunConfiguration createTemplateConfiguration(@NotNull Project project) {
return new DemoRunConfiguration(project, this, "Demo");
}
@Nullable
@Override
public Class<? extends BaseState> getOptionsClass() {
return DemoRunConfigurationOptions.class;
}
}
Implement corresponding configuration options class extending RunConfigurationOptions to store settings.
// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.sdk.runConfiguration;
import com.intellij.execution.configurations.RunConfigurationOptions;
import com.intellij.openapi.components.StoredProperty;
public class DemoRunConfigurationOptions extends RunConfigurationOptions {
private final StoredProperty<String> myScriptName = string("").provideDelegate(this, "scriptName");
public String getScriptName() {
return myScriptName.getValue(this);
}
public void setScriptName(String scriptName) {
myScriptName.setValue(this, scriptName);
}
}
Implement a Run Configuration
To make your changes visible from the UI, implement a new Run Configuration.
Note: In most of the cases you can derive a custom Run Configuration class from the RunConfigurationBase. If you need to implement specific settings externalization rules and I/O behaviour, use RunConfiguration interface.
// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.sdk.runConfiguration;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.configurations.*;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessHandlerFactory;
import com.intellij.execution.process.ProcessTerminatedListener;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class DemoRunConfiguration extends RunConfigurationBase<DemoRunConfigurationOptions> {
protected DemoRunConfiguration(Project project, ConfigurationFactory factory, String name) {
super(project, factory, name);
}
@NotNull
@Override
protected DemoRunConfigurationOptions getOptions() {
return (DemoRunConfigurationOptions) super.getOptions();
}
public String getScriptName() {
return getOptions().getScriptName();
}
public void setScriptName(String scriptName) {
getOptions().setScriptName(scriptName);
}
@NotNull
@Override
public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() {
return new DemoSettingsEditor();
}
@Override
public void checkConfiguration() {
}
@Nullable
@Override
public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment executionEnvironment) {
return new CommandLineState(executionEnvironment) {
@NotNull
@Override
protected ProcessHandler startProcess() throws ExecutionException {
GeneralCommandLine commandLine = new GeneralCommandLine(getOptions().getScriptName());
OSProcessHandler processHandler = ProcessHandlerFactory.getInstance().createColoredProcessHandler(commandLine);
ProcessTerminatedListener.attach(processHandler);
return processHandler;
}
};
}
}