package jetbrains.mps.debugger.api.runner;
/*Generated by MPS */
import com.intellij.execution.runners.GenericProgramRunner;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.jetbrains.annotations.NotNull;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.executors.DefaultDebugExecutor;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import jetbrains.mps.execution.api.configurations.BaseMpsRunConfiguration;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.ExecutionException;
import org.jetbrains.annotations.Nullable;
import com.intellij.execution.Executor;
import jetbrains.mps.debug.api.IDebugger;
import jetbrains.mps.debug.api.run.DebuggerRunProfileState;
import jetbrains.mps.debug.api.AbstractDebugSessionCreator;
import com.intellij.execution.ExecutionResult;
import jetbrains.mps.debug.api.AbstractDebugSession;
import jetbrains.mps.debug.api.DebugSessionManagerComponent;
import jetbrains.mps.debugger.api.ui.tool.DebuggerToolContentBuilder;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.execution.configurations.RunConfiguration;
public class MPSDebugRunner extends GenericProgramRunner {
private static final Logger LOG = LogManager.getLogger(MPSDebugRunner.class);
public MPSDebugRunner() {
}
@Override
public boolean canRun(@NotNull final String executorId, @NotNull final RunProfile profile) {
try {
return executorId.equals(DefaultDebugExecutor.EXECUTOR_ID) && (isNewRunConfiguration(profile) || isOldRunConfiguration(profile));
} catch (Throwable throwable) {
LOG.debug(throwable.getMessage());
return false;
}
}
private boolean isOldRunConfiguration(RunProfile profile) {
try {
Method method = profile.getClass().getMethod("isDebuggable()");
if (method != null) {
Boolean result = (Boolean) method.invoke(profile);
return result;
}
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
LOG.error(e);
} catch (IllegalAccessException e) {
}
return false;
}
private boolean isNewRunConfiguration(RunProfile profile) {
return (profile instanceof BaseMpsRunConfiguration) && (((BaseMpsRunConfiguration) profile).canExecute(DefaultDebugExecutor.EXECUTOR_ID));
}
@NotNull
@Override
public String getRunnerId() {
return "Default Debug Runner";
}
@Override
protected RunContentDescriptor doExecute(final Project project, final RunProfileState state, final RunContentDescriptor contentToReuse, final ExecutionEnvironment env) throws ExecutionException {
// FileDocumentManager.getInstance().saveAllDocuments();
return createContentDescriptor(project, env.getExecutor(), state, contentToReuse, env);
}
@Nullable
protected RunContentDescriptor createContentDescriptor(Project project, Executor executor, RunProfileState state, RunContentDescriptor contentToReuse, ExecutionEnvironment env) throws ExecutionException {
IDebugger debugger;
// todo get connection settings
if (state instanceof DebuggerRunProfileState) {
debugger = ((DebuggerRunProfileState) state).getDebuggerConfiguration().getDebugger();
} else {
throw new ExecutionException("Unknown Run Profile State");
}
if (debugger == null) {
throw new ExecutionException("Can't Start Debugger");
}
AbstractDebugSessionCreator debugSessionCreator = debugger.createDebugSessionCreator(project);
ExecutionResult executionResult = debugSessionCreator.startSession(executor, this, state, project);
AbstractDebugSession debugSession = debugSessionCreator.getDebugSession();
assert debugSession.getProcessHandler() == executionResult.getProcessHandler() : "Two process handlers for the same debug session";
DebugSessionManagerComponent.getInstance(project).addDebugSession(debugSession);
DebuggerToolContentBuilder contentBuilder = new DebuggerToolContentBuilder(project, this, executor, executionResult, env);
return contentBuilder.showRunContent(contentToReuse);
}
@Override
public SettingsEditor getSettingsEditor(final Executor executor, RunConfiguration configuration) {
return null;
}
}