Skip to content

Commit

Permalink
Install package command
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Nov 10, 2017
1 parent 97ffefc commit f771895
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
},
{
"command": "go.show.commands",
"title": "Go: Show Other Commands...",
"title": "Go: Show All Commands...",
"description": "Shows all commands from the Go extension in the wuick pick"
},
{
Expand All @@ -190,7 +190,7 @@
},
{
"command": "go.lint.package",
"title": "Go: Lint Package",
"title": "Go: Lint Current Package",
"description": "Run linter in the package of the current file."
},
{
Expand All @@ -200,7 +200,7 @@
},
{
"command": "go.vet.package",
"title": "Go: Vet Package",
"title": "Go: Vet Current Package",
"description": "Run go vet in the package of the current file."
},
{
Expand All @@ -210,13 +210,18 @@
},
{
"command": "go.build.package",
"title": "Go: Build Package",
"title": "Go: Build Current Package",
"description": "Build the package of the current file."
},
{
"command": "go.build.workspace",
"title": "Go: Build Workspace",
"description": "Build the current workspace."
},
{
"command": "go.install.package",
"title": "Go: Install Current Package",
"description": "Install the current package."
}
],
"debuggers": [
Expand Down
6 changes: 5 additions & 1 deletion src/goBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { getCurrentGoWorkspaceFromGOPATH } from './goPath';
export function buildCode(buildWorkspace?: boolean) {
let editor = vscode.window.activeTextEditor;
if (!editor && !buildWorkspace) {
vscode.window.showInformationMessage('No editor is active, cant find current package to build');
vscode.window.showInformationMessage('No editor is active, cannot find current package to build');
return;
}
if (editor.document.languageId !== 'go' && !buildWorkspace) {
vscode.window.showInformationMessage('File in the active editor is not a Go file, cannot find current package to build');
return;
}

Expand Down
46 changes: 46 additions & 0 deletions src/goInstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import path = require('path');
import vscode = require('vscode');
import { getToolsEnvVars, getCurrentGoPath } from './util';
import { outputChannel } from './goStatus';
import { getCurrentGoWorkspaceFromGOPATH, getGoRuntimePath } from './goPath';
import cp = require('child_process');

export function installCurrentPackage() {
let 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;
}

let goRuntimePath = getGoRuntimePath();
if (!goRuntimePath) {
vscode.window.showInformationMessage('Cannot find "go" binary. Update PATH or GOROOT appropriately');
return;
}

const env = Object.assign({}, getToolsEnvVars());
const cwd = path.dirname(editor.document.uri.fsPath);
const goConfig = vscode.workspace.getConfiguration('go', editor.document.uri);
const args = ['install'];

if (goConfig['buildTags']) {
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 ? 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`);
});
}
6 changes: 5 additions & 1 deletion src/goLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { outputChannel } from './goStatus';
export function lintCode(lintWorkspace?: boolean) {
let editor = vscode.window.activeTextEditor;
if (!editor && !lintWorkspace) {
vscode.window.showInformationMessage('No editor is active, cant find current package to lint');
vscode.window.showInformationMessage('No editor is active, cannot find current package to lint');
return;
}
if (editor.document.languageId !== 'go' && !lintWorkspace) {
vscode.window.showInformationMessage('File in the active editor is not a Go file, cannot find current package to lint');
return;
}

Expand Down
3 changes: 3 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { playgroundCommand } from './goPlayground';
import { lintCode } from './goLint';
import { vetCode } from './goVet';
import { buildCode } from './goBuild';
import { installCurrentPackage } from './goInstall';

export let errorDiagnosticCollection: vscode.DiagnosticCollection;
export let warningDiagnosticCollection: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -304,6 +305,8 @@ export function activate(ctx: vscode.ExtensionContext): void {

ctx.subscriptions.push(vscode.commands.registerCommand('go.build.workspace', () => buildCode(true)));

ctx.subscriptions.push(vscode.commands.registerCommand('go.install.package', installCurrentPackage));

vscode.languages.setLanguageConfiguration(GO_MODE.language, {
indentationRules: {
decreaseIndentPattern: /^\s*(\bcase\b.*:|\bdefault\b:|}[),]?|\)[,]?)$/,
Expand Down
6 changes: 5 additions & 1 deletion src/goVet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { outputChannel } from './goStatus';
export function vetCode(vetWorkspace?: boolean) {
let editor = vscode.window.activeTextEditor;
if (!editor && !vetWorkspace) {
vscode.window.showInformationMessage('No editor is active, cant find current package to vet');
vscode.window.showInformationMessage('No editor is active, cannot find current package to vet');
return;
}
if (editor.document.languageId !== 'go' && !vetWorkspace) {
vscode.window.showInformationMessage('File in the active editor is not a Go file, cannot find current package to vet');
return;
}

Expand Down

0 comments on commit f771895

Please sign in to comment.