processes.ts 6.7 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

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';

14
import { ValidationStatus, ValidationState, ILogger, Parser } from 'vs/base/common/parsers';
E
Erich Gamma 已提交
15 16

/**
17
 * Options to be passed to the external program or shell.
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */
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 {
	/**
35 36 37
	 * The command to be executed. Can be an external program or a shell
	 * command.
	 */
E
Erich Gamma 已提交
38 39 40
	command: string;

	/**
41 42 43
	 * 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 已提交
44 45 46
	isShellCommand: boolean;

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

	/**
52 53
	 * The command options used when the command is executed. Can be omitted.
	 */
E
Erich Gamma 已提交
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
	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 已提交
87
	code?: TerminateResponseCode;
E
Erich Gamma 已提交
88 89 90
	error?: any;
}

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

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

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

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

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

		/**
129 130 131 132 133
		 * 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 已提交
134 135 136
		isShellCommand?: boolean;

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

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

	export interface Executable extends BaseExecutable {

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

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

		/**
160 161
		 * Linux specific executable configuration
		 */
E
Erich Gamma 已提交
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
		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;
	}
}