integrated debug terminal: support PowerShell on all platforms

This commit is contained in:
Andre Weinand 2016-09-15 00:40:38 +02:00
parent 8d6a8863fd
commit 3d1a71250d

View file

@ -15,7 +15,9 @@ export interface IIntegratedTerminalConfiguration {
terminal: { terminal: {
integrated: { integrated: {
shell: { shell: {
windows: string windows: string,
linux: string,
osx: string
} }
} }
}; };
@ -42,51 +44,61 @@ export class TerminalSupport {
} }
private static prepareCommand(args: DebugProtocol.RunInTerminalRequestArguments, configurationService: IConfigurationService): string { private static prepareCommand(args: DebugProtocol.RunInTerminalRequestArguments, configurationService: IConfigurationService): string {
let command = '';
const quote1 = (s: string) => {
s = s.replace(/\"/g, '""');
return (s.indexOf(' ') >= 0 || s.indexOf('"') >= 0) ? `"${s}"` : s;
};
// get the shell configuration for the current platform
let shell: string;
const shell_config = configurationService.getConfiguration<IIntegratedTerminalConfiguration>().terminal.integrated.shell;
if (platform.isWindows) { if (platform.isWindows) {
shell = shell_config.windows;
} else if (platform.isLinux) {
shell = shell_config.linux;
} else if (platform.isMacintosh) {
shell = shell_config.osx;
}
const quote = (s: string) => { shell = shell.toLowerCase();
s = s.replace(/\"/g, '""');
return (s.indexOf(' ') >= 0 || s.indexOf('"') >= 0) ? `"${s}"` : s;
};
const windows_shell = configurationService.getConfiguration<IIntegratedTerminalConfiguration>().terminal.integrated.shell.windows; let command = '';
const isPowerShell = windows_shell ? windows_shell.toLowerCase().indexOf('powershell') >= 0 : false; if (shell.indexOf('powershell') >= 0) {
if (isPowerShell) {
if (args.cwd) { if (args.cwd) {
command += `cd ${quote(args.cwd)}; `; command += `cd ${quote1(args.cwd)}; `;
} }
if (args.env) { if (args.env) {
for (let key in args.env) { for (let key in args.env) {
command += `$env:${key}='${args.env[key]}'; `; command += `$env:${key}='${args.env[key]}'; `;
}
}
for (let a of args.args) {
command += `${quote(a)} `;
} }
}
for (let a of args.args) {
command += `${quote1(a)} `;
}
} else { } else if (shell.indexOf('cmd.exe') >= 0) {
if (args.cwd) { if (args.cwd) {
command += `cd ${quote(args.cwd)} && `; command += `cd ${quote1(args.cwd)} && `;
} }
if (args.env) { if (args.env) {
command += 'cmd /C "'; command += 'cmd /C "';
for (let key in args.env) { for (let key in args.env) {
command += `set "${key}=${args.env[key]}" && `; command += `set "${key}=${args.env[key]}" && `;
}
}
for (let a of args.args) {
command += `${quote(a)} `;
}
if (args.env) {
command += '"';
} }
} }
for (let a of args.args) {
command += `${quote1(a)} `;
}
if (args.env) {
command += '"';
}
} else { } else {
// fallback: unix shell
const quote = (s: string) => { const quote = (s: string) => {
s = s.replace(/\"/g, '\\"'); s = s.replace(/\"/g, '\\"');
return s.indexOf(' ') >= 0 ? `"${s}"` : s; return s.indexOf(' ') >= 0 ? `"${s}"` : s;
@ -105,6 +117,7 @@ export class TerminalSupport {
for (let a of args.args) { for (let a of args.args) {
command += `${quote(a)} `; command += `${quote(a)} `;
} }
} }
return command; return command;