forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goPath.ts
230 lines (202 loc) · 6.9 KB
/
goPath.ts
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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/
'use strict';
/**
* This file is loaded by both the extension and debug adapter, so it cannot import 'vscode'
*/
import fs = require('fs');
import os = require('os');
import path = require('path');
let binPathCache: { [bin: string]: string } = {};
export const envPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
export function getBinPathFromEnvVar(toolName: string, envVarValue: string, appendBinToPath: boolean): string {
toolName = correctBinname(toolName);
if (envVarValue) {
const paths = envVarValue.split(path.delimiter);
for (const p of paths) {
const binpath = path.join(p, appendBinToPath ? 'bin' : '', toolName);
if (executableFileExists(binpath)) {
return binpath;
}
}
}
return null;
}
export function getBinPathWithPreferredGopath(toolName: string, preferredGopaths: string[], alternateTool?: string) {
if (binPathCache[toolName]) {
return binPathCache[toolName];
}
if (alternateTool && path.isAbsolute(alternateTool) && executableFileExists(alternateTool)) {
binPathCache[toolName] = alternateTool;
return alternateTool;
}
const binname = alternateTool && !path.isAbsolute(alternateTool) ? alternateTool : toolName;
const pathFromGoBin = getBinPathFromEnvVar(binname, process.env['GOBIN'], false);
if (pathFromGoBin) {
binPathCache[toolName] = pathFromGoBin;
return pathFromGoBin;
}
for (const preferred of preferredGopaths) {
if (typeof preferred === 'string') {
// Search in the preferred GOPATH workspace's bin folder
const pathFrompreferredGoPath = getBinPathFromEnvVar(binname, preferred, true);
if (pathFrompreferredGoPath) {
binPathCache[toolName] = pathFrompreferredGoPath;
return pathFrompreferredGoPath;
}
}
}
// Check GOROOT (go, gofmt, godoc would be found here)
const pathFromGoRoot = getBinPathFromEnvVar(binname, process.env['GOROOT'], true);
if (pathFromGoRoot) {
binPathCache[toolName] = pathFromGoRoot;
return pathFromGoRoot;
}
// Finally search PATH parts
const pathFromPath = getBinPathFromEnvVar(binname, envPath, false);
if (pathFromPath) {
binPathCache[toolName] = pathFromPath;
return pathFromPath;
}
// Check default path for go
if (toolName === 'go') {
const defaultPathForGo = process.platform === 'win32' ? 'C:\\Go\\bin\\go.exe' : '/usr/local/go/bin/go';
if (executableFileExists(defaultPathForGo)) {
binPathCache[toolName] = defaultPathForGo;
return defaultPathForGo;
}
return;
}
// Else return the binary name directly (this will likely always fail downstream)
return toolName;
}
function correctBinname(toolName: string) {
if (process.platform === 'win32') {
return toolName + '.exe';
}
return toolName;
}
function executableFileExists(filePath: string): boolean {
let exists = true;
try {
exists = fs.statSync(filePath).isFile();
if (exists) {
fs.accessSync(filePath, fs.constants.F_OK | fs.constants.X_OK);
}
} catch (e) {
exists = false;
}
return exists;
}
export function fileExists(filePath: string): boolean {
try {
return fs.statSync(filePath).isFile();
} catch (e) {
return false;
}
}
export function clearCacheForTools() {
binPathCache = {};
}
/**
* Exapnds ~ to homedir in non-Windows platform
*/
export function resolveHomeDir(inputPath: string): string {
if (!inputPath || !inputPath.trim()) {
return inputPath;
}
return inputPath.startsWith('~') ? path.join(os.homedir(), inputPath.substr(1)) : inputPath;
}
export function stripBOM(s: string): string {
if (s && s[0] === '\uFEFF') {
s = s.substr(1);
}
return s;
}
export function parseEnvFile(envFilePath: string): { [key: string]: string } {
const env: { [key: string]: any } = {};
if (!envFilePath) {
return env;
}
try {
const buffer = stripBOM(fs.readFileSync(envFilePath, 'utf8'));
buffer.split('\n').forEach((line) => {
const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r !== null) {
let value = r[2] || '';
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
value = value.replace(/\\n/gm, '\n');
}
env[r[1]] = value.replace(/(^['"]|['"]$)/g, '');
}
});
return env;
} catch (e) {
throw new Error(`Cannot load environment variables from file ${envFilePath}`);
}
}
// Walks up given folder path to return the closest ancestor that has `src` as a child
export function getInferredGopath(folderPath: string): string {
if (!folderPath) {
return;
}
const dirs = folderPath.toLowerCase().split(path.sep);
// find src directory closest to given folder path
const srcIdx = dirs.lastIndexOf('src');
if (srcIdx > 0) {
return folderPath.substr(0, dirs.slice(0, srcIdx).join(path.sep).length);
}
}
/**
* Returns the workspace in the given Gopath to which given directory path belongs to
* @param gopath string Current Gopath. Can be ; or : separated (as per os) to support multiple paths
* @param currentFileDirPath string
*/
export function getCurrentGoWorkspaceFromGOPATH(gopath: string, currentFileDirPath: string): string {
if (!gopath) {
return;
}
const workspaces: string[] = gopath.split(path.delimiter);
let currentWorkspace = '';
currentFileDirPath = fixDriveCasingInWindows(currentFileDirPath);
// Find current workspace by checking if current file is
// under any of the workspaces in $GOPATH
for (const workspace of workspaces) {
const possibleCurrentWorkspace = path.join(workspace, 'src');
if (
currentFileDirPath.startsWith(possibleCurrentWorkspace) ||
(process.platform === 'win32' &&
currentFileDirPath.toLowerCase().startsWith(possibleCurrentWorkspace.toLowerCase()))
) {
// In case of nested workspaces, (example: both /Users/me and /Users/me/src/a/b/c are in $GOPATH)
// both parent & child workspace in the nested workspaces pair can make it inside the above if block
// Therefore, the below check will take longer (more specific to current file) of the two
if (possibleCurrentWorkspace.length > currentWorkspace.length) {
currentWorkspace = currentFileDirPath.substr(0, possibleCurrentWorkspace.length);
}
}
}
return currentWorkspace;
}
// Workaround for issue in https://github.com/Microsoft/vscode/issues/9448#issuecomment-244804026
export function fixDriveCasingInWindows(pathToFix: string): string {
return process.platform === 'win32' && pathToFix
? pathToFix.substr(0, 1).toUpperCase() + pathToFix.substr(1)
: pathToFix;
}
/**
* Returns the tool name from the given path to the tool
* @param toolPath
*/
export function getToolFromToolPath(toolPath: string): string | undefined {
if (!toolPath) {
return;
}
let tool = path.basename(toolPath);
if (process.platform === 'win32' && tool.endsWith('.exe')) {
tool = tool.substr(0, tool.length - 4);
}
return tool;
}