forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.ts
More file actions
286 lines (228 loc) · 7.97 KB
/
Copy pathTerminal.ts
File metadata and controls
286 lines (228 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as Path from 'path';
import * as FS from 'fs';
import * as CP from 'child_process';
export class Terminal
{
private static _terminalService: ITerminalService;
public static launchInTerminal(dir: string, args: string[], envVars: { [key: string]: string; }): Promise<CP.ChildProcess> {
return this.terminalService().launchInTerminal(dir, args, envVars);
}
public static killTree(processId: number): void {
this.terminalService().killTree(processId);
}
/*
* Is the given runtime executable on the PATH.
*/
public static isOnPath(program: string): boolean {
return this.terminalService().isOnPath(program);
}
private static terminalService(): ITerminalService {
if (!this._terminalService) {
if (process.platform === 'win32') {
this._terminalService = new WindowsTerminalService();
} else if (process.platform === 'darwin') {
this._terminalService = new MacTerminalService();
} else if (process.platform === 'linux') {
this._terminalService = new LinuxTerminalService();
} else {
this._terminalService = new DefaultTerminalService();
}
}
return this._terminalService;
}
}
export class TerminalError {
public message: string;
public linkId: number;
constructor(message: string, linkId?: number) {
this.message = message;
this.linkId = linkId;
}
}
interface ITerminalService {
launchInTerminal(dir: string, args: string[], envVars: { [key: string]: string; }): Promise<CP.ChildProcess>;
killTree(pid: number): void;
isOnPath(program: string): boolean;
}
class DefaultTerminalService implements ITerminalService {
protected static TERMINAL_TITLE = "Python Console";
private static WHICH = '/usr/bin/which';
public launchInTerminal(dir: string, args: string[], envVars: { [key: string]: string; }): Promise<CP.ChildProcess> {
return new Promise<CP.ChildProcess>( (resolve, reject) => {
reject(new TerminalError(`External console not implemented on '${process.platform}'.`));
});
}
public killTree(pid: number): void {
// on linux and OS X we kill all direct and indirect child processes as well
try {
const cmd = Path.join(__dirname, './terminateProcess.sh');
CP.spawnSync(cmd, [ pid.toString() ]);
} catch (err) {
}
}
public isOnPath(program: string): boolean {
try {
if (FS.existsSync(DefaultTerminalService.WHICH)) {
CP.execSync(`${DefaultTerminalService.WHICH} '${program}'`);
} else {
// do not report error if 'which' doesn't exist
}
return true;
}
catch (Exception) {
}
return false;
}
}
class WindowsTerminalService extends DefaultTerminalService {
private static CMD = 'cmd.exe';
private static WHERE = 'C:\\Windows\\System32\\where.exe';
private static TASK_KILL = 'C:\\Windows\\System32\\taskkill.exe';
public launchInTerminal(dir: string, args: string[], envVars: { [key: string]: string; }): Promise<CP.ChildProcess> {
return new Promise<CP.ChildProcess>( (resolve, reject) => {
const title = `"${dir} - ${WindowsTerminalService.TERMINAL_TITLE}"`;
const command = `""${args.join('" "')}" & pause"`; // use '|' to only pause on non-zero exit code
const cmdArgs = [
'/c', 'start', title, '/wait',
'cmd.exe', '/c', command
];
// merge environment variables into a copy of the process.env
const env = extendObject(extendObject( { }, process.env), envVars);
const options: any = {
cwd: dir,
env: env,
windowsVerbatimArguments: true
};
const cmd = CP.spawn(WindowsTerminalService.CMD, cmdArgs, options);
cmd.on('error', reject);
resolve(cmd);
});
}
public killTree(pid: number): void {
// when killing a process in Windows its child processes are *not* killed but become root processes.
// Therefore we use TASKKILL.EXE
try {
CP.execSync(`${WindowsTerminalService.TASK_KILL} /F /T /PID ${pid}`);
}
catch (err) {
}
}
public isOnPath(program: string): boolean {
try {
if (FS.existsSync(WindowsTerminalService.WHERE)) {
CP.execSync(`${WindowsTerminalService.WHERE} ${program}`);
} else {
// do not report error if 'where' doesn't exist
}
return true;
}
catch (Exception) {
// ignore
}
return false;
}
}
class LinuxTerminalService extends DefaultTerminalService {
private static LINUX_TERM = '/usr/bin/gnome-terminal'; //private const string LINUX_TERM = "/usr/bin/x-terminal-emulator";
private static WAIT_MESSAGE = "Press any key to continue...";
public launchInTerminal(dir: string, args: string[], envVars: { [key: string]: string; }): Promise<CP.ChildProcess> {
return new Promise<CP.ChildProcess>( (resolve, reject) => {
if (!FS.existsSync(LinuxTerminalService.LINUX_TERM)) {
reject(new TerminalError(`'${LinuxTerminalService.LINUX_TERM}' not found`, 20002));
return;
}
const bashCommand = `${quote(args)}; echo; read -p "${LinuxTerminalService.WAIT_MESSAGE}" -n1;`;
const termArgs = [
'--title', `"${LinuxTerminalService.TERMINAL_TITLE}"`,
'-x', 'bash', '-c',
`''${bashCommand}''` // wrapping argument in two sets of ' because node is so "friendly" that it removes one set...
];
// merge environment variables into a copy of the process.env
const env = extendObject(extendObject( { }, process.env), envVars);
const options: any = {
cwd: dir,
env: env
};
const cmd = CP.spawn(LinuxTerminalService.LINUX_TERM, termArgs, options);
cmd.on('error', reject);
cmd.on('exit', (code: number) => {
if (code === 0) { // OK
resolve(); // since cmd is not the terminal process but just a launcher, we do not pass it in the resolve to the caller
} else {
reject(new TerminalError(`${LinuxTerminalService.LINUX_TERM} failed with exit code ${code}`));
}
});
});
}
}
class MacTerminalService extends DefaultTerminalService {
private static OSASCRIPT = '/usr/bin/osascript'; // osascript is the AppleScript interpreter on OS X
public launchInTerminal(dir: string, args: string[], envVars: { [key: string]: string; }): Promise<CP.ChildProcess> {
return new Promise<CP.ChildProcess>( (resolve, reject) => {
// first fix the PATH so that 'runtimePath' can be found if installed with 'brew'
// Utilities.FixPathOnOSX();
// On OS X we do not launch the program directly but we launch an AppleScript that creates (or reuses) a Terminal window
// and then launches the program inside that window.
const osaArgs = [
Path.join(__dirname, './TerminalHelper.scpt'),
'-t', MacTerminalService.TERMINAL_TITLE,
'-w', dir,
];
for (let a of args) {
osaArgs.push('-pa');
osaArgs.push(a);
}
if (envVars) {
for (let key in envVars) {
osaArgs.push('-e');
osaArgs.push(key + '=' + envVars[key]);
}
}
let stderr = '';
const osa = CP.spawn(MacTerminalService.OSASCRIPT, osaArgs);
osa.on('error', reject);
osa.stderr.on('data', (data) => {
stderr += data.toString();
});
osa.on('exit', (code: number) => {
if (code === 0) { // OK
resolve(); // since cmd is not the terminal process but just the osa tool, we do not pass it in the resolve to the caller
} else {
if (stderr) {
reject(new TerminalError(stderr));
} else {
reject(new TerminalError(`{MacTerminalService.OSASCRIPT} failed with exit code ${code}`));
}
}
});
});
}
}
// ---- private utilities ----
/**
* Quote args if necessary and combine into a space separated string.
*/
function quote(args: string[]): string {
let r = '';
for (let a of args) {
if (a.indexOf(' ') >= 0) {
r += '"' + a + '"';
} else {
r += a;
}
r += ' ';
}
return r;
}
function extendObject<T> (objectCopy: T, object: T): T {
for (let key in object) {
if (object.hasOwnProperty(key)) {
objectCopy[key] = object[key];
}
}
return objectCopy;
}