Skip to content

Commit baadb10

Browse files
committed
Merge pull request #660 from d3xter/go_vet_background
Run GoVet in background
2 parents 88eadcc + 485d80d commit baadb10

File tree

9 files changed

+232
-75
lines changed

9 files changed

+232
-75
lines changed

src/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@
305305
<extensions defaultExtensionNs="com.intellij">
306306
<toolWindow id="Go" anchor="bottom" icon="/icons/go_13x13.png" factoryClass="ro.redeul.google.go.ide.ui.GoToolWindowFactory" />
307307

308+
<stepsBeforeRunProvider implementation="ro.redeul.google.go.runner.beforeRunTasks.GoVetTaskProvider" />
309+
308310
<applicationService
309311
serviceInterface="ro.redeul.google.go.ide.GoGlobalSettings"
310312
serviceImplementation="ro.redeul.google.go.ide.GoGlobalSettings"/>

src/ro/redeul/google/go/runner/GoApplicationConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public class GoApplicationConfiguration extends ModuleBasedConfiguration<GoAppli
4848
public String goOutputDir = "";
4949
public String workingDir = "";
5050
public String envVars = "";
51-
public Boolean goVetEnabled = false;
5251

5352
public GoApplicationConfiguration(String name, Project project, GoApplicationConfigurationType configurationType) {
5453
super(name, new GoApplicationModuleBasedConfiguration(project), configurationType.getConfigurationFactories()[0]);

src/ro/redeul/google/go/runner/GoRunProfileState.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,6 @@ protected ProcessHandler startProcess() throws ExecutionException {
6161
GoToolWindow toolWindow = GoToolWindow.getInstance(m_project);
6262
toolWindow.setTitle(TITLE);
6363

64-
if (m_configuration.goVetEnabled) {
65-
try {
66-
String[] goEnv = GoSdkUtil.convertEnvMapToArray(sysEnv);
67-
68-
String command = String.format(
69-
"%s vet ./...",
70-
goExecName
71-
);
72-
73-
Runtime rt = Runtime.getRuntime();
74-
Process proc = rt.exec(command, goEnv, new File(projectDir));
75-
OSProcessHandler handler = new OSProcessHandler(proc, null);
76-
toolWindow.attachConsoleViewToProcess(handler);
77-
toolWindow.printNormalMessage(String.format("%s%n", command));
78-
handler.startNotify();
79-
80-
if (proc.waitFor() == 0) {
81-
VirtualFileManager.getInstance().syncRefresh();
82-
toolWindow.printNormalMessage(String.format("%nFinished running go vet on project %s%n", projectDir));
83-
} else {
84-
toolWindow.printErrorMessage(String.format("%nCouldn't vet project %s%n", projectDir));
85-
throw new CantRunException(String.format("Error while processing %s vet command.", goExecName));
86-
}
87-
} catch (Exception e) {
88-
throw new CantRunException(String.format("Error while processing %s vet command.", goExecName));
89-
}
90-
}
91-
9264
if (!m_configuration.goBuildBeforeRun) {
9365
// Just run
9466
GeneralCommandLine commandLine = new GeneralCommandLine();
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package ro.redeul.google.go.runner.beforeRunTasks;
2+
3+
import com.intellij.execution.CantRunException;
4+
import com.intellij.execution.filters.TextConsoleBuilderFactory;
5+
import com.intellij.execution.process.OSProcessHandler;
6+
import com.intellij.execution.ui.ConsoleView;
7+
import com.intellij.execution.ui.ConsoleViewContentType;
8+
import com.intellij.icons.AllIcons;
9+
import com.intellij.openapi.application.AccessToken;
10+
import com.intellij.openapi.application.ApplicationManager;
11+
import com.intellij.openapi.application.ModalityState;
12+
import com.intellij.openapi.application.ReadAction;
13+
import com.intellij.openapi.diagnostic.Logger;
14+
import com.intellij.openapi.progress.ProgressIndicator;
15+
import com.intellij.openapi.progress.Task;
16+
import com.intellij.openapi.project.Project;
17+
import com.intellij.openapi.projectRoots.Sdk;
18+
import com.intellij.openapi.util.EmptyRunnable;
19+
import com.intellij.openapi.vfs.VirtualFileManager;
20+
import com.intellij.openapi.wm.ToolWindow;
21+
import com.intellij.openapi.wm.ToolWindowAnchor;
22+
import com.intellij.openapi.wm.ToolWindowManager;
23+
import com.intellij.ui.content.Content;
24+
import com.intellij.ui.content.ContentFactory;
25+
import org.jetbrains.annotations.NotNull;
26+
import org.jetbrains.annotations.Nullable;
27+
import ro.redeul.google.go.config.sdk.GoSdkData;
28+
import ro.redeul.google.go.ide.ui.GoToolWindow;
29+
import ro.redeul.google.go.runner.GoApplicationConfiguration;
30+
import ro.redeul.google.go.runner.GoCommonConsoleView;
31+
import ro.redeul.google.go.sdk.GoSdkUtil;
32+
33+
import java.io.File;
34+
import java.util.Map;
35+
36+
/**
37+
* Created by d3xter on 13.03.14.
38+
*/
39+
public class GoVetRunner extends Task.Backgroundable {
40+
private static final Logger LOG = Logger.getInstance(GoVetRunner.class);
41+
private GoApplicationConfiguration goConfig;
42+
43+
public GoVetRunner(@Nullable Project project, @NotNull String title, boolean canBeCancelled, GoApplicationConfiguration goConfig) {
44+
super(project, title, canBeCancelled);
45+
46+
this.goConfig = goConfig;
47+
}
48+
49+
public GoVetRunner(@Nullable Project project, @NotNull String title, GoApplicationConfiguration goConfig) {
50+
this(project, title, false, goConfig);
51+
}
52+
53+
@Override
54+
public void run(@NotNull ProgressIndicator indicator) {
55+
LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed());
56+
57+
GoToolWindow toolWindow = GoToolWindow.getInstance(myProject);
58+
59+
indicator.setText(this.myTitle);
60+
indicator.setFraction(0);
61+
62+
Sdk sdk = GoSdkUtil.getGoogleGoSdkForProject(myProject);
63+
if ( sdk == null ) {
64+
LOG.error("No Go Sdk defined for this project");
65+
}
66+
67+
final GoSdkData sdkData = (GoSdkData)sdk.getSdkAdditionalData();
68+
if ( sdkData == null ) {
69+
LOG.error("No Go Sdk defined for this project");
70+
}
71+
72+
String goExecName = sdkData.GO_BIN_PATH;
73+
String projectDir = myProject.getBasePath();
74+
75+
try {
76+
Map<String,String> sysEnv = GoSdkUtil.getExtendedSysEnv(sdkData, projectDir, goConfig.envVars);
77+
String[] goEnv = GoSdkUtil.convertEnvMapToArray(sysEnv);
78+
79+
String command = String.format(
80+
"%s vet ./...%n",
81+
goExecName
82+
);
83+
84+
Runtime rt = Runtime.getRuntime();
85+
Process proc = rt.exec(command, goEnv, new File(projectDir));
86+
OSProcessHandler handler = new OSProcessHandler(proc, null);
87+
toolWindow.attachConsoleViewToProcess(handler);
88+
toolWindow.printNormalMessage(String.format("%s%n", command));
89+
handler.startNotify();
90+
91+
if (proc.waitFor() == 0) {
92+
ApplicationManager.getApplication().invokeLater(new Runnable() {
93+
@Override
94+
public void run() {
95+
VirtualFileManager.getInstance().syncRefresh();
96+
}
97+
});
98+
toolWindow.printNormalMessage(String.format("%nFinished running go vet on project %s%n", projectDir));
99+
} else {
100+
toolWindow.printErrorMessage(String.format("%nCouldn't vet project %s%n", projectDir));
101+
}
102+
} catch (Exception e) {
103+
toolWindow.printErrorMessage(String.format("Error while processing %s vet command.%n", goExecName));
104+
105+
//An Exception shouldn't happen, so print a log-error
106+
LOG.error(String.format("Error while processing %s vet command.%n", goExecName));
107+
}
108+
finally {
109+
indicator.setFraction(100);
110+
}
111+
}
112+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ro.redeul.google.go.runner.beforeRunTasks;
2+
3+
import com.intellij.execution.BeforeRunTask;
4+
import com.intellij.execution.configurations.RunConfiguration;
5+
import com.intellij.icons.AllIcons;
6+
import com.intellij.openapi.util.Key;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
/**
10+
* Created by d3xter on 13.03.14.
11+
*/
12+
public class GoVetTask extends BeforeRunTask<GoVetTask> {
13+
private RunConfiguration config;
14+
15+
protected GoVetTask(@NotNull Key<GoVetTask> providerId, RunConfiguration config) {
16+
super(providerId);
17+
18+
this.config = config;
19+
}
20+
21+
public RunConfiguration getConfiguration() {
22+
return this.config;
23+
}
24+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ro.redeul.google.go.runner.beforeRunTasks;
2+
3+
import com.intellij.execution.BeforeRunTaskProvider;
4+
import com.intellij.execution.configurations.RunConfiguration;
5+
import com.intellij.execution.runners.ExecutionEnvironment;
6+
import com.intellij.openapi.actionSystem.DataContext;
7+
import com.intellij.openapi.application.ApplicationManager;
8+
import com.intellij.openapi.application.ModalityState;
9+
import com.intellij.openapi.diagnostic.Logger;
10+
import com.intellij.openapi.fileEditor.FileDocumentManager;
11+
import com.intellij.openapi.util.Key;
12+
import org.jetbrains.annotations.Nullable;
13+
import ro.redeul.google.go.GoIcons;
14+
import ro.redeul.google.go.runner.GaeLocalConfiguration;
15+
import ro.redeul.google.go.runner.GoApplicationConfiguration;
16+
17+
import javax.swing.*;
18+
19+
/**
20+
* Created by d3xter on 13.03.14.
21+
*/
22+
public class GoVetTaskProvider extends BeforeRunTaskProvider<GoVetTask> {
23+
private static final Logger LOG = Logger.getInstance(GoVetTaskProvider.class);
24+
private final Key<GoVetTask> TaskID = new Key<GoVetTask>("GoVet");
25+
26+
@Override
27+
public Key<GoVetTask> getId() {
28+
return TaskID;
29+
}
30+
31+
@Override
32+
public String getName() {
33+
return "Go Vet";
34+
}
35+
36+
@Nullable
37+
@Override
38+
public Icon getTaskIcon(GoVetTask task) {
39+
if(task.getConfiguration() instanceof GaeLocalConfiguration) {
40+
return GoIcons.GAE_ICON_13x13;
41+
} else {
42+
return GoIcons.GO_ICON_13x13;
43+
}
44+
}
45+
46+
@Override
47+
public String getDescription(GoVetTask task) {
48+
return "Run Go Vet";
49+
}
50+
51+
@Nullable
52+
@Override
53+
public Icon getIcon() {
54+
return GoIcons.GO_ICON_13x13;
55+
}
56+
57+
@Override
58+
public boolean isConfigurable() {
59+
return false;
60+
}
61+
62+
@Nullable
63+
@Override
64+
public GoVetTask createTask(RunConfiguration runConfiguration) {
65+
return new GoVetTask(TaskID, runConfiguration);
66+
}
67+
68+
@Override
69+
public boolean configureTask(RunConfiguration runConfiguration, GoVetTask task) {
70+
return false;
71+
}
72+
73+
@Override
74+
public boolean canExecuteTask(RunConfiguration configuration, GoVetTask task) {
75+
//Only run Go apps at the moment
76+
return configuration instanceof GoApplicationConfiguration;
77+
}
78+
79+
@Override
80+
public boolean executeTask(DataContext context, final RunConfiguration configuration, final ExecutionEnvironment env, GoVetTask task) {
81+
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
82+
@Override
83+
public void run() {
84+
85+
FileDocumentManager.getInstance().saveAllDocuments();
86+
87+
new GoVetRunner(env.getProject(), "Go Vet", (GoApplicationConfiguration)configuration).queue();
88+
}
89+
}, ModalityState.NON_MODAL);
90+
91+
return true;
92+
}
93+
}

src/ro/redeul/google/go/runner/ui/GoApplicationConfigurationEditor.form

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<properties/>
1818
<border type="none"/>
1919
<children>
20-
<grid id="38d58" layout-manager="GridLayoutManager" row-count="10" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
20+
<grid id="38d58" layout-manager="GridLayoutManager" row-count="9" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
2121
<margin top="0" left="0" bottom="0" right="0"/>
2222
<constraints>
2323
<tabbedpane title="Run"/>
@@ -133,14 +133,6 @@
133133
<toolTipText value="You must use &quot;;&quot; in order to separate values. Ex. GOROOT=/usr/lib/go/;GOPATH=/home/Usr/go/"/>
134134
</properties>
135135
</component>
136-
<component id="9f869" class="javax.swing.JCheckBox" binding="runGoVetBeforeCheckBox" default-binding="true">
137-
<constraints>
138-
<grid row="9" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
139-
</constraints>
140-
<properties>
141-
<text value="Run go &amp;vet before running anything else"/>
142-
</properties>
143-
</component>
144136
<component id="3465" class="javax.swing.JLabel">
145137
<constraints>
146138
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>

src/ro/redeul/google/go/runner/ui/GoApplicationConfigurationEditor.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class GoApplicationConfigurationEditor extends SettingsEditor<GoApplicati
3232
private RawCommandLineEditor m_debugBuilderArguments;
3333
private TextFieldWithBrowseButton workingDirectoryBrowser;
3434
private RawCommandLineEditor envVars;
35-
private JCheckBox runGoVetBeforeCheckBox;
3635
private JTabbedPane tabbedPane1;
3736
private JLabel gdbVersionWarning;
3837
private TextFieldWithBrowseButton m_gdbPath;
@@ -54,7 +53,6 @@ protected void resetEditorFrom(GoApplicationConfiguration configuration) {
5453
}
5554

5655
envVars.setText(configuration.envVars);
57-
runGoVetBeforeCheckBox.setSelected(configuration.goVetEnabled);
5856

5957
//Debug stuff
6058
if (configuration.debugBuilderArguments.isEmpty()) {
@@ -81,7 +79,6 @@ protected void applyEditorTo(GoApplicationConfiguration configuration) throws Co
8179
configuration.goOutputDir = buildDirectoryPathBrowser.getText();
8280
configuration.workingDir = workingDirectoryBrowser.getText();
8381
configuration.envVars = envVars.getText();
84-
configuration.goVetEnabled = runGoVetBeforeCheckBox.isSelected();
8582

8683
//Debug stuff
8784
String gdbPath = m_gdbPath.getText();

src/uk/co/cwspencer/ideagdb/run/GoDebugProfileState.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,6 @@ public ExecutionResult execute(Executor executor, @NotNull ProgramRunner runner)
7575
GoToolWindow toolWindow = GoToolWindow.getInstance(project);
7676
toolWindow.setTitle(TITLE);
7777

78-
if (m_configuration.goVetEnabled) {
79-
try {
80-
String[] goEnv = GoSdkUtil.convertEnvMapToArray(sysEnv);
81-
82-
String command = String.format(
83-
"%s vet ./...",
84-
goExecName
85-
);
86-
87-
Runtime rt = Runtime.getRuntime();
88-
Process proc = rt.exec(command, goEnv, new File(projectDir));
89-
OSProcessHandler handler = new OSProcessHandler(proc, null);
90-
toolWindow.attachConsoleViewToProcess(handler);
91-
toolWindow.printNormalMessage(String.format("%s%n", command));
92-
handler.startNotify();
93-
94-
if (proc.waitFor() == 0) {
95-
VirtualFileManager.getInstance().syncRefresh();
96-
97-
toolWindow.printNormalMessage(String.format("%nFinished running go vet on project %s%n", projectDir));
98-
} else {
99-
toolWindow.printErrorMessage(String.format("%nCouldn't vet project %s%n", projectDir));
100-
throw new CantRunException(String.format("Error while processing %s vet command.", goExecName));
101-
}
102-
103-
104-
} catch (Exception e) {
105-
e.printStackTrace();
106-
Messages.showErrorDialog(String.format("Error while processing %s vet command.", goExecName), "Error on Google Go Plugin");
107-
108-
throw new CantRunException(String.format("Error while processing %s vet command.", goExecName));
109-
}
110-
}
111-
11278
// Build and run
11379
String execName = m_configuration.goOutputDir.concat("/").concat(project.getName());
11480

0 commit comments

Comments
 (0)