processes.ts 7.3 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
A
Alex Dima 已提交
5
'use strict';
E
Erich Gamma 已提交
6 7 8 9 10 11 12 13 14 15 16 17

import NLS = require('vs/nls');

import * as Objects from 'vs/base/common/objects';
import * as Platform from 'vs/base/common/platform';
import { IStringDictionary } from 'vs/base/common/collections';
import * as Types from 'vs/base/common/types';

import { ValidationStatus, ValidationState, ILogger, Parser, ISystemVariables } from 'vs/base/common/parsers';


/**
18
 * Options to be passed to the external program or shell.
E
Erich Gamma 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */
export interface CommandOptions {
	/**
	 * The current working directory of the executed program or shell.
	 * If omitted VSCode's current workspace root is used.
	 */
	cwd?: string;

	/**
	 * The environment of the executed program or shell. If omitted
	 * the parent process' environment is used.
	 */
	env?: { [key:string]: string; };
}

export interface Executable {
	/**
36 37 38
	 * The command to be executed. Can be an external program or a shell
	 * command.
	 */
E
Erich Gamma 已提交
39 40 41
	command: string;

	/**
42 43 44
	 * Specifies whether the command is a shell command and therefore must
	 * be executed in a shell interpreter (e.g. cmd.exe, bash, ...).
	 */
E
Erich Gamma 已提交
45 46 47
	isShellCommand: boolean;

	/**
48 49
	 * The arguments passed to the command.
	 */
E
Erich Gamma 已提交
50 51 52
	args: string[];

	/**
53 54
	 * The command options used when the command is executed. Can be omitted.
	 */
E
Erich Gamma 已提交
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
	options?: CommandOptions;
}

export interface ForkOptions extends CommandOptions {
	execArgv?: string[];
}

export enum Source {
	stdout,
	stderr
}

/**
 * The data send via a success callback
 */
export interface SuccessData {
	error?:Error;
	cmdCode?:number;
	terminated?:boolean;
}

/**
 * The data send via a error callback
 */
export interface ErrorData {
	error?:Error;
	terminated?:boolean;
	stdout?:string;
	stderr?:string;
}

export interface TerminateResponse {
	success: boolean;
D
Dirk Baeumer 已提交
88
	code?: TerminateResponseCode;
E
Erich Gamma 已提交
89 90 91
	error?: any;
}

D
Dirk Baeumer 已提交
92 93 94 95 96 97 98
export enum TerminateResponseCode {
	Success = 0,
	Unknown = 1,
	AccessDenied = 2,
	ProcessNotFound = 3,
}

E
Erich Gamma 已提交
99 100
export namespace Config {
	/**
101 102
	 * Options to be passed to the external program or shell
	 */
E
Erich Gamma 已提交
103 104
	export interface CommandOptions {
		/**
105 106 107
		 * The current working directory of the executed program or shell.
		 * If omitted VSCode's current workspace root is used.
		 */
E
Erich Gamma 已提交
108 109 110
		cwd?: string;

		/**
111 112 113
		 * The additional environment of the executed program or shell. If omitted
		 * the parent process' environment is used.
		 */
E
Erich Gamma 已提交
114 115 116
		env?: IStringDictionary<string>;

		/**
117 118
		 * Index signature
		 */
E
Erich Gamma 已提交
119 120 121 122 123
		[key:string]: string | string[] | IStringDictionary<string>;
	}

	export interface BaseExecutable {
		/**
124 125 126
		 * The command to be executed. Can be an external program or a shell
		 * command.
		 */
E
Erich Gamma 已提交
127 128 129
		command?: string;

		/**
130 131 132 133 134
		 * Specifies whether the command is a shell command and therefore must
		 * be executed in a shell interpreter (e.g. cmd.exe, bash, ...).
		 *
		 * Defaults to false if omitted.
		 */
E
Erich Gamma 已提交
135 136 137
		isShellCommand?: boolean;

		/**
138 139
		 * The arguments passed to the command. Can be omitted.
		 */
E
Erich Gamma 已提交
140 141 142
		args?: string[];

		/**
143 144
		 * The command options used when the command is executed. Can be omitted.
		 */
E
Erich Gamma 已提交
145 146 147 148 149 150
		options?: CommandOptions;
	}

	export interface Executable extends BaseExecutable {

