This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 290
/
GoBuildManager.java
296 lines (233 loc) · 10.5 KB
/
GoBuildManager.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************************
* Copyright (c) 2015, 2015 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.loc;
import java.nio.file.Path;
import java.util.Collection;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import com.googlecode.goclipse.core.GoCoreMessages;
import com.googlecode.goclipse.core.GoEnvironmentPrefs;
import com.googlecode.goclipse.core.GoProjectEnvironment;
import com.googlecode.goclipse.tooling.GoBuildOutputProcessor;
import com.googlecode.goclipse.tooling.GoPackageName;
import com.googlecode.goclipse.tooling.env.GoEnvironment;
import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.operations.OperationInfo;
import melnorme.lang.ide.core.operations.ToolMarkersUtil;
import melnorme.lang.ide.core.operations.build.BuildManager;
import melnorme.lang.ide.core.operations.build.BuildTarget;
import melnorme.lang.ide.core.operations.build.BuildTargetRunner;
import melnorme.lang.ide.core.operations.build.BuildTargetRunner.BuildConfiguration;
import melnorme.lang.ide.core.operations.build.BuildTargetRunner.BuildType;
import melnorme.lang.ide.core.operations.build.CommonBuildTargetOperation;
import melnorme.lang.ide.core.project_model.AbstractBundleInfo;
import melnorme.lang.ide.core.project_model.LangBundleModel;
import melnorme.lang.ide.core.project_model.ProjectBuildInfo;
import melnorme.lang.ide.core.utils.ResourceUtils;
import melnorme.lang.tooling.data.StatusLevel;
import melnorme.lang.utils.ProcessUtils;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.collections.Indexable;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location;
import melnorme.utilbox.misc.MiscUtil;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;
public class GoBuildManager extends BuildManager {
public GoBuildManager(LangBundleModel<? extends AbstractBundleInfo> bundleModel) {
super(bundleModel);
}
public static final String BUILD_TYPE_Build = "build";
public static final String BUILD_TYPE_BuildTests = "build-tests";
public static final String BUILD_TYPE_RunTests = "[run-tests]";
public static final Indexable<BuildType> BUILD_TYPES = createDefaultBuildTypes();
public static final Indexable<String> BUILD_TYPES_Names =
BUILD_TYPES.map((buildType) -> buildType.getName());
public static ArrayList2<BuildType> createDefaultBuildTypes() {
return ArrayList2.create(
new GoDefaultBuildType(),
new GoTestBuildType(),
new GoRunTestsBuildType()
);
}
@Override
protected Indexable<BuildType> getBuildTypes_do() {
return BUILD_TYPES;
}
@Override
public BuildTarget getBuildTargetFor(ProjectBuildInfo projectBuildInfo, String targetName) throws CommonException {
BuildTarget buildTarget = super.getBuildTargetFor(projectBuildInfo, targetName);
if(buildTarget != null) {
return buildTarget;
}
return createBuildTarget(targetName, false, null);
}
@Override
public BuildTargetRunner getBuildTargetOperation(IProject project, BuildTarget buildTarget)
throws CommonException {
String targetName = buildTarget.getTargetName();
String buildConfigName = getBuildConfigString(targetName);
String buildTypeName = getBuildTypeString(targetName);
/* FIXME: adapt this code in parent */
BuildConfiguration buildConfiguration = new BuildConfiguration(buildConfigName, null);
return createBuildTargetOperation(project, buildConfiguration, buildTypeName, buildTarget);
}
@Override
public BuildTargetRunner createBuildTargetOperation(IProject project, BuildConfiguration buildConfig,
String buildTypeName, BuildTarget buildSettings) {
return new BuildTargetRunner(project, buildConfig, buildTypeName, buildSettings.getBuildOptions()) {
@Override
public CommonBuildTargetOperation getBuildOperation(OperationInfo parentOpInfo, Path buildToolPath,
boolean fullBuild) {
return new GoBuildTargetOperation(parentOpInfo, project, buildToolPath, this, fullBuild);
}
};
}
/* ----------------- ----------------- */
public static abstract class AbstractGoBuildType extends BuildType {
public AbstractGoBuildType(String name) {
super(name);
}
protected GoPackageName getValidGoPackageName(String goPackageString) throws CommonException {
return GoPackageName.createValid(goPackageString);
}
@Override
public String getDefaultBuildOptions(BuildTargetRunner buildTargetOp) throws CommonException {
String goPackageSpec = getGoPackageSpec(buildTargetOp.getProject(), buildTargetOp.getBuildConfigName());
return getBuildCommand() + " -v -gcflags \"-N -l\" " + goPackageSpec;
}
protected abstract String getBuildCommand();
protected String getGoPackageSpec(IProject project, String goPackageSpec) throws CommonException {
if(goPackageSpec == null || goPackageSpec.isEmpty()) {
GoEnvironment goEnv = GoProjectEnvironment.getGoEnvironment(project);
GoPackageName goPackage = goEnv.getGoPath().findGoPackageForLocation(loc(project.getLocation()));
if(goPackage != null) {
goPackageSpec = goPackage.toString();
} else {
goPackageSpec = ".";
}
goPackageSpec += "/...";
}
return goPackageSpec;
}
@Override
public String getArtifactPath(BuildTargetRunner buildTargetOp) throws CommonException {
Location binFolderLocation = GoProjectEnvironment.getBinFolderLocation(buildTargetOp.getProject());
String binFilePath = getBinFilePath(getValidGoPackageName(buildTargetOp.getBuildConfigName()));
return binFolderLocation.resolve(binFilePath + MiscUtil.getExecutableSuffix()).toString();
}
protected String getBinFilePath(GoPackageName goPackageName) throws CommonException {
return goPackageName.getLastSegment();
}
}
public static class GoDefaultBuildType extends AbstractGoBuildType {
public GoDefaultBuildType() {
super(BUILD_TYPE_Build);
}
@Override
protected String getBuildCommand() {
return "install";
}
}
public static class GoTestBuildType extends AbstractGoBuildType {
public GoTestBuildType() {
super(BUILD_TYPE_BuildTests);
}
@Override
protected String getBuildCommand() {
return "test -c";
}
@Override
protected String getBinFilePath(GoPackageName goPackageName) throws CommonException {
return super.getBinFilePath(goPackageName) + ".test";
}
}
public static class GoRunTestsBuildType extends AbstractGoBuildType {
public GoRunTestsBuildType() {
super(BUILD_TYPE_RunTests);
}
@Override
protected String getBuildCommand() {
return "test";
}
@Override
public String getArtifactPath(BuildTargetRunner buildTargetOp) throws CommonException {
throw new CommonException("This configuration does not produce executable artifacts.");
}
}
public static class GoBuildTargetOperation extends CommonBuildTargetOperation {
public GoBuildTargetOperation(OperationInfo parentOpInfo, IProject project,
Path buildToolPath, BuildTargetRunner buildTargetOp, boolean fullBuild) {
super(buildTargetOp.getBuildManager(), parentOpInfo, project, buildToolPath, buildTargetOp, fullBuild);
}
protected GoEnvironment goEnv;
protected Location sourceRootDir;
@Override
protected void addMainArguments(ArrayList2<String> commands) {
}
@Override
protected ProcessBuilder getProcessBuilder(ArrayList2<String> commands) throws CoreException, CommonException {
Location projectLocation = ResourceUtils.getProjectLocation(project);
goEnv = getValidGoEnvironment(project);
if(GoProjectEnvironment.isProjectInsideGoPath(project, goEnv.getGoPath())) {
sourceRootDir = projectLocation;
} else {
sourceRootDir = projectLocation.resolve_valid("src");
checkGoFilesInSourceRoot();
}
ProcessBuilder pb = ProcessUtils.createProcessBuilder(commands, sourceRootDir);
goEnv.setupProcessEnv(pb, true);
return pb;
}
protected void checkGoFilesInSourceRoot() throws CoreException {
CheckSrcFolderRootFilesWithNoPackage srcCheck = new CheckSrcFolderRootFilesWithNoPackage();
srcCheck.checkDir(sourceRootDir);
if(srcCheck.containsGoSources) {
LangCore.getToolManager().notifyMessage(StatusLevel.WARNING, "Go build: Warning!",
GoCoreMessages.ERROR_SrcRootContainsGoFiles(sourceRootDir));
}
}
@Override
protected void processBuildOutput(ExternalProcessResult buildAllResult)
throws CoreException, CommonException, OperationCancellation {
GoBuildOutputProcessor buildOutput = new GoBuildOutputProcessor() {
@Override
protected void handleMessageParseError(CommonException ce) {
LangCore.logError(ce.getMessage(), ce.getCause());
}
};
buildOutput.parseOutput(buildAllResult);
ToolMarkersUtil.addErrorMarkers(buildOutput.getBuildErrors(), sourceRootDir);
}
}
protected static GoEnvironment getValidGoEnvironment(IProject project) throws CoreException {
try {
return GoProjectEnvironment.getValidatedGoEnvironment(project);
} catch (CommonException ce) {
throw LangCore.createCoreException(ce);
}
}
protected static ArrayList2<String> getToolCommandLine() throws CoreException {
String compilerPath = GoEnvironmentPrefs.COMPILER_PATH.get();
if(compilerPath.isEmpty()) {
throw LangCore.createCoreException("Compiler Path not defined.", null);
}
return new ArrayList2<>(compilerPath);
}
protected static 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());
}
}
}