-
Notifications
You must be signed in to change notification settings - Fork 797
/
Copy pathgoInstall.ts
66 lines (56 loc) · 2.53 KB
/
goInstall.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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
import cp = require('child_process');
import path = require('path');
import vscode = require('vscode');
import { getGoConfig } from './config';
import { toolExecutionEnvironment } from './goEnv';
import { isModSupported } from './goModules';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getModuleCache } from './util';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';
export async function installCurrentPackage(): Promise<void> {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active, cannot find current package to install');
return;
}
if (editor.document.languageId !== 'go') {
vscode.window.showInformationMessage(
'File in the active editor is not a Go file, cannot find current package to install'
);
return;
}
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go install" to install the package as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
const env = toolExecutionEnvironment();
const cwd = path.dirname(editor.document.uri.fsPath);
const isMod = await isModSupported(editor.document.uri);
// Skip installing if cwd is in the module cache
if (isMod && cwd.startsWith(getModuleCache())) {
return;
}
const goConfig = getGoConfig();
const buildFlags = goConfig['buildFlags'] || [];
const args = ['install', ...buildFlags];
if (goConfig['buildTags'] && buildFlags.indexOf('-tags') === -1) {
args.push('-tags', goConfig['buildTags']);
}
// Find the right importPath instead of directly using `.`. Fixes https://github.com/Microsoft/vscode-go/issues/846
const currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd);
const importPath = currentGoWorkspace && !isMod ? cwd.substr(currentGoWorkspace.length + 1) : '.';
args.push(importPath);
outputChannel.clear();
outputChannel.show();
outputChannel.appendLine(`Installing ${importPath === '.' ? 'current package' : importPath}`);
cp.execFile(goRuntimePath, args, { env, cwd }, (err, stdout, stderr) => {
outputChannel.appendLine(err ? `Installation failed: ${stderr}` : 'Installation successful');
});
}