mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-30 23:27:24 +00:00
Add proper process teminate method
This commit is contained in:
parent
c894a3e19b
commit
b84d0fc1a3
6 changed files with 116 additions and 10 deletions
|
@ -1,9 +1,10 @@
|
|||
import * as child_process from 'child_process';
|
||||
import * as path from 'path';
|
||||
import * as timers from 'timers';
|
||||
import * as vscode from 'vscode';
|
||||
import { terminate } from '../utils/processes';
|
||||
import { StatusDisplay } from './watch_status';
|
||||
|
||||
|
||||
export class CargoWatchProvider {
|
||||
private diagnosticCollection?: vscode.DiagnosticCollection;
|
||||
private cargoProcess?: child_process.ChildProcess;
|
||||
|
@ -21,24 +22,25 @@ export class CargoWatchProvider {
|
|||
// Start the cargo watch with json message
|
||||
this.cargoProcess = child_process.spawn(
|
||||
'cargo',
|
||||
['watch', '-x', '"check --message-format json"'],
|
||||
['watch', '-x', '\"check --message-format json\"'],
|
||||
{
|
||||
// stdio: ['ignore', 'pipe', 'ignore'],
|
||||
shell: true,
|
||||
cwd: vscode.workspace.rootPath
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
cwd: vscode.workspace.rootPath,
|
||||
windowsVerbatimArguments: true,
|
||||
}
|
||||
);
|
||||
|
||||
this.cargoProcess.stdout.on('data', (s: string) => {
|
||||
this.processOutput(s);
|
||||
console.log(s);
|
||||
});
|
||||
|
||||
this.cargoProcess.stderr.on('data', (s: string) => {
|
||||
// console.error('Error on cargo watch : ' + s);
|
||||
console.error('Error on cargo watch : ' + s);
|
||||
});
|
||||
|
||||
this.cargoProcess.on('error', (err: Error) => {
|
||||
// console.error('Error on spawn cargo process : ' + err);
|
||||
console.error('Error on spawn cargo process : ' + err);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -50,6 +52,7 @@ export class CargoWatchProvider {
|
|||
|
||||
if (this.cargoProcess) {
|
||||
this.cargoProcess.kill();
|
||||
terminate(this.cargoProcess);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import * as timers from 'timers';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
||||
|
|
40
editors/code/src/utils/processes.ts
Normal file
40
editors/code/src/utils/processes.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
'use strict';
|
||||
|
||||
import * as cp from 'child_process';
|
||||
import ChildProcess = cp.ChildProcess;
|
||||
|
||||
import { join } from 'path';
|
||||
|
||||
const isWindows = (process.platform === 'win32');
|
||||
const isMacintosh = (process.platform === 'darwin');
|
||||
const isLinux = (process.platform === 'linux');
|
||||
export function terminate(process: ChildProcess, cwd?: string): boolean {
|
||||
if (isWindows) {
|
||||
try {
|
||||
// This we run in Atom execFileSync is available.
|
||||
// Ignore stderr since this is otherwise piped to parent.stderr
|
||||
// which might be already closed.
|
||||
const options: any = {
|
||||
stdio: ['pipe', 'pipe', 'ignore']
|
||||
};
|
||||
if (cwd) {
|
||||
options.cwd = cwd
|
||||
}
|
||||
(cp).execFileSync('taskkill', ['/T', '/F', '/PID', process.pid.toString()], options);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
} else if (isLinux || isMacintosh) {
|
||||
try {
|
||||
const cmd = join(__dirname, 'terminateProcess.sh');
|
||||
const result = cp.spawnSync(cmd, [process.pid.toString()]);
|
||||
return result.error ? false : true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
process.kill('SIGKILL');
|
||||
return true;
|
||||
}
|
||||
}
|
12
editors/code/src/utils/terminateProcess.sh
Normal file
12
editors/code/src/utils/terminateProcess.sh
Normal file
|
@ -0,0 +1,12 @@
|
|||
#!/bin/bash
|
||||
|
||||
terminateTree() {
|
||||
for cpid in $(pgrep -P $1); do
|
||||
terminateTree $cpid
|
||||
done
|
||||
kill -9 $1 > /dev/null 2>&1
|
||||
}
|
||||
|
||||
for pid in $*; do
|
||||
terminateTree $pid
|
||||
done
|
Loading…
Add table
Add a link
Reference in a new issue