diff --git a/package.json b/package.json index 7b3af5163..a0b6af099 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/goCheck.ts b/src/goCheck.ts index 511c19edf..f68612acf 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -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 = (process.env.PATH).split((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."); } diff --git a/src/goMain.ts b/src/goMain.ts index b052c267c..610083f68 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -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'; @@ -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; @@ -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*\}.*$ diff --git a/src/goPath.ts b/src/goPath.ts index 9a90e193a..6d2af291c 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -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 = (process.env.PATH).split((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];