This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
GoBuilder.java
169 lines (134 loc) · 5.68 KB
/
GoBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
* Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package com.googlecode.goclipse.core.operations;
import static melnorme.lang.ide.core.utils.ResourceUtils.getProjectLocation;
import java.util.Collection;
import java.util.Map;
import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.operations.LangProjectBuilder;
import melnorme.lang.ide.core.operations.LangProjectBuilderExt;
import melnorme.lang.ide.core.operations.SDKLocationValidator;
import melnorme.lang.tooling.data.LocationValidator;
import melnorme.lang.tooling.data.StatusException;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import com.googlecode.goclipse.core.GoEnvironmentPrefs;
import com.googlecode.goclipse.core.GoProjectEnvironment;
import com.googlecode.goclipse.core.GoProjectPrefConstants;
import com.googlecode.goclipse.tooling.GoBuildOutputProcessor;
import com.googlecode.goclipse.tooling.GoPackageName;
import com.googlecode.goclipse.tooling.env.GoEnvironment;
/**
* Go builder. TODO refactor to use {@link LangProjectBuilderExt}
*/
public class GoBuilder extends LangProjectBuilder {
public static class GoSDKLocationValidator extends SDKLocationValidator {
public GoSDKLocationValidator() {
super("GOROOT:");
}
@Override
protected String getSDKExecutable_append() {
return "bin/go";
}
}
public GoBuilder() {
}
@Override
protected LocationValidator getSDKLocationValidator() {
return new GoSDKLocationValidator();
}
@Override
protected String getSDKToolPath() throws CoreException {
String pathString = GoEnvironmentPrefs.GO_ROOT.get(); // We use a different pref other than the LANG default
try {
return getSDKLocationValidator().getValidatedField(pathString).toPathString();
} catch (StatusException se) {
throw LangCore.createCoreException(se);
}
}
@Override
protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor monitor) throws CoreException {
return super.build(kind, args, monitor);
}
@Override
protected void handleFirstOfKind() {
GoToolManager.getDefault().notifyBuildStarting(null, true);
}
@Override
protected void handleLastOfKind() {
GoToolManager.getDefault().notifyBuildTerminated(null);
}
@Override
protected IProject[] doBuild(final IProject project, int kind, Map<String, String> args, IProgressMonitor monitor)
throws CoreException {
GoToolManager.getDefault().notifyBuildStarting(project, false);
GoEnvironment goEnv = getValidGoEnvironment(project);
ArrayList2<String> goBuildCmdLine = getGoToolCommandLine();
goBuildCmdLine.addElements("install", "-v");
goBuildCmdLine.addElements(GoProjectPrefConstants.GO_BUILD_EXTRA_OPTIONS.getParsedArguments(project));
goBuildCmdLine.addElements("./...");
// addSourcePackagesToCmdLine(project, goBuildCmdLine, goEnv);
Location projectLocation = getProjectLocation(project);
Location sourceRootDir;
if(GoProjectEnvironment.isProjectInsideGoPath(project, goEnv.getGoPath())) {
sourceRootDir = projectLocation;
} else {
sourceRootDir = projectLocation.resolve_valid("src");
}
ExternalProcessResult buildAllResult =
GoToolManager.getDefault().runBuildTool(goEnv, monitor, sourceRootDir, goBuildCmdLine);
GoBuildOutputProcessor buildOutput = new GoBuildOutputProcessor() {
@Override
protected void handleLineParseError(CommonException ce) {
LangCore.logError(ce.getMessage(), ce.getCause());
}
};
buildOutput.parseOutput(buildAllResult);
addErrorMarkers(buildOutput.getBuildErrors(), sourceRootDir);
return null;
}
protected GoEnvironment getValidGoEnvironment(IProject project) throws CoreException {
try {
return GoProjectEnvironment.getValidatedGoEnvironment(project);
} catch (CommonException ce) {
throw LangCore.createCoreException(ce);
}
}
protected ArrayList2<String> getGoToolCommandLine() throws CoreException {
String compilerPath = GoEnvironmentPrefs.COMPILER_PATH.get();
if(compilerPath.isEmpty()) {
throw LangCore.createCoreException("Compiler Path not defined.", null);
}
return new ArrayList2<>(compilerPath);
}
protected void addSourcePackagesToCmdLine(final IProject project, ArrayList2<String> goBuildCmdLine,
GoEnvironment goEnvironment) throws CoreException {
Collection<GoPackageName> sourcePackages = GoProjectEnvironment.getSourcePackages(project, goEnvironment);
for (GoPackageName goPackageName : sourcePackages) {
goBuildCmdLine.add(goPackageName.getFullNameAsString());
}
}
@Override
protected void clean(IProgressMonitor monitor) throws CoreException {
deleteProjectBuildMarkers();
IProject project = getProject();
GoEnvironment goEnv = getValidGoEnvironment(project);
ArrayList2<String> goBuildCmdLine = getGoToolCommandLine();
goBuildCmdLine.addElements("clean", "-i", "-x");
addSourcePackagesToCmdLine(project, goBuildCmdLine, goEnv);
GoToolManager.getDefault().runBuildTool(goEnv, monitor, getProjectLocation(project), goBuildCmdLine);
}
}