This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathgoPath.ts
188 lines (163 loc) · 6.18 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
/*---------------------------------------------------------
* 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 path = require('path');
import os = require('os');
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) {
let paths = envVarValue.split(path.delimiter);
for (let i = 0; i < paths.length; i++) {
let binpath = path.join(paths[i], appendBinToPath ? 'bin' : '', toolName);
if (fileExists(binpath)) {
return binpath;
}
}
}
return null;
}
export function getBinPathWithPreferredGopath(toolName: string, preferredGopaths: string[], alternateTools?: { [key: string]: string; }) {
if (binPathCache[toolName]) return binPathCache[toolName];
const alternateTool = (alternateTools && alternateTools[toolName]) ? resolveHomeDir(alternateTools[toolName]) : null;
if (alternateTool && path.isAbsolute(alternateTool) && fileExists(alternateTool)) {
binPathCache[toolName] = alternateTool;
return alternateTool;
}
const binname = (alternateTool && !path.isAbsolute(alternateTool)) ? alternateTool : toolName;
for (let i = 0; i < preferredGopaths.length; i++) {
if (typeof preferredGopaths[i] === 'string') {
// Search in the preferred GOPATH workspace's bin folder
let pathFrompreferredGoPath = getBinPathFromEnvVar(binname, preferredGopaths[i], true);
if (pathFrompreferredGoPath) {
binPathCache[toolName] = pathFrompreferredGoPath;
return pathFrompreferredGoPath;
}
}
}
// Check GOROOT (go, gofmt, godoc would be found here)
let pathFromGoRoot = getBinPathFromEnvVar(binname, process.env['GOROOT'], true);
if (pathFromGoRoot) {
binPathCache[toolName] = pathFromGoRoot;
return pathFromGoRoot;
}
// Finally search PATH parts
let pathFromPath = getBinPathFromEnvVar(binname, envPath, false);
if (pathFromPath) {
binPathCache[toolName] = pathFromPath;
return pathFromPath;
}
// Check default path for go
if (toolName === 'go') {
let defaultPathForGo = process.platform === 'win32' ? 'C:\\Go\\bin\\go.exe' : '/usr/local/go/bin/go';
if (fileExists(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';
else
return toolName;
}
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(path: string): { [key: string]: string } {
const env = {};
if (!path) {
return env;
}
try {
const buffer = stripBOM(fs.readFileSync(path, '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 (`Cannot load environment variables from file ${path}`);
}
}
// 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;
}
let dirs = folderPath.toLowerCase().split(path.sep);
// find src directory closest to given folder path
let 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;
}
let 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 (let i = 0; i < workspaces.length; i++) {
const possibleCurrentWorkspace = path.join(workspaces[i], '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;
}