Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

[WIP] Adding "Go: Build", "Go: Test" and "Go: Install" commands #69

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
"command": "go.gopath",
"title": "Go: Current GOPATH",
"description": "See the currently set GOPATH."
},
{
"command": "go.gobuild",
"title": "Go: Build",
"description": "Run 'go build' in the current workspace."
}
],
"debuggers": [
Expand Down
10 changes: 2 additions & 8 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ import cp = require('child_process');
import path = require('path');
import os = require('os');
import fs = require('fs');
import { getBinPath } from './goPath'
import { getBinPath, getGoBin } from './goPath'

//TODO: Less hacky?
var go: string;
if (process.env.GOROOT) {
go = path.join(process.env["GOROOT"], "bin", "go");
} else if (process.env.PATH) {
var pathparts = (<string>process.env.PATH).split((<any>path).delimiter);
go = pathparts.map(dir => path.join(dir, 'go' + (os.platform() == "win32" ? ".exe" : ""))).filter(candidate => fs.existsSync(candidate))[0];
}
var go = getGoBin();
if (!go) {
vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT. Set location manual in 'go.goroot' setting.");
}
Expand Down
29 changes: 26 additions & 3 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
'use strict';

import vscode = require('vscode');
import fs = require('fs');
import path = require('path');
import cp = require('child_process');
import * as cp from 'child_process';
import { dirname } from 'path';
import { GoCompletionItemProvider } from './goSuggest';
import { GoHoverProvider } from './goExtraInfo';
import { GoDefinitionProvider } from './goDeclaration';
Expand All @@ -19,6 +18,7 @@ import { check, ICheckResult } from './goCheck';
import { setupGoPathAndOfferToInstallTools } from './goInstallTools'
import { GO_MODE } from './goMode'
import { showHideStatus } from './goStatus'
import { getGoBin } from './goPath'

let diagnosticCollection: vscode.DiagnosticCollection;

Expand All @@ -43,6 +43,29 @@ export function activate(ctx: vscode.ExtensionContext): void {
vscode.window.showInformationMessage("Current GOPATH:" + gopath);
}));

ctx.subscriptions.push(vscode.commands.registerCommand("go.gobuild", () => {
var channel = vscode.window.createOutputChannel('Go');
channel.show(2);
var go = getGoBin();
channel.appendLine("\n\nRunning 'go build':\n")
var cwd: string;
if (vscode.window.activeTextEditor) {
cwd = dirname(vscode.window.activeTextEditor.document.fileName);
} else if (vscode.workspace.rootPath) {
cwd = vscode.workspace.rootPath;
}
var buildProc = cp.spawn(go, ["build"], { cwd });
buildProc.stdout.on('data', data => {
channel.append(data.toString())
});
buildProc.stderr.on('data', data => {
channel.append(data.toString())
});
buildProc.on('close', code => {
channel.append("\n'go build' completed with code " + code);
});
}));

vscode.languages.setLanguageConfiguration(GO_MODE.language, {
indentationRules: {
// ^(.*\*/)?\s*\}.*$
Expand Down
12 changes: 12 additions & 0 deletions src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@

import fs = require('fs');
import path = require('path');
import os = require('os');

var binPathCache: { [bin: string]: string; } = {}

export function getGoBin() {
//TODO: Less hacky?
if (process.env.GOROOT) {
return path.join(process.env["GOROOT"], "bin", "go");
} else if (process.env.PATH) {
var pathparts = (<string>process.env.PATH).split((<any>path).delimiter);
return pathparts.map(dir => path.join(dir, 'go' + (os.platform() == "win32" ? ".exe" : ""))).filter(candidate => fs.existsSync(candidate))[0];
}
return undefined;
}

export function getBinPath(binname: string) {
binname = correctBinname(binname);
if (binPathCache[binname]) return binPathCache[binname];
Expand Down