This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
GoProjectEnvironment.java
131 lines (109 loc) · 5.11 KB
/
GoProjectEnvironment.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
/*******************************************************************************
* Copyright (c) 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;
import static melnorme.lang.ide.core.utils.ResourceUtils.loc;
import static melnorme.utilbox.misc.StringUtil.emptyAsNull;
import static melnorme.utilbox.misc.StringUtil.nullAsEmpty;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import com.googlecode.goclipse.tooling.GoPackageName;
import com.googlecode.goclipse.tooling.env.GoArch;
import com.googlecode.goclipse.tooling.env.GoEnvironment;
import com.googlecode.goclipse.tooling.env.GoEnvironmentConstants;
import com.googlecode.goclipse.tooling.env.GoOs;
import com.googlecode.goclipse.tooling.env.GoPath;
import com.googlecode.goclipse.tooling.env.GoRoot;
import com.googlecode.goclipse.tooling.env.GoWorkspaceLocation;
import melnorme.lang.ide.core.utils.ResourceUtils;
import melnorme.lang.ide.core.utils.prefs.IProjectPreference;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location;
public class GoProjectEnvironment implements GoEnvironmentConstants {
public static GoRoot getEffectiveGoRoot(IProject project) {
String prefValue = GoEnvironmentPrefs.GO_ROOT.getEffectiveValue(project);
return new GoRoot(nullAsEmpty(prefValue));
}
public static GoArch getEffectiveGoArch(IProject project) {
String strValue = emptyAsNull(GoEnvironmentPrefs.GO_ARCH.getEffectiveValue(project));
return strValue == null ? null : new GoArch(strValue);
}
public static GoOs getEffectiveGoOs(IProject project) {
String strValue = emptyAsNull(GoEnvironmentPrefs.GO_OS.getEffectiveValue(project));
return strValue == null ? null : new GoOs(strValue);
}
public static GoPath getEffectiveGoPath(IProject project) {
String goPathPref = getEffectiveValueFromEnv(GoEnvironmentPrefs.GO_PATH, project, GOPATH);
GoPath rawGoPath = new GoPath(goPathPref);
if(project == null) {
return rawGoPath;
}
Location projectLoc = ResourceUtils.getResourceLocation(project);
if(projectLoc == null) {
return rawGoPath;
}
GoWorkspaceLocation goPathEntry = rawGoPath.findGoPathEntryForSourcePath(projectLoc);
if(goPathEntry != null) {
// GOPATH already contains project location
return rawGoPath;
}
// Implicitly add project location to the end of GOPATH
ArrayList2<String> newGoPathEntries = new ArrayList2<>(rawGoPath.getGoPathEntries());
newGoPathEntries.add(projectLoc.toPathString());
return new GoPath(newGoPathEntries);
}
public static boolean isProjectInsideGoPathSourceFolder(IProject project) throws CommonException {
GoPath goPath = getEffectiveGoPath(project);
return isProjectInsideGoPathSourceFolder(project, goPath);
}
public static boolean isProjectInsideGoPathSourceFolder(IProject project, GoPath goPath) throws CommonException {
return goPath.findGoPathEntryForSourcePath(ResourceUtils.getProjectLocation2(project)) != null;
}
protected static String getEffectiveValueFromEnv(IProjectPreference<String> pref, IProject project,
String envAlternative) {
String prefValue = pref.getEffectiveValue(project);
if(prefValue == null) {
return nullAsEmpty(System.getenv(envAlternative));
}
return prefValue;
}
public static GoEnvironment getGoEnvironmentFromLocation(Location fileLocation) {
IProject project = ResourceUtils.getProject(fileLocation);
return getGoEnvironment(project);
}
/**
* @return {@link GoEnvironment} for given project.
* @param project - can be null.
*/
public static GoEnvironment getGoEnvironment(IProject project) {
GoRoot goRoot = getEffectiveGoRoot(project);
GoArch goArch = getEffectiveGoArch(project);
GoOs goOs = getEffectiveGoOs(project);
GoPath goPath = getEffectiveGoPath(project);
return new GoEnvironment(goRoot, goArch, goOs, goPath);
}
public static boolean isValid(IProject project) {
return getGoEnvironment(project).isValid();
}
public static GoEnvironment getValidatedGoEnvironment(final IProject project) throws CommonException {
GoEnvironment goEnv = getGoEnvironment(project);
goEnv.validate();
return goEnv;
}
public static ArrayList2<GoPackageName> findSourcePackages(IProject project, GoEnvironment goEnvironment)
throws CoreException {
return goEnvironment.getGoPath().findGoSourcePackages(ResourceUtils.getProjectLocation(project));
}
public static Location getBinFolderLocation(IProject project) throws CommonException {
GoEnvironment goEnv = getGoEnvironment(project);
return goEnv.getBinFolderLocationForSubLocation(loc(project.getLocation()));
}
}