		/**
151 152
		 * Windows specific executable configuration
		 */
E
Erich Gamma 已提交
153 154 155
		windows?: BaseExecutable;

		/**
156 157
		 * Mac specific executable configuration
		 */
E
Erich Gamma 已提交
158 159 160
		osx?: BaseExecutable;

		/**
161 162
		 * Linux specific executable configuration
		 */
E
Erich Gamma 已提交
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
		linux?: BaseExecutable;

	}
}

export interface ParserOptions {
	globals?: Executable;
	emptyCommand?: boolean;
	noDefaults?: boolean;
}

export class ExecutableParser extends Parser {

	constructor(logger: ILogger, validationStatus: ValidationStatus = new ValidationStatus()) {
		super(logger, validationStatus);
	}

	public parse(json: Config.Executable, parserOptions: ParserOptions = { globals: null, emptyCommand: false, noDefaults: false }): Executable {
		let result = this.parseExecutable(json, parserOptions.globals);
		if (this.status.isFatal()) {
			return result;
		}
		let osExecutable: Executable;
		if (json.windows && Platform.platform === Platform.Platform.Windows) {
			osExecutable = this.parseExecutable(json.windows);
		} else if (json.osx && Platform.platform === Platform.Platform.Mac) {
			osExecutable = this.parseExecutable(json.osx);
		} else if (json.linux && Platform.platform === Platform.Platform.Linux) {
			osExecutable = this.parseExecutable(json.linux);
		}
		if (osExecutable) {
			result = ExecutableParser.mergeExecutable(result, osExecutable);
		}
		if ((!result || !result.command) && !parserOptions.emptyCommand) {
			this.status.state = ValidationState.Fatal;
			this.log(NLS.localize('ExecutableParser.commandMissing', 'Error: executable info must define a command of type string.'));
			return null;
		}
		if (!parserOptions.noDefaults) {
			Parser.merge(result, {
				command: undefined,
				isShellCommand: false,
				args: [],
				options: {}
			}, false);
		}
		return result;
	}

	public parseExecutable(json: Config.BaseExecutable, globals?: Executable): Executable {
		let command: string = undefined;
		let isShellCommand: boolean = undefined;
		let args: string[] = undefined;
		let options: CommandOptions = undefined;

		if (this.is(json.command, Types.isString)) {
			command = json.command;
		}
		if (this.is(json.isShellCommand, Types.isBoolean, ValidationState.Warning, NLS.localize('ExecutableParser.isShellCommand', 'Warning: isShellCommand must be of type boolean. Ignoring value {0}.', json.isShellCommand))) {
			isShellCommand = json.isShellCommand;
		}
		if (this.is(json.args, Types.isStringArray, ValidationState.Warning, NLS.localize('ExecutableParser.args', 'Warning: args must be of type string[]. Ignoring value {0}.', json.isShellCommand))) {
			args = json.args.slice(0);
		}
		if (this.is(json.options, Types.isObject)) {
			options = this.parseCommandOptions(json.options);
		}
		return { command, isShellCommand, args, options };
	}

	private parseCommandOptions(json: Config.CommandOptions): CommandOptions {
		let result: CommandOptions = {};
		if (!json) {
			return result;
		}
		if (this.is(json.cwd, Types.isString, ValidationState.Warning, NLS.localize('ExecutableParser.invalidCWD', 'Warning: options.cwd must be of type string. Ignoring value {0}.', json.cwd))) {
			result.cwd = json.cwd;
		}
		if (!Types.isUndefined(json.env)) {
			result.env = Objects.clone(json.env);
		}
		return result;
	}

	public static mergeExecutable(executable: Executable, other: Executable): Executable {
		if (!executable) {
			return other;
		}
		Parser.merge(executable, other, true);
		return executable;
	}
}

export function resolveCommandOptions(options: CommandOptions, variables: ISystemVariables): CommandOptions {
	let result = Objects.clone(options);
	if (result.cwd) {
		result.cwd = variables.resolve(result.cwd);
	}
	if (result.env) {
		result.env = variables.resolve(result.env);
	}
	return result;
}

export function resolveExecutable(executable: Executable, variables: ISystemVariables): Executable {
	let result = Objects.clone(executable);
	result.command = variables.resolve(result.command);
	result.args = variables.resolve(result.args);
	if (result.options) {
		result.options = resolveCommandOptions(result.options, variables);
	}
	return result;
}