-
Notifications
You must be signed in to change notification settings - Fork 643
add playground command #1270
add playground command #1270
Changes from 4 commits
cfef8d0
49a9c9f
1510598
c22c7b7
e7951a6
caaedef
9559b2c
eb8bfdb
c2851f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import vscode = require('vscode'); | ||
| import { execFile } from 'child_process'; | ||
| import { outputChannel } from './goStatus'; | ||
| import { getBinPath } from './util'; | ||
| import { promptForMissingTool } from './goInstallTools'; | ||
|
|
||
| // isENOENT checks if the given error results from a missing tool installation | ||
| export const isENOENT = (err: Error): Boolean => ( | ||
| !!err && (<any>err).code === 'ENOENT' | ||
| ); | ||
|
|
||
| // flags describes the configuration toggles for the command | ||
| type flags = { [key: string]: Boolean }; | ||
|
|
||
| // IPlaygroundUploader needs to be implemented by the uploader passed to createCommandWith | ||
| export interface IPlaygroundUploader { | ||
| upload(code: string, config: flags): Promise<string>; | ||
| } | ||
|
|
||
| // createCommandWith retrieves the go.playground configuration and passes | ||
| // it to the given `uploader`, together with the current editor selection | ||
| // (or the full content of the editor window if the selection is empty) | ||
| export const createCommandWith = (uploader: IPlaygroundUploader) => (): Promise<any> => { | ||
| const editor = vscode.window.activeTextEditor; | ||
| const config: flags = vscode.workspace.getConfiguration('go', editor.document.uri).get('playground'); | ||
|
|
||
| const selection = editor.selection; | ||
| const code = selection.isEmpty | ||
| ? editor.document.getText() | ||
| : editor.document.getText(selection); | ||
|
|
||
| outputChannel.show(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear the output channel before sending any output |
||
| outputChannel.appendLine('Upload to the Go Playground in progress...\n'); | ||
|
|
||
| return uploader.upload(code, config) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not make the call to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing that it's a common pattern in this extension to make multiple tools available (although maybe not necessarily in that case) for the same task, I thought it'd be a good idea to separate the editor part of the command from the actual uploading. Also, it makes writing tests a lot easier. I think I'd like to keep it that way if possible. |
||
| .then(result => outputChannel.append(result)) | ||
| .catch(err => { | ||
| if ((<any>err).missingTool) { | ||
| promptForMissingTool(err.missingTool); | ||
| } else { | ||
| vscode.window.showErrorMessage(err.message); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the absence of the There are 2 ways to fix this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even after installing Feel free to use |
||
| } | ||
| }); | ||
| }; | ||
|
|
||
| // GoplayUploader implements `IPlaygroundUploader` using command goplay | ||
| export class GoplayUploader implements IPlaygroundUploader { | ||
| private TOOL_CMD_NAME = 'goplay'; | ||
| private BINARY_LOCATION = getBinPath(this.TOOL_CMD_NAME); | ||
| static stringifyFlags(f: flags): string[] { | ||
| return Object.keys(f).map(key => `-${key}=${f[key]}`); | ||
| } | ||
| upload(code: string, config: flags): Promise<string> { | ||
| return new Promise<string>((resolve, reject) => { | ||
| execFile(this.BINARY_LOCATION, [...GoplayUploader.stringifyFlags(config), '-'], (err, stdout, stderr) => { | ||
| if (isENOENT(err)) { | ||
| (<any>err).missingTool = this.TOOL_CMD_NAME; | ||
| return reject(err); | ||
| } | ||
| if (err) { | ||
| return reject(new Error(`${this.TOOL_CMD_NAME}: ${stdout || stderr || err.message}`)); | ||
| } | ||
| resolve(this.formatStdout(stdout || stderr, config)); | ||
| }).stdin.end(code); | ||
| }); | ||
| } | ||
| private formatStdout(result: string, config: flags) { | ||
| return `Output from the Go Playground: | ||
| ${result} | ||
| Finished running tool: ${this.BINARY_LOCATION} ${GoplayUploader.stringifyFlags(config).join(' ')} -\n`; | ||
| } | ||
| } | ||
|
|
||
| // the default export is the function that will be registered as the handler | ||
| // for the go.playground extension command in goMain.ts | ||
| export default createCommandWith(new GoplayUploader()); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package main |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show an error message if there is no active editor