GoToolWindow toolWindow = GoToolWindow.getInstance(m_project);
toolWindow.setTitle(TITLE);
if (!m_configuration.goBuildBeforeRun && !m_configuration.runPackage) {
// Just run
GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath(goExecName);
commandLine.addParameter("run");
if (m_configuration.runBuilderArguments != null && m_configuration.runBuilderArguments.trim().length() > 0) {
commandLine.getParametersList().addParametersString(m_configuration.runBuilderArguments);
}
commandLine.addParameter(m_configuration.scriptName);
if (m_configuration.scriptArguments != null && m_configuration.scriptArguments.trim().length() > 0) {
commandLine.getParametersList().addParametersString(m_configuration.scriptArguments);
}
commandLine.getEnvironment().putAll(sysEnv);
commandLine.withWorkDirectory(m_configuration.workingDir);
return GoApplicationProcessHandler.runCommandLine(commandLine);
}
// Build and run
String execName;
if (m_configuration.runExecutableName != null && m_configuration.runExecutableName.trim().length() > 0) {
execName = m_configuration.goOutputDir.concat("/").concat(m_configuration.runExecutableName);
}
else {
execName = m_configuration.goOutputDir.concat("/").concat(m_configuration.getName());
}
if (execName.endsWith(".go")) {
execName = execName.substring(0, execName.length() - 3);
}
if (GoSdkUtil.isHostOsWindows()) {
if (!execName.endsWith(".exe")) {
execName = execName.concat(".exe");
}
}
(new File(execName)).delete();
try {
String[] goEnv = GoSdkUtil.convertEnvMapToArray(sysEnv);
String scriptOrPackage;
if (m_configuration.runPackage) {
scriptOrPackage = new java.io.File(m_configuration.getProject().getBaseDir().getPath().concat("/src")).toURI().relativize(new java.io.File(m_configuration.packageDir).toURI()).getPath();
}
else {
scriptOrPackage = m_configuration.scriptName;
}
String[] command = GoSdkUtil.computeGoBuildCommand(goExecName, m_configuration.runBuilderArguments, execName, scriptOrPackage);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command, goEnv, new File(projectDir));
OSProcessHandler handler = new OSProcessHandler(proc, null);
toolWindow.attachConsoleViewToProcess(handler);
toolWindow.printNormalMessage(String.format("%s%n", StringUtil.join(command, " ")));
toolWindow.showAndCreate(m_project);
handler.startNotify();
if (proc.waitFor() == 0) {
VirtualFileManager.getInstance().syncRefresh();
toolWindow.printNormalMessage(String.format("%nFinished building project %s%n", execName));
} else {
toolWindow.printErrorMessage(String.format("%nCould't build project %s%n", execName));
throw new CantRunException(String.format("Error while processing %s build command.", goExecName));
}
} catch (Exception e) {
throw new CantRunException(String.format("Error while processing %s build command.", goExecName));
}
// Now run the build
GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath(execName);
commandLine.withWorkDirectory(m_configuration.workingDir);
if (m_configuration.scriptArguments != null && m_configuration.scriptArguments.trim().length() > 0) {
commandLine.getParametersList().addParametersString(m_configuration.scriptArguments);
}
return GoApplicationProcessHandler.runCommandLine(commandLine);
}