diff options
author | Igor Matuszewski <[email protected]> | 2019-03-18 21:30:23 +0000 |
---|---|---|
committer | Igor Matuszewski <[email protected]> | 2019-03-18 21:30:23 +0000 |
commit | 60cac299640912ad1ad75644bfa0088d7ba6e367 (patch) | |
tree | 50aab7bd4a56995fa965ebd4b53d70506a8b9b4d /editors/code/src/commands | |
parent | 5c3cc8c95f392c523bb638d78e0217780d6e8476 (diff) |
Separate out the interactive cargo watch procedure
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r-- | editors/code/src/commands/runnables.ts | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 23fd280b4..285afaaf6 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts | |||
@@ -1,5 +1,8 @@ | |||
1 | import { exec } from 'child_process'; | ||
2 | import * as util from 'util'; | ||
1 | import * as vscode from 'vscode'; | 3 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 4 | import * as lc from 'vscode-languageclient'; |
5 | |||
3 | import { Server } from '../server'; | 6 | import { Server } from '../server'; |
4 | 7 | ||
5 | interface RunnablesParams { | 8 | interface RunnablesParams { |
@@ -33,7 +36,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition { | |||
33 | env?: { [key: string]: string }; | 36 | env?: { [key: string]: string }; |
34 | } | 37 | } |
35 | 38 | ||
36 | export function createTask(spec: Runnable): vscode.Task { | 39 | function createTask(spec: Runnable): vscode.Task { |
37 | const TASK_SOURCE = 'Rust'; | 40 | const TASK_SOURCE = 'Rust'; |
38 | const definition: CargoTaskDefinition = { | 41 | const definition: CargoTaskDefinition = { |
39 | type: 'cargo', | 42 | type: 'cargo', |
@@ -143,3 +146,66 @@ export const autoCargoWatchTask: vscode.Task = { | |||
143 | runOn: 2 // RunOnOptions.folderOpen | 146 | runOn: 2 // RunOnOptions.folderOpen |
144 | } as unknown) as vscode.RunOptions | 147 | } as unknown) as vscode.RunOptions |
145 | }; | 148 | }; |
149 | |||
150 | /** | ||
151 | * Interactively asks the user whether we should run `cargo check` in order to | ||
152 | * provide inline diagnostics; the user is met with a series of dialog boxes | ||
153 | * that, when accepted, allow us to `cargo install cargo-watch` and then run it. | ||
154 | */ | ||
155 | export async function interactivelyStartCargoWatch() { | ||
156 | const execAsync = util.promisify(exec); | ||
157 | |||
158 | const watch = await vscode.window.showInformationMessage( | ||
159 | 'Start watching changes with cargo? (Executes `cargo watch`, provides inline diagnostics)', | ||
160 | 'yes', | ||
161 | 'no' | ||
162 | ); | ||
163 | if (watch === 'no') { | ||
164 | return; | ||
165 | } | ||
166 | |||
167 | const { stderr } = await execAsync('cargo watch --version').catch(e => e); | ||
168 | if (stderr.includes('no such subcommand: `watch`')) { | ||
169 | const msg = | ||
170 | 'The `cargo-watch` subcommand is not installed. Install? (takes ~1-2 minutes)'; | ||
171 | const install = await vscode.window.showInformationMessage( | ||
172 | msg, | ||
173 | 'yes', | ||
174 | 'no' | ||
175 | ); | ||
176 | if (install === 'no') { | ||
177 | return; | ||
178 | } | ||
179 | |||
180 | const label = 'install-cargo-watch'; | ||
181 | const taskFinished = new Promise((resolve, reject) => { | ||
182 | let disposable = vscode.tasks.onDidEndTask(({ execution }) => { | ||
183 | if (execution.task.name === label) { | ||
184 | disposable.dispose(); | ||
185 | resolve(); | ||
186 | } | ||
187 | }); | ||
188 | }); | ||
189 | |||
190 | vscode.tasks.executeTask( | ||
191 | createTask({ | ||
192 | label, | ||
193 | bin: 'cargo', | ||
194 | args: ['install', 'cargo-watch'], | ||
195 | env: {} | ||
196 | }) | ||
197 | ); | ||
198 | await taskFinished; | ||
199 | const { stderr } = await execAsync('cargo watch --version').catch( | ||
200 | e => e | ||
201 | ); | ||
202 | if (stderr !== '') { | ||
203 | vscode.window.showErrorMessage( | ||
204 | `Couldn't install \`cargo-\`watch: ${stderr}` | ||
205 | ); | ||
206 | return; | ||
207 | } | ||
208 | } | ||
209 | |||
210 | vscode.tasks.executeTask(autoCargoWatchTask); | ||
211 | } | ||