taskConfiguration.ts 62.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as nls from 'vs/nls';
E
Erich Gamma 已提交
7 8 9

import * as Objects from 'vs/base/common/objects';
import { IStringDictionary } from 'vs/base/common/collections';
10
import { Platform } from 'vs/base/common/platform';
E
Erich Gamma 已提交
11 12 13
import * as Types from 'vs/base/common/types';
import * as UUID from 'vs/base/common/uuid';

M
Matt Bierner 已提交
14
import { ValidationStatus, IProblemReporter as IProblemReporterBase } from 'vs/base/common/parsers';
15 16
import {
	NamedProblemMatcher, ProblemMatcher, ProblemMatcherParser, Config as ProblemMatcherConfig,
17
	isNamedProblemMatcher, ProblemMatcherRegistry
18
} from 'vs/workbench/parts/tasks/common/problemMatcher';
19

S
Sandeep Somavarapu 已提交
20
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
21

22
import * as Tasks from '../common/tasks';
D
Dirk Baeumer 已提交
23
import { TaskDefinitionRegistry } from '../common/taskDefinitionRegistry';
E
Erich Gamma 已提交
24

25 26
import { TaskDefinition } from 'vs/workbench/parts/tasks/node/tasks';

27
export const enum ShellQuoting {
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
	/**
	 * Default is character escaping.
	 */
	escape = 1,

	/**
	 * Default is strong quoting
	 */
	strong = 2,

	/**
	 * Default is weak quoting.
	 */
	weak = 3
}

export interface ShellQuotingOptions {
	/**
	 * The character used to do character escaping.
	 */
	escape?: string | {
		escapeChar: string;
		charsToEscape: string;
	};

	/**
	 * The character used for string quoting.
	 */
	strong?: string;

	/**
	 * The character used for weak quoting.
	 */
	weak?: string;
}

D
Dirk Baeumer 已提交
64
export interface ShellConfiguration {
65
	executable?: string;
D
Dirk Baeumer 已提交
66
	args?: string[];
67
	quoting?: ShellQuotingOptions;
D
Dirk Baeumer 已提交
68 69
}

70
export interface CommandOptionsConfig {
D
Dirk Baeumer 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	/**
	 * The current working directory of the executed program or shell.
	 * If omitted VSCode's current workspace root is used.
	 */
	cwd?: string;

	/**
	 * The additional environment of the executed program or shell. If omitted
	 * the parent process' environment is used.
	 */
	env?: IStringDictionary<string>;

	/**
	 * The shell configuration;
	 */
	shell?: ShellConfiguration;
}

89
export interface PresentationOptionsConfig {
90 91 92 93 94
	/**
	 * Controls whether the terminal executing a task is brought to front or not.
	 * Defaults to `RevealKind.Always`.
	 */
	reveal?: string;
D
Dirk Baeumer 已提交
95 96

	/**
97 98 99 100 101 102 103 104 105 106 107
	 * Controls whether the executed command is printed to the output window or terminal as well.
	 */
	echo?: boolean;

	/**
	 * Controls whether the terminal is focus when this task is executed
	 */
	focus?: boolean;

	/**
	 * Controls whether the task runs in a new terminal
D
Dirk Baeumer 已提交
108
	 */
109
	panel?: string;
110 111 112 113

	/**
	 * Controls whether to show the "Terminal will be reused by tasks, press any key to close it" message.
	 */
D
Dirk Baeumer 已提交
114
	showReuseMessage?: boolean;
115 116 117 118

	/**
	 * Controls whether the terminal should be cleared before running the task.
	 */
119
	clear?: boolean;
120 121
}

122
export interface RunOptionsConfig {
123
	reevaluateOnRerun?: boolean;
A
Alex Ross 已提交
124
	runOn?: string;
A
Alex Ross 已提交
125 126
}

127
export interface TaskIdentifier {
D
Dirk Baeumer 已提交
128
	type?: string;
129 130 131 132 133 134 135 136
	[name: string]: any;
}

export namespace TaskIdentifier {
	export function is(value: any): value is TaskIdentifier {
		let candidate: TaskIdentifier = value;
		return candidate !== void 0 && Types.isString(value.type);
	}
137
}
D
Dirk Baeumer 已提交
138

139
export interface LegacyTaskProperties {
E
Erich Gamma 已提交
140
	/**
141 142
	 * @deprecated Use `isBackground` instead.
	 * Whether the executed command is kept alive and is watching the file system.
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
	isWatching?: boolean;

	/**
	 * @deprecated Use `group` instead.
	 * Whether this task maps to the default build command.
	 */
	isBuildCommand?: boolean;

	/**
	 * @deprecated Use `group` instead.
	 * Whether this task maps to the default test command.
	 */
	isTestCommand?: boolean;
}

export interface LegacyCommandProperties {

	/**
	 * Whether this is a shell or process
	 */
	type?: string;

	/**
	 * @deprecated Use presentation options
	 * Controls whether the output view of the running tasks is brought to front or not.
	 * See BaseTaskRunnerConfiguration#showOutput for details.
	 */
	showOutput?: string;

	/**
	 * @deprecated Use presentation options
	 * Controls whether the executed command is printed to the output windows as well.
	 */
	echoCommand?: boolean;

179 180 181
	/**
	 * @deprecated Use presentation instead
	 */
182
	terminal?: PresentationOptionsConfig;
183

184 185 186 187 188 189 190 191 192 193 194 195
	/**
	 * @deprecated Use inline commands.
	 * See BaseTaskRunnerConfiguration#suppressTaskName for details.
	 */
	suppressTaskName?: boolean;

	/**
	 * Some commands require that the task argument is highlighted with a special
	 * prefix (e.g. /t: for msbuild). This property can be used to control such
	 * a prefix.
	 */
	taskSelector?: string;
196 197

	/**
D
Dirk Baeumer 已提交
198
	 * @deprecated use the task type instead.
199 200 201 202 203
	 * 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.
	 */
D
Dirk Baeumer 已提交
204
	isShellCommand?: boolean | ShellConfiguration;
205 206
}

207
export type CommandString = string | string[] | { value: string | string[], quoting: 'escape' | 'strong' | 'weak' };
208

209 210 211 212
export namespace CommandString {
	export function value(value: CommandString): string {
		if (Types.isString(value)) {
			return value;
213 214
		} else if (Types.isStringArray(value)) {
			return value.join(' ');
215
		} else {
216 217 218 219 220
			if (Types.isString(value.value)) {
				return value.value;
			} else {
				return value.value.join(' ');
			}
221 222 223 224 225
		}
	}
}

export interface BaseCommandProperties {
226 227 228 229 230

	/**
	 * The command to be executed. Can be an external program or a shell
	 * command.
	 */
231
	command?: CommandString;
232 233 234 235

	/**
	 * The command options used when the command is executed. Can be omitted.
	 */
236
	options?: CommandOptionsConfig;
237 238 239 240

	/**
	 * The arguments passed to the command or additional arguments passed to the
	 * command when using a global command.
E
Erich Gamma 已提交
241
	 */
242
	args?: CommandString[];
243 244 245
}


246
export interface CommandProperties extends BaseCommandProperties {
D
Dirk Baeumer 已提交
247

248
	/**
249
	 * Windows specific command properties
250
	 */
251
	windows?: BaseCommandProperties;
252

253
	/**
254
	 * OSX specific command properties
255
	 */
256
	osx?: BaseCommandProperties;
257 258

	/**
259
	 * linux specific command properties
260
	 */
261 262
	linux?: BaseCommandProperties;
}
263

D
Dirk Baeumer 已提交
264 265
export interface GroupKind {
	kind?: string;
266
	isDefault?: boolean;
D
Dirk Baeumer 已提交
267 268
}

269
export interface ConfigurationProperties {
270
	/**
271
	 * The task's name
272
	 */
273
	taskName?: string;
E
Erich Gamma 已提交
274

275 276 277 278 279
	/**
	 * The UI label used for the task.
	 */
	label?: string;

E
Erich Gamma 已提交
280
	/**
281 282
	 * An optional indentifier which can be used to reference a task
	 * in a dependsOn or other attributes.
E
Erich Gamma 已提交
283
	 */
284
	identifier?: string;
E
Erich Gamma 已提交
285

286 287 288 289 290
	/**
	 * Whether the executed command is kept alive and runs in the background.
	 */
	isBackground?: boolean;

D
Dirk Baeumer 已提交
291 292 293 294 295
	/**
	 * Whether the task should prompt on close for confirmation if running.
	 */
	promptOnClose?: boolean;

E
Erich Gamma 已提交
296
	/**
297 298
	 * Defines the group the task belongs too.
	 */
D
Dirk Baeumer 已提交
299
	group?: string | GroupKind;
300 301

	/**
302
	 * The other tasks the task depend on
E
Erich Gamma 已提交
303
	 */
304
	dependsOn?: string | TaskIdentifier | (string | TaskIdentifier)[];
E
Erich Gamma 已提交
305 306

	/**
307
	 * Controls the behavior of the used terminal
E
Erich Gamma 已提交
308
	 */
309
	presentation?: PresentationOptionsConfig;
E
Erich Gamma 已提交
310

311 312 313
	/**
	 * Controls shell options.
	 */
314
	options?: CommandOptionsConfig;
315

E
Erich Gamma 已提交
316
	/**
317 318
	 * The problem matcher(s) to use to capture problems in the tasks
	 * output.
E
Erich Gamma 已提交
319
	 */
320
	problemMatcher?: ProblemMatcherConfig.ProblemMatcherType;
A
Alex Ross 已提交
321 322 323 324

	/**
	 * Task run options. Control run related properties.
	 */
325
	runOptions?: RunOptionsConfig;
326
}
E
Erich Gamma 已提交
327

328
export interface CustomTask extends CommandProperties, ConfigurationProperties {
E
Erich Gamma 已提交
329
	/**
330
	 * Custom tasks have the type CUSTOMIZED_TASK_TYPE
E
Erich Gamma 已提交
331
	 */
332
	type?: string;
E
Erich Gamma 已提交
333

334
}
335

336
export interface ConfiguringTask extends ConfigurationProperties {
E
Erich Gamma 已提交
337
	/**
338
	 * The contributed type of the task
E
Erich Gamma 已提交
339
	 */
340
	type?: string;
E
Erich Gamma 已提交
341 342 343 344 345
}

/**
 * The base task runner configuration
 */
346
export interface BaseTaskRunnerConfiguration {
E
Erich Gamma 已提交
347 348 349 350 351

	/**
	 * The command to be executed. Can be an external program or a shell
	 * command.
	 */
352
	command?: CommandString;
E
Erich Gamma 已提交
353 354

	/**
355 356
	 * @deprecated Use type instead
	 *
E
Erich Gamma 已提交
357 358 359 360 361 362 363
	 * 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.
	 */
	isShellCommand?: boolean;

364 365 366 367 368
	/**
	 * The task type
	 */
	type?: string;

E
Erich Gamma 已提交
369 370 371
	/**
	 * The command options used when the command is executed. Can be omitted.
	 */
372
	options?: CommandOptionsConfig;
E
Erich Gamma 已提交
373 374 375 376

	/**
	 * The arguments passed to the command. Can be omitted.
	 */
377
	args?: CommandString[];
E
Erich Gamma 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

	/**
	 * Controls whether the output view of the running tasks is brought to front or not.
	 * Valid values are:
	 *   "always": bring the output window always to front when a task is executed.
	 *   "silent": only bring it to front if no problem matcher is defined for the task executed.
	 *   "never": never bring the output window to front.
	 *
	 * If omitted "always" is used.
	 */
	showOutput?: string;

	/**
	 * Controls whether the executed command is printed to the output windows as well.
	 */
	echoCommand?: boolean;

395 396 397 398
	/**
	 * The group
	 */
	group?: string | GroupKind;
D
Dirk Baeumer 已提交
399 400 401
	/**
	 * Controls the behavior of the used terminal
	 */
402
	presentation?: PresentationOptionsConfig;
D
Dirk Baeumer 已提交
403

E
Erich Gamma 已提交
404 405 406 407 408 409 410 411 412 413 414 415
	/**
	 * If set to false the task name is added as an additional argument to the
	 * command when executed. If set to true the task name is suppressed. If
	 * omitted false is used.
	 */
	suppressTaskName?: boolean;

	/**
	 * Some commands require that the task argument is highlighted with a special
	 * prefix (e.g. /t: for msbuild). This property can be used to control such
	 * a prefix.
	 */
J
Johannes Rieken 已提交
416
	taskSelector?: string;
E
Erich Gamma 已提交
417 418 419 420 421 422 423 424 425

	/**
	 * The problem matcher(s) to used if a global command is exucuted (e.g. no tasks
	 * are defined). A tasks.json file can either contain a global problemMatcher
	 * property or a tasks property but not both.
	 */
	problemMatcher?: ProblemMatcherConfig.ProblemMatcherType;

	/**
426 427
	 * @deprecated Use `isBackground` instead.
	 *
E
Erich Gamma 已提交
428
	 * Specifies whether a global command is a watching the filesystem. A task.json
429
	 * file can either contain a global isWatching property or a tasks property
E
Erich Gamma 已提交
430 431 432 433
	 * but not both.
	 */
	isWatching?: boolean;

434 435 436 437 438
	/**
	 * Specifies whether a global command is a background task.
	 */
	isBackground?: boolean;

D
Dirk Baeumer 已提交
439 440 441 442 443
	/**
	 * Whether the task should prompt on close for confirmation if running.
	 */
	promptOnClose?: boolean;

E
Erich Gamma 已提交
444 445 446 447
	/**
	 * The configuration of the available tasks. A tasks.json file can either
	 * contain a global problemMatcher property or a tasks property but not both.
	 */
448
	tasks?: (CustomTask | ConfiguringTask)[];
E
Erich Gamma 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461

	/**
	 * Problem matcher declarations
	 */
	declares?: ProblemMatcherConfig.NamedProblemMatcher[];
}

/**
 * A configuration of an external build system. BuildConfiguration.buildSystem
 * must be set to 'program'
 */
export interface ExternalTaskRunnerConfiguration extends BaseTaskRunnerConfiguration {

462 463
	_runner?: string;

464 465 466 467 468
	/**
	 * Determines the runner to use
	 */
	runner?: string;

E
Erich Gamma 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	/**
	 * The config's version number
	 */
	version: string;

	/**
	 * Windows specific task configuration
	 */
	windows?: BaseTaskRunnerConfiguration;

	/**
	 * Mac specific task configuration
	 */
	osx?: BaseTaskRunnerConfiguration;

	/**
	 * Linux speciif task configuration
	 */
	linux?: BaseTaskRunnerConfiguration;
}

enum ProblemMatcherKind {
	Unknown,
	String,
	ProblemMatcher,
	Array
}

497 498
const EMPTY_ARRAY: any[] = [];
Object.freeze(EMPTY_ARRAY);
E
Erich Gamma 已提交
499

500
function assignProperty<T, K extends keyof T>(target: T, source: Partial<T>, key: K) {
501 502 503
	if (source[key] !== void 0) {
		target[key] = source[key];
	}
E
Erich Gamma 已提交
504 505
}

506
function fillProperty<T, K extends keyof T>(target: T, source: Partial<T>, key: K) {
507 508 509 510 511 512
	if (target[key] === void 0 && source[key] !== void 0) {
		target[key] = source[key];
	}
}


513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
interface ParserType<T> {
	isEmpty(value: T): boolean;
	assignProperties(target: T, source: T): T;
	fillProperties(target: T, source: T): T;
	fillDefaults(value: T, context: ParseContext): T;
	freeze(value: T): Readonly<T>;
}

interface MetaData<T, U> {
	property: keyof T;
	type?: ParserType<U>;
}


function _isEmpty<T>(this: void, value: T, properties: MetaData<T, any>[]): boolean {
	if (value === void 0 || value === null) {
		return true;
	}
	for (let meta of properties) {
		let property = value[meta.property];
		if (property !== void 0 && property !== null) {
			if (meta.type !== void 0 && !meta.type.isEmpty(property)) {
				return false;
			} else if (!Array.isArray(property) || property.length > 0) {
				return false;
			}
		}
	}
	return true;
}

function _assignProperties<T>(this: void, target: T, source: T, properties: MetaData<T, any>[]): T {
	if (_isEmpty(source, properties)) {
		return target;
	}
	if (_isEmpty(target, properties)) {
		return source;
	}
	for (let meta of properties) {
		let property = meta.property;
		let value: any;
		if (meta.type !== void 0) {
			value = meta.type.assignProperties(target[property], source[property]);
		} else {
			value = source[property];
		}
		if (value !== void 0 && value !== null) {
			target[property] = value;
		}
	}
	return target;
}

function _fillProperties<T>(this: void, target: T, source: T, properties: MetaData<T, any>[]): T {
	if (_isEmpty(source, properties)) {
		return target;
	}
	if (_isEmpty(target, properties)) {
		return source;
	}
	for (let meta of properties) {
		let property = meta.property;
		let value: any;
		if (meta.type) {
			value = meta.type.fillProperties(target[property], source[property]);
578
		} else if (target[property] === void 0) {
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
			value = source[property];
		}
		if (value !== void 0 && value !== null) {
			target[property] = value;
		}
	}
	return target;
}

function _fillDefaults<T>(this: void, target: T, defaults: T, properties: MetaData<T, any>[], context: ParseContext): T {
	if (target && Object.isFrozen(target)) {
		return target;
	}
	if (target === void 0 || target === null) {
		if (defaults !== void 0 && defaults !== null) {
J
Johannes Rieken 已提交
594
			return Objects.deepClone(defaults);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
		} else {
			return undefined;
		}
	}
	for (let meta of properties) {
		let property = meta.property;
		if (target[property] !== void 0) {
			continue;
		}
		let value: any;
		if (meta.type) {
			value = meta.type.fillDefaults(target[property], context);
		} else {
			value = defaults[property];
		}

		if (value !== void 0 && value !== null) {
			target[property] = value;
		}
	}
	return target;
}

function _freeze<T>(this: void, target: T, properties: MetaData<T, any>[]): Readonly<T> {
	if (target === void 0 || target === null) {
		return undefined;
	}
	if (Object.isFrozen(target)) {
		return target;
	}
	for (let meta of properties) {
		if (meta.type) {
			let value = target[meta.property];
			if (value) {
				meta.type.freeze(value);
			}
		}
	}
	Object.freeze(target);
	return target;
}

A
Alex Ross 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
export namespace RunOnOptions {
	export function fromString(value: string | undefined): Tasks.RunOnOptions {
		if (!value) {
			return Tasks.RunOnOptions.default;
		}
		switch (value.toLowerCase()) {
			case 'folderopen':
				return Tasks.RunOnOptions.folderOpen;
			case 'default':
			default:
				return Tasks.RunOnOptions.default;
		}
	}
}

export namespace RunOptions {
	export function fromConfiguration(value: RunOptionsConfig | undefined): Tasks.RunOptions {
		return {
655
			reevaluateOnRerun: value ? value.reevaluateOnRerun : true,
A
Alex Ross 已提交
656 657 658 659 660
			runOn: value ? RunOnOptions.fromString(value.runOn) : Tasks.RunOnOptions.default
		};
	}
}

661
interface ParseContext {
S
Sandeep Somavarapu 已提交
662
	workspaceFolder: IWorkspaceFolder;
663
	problemReporter: IProblemReporter;
664
	namedProblemMatchers: IStringDictionary<NamedProblemMatcher>;
665
	uuidMap: UUIDMap;
666 667
	engine: Tasks.ExecutionEngine;
	schemaVersion: Tasks.JsonSchemaVersion;
668
	platform: Platform;
E
Erich Gamma 已提交
669 670
}

671

672
namespace ShellConfiguration {
673

674
	const properties: MetaData<Tasks.ShellConfiguration, void>[] = [{ property: 'executable' }, { property: 'args' }, { property: 'quoting' }];
675

676 677
	export function is(value: any): value is ShellConfiguration {
		let candidate: ShellConfiguration = value;
D
Dirk Baeumer 已提交
678
		return candidate && (Types.isString(candidate.executable) || Types.isStringArray(candidate.args));
679 680 681 682 683 684
	}

	export function from(this: void, config: ShellConfiguration, context: ParseContext): Tasks.ShellConfiguration {
		if (!is(config)) {
			return undefined;
		}
D
Dirk Baeumer 已提交
685 686 687 688
		let result: ShellConfiguration = {};
		if (config.executable !== void 0) {
			result.executable = config.executable;
		}
689 690 691
		if (config.args !== void 0) {
			result.args = config.args.slice();
		}
692 693 694 695
		if (config.quoting !== void 0) {
			result.quoting = Objects.deepClone(config.quoting);
		}

696 697 698
		return result;
	}

699 700
	export function isEmpty(this: void, value: Tasks.ShellConfiguration): boolean {
		return _isEmpty(value, properties);
701 702
	}

703 704
	export function assignProperties(this: void, target: Tasks.ShellConfiguration, source: Tasks.ShellConfiguration): Tasks.ShellConfiguration {
		return _assignProperties(target, source, properties);
705 706
	}

707 708
	export function fillProperties(this: void, target: Tasks.ShellConfiguration, source: Tasks.ShellConfiguration): Tasks.ShellConfiguration {
		return _fillProperties(target, source, properties);
709 710
	}

711 712
	export function fillDefaults(this: void, value: Tasks.ShellConfiguration, context: ParseContext): Tasks.ShellConfiguration {
		return value;
713 714
	}

715
	export function freeze(this: void, value: Tasks.ShellConfiguration): Readonly<Tasks.ShellConfiguration> {
716
		if (!value) {
717
			return undefined;
718
		}
719
		return Object.freeze(value);
720 721 722
	}
}

723
namespace CommandOptions {
724 725

	const properties: MetaData<Tasks.CommandOptions, Tasks.ShellConfiguration>[] = [{ property: 'cwd' }, { property: 'env' }, { property: 'shell', type: ShellConfiguration }];
726
	const defaults: CommandOptionsConfig = { cwd: '${workspaceFolder}' };
727

728
	export function from(this: void, options: CommandOptionsConfig, context: ParseContext): Tasks.CommandOptions {
729
		let result: Tasks.CommandOptions = {};
730 731 732 733
		if (options.cwd !== void 0) {
			if (Types.isString(options.cwd)) {
				result.cwd = options.cwd;
			} else {
734
				context.problemReporter.warn(nls.localize('ConfigurationParser.invalidCWD', 'Warning: options.cwd must be of type string. Ignoring value {0}\n', options.cwd));
735 736 737
			}
		}
		if (options.env !== void 0) {
J
Johannes Rieken 已提交
738
			result.env = Objects.deepClone(options.env);
739
		}
D
Dirk Baeumer 已提交
740
		result.shell = ShellConfiguration.from(options.shell, context);
741
		return isEmpty(result) ? undefined : result;
E
Erich Gamma 已提交
742 743
	}

744
	export function isEmpty(value: Tasks.CommandOptions): boolean {
745
		return _isEmpty(value, properties);
E
Erich Gamma 已提交
746 747
	}

748
	export function assignProperties(target: Tasks.CommandOptions, source: Tasks.CommandOptions): Tasks.CommandOptions {
749 750
		if (isEmpty(source)) {
			return target;
E
Erich Gamma 已提交
751
		}
752 753
		if (isEmpty(target)) {
			return source;
E
Erich Gamma 已提交
754
		}
755
		assignProperty(target, source, 'cwd');
756 757 758 759 760
		if (target.env === void 0) {
			target.env = source.env;
		} else if (source.env !== void 0) {
			let env: { [key: string]: string; } = Object.create(null);
			Object.keys(target.env).forEach(key => env[key] = target.env[key]);
761
			Object.keys(source.env).forEach(key => env[key] = source.env[key]);
762 763
			target.env = env;
		}
764 765 766 767 768
		target.shell = ShellConfiguration.assignProperties(target.shell, source.shell);
		return target;
	}

	export function fillProperties(target: Tasks.CommandOptions, source: Tasks.CommandOptions): Tasks.CommandOptions {
769
		return _fillProperties(target, source, properties);
E
Erich Gamma 已提交
770 771
	}

772 773
	export function fillDefaults(value: Tasks.CommandOptions, context: ParseContext): Tasks.CommandOptions {
		return _fillDefaults(value, defaults, properties, context);
774 775
	}

776 777
	export function freeze(value: Tasks.CommandOptions): Readonly<Tasks.CommandOptions> {
		return _freeze(value, properties);
E
Erich Gamma 已提交
778
	}
779
}
E
Erich Gamma 已提交
780

781
namespace CommandConfiguration {
782

783
	export namespace PresentationOptions {
784
		const properties: MetaData<Tasks.PresentationOptions, void>[] = [{ property: 'echo' }, { property: 'reveal' }, { property: 'focus' }, { property: 'panel' }, { property: 'showReuseMessage' }, { property: 'clear' }];
785

786
		interface PresentationOptionsShape extends LegacyCommandProperties {
787
			presentation?: PresentationOptionsConfig;
788 789 790
		}

		export function from(this: void, config: PresentationOptionsShape, context: ParseContext): Tasks.PresentationOptions {
791 792 793
			let echo: boolean;
			let reveal: Tasks.RevealKind;
			let focus: boolean;
794
			let panel: Tasks.PanelKind;
D
Dirk Baeumer 已提交
795
			let showReuseMessage: boolean;
796
			let clear: boolean;
D
Dirk Baeumer 已提交
797 798 799 800 801 802
			if (Types.isBoolean(config.echoCommand)) {
				echo = config.echoCommand;
			}
			if (Types.isString(config.showOutput)) {
				reveal = Tasks.RevealKind.fromString(config.showOutput);
			}
803 804 805 806
			let presentation = config.presentation || config.terminal;
			if (presentation) {
				if (Types.isBoolean(presentation.echo)) {
					echo = presentation.echo;
D
Dirk Baeumer 已提交
807
				}
808 809
				if (Types.isString(presentation.reveal)) {
					reveal = Tasks.RevealKind.fromString(presentation.reveal);
D
Dirk Baeumer 已提交
810
				}
811 812
				if (Types.isBoolean(presentation.focus)) {
					focus = presentation.focus;
813
				}
814 815
				if (Types.isString(presentation.panel)) {
					panel = Tasks.PanelKind.fromString(presentation.panel);
816
				}
D
Dirk Baeumer 已提交
817 818
				if (Types.isBoolean(presentation.showReuseMessage)) {
					showReuseMessage = presentation.showReuseMessage;
819
				}
820 821
				if (Types.isBoolean(presentation.clear)) {
					clear = presentation.clear;
822
				}
D
Dirk Baeumer 已提交
823
			}
D
Dirk Baeumer 已提交
824
			if (echo === void 0 && reveal === void 0 && focus === void 0 && panel === void 0 && showReuseMessage === void 0) {
D
Dirk Baeumer 已提交
825 826
				return undefined;
			}
827
			return { echo, reveal, focus, panel, showReuseMessage, clear };
D
Dirk Baeumer 已提交
828 829
		}

830
		export function assignProperties(target: Tasks.PresentationOptions, source: Tasks.PresentationOptions): Tasks.PresentationOptions {
831
			return _assignProperties(target, source, properties);
832 833
		}

834
		export function fillProperties(target: Tasks.PresentationOptions, source: Tasks.PresentationOptions): Tasks.PresentationOptions {
835
			return _fillProperties(target, source, properties);
D
Dirk Baeumer 已提交
836 837
		}

838
		export function fillDefaults(value: Tasks.PresentationOptions, context: ParseContext): Tasks.PresentationOptions {
839
			let defaultEcho = context.engine === Tasks.ExecutionEngine.Terminal ? true : false;
840
			return _fillDefaults(value, { echo: defaultEcho, reveal: Tasks.RevealKind.Always, focus: false, panel: Tasks.PanelKind.Shared, showReuseMessage: true, clear: false }, properties, context);
D
Dirk Baeumer 已提交
841 842
		}

843
		export function freeze(value: Tasks.PresentationOptions): Readonly<Tasks.PresentationOptions> {
844
			return _freeze(value, properties);
D
Dirk Baeumer 已提交
845 846
		}

847
		export function isEmpty(this: void, value: Tasks.PresentationOptions): boolean {
848
			return _isEmpty(value, properties);
D
Dirk Baeumer 已提交
849 850 851
		}
	}

852 853 854 855 856 857 858
	namespace ShellString {
		export function from(this: void, value: CommandString): Tasks.CommandString {
			if (value === void 0 || value === null) {
				return undefined;
			}
			if (Types.isString(value)) {
				return value;
859 860 861 862 863 864 865 866 867 868 869 870 871
			} else if (Types.isStringArray(value)) {
				return value.join(' ');
			} else {
				let quoting = Tasks.ShellQuoting.from(value.quoting);
				let result = Types.isString(value.value) ? value.value : Types.isStringArray(value.value) ? value.value.join(' ') : undefined;
				if (result) {
					return {
						value: result,
						quoting: quoting
					};
				} else {
					return undefined;
				}
872 873 874 875
			}
		}
	}

876 877 878 879 880 881 882 883 884 885 886
	interface BaseCommandConfiguationShape extends BaseCommandProperties, LegacyCommandProperties {
	}

	interface CommandConfiguationShape extends BaseCommandConfiguationShape {
		windows?: BaseCommandConfiguationShape;
		osx?: BaseCommandConfiguationShape;
		linux?: BaseCommandConfiguationShape;
	}

	const properties: MetaData<Tasks.CommandConfiguration, any>[] = [
		{ property: 'runtime' }, { property: 'name' }, { property: 'options', type: CommandOptions },
887
		{ property: 'args' }, { property: 'taskSelector' }, { property: 'suppressTaskName' },
888
		{ property: 'presentation', type: PresentationOptions }
889 890
	];

891 892
	export function from(this: void, config: CommandConfiguationShape, context: ParseContext): Tasks.CommandConfiguration {
		let result: Tasks.CommandConfiguration = fromBase(config, context);
893

894
		let osConfig: Tasks.CommandConfiguration = undefined;
895
		if (config.windows && context.platform === Platform.Windows) {
896
			osConfig = fromBase(config.windows, context);
897
		} else if (config.osx && context.platform === Platform.Mac) {
898
			osConfig = fromBase(config.osx, context);
899
		} else if (config.linux && context.platform === Platform.Linux) {
900 901 902
			osConfig = fromBase(config.linux, context);
		}
		if (osConfig) {
903
			result = assignProperties(result, osConfig, context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0);
904 905 906 907
		}
		return isEmpty(result) ? undefined : result;
	}

908 909 910
	function fromBase(this: void, config: BaseCommandConfiguationShape, context: ParseContext): Tasks.CommandConfiguration {
		let result: Tasks.CommandConfiguration = {
			name: undefined,
911
			runtime: undefined,
912
			presentation: undefined
913
		};
914 915

		result.name = ShellString.from(config.command);
D
Dirk Baeumer 已提交
916
		if (Types.isString(config.type)) {
917 918 919
			if (config.type === 'shell' || config.type === 'process') {
				result.runtime = Tasks.RuntimeType.fromString(config.type);
			}
D
Dirk Baeumer 已提交
920 921 922
		}
		let isShellConfiguration = ShellConfiguration.is(config.isShellCommand);
		if (Types.isBoolean(config.isShellCommand) || isShellConfiguration) {
923
			result.runtime = Tasks.RuntimeType.Shell;
924
		} else if (config.isShellCommand !== void 0) {
925 926
			result.runtime = !!config.isShellCommand ? Tasks.RuntimeType.Shell : Tasks.RuntimeType.Process;
		}
927

928
		if (config.args !== void 0) {
929 930 931
			result.args = [];
			for (let arg of config.args) {
				let converted = ShellString.from(arg);
D
Dirk Baeumer 已提交
932
				if (converted !== void 0) {
933 934
					result.args.push(converted);
				} else {
935 936 937 938 939 940
					context.problemReporter.error(
						nls.localize(
							'ConfigurationParser.inValidArg',
							'Error: command argument must either be a string or a quoted string. Provided value is:\n{0}',
							arg ? JSON.stringify(arg, undefined, 4) : 'undefined'
						));
941
				}
942
			}
E
Erich Gamma 已提交
943
		}
944 945
		if (config.options !== void 0) {
			result.options = CommandOptions.from(config.options, context);
D
Dirk Baeumer 已提交
946 947
			if (result.options && result.options.shell === void 0 && isShellConfiguration) {
				result.options.shell = ShellConfiguration.from(config.isShellCommand as ShellConfiguration, context);
948
				if (context.engine !== Tasks.ExecutionEngine.Terminal) {
D
Dirk Baeumer 已提交
949 950 951
					context.problemReporter.warn(nls.localize('ConfigurationParser.noShell', 'Warning: shell configuration is only supported when executing tasks in the terminal.'));
				}
			}
E
Erich Gamma 已提交
952
		}
953 954 955
		let panel = PresentationOptions.from(config, context);
		if (panel) {
			result.presentation = panel;
E
Erich Gamma 已提交
956
		}
957 958
		if (Types.isString(config.taskSelector)) {
			result.taskSelector = config.taskSelector;
E
Erich Gamma 已提交
959
		}
960 961 962
		if (Types.isBoolean(config.suppressTaskName)) {
			result.suppressTaskName = config.suppressTaskName;
		}
963
		return isEmpty(result) ? undefined : result;
E
Erich Gamma 已提交
964 965
	}

966 967 968 969
	export function hasCommand(value: Tasks.CommandConfiguration): boolean {
		return value && !!value.name;
	}

970
	export function isEmpty(value: Tasks.CommandConfiguration): boolean {
971
		return _isEmpty(value, properties);
972 973
	}

974
	export function assignProperties(target: Tasks.CommandConfiguration, source: Tasks.CommandConfiguration, overwriteArgs: boolean): Tasks.CommandConfiguration {
975 976
		if (isEmpty(source)) {
			return target;
E
Erich Gamma 已提交
977
		}
978 979
		if (isEmpty(target)) {
			return source;
E
Erich Gamma 已提交
980
		}
981
		assignProperty(target, source, 'name');
982
		assignProperty(target, source, 'runtime');
983 984
		assignProperty(target, source, 'taskSelector');
		assignProperty(target, source, 'suppressTaskName');
985
		if (source.args !== void 0) {
986
			if (target.args === void 0 || overwriteArgs) {
987 988 989
				target.args = source.args;
			} else {
				target.args = target.args.concat(source.args);
E
Erich Gamma 已提交
990 991
			}
		}
992
		target.presentation = PresentationOptions.assignProperties(target.presentation, source.presentation);
993 994 995 996
		target.options = CommandOptions.assignProperties(target.options, source.options);
		return target;
	}

997 998 999 1000
	export function fillProperties(target: Tasks.CommandConfiguration, source: Tasks.CommandConfiguration): Tasks.CommandConfiguration {
		return _fillProperties(target, source, properties);
	}

1001 1002 1003 1004 1005 1006
	export function fillGlobals(target: Tasks.CommandConfiguration, source: Tasks.CommandConfiguration, taskName: string): Tasks.CommandConfiguration {
		if (isEmpty(source)) {
			return target;
		}
		target = target || {
			name: undefined,
1007
			runtime: undefined,
1008
			presentation: undefined
1009
		};
1010 1011 1012 1013
		if (target.name === void 0) {
			fillProperty(target, source, 'name');
			fillProperty(target, source, 'taskSelector');
			fillProperty(target, source, 'suppressTaskName');
1014
			let args: Tasks.CommandString[] = source.args ? source.args.slice() : [];
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
			if (!target.suppressTaskName) {
				if (target.taskSelector !== void 0) {
					args.push(target.taskSelector + taskName);
				} else {
					args.push(taskName);
				}
			}
			if (target.args) {
				args = args.concat(target.args);
			}
			target.args = args;
		}
1027
		fillProperty(target, source, 'runtime');
1028

1029
		target.presentation = PresentationOptions.fillProperties(target.presentation, source.presentation);
1030 1031
		target.options = CommandOptions.fillProperties(target.options, source.options);

1032
		return target;
E
Erich Gamma 已提交
1033 1034
	}

1035
	export function fillDefaults(value: Tasks.CommandConfiguration, context: ParseContext): void {
1036 1037 1038
		if (!value || Object.isFrozen(value)) {
			return;
		}
1039 1040
		if (value.name !== void 0 && value.runtime === void 0) {
			value.runtime = Tasks.RuntimeType.Process;
1041
		}
1042
		value.presentation = PresentationOptions.fillDefaults(value.presentation, context);
1043
		if (!isEmpty(value)) {
1044
			value.options = CommandOptions.fillDefaults(value.options, context);
1045
		}
1046 1047 1048
		if (value.args === void 0) {
			value.args = EMPTY_ARRAY;
		}
1049 1050
		if (value.suppressTaskName === void 0) {
			value.suppressTaskName = false;
E
Erich Gamma 已提交
1051 1052 1053
		}
	}

1054 1055
	export function freeze(value: Tasks.CommandConfiguration): Readonly<Tasks.CommandConfiguration> {
		return _freeze(value, properties);
E
Erich Gamma 已提交
1056
	}
1057
}
E
Erich Gamma 已提交
1058

1059 1060 1061
namespace ProblemMatcherConverter {

	export function namedFrom(this: void, declares: ProblemMatcherConfig.NamedProblemMatcher[], context: ParseContext): IStringDictionary<NamedProblemMatcher> {
J
Johannes Rieken 已提交
1062
		let result: IStringDictionary<NamedProblemMatcher> = Object.create(null);
1063 1064

		if (!Types.isArray(declares)) {
E
Erich Gamma 已提交
1065 1066
			return result;
		}
1067
		(<ProblemMatcherConfig.NamedProblemMatcher[]>declares).forEach((value) => {
1068
			let namedProblemMatcher = (new ProblemMatcherParser(context.problemReporter)).parse(value);
1069
			if (isNamedProblemMatcher(namedProblemMatcher)) {
E
Erich Gamma 已提交
1070
				result[namedProblemMatcher.name] = namedProblemMatcher;
1071
			} else {
1072
				context.problemReporter.error(nls.localize('ConfigurationParser.noName', 'Error: Problem Matcher in declare scope must have a name:\n{0}\n', JSON.stringify(value, undefined, 4)));
E
Erich Gamma 已提交
1073 1074 1075 1076 1077
			}
		});
		return result;
	}

1078 1079 1080 1081 1082 1083 1084
	export function from(this: void, config: ProblemMatcherConfig.ProblemMatcherType, context: ParseContext): ProblemMatcher[] {
		let result: ProblemMatcher[] = [];
		if (config === void 0) {
			return result;
		}
		let kind = getProblemMatcherKind(config);
		if (kind === ProblemMatcherKind.Unknown) {
1085
			context.problemReporter.warn(nls.localize(
1086 1087 1088
				'ConfigurationParser.unknownMatcherKind',
				'Warning: the defined problem matcher is unknown. Supported types are string | ProblemMatcher | (string | ProblemMatcher)[].\n{0}\n',
				JSON.stringify(config, null, 4)));
E
Erich Gamma 已提交
1089
			return result;
1090
		} else if (kind === ProblemMatcherKind.String || kind === ProblemMatcherKind.ProblemMatcher) {
1091
			let matcher = resolveProblemMatcher(config as ProblemMatcherConfig.ProblemMatcher, context);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			if (matcher) {
				result.push(matcher);
			}
		} else if (kind === ProblemMatcherKind.Array) {
			let problemMatchers = <(string | ProblemMatcherConfig.ProblemMatcher)[]>config;
			problemMatchers.forEach(problemMatcher => {
				let matcher = resolveProblemMatcher(problemMatcher, context);
				if (matcher) {
					result.push(matcher);
				}
			});
		}
		return result;
	}

	function getProblemMatcherKind(this: void, value: ProblemMatcherConfig.ProblemMatcherType): ProblemMatcherKind {
		if (Types.isString(value)) {
			return ProblemMatcherKind.String;
		} else if (Types.isArray(value)) {
			return ProblemMatcherKind.Array;
		} else if (!Types.isUndefined(value)) {
			return ProblemMatcherKind.ProblemMatcher;
E
Erich Gamma 已提交
1114
		} else {
1115
			return ProblemMatcherKind.Unknown;
E
Erich Gamma 已提交
1116 1117 1118
		}
	}

1119 1120 1121 1122 1123 1124 1125
	function resolveProblemMatcher(this: void, value: string | ProblemMatcherConfig.ProblemMatcher, context: ParseContext): ProblemMatcher {
		if (Types.isString(value)) {
			let variableName = <string>value;
			if (variableName.length > 1 && variableName[0] === '$') {
				variableName = variableName.substring(1);
				let global = ProblemMatcherRegistry.get(variableName);
				if (global) {
J
Johannes Rieken 已提交
1126
					return Objects.deepClone(global);
1127 1128 1129
				}
				let localProblemMatcher = context.namedProblemMatchers[variableName];
				if (localProblemMatcher) {
J
Johannes Rieken 已提交
1130
					localProblemMatcher = Objects.deepClone(localProblemMatcher);
1131 1132 1133 1134 1135
					// remove the name
					delete localProblemMatcher.name;
					return localProblemMatcher;
				}
			}
1136
			context.problemReporter.error(nls.localize('ConfigurationParser.invalidVaraibleReference', 'Error: Invalid problemMatcher reference: {0}\n', value));
1137 1138 1139
			return undefined;
		} else {
			let json = <ProblemMatcherConfig.ProblemMatcher>value;
1140
			return new ProblemMatcherParser(context.problemReporter).parse(json);
1141 1142 1143 1144
		}
	}
}

1145 1146 1147
const source: Tasks.TaskSource = {
	kind: Tasks.TaskSourceKind.Workspace,
	label: 'Workspace',
1148
	config: undefined
1149
};
1150

1151
namespace GroupKind {
1152
	export function from(this: void, external: string | GroupKind): [string, Tasks.GroupType] {
1153 1154 1155 1156 1157
		if (external === void 0) {
			return undefined;
		}
		if (Types.isString(external)) {
			if (Tasks.TaskGroup.is(external)) {
1158
				return [external, Tasks.GroupType.user];
1159
			} else {
D
Dirk Baeumer 已提交
1160 1161 1162
				return undefined;
			}
		}
1163 1164 1165 1166
		if (!Types.isString(external.kind) || !Tasks.TaskGroup.is(external.kind)) {
			return undefined;
		}
		let group: string = external.kind;
1167
		let isDefault: boolean = !!external.isDefault;
1168

1169
		return [group, isDefault ? Tasks.GroupType.default : Tasks.GroupType.user];
D
Dirk Baeumer 已提交
1170
	}
1171 1172
}

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
namespace TaskDependency {
	export function from(this: void, external: string | TaskIdentifier, context: ParseContext): Tasks.TaskDependency {
		if (Types.isString(external)) {
			return { workspaceFolder: context.workspaceFolder, task: external };
		} else if (TaskIdentifier.is(external)) {
			return { workspaceFolder: context.workspaceFolder, task: TaskDefinition.createTaskIdentifier(external as Tasks.TaskIdentifier, context.problemReporter) };
		} else {
			return undefined;
		}
	}
}

1185
namespace ConfigurationProperties {
D
Dirk Baeumer 已提交
1186

1187
	const properties: MetaData<Tasks.ConfigurationProperties, any>[] = [
1188

1189 1190 1191 1192
		{ property: 'name' }, { property: 'identifier' }, { property: 'group' }, { property: 'isBackground' },
		{ property: 'promptOnClose' }, { property: 'dependsOn' },
		{ property: 'presentation', type: CommandConfiguration.PresentationOptions }, { property: 'problemMatchers' }
	];
1193

1194
	export function from(this: void, external: ConfigurationProperties, context: ParseContext, includeCommandOptions: boolean): Tasks.ConfigurationProperties {
1195
		if (!external) {
1196
			return undefined;
E
Erich Gamma 已提交
1197
		}
1198 1199 1200 1201
		let result: Tasks.ConfigurationProperties = {};
		if (Types.isString(external.taskName)) {
			result.name = external.taskName;
		}
1202 1203 1204
		if (Types.isString(external.label) && context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0) {
			result.name = external.label;
		}
1205 1206 1207 1208 1209 1210 1211 1212 1213
		if (Types.isString(external.identifier)) {
			result.identifier = external.identifier;
		}
		if (external.isBackground !== void 0) {
			result.isBackground = !!external.isBackground;
		}
		if (external.promptOnClose !== void 0) {
			result.promptOnClose = !!external.promptOnClose;
		}
D
Dirk Baeumer 已提交
1214 1215 1216
		if (external.group !== void 0) {
			if (Types.isString(external.group) && Tasks.TaskGroup.is(external.group)) {
				result.group = external.group;
1217
				result.groupType = Tasks.GroupType.user;
D
Dirk Baeumer 已提交
1218 1219 1220 1221
			} else {
				let values = GroupKind.from(external.group);
				if (values) {
					result.group = values[0];
1222
					result.groupType = values[1];
D
Dirk Baeumer 已提交
1223 1224
				}
			}
1225 1226
		}
		if (external.dependsOn !== void 0) {
1227 1228 1229
			if (Types.isArray(external.dependsOn)) {
				result.dependsOn = external.dependsOn.map(item => TaskDependency.from(item, context));
			} else {
1230
				result.dependsOn = [TaskDependency.from(external.dependsOn, context)];
1231
			}
1232
		}
1233
		if (includeCommandOptions && (external.presentation !== void 0 || (external as LegacyCommandProperties).terminal !== void 0)) {
1234 1235
			result.presentation = CommandConfiguration.PresentationOptions.from(external, context);
		}
1236 1237 1238
		if (includeCommandOptions && (external.options !== void 0)) {
			result.options = CommandOptions.from(external.options, context);
		}
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		if (external.problemMatcher) {
			result.problemMatchers = ProblemMatcherConverter.from(external.problemMatcher, context);
		}
		return isEmpty(result) ? undefined : result;
	}

	export function isEmpty(this: void, value: Tasks.ConfigurationProperties): boolean {
		return _isEmpty(value, properties);
	}
}

namespace ConfiguringTask {

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
	const grunt = 'grunt.';
	const jake = 'jake.';
	const gulp = 'gulp.';
	const npm = 'vscode.npm.';
	const typescript = 'vscode.typescript.';

	interface CustomizeShape {
		customize: string;
	}

1262
	export function from(this: void, external: ConfiguringTask, context: ParseContext, index: number): Tasks.ConfiguringTask {
1263 1264 1265 1266
		if (!external) {
			return undefined;
		}
		let type = external.type;
1267 1268
		let customize = (external as CustomizeShape).customize;
		if (!type && !customize) {
1269
			context.problemReporter.error(nls.localize('ConfigurationParser.noTaskType', 'Error: tasks configuration must have a type property. The configuration will be ignored.\n{0}\n', JSON.stringify(external, null, 4)));
1270 1271
			return undefined;
		}
D
Dirk Baeumer 已提交
1272
		let typeDeclaration = TaskDefinitionRegistry.get(type);
1273
		if (!typeDeclaration) {
1274
			let message = nls.localize('ConfigurationParser.noTypeDefinition', 'Error: there is no registered task type \'{0}\'. Did you miss to install an extension that provides a corresponding task provider?', type);
1275 1276 1277
			context.problemReporter.error(message);
			return undefined;
		}
1278
		let identifier: Tasks.TaskIdentifier;
1279 1280
		if (Types.isString(customize)) {
			if (customize.indexOf(grunt) === 0) {
1281
				identifier = { type: 'grunt', task: customize.substring(grunt.length) };
1282
			} else if (customize.indexOf(jake) === 0) {
1283
				identifier = { type: 'jake', task: customize.substring(jake.length) };
1284
			} else if (customize.indexOf(gulp) === 0) {
1285
				identifier = { type: 'gulp', task: customize.substring(gulp.length) };
1286
			} else if (customize.indexOf(npm) === 0) {
1287
				identifier = { type: 'npm', script: customize.substring(npm.length + 4) };
1288
			} else if (customize.indexOf(typescript) === 0) {
1289
				identifier = { type: 'typescript', tsconfig: customize.substring(typescript.length + 6) };
1290
			}
1291
		} else {
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
			if (Types.isString(external.type)) {
				identifier = external as Tasks.TaskIdentifier;
			}
		}
		if (identifier === void 0) {
			context.problemReporter.error(nls.localize(
				'ConfigurationParser.missingType',
				'Error: the task configuration \'{0}\' is missing the required property \'type\'. The task configuration will be ignored.', JSON.stringify(external, undefined, 0)
			));
			return undefined;
1302
		}
1303
		let taskIdentifier: Tasks.KeyedTaskIdentifier = TaskDefinition.createTaskIdentifier(identifier, context.problemReporter);
1304
		if (taskIdentifier === void 0) {
1305 1306
			context.problemReporter.error(nls.localize(
				'ConfigurationParser.incorrectType',
D
Dániel Tar 已提交
1307
				'Error: the task configuration \'{0}\' is using an unknown type. The task configuration will be ignored.', JSON.stringify(external, undefined, 0)
1308
			));
1309
			return undefined;
1310
		}
1311
		let configElement: Tasks.TaskSourceConfigElement = {
1312
			workspaceFolder: context.workspaceFolder,
1313 1314 1315 1316
			file: '.vscode\\tasks.json',
			index,
			element: external
		};
1317 1318 1319
		let result: Tasks.ConfiguringTask = {
			type: type,
			configures: taskIdentifier,
1320
			_id: `${typeDeclaration.extensionId}.${taskIdentifier._key}`,
M
Matt Bierner 已提交
1321
			_source: Objects.assign({} as Tasks.WorkspaceTaskSource, source, { config: configElement }),
A
Alex Ross 已提交
1322
			_label: undefined,
A
Alex Ross 已提交
1323
			runOptions: RunOptions.fromConfiguration(external.runOptions)
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
		};
		let configuration = ConfigurationProperties.from(external, context, true);
		if (configuration) {
			result = Objects.assign(result, configuration);
			if (result.name) {
				result._label = result.name;
			} else {
				let label = result.configures.type;
				if (typeDeclaration.required && typeDeclaration.required.length > 0) {
					for (let required of typeDeclaration.required) {
						let value = result.configures[required];
						if (value) {
							label = label + ' ' + value;
							break;
						}
					}
D
Dirk Baeumer 已提交
1340
				}
1341
				result._label = label;
1342
			}
1343 1344
			if (!result.identifier) {
				result.identifier = taskIdentifier._key;
E
Erich Gamma 已提交
1345
			}
1346 1347 1348 1349 1350 1351 1352
		}
		return result;
	}
}

namespace CustomTask {

1353
	export function from(this: void, external: CustomTask, context: ParseContext, index: number): Tasks.CustomTask {
1354 1355 1356 1357 1358
		if (!external) {
			return undefined;
		}
		let type = external.type;
		if (type === void 0 || type === null) {
1359
			type = Tasks.CUSTOMIZED_TASK_TYPE;
1360
		}
1361
		if (type !== Tasks.CUSTOMIZED_TASK_TYPE && type !== 'shell' && type !== 'process') {
1362
			context.problemReporter.error(nls.localize('ConfigurationParser.notCustom', 'Error: tasks is not declared as a custom task. The configuration will be ignored.\n{0}\n', JSON.stringify(external, null, 4)));
1363 1364 1365
			return undefined;
		}
		let taskName = external.taskName;
1366 1367 1368
		if (Types.isString(external.label) && context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0) {
			taskName = external.label;
		}
1369
		if (!taskName) {
D
Dirk Baeumer 已提交
1370
			context.problemReporter.error(nls.localize('ConfigurationParser.noTaskName', 'Error: a task must provide a label property. The task will be ignored.\n{0}\n', JSON.stringify(external, null, 4)));
1371 1372 1373 1374
			return undefined;
		}

		let result: Tasks.CustomTask = {
1375
			type: Tasks.CUSTOMIZED_TASK_TYPE,
1376
			_id: context.uuidMap.getUUID(taskName),
M
Matt Bierner 已提交
1377
			_source: Objects.assign({} as Tasks.WorkspaceTaskSource, source, { config: { index, element: external, file: '.vscode\\tasks.json', workspaceFolder: context.workspaceFolder } }),
1378 1379 1380
			_label: taskName,
			name: taskName,
			identifier: taskName,
1381
			hasDefinedMatchers: false,
A
Alex Ross 已提交
1382
			command: undefined,
A
Alex Ross 已提交
1383
			runOptions: RunOptions.fromConfiguration(external.runOptions)
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
		};
		let configuration = ConfigurationProperties.from(external, context, false);
		if (configuration) {
			result = Objects.assign(result, configuration);
		}
		let supportLegacy: boolean = true; //context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0;
		if (supportLegacy) {
			let legacy: LegacyTaskProperties = external as LegacyTaskProperties;
			if (result.isBackground === void 0 && legacy.isWatching !== void 0) {
				result.isBackground = !!legacy.isWatching;
1394
			}
1395 1396 1397 1398 1399
			if (result.group === void 0) {
				if (legacy.isBuildCommand === true) {
					result.group = Tasks.TaskGroup.Build;
				} else if (legacy.isTestCommand === true) {
					result.group = Tasks.TaskGroup.Test;
1400
				}
1401
			}
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
		}
		let command: Tasks.CommandConfiguration = CommandConfiguration.from(external, context);
		if (command) {
			result.command = command;
		}
		if (external.command !== void 0) {
			// if the task has its own command then we suppress the
			// task name by default.
			command.suppressTaskName = true;
		}
		return result;
	}

	export function fillGlobals(task: Tasks.CustomTask, globals: Globals): void {
		// We only merge a command from a global definition if there is no dependsOn
1417 1418
		// or there is a dependsOn and a defined command.
		if (CommandConfiguration.hasCommand(task.command) || task.dependsOn === void 0) {
1419 1420
			task.command = CommandConfiguration.fillGlobals(task.command, globals.command, task.name);
		}
1421 1422 1423 1424
		if (task.problemMatchers === void 0 && globals.problemMatcher !== void 0) {
			task.problemMatchers = Objects.deepClone(globals.problemMatcher);
			task.hasDefinedMatchers = true;
		}
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
		// promptOnClose is inferred from isBackground if available
		if (task.promptOnClose === void 0 && task.isBackground === void 0 && globals.promptOnClose !== void 0) {
			task.promptOnClose = globals.promptOnClose;
		}
	}

	export function fillDefaults(task: Tasks.CustomTask, context: ParseContext): void {
		CommandConfiguration.fillDefaults(task.command, context);
		if (task.promptOnClose === void 0) {
			task.promptOnClose = task.isBackground !== void 0 ? !task.isBackground : true;
		}
		if (task.isBackground === void 0) {
			task.isBackground = false;
		}
		if (task.problemMatchers === void 0) {
			task.problemMatchers = EMPTY_ARRAY;
		}
1442 1443
		if (task.group !== void 0 && task.groupType === void 0) {
			task.groupType = Tasks.GroupType.user;
1444
		}
1445 1446
	}

1447
	export function createCustomTask(contributedTask: Tasks.ContributedTask, configuredProps: Tasks.ConfigurationProperties & { _id: string, _source: Tasks.WorkspaceTaskSource }): Tasks.CustomTask {
1448
		let result: Tasks.CustomTask = {
1449
			_id: configuredProps._id,
1450
			_source: Objects.assign({}, configuredProps._source, { customizes: contributedTask.defines }),
1451
			_label: configuredProps.name || contributedTask._label,
1452
			type: Tasks.CUSTOMIZED_TASK_TYPE,
1453 1454
			command: contributedTask.command,
			name: configuredProps.name || contributedTask.name,
1455
			identifier: configuredProps.identifier || contributedTask.identifier,
A
Alex Ross 已提交
1456 1457
			hasDefinedMatchers: false,
			runOptions: contributedTask.runOptions,
1458 1459 1460 1461
		};
		let resultConfigProps: Tasks.ConfigurationProperties = result;

		assignProperty(resultConfigProps, configuredProps, 'group');
1462
		assignProperty(resultConfigProps, configuredProps, 'groupType');
1463 1464 1465 1466 1467 1468
		assignProperty(resultConfigProps, configuredProps, 'isBackground');
		assignProperty(resultConfigProps, configuredProps, 'dependsOn');
		assignProperty(resultConfigProps, configuredProps, 'problemMatchers');
		assignProperty(resultConfigProps, configuredProps, 'promptOnClose');
		result.command.presentation = CommandConfiguration.PresentationOptions.assignProperties(
			result.command.presentation, configuredProps.presentation);
1469
		result.command.options = CommandOptions.assignProperties(result.command.options, configuredProps.options);
1470 1471 1472

		let contributedConfigProps: Tasks.ConfigurationProperties = contributedTask;
		fillProperty(resultConfigProps, contributedConfigProps, 'group');
1473
		fillProperty(resultConfigProps, contributedConfigProps, 'groupType');
1474 1475 1476 1477 1478 1479
		fillProperty(resultConfigProps, contributedConfigProps, 'isBackground');
		fillProperty(resultConfigProps, contributedConfigProps, 'dependsOn');
		fillProperty(resultConfigProps, contributedConfigProps, 'problemMatchers');
		fillProperty(resultConfigProps, contributedConfigProps, 'promptOnClose');
		result.command.presentation = CommandConfiguration.PresentationOptions.fillProperties(
			result.command.presentation, contributedConfigProps.presentation);
1480
		result.command.options = CommandOptions.fillProperties(result.command.options, contributedConfigProps.options);
1481

1482 1483 1484 1485
		if (contributedTask.hasDefinedMatchers === true) {
			result.hasDefinedMatchers = true;
		}

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
		return result;
	}
}

interface TaskParseResult {
	custom: Tasks.CustomTask[];
	configured: Tasks.ConfiguringTask[];
}

namespace TaskParser {

	function isCustomTask(value: CustomTask | ConfiguringTask): value is CustomTask {
		let type = value.type;
1499
		let customize = (value as any).customize;
1500
		return customize === void 0 && (type === void 0 || type === null || type === Tasks.CUSTOMIZED_TASK_TYPE || type === 'shell' || type === 'process');
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	}

	export function from(this: void, externals: (CustomTask | ConfiguringTask)[], globals: Globals, context: ParseContext): TaskParseResult {
		let result: TaskParseResult = { custom: [], configured: [] };
		if (!externals) {
			return result;
		}
		let defaultBuildTask: { task: Tasks.Task; rank: number; } = { task: undefined, rank: -1 };
		let defaultTestTask: { task: Tasks.Task; rank: number; } = { task: undefined, rank: -1 };
		let schema2_0_0: boolean = context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0;

1512 1513
		for (let index = 0; index < externals.length; index++) {
			let external = externals[index];
1514
			if (isCustomTask(external)) {
1515
				let customTask = CustomTask.from(external, context, index);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
				if (customTask) {
					CustomTask.fillGlobals(customTask, globals);
					CustomTask.fillDefaults(customTask, context);
					if (schema2_0_0) {
						if ((customTask.command === void 0 || customTask.command.name === void 0) && (customTask.dependsOn === void 0 || customTask.dependsOn.length === 0)) {
							context.problemReporter.error(nls.localize(
								'taskConfiguration.noCommandOrDependsOn', 'Error: the task \'{0}\' neither specifies a command nor a dependsOn property. The task will be ignored. Its definition is:\n{1}',
								customTask.name, JSON.stringify(external, undefined, 4)
							));
							continue;
						}
					} else {
						if (customTask.command === void 0 || customTask.command.name === void 0) {
							context.problemReporter.warn(nls.localize(
								'taskConfiguration.noCommand', 'Error: the task \'{0}\' doesn\'t define a command. The task will be ignored. Its definition is:\n{1}',
								customTask.name, JSON.stringify(external, undefined, 4)
							));
							continue;
						}
					}
					if (customTask.group === Tasks.TaskGroup.Build && defaultBuildTask.rank < 2) {
						defaultBuildTask.task = customTask;
						defaultBuildTask.rank = 2;
					} else if (customTask.group === Tasks.TaskGroup.Test && defaultTestTask.rank < 2) {
						defaultTestTask.task = customTask;
						defaultTestTask.rank = 2;
					} else if (customTask.name === 'build' && defaultBuildTask.rank < 1) {
						defaultBuildTask.task = customTask;
						defaultBuildTask.rank = 1;
					} else if (customTask.name === 'test' && defaultTestTask.rank < 1) {
						defaultTestTask.task = customTask;
						defaultTestTask.rank = 1;
					}
					result.custom.push(customTask);
D
Dirk Baeumer 已提交
1550 1551
				}
			} else {
1552
				let configuredTask = ConfiguringTask.from(external, context, index);
1553 1554
				if (configuredTask) {
					result.configured.push(configuredTask);
D
Dirk Baeumer 已提交
1555
				}
E
Erich Gamma 已提交
1556
			}
1557
		}
1558
		if (defaultBuildTask.rank > -1 && defaultBuildTask.rank < 2) {
1559
			defaultBuildTask.task.group = Tasks.TaskGroup.Build;
1560
			defaultBuildTask.task.groupType = Tasks.GroupType.user;
1561
		} else if (defaultTestTask.rank > -1 && defaultTestTask.rank < 2) {
1562
			defaultTestTask.task.group = Tasks.TaskGroup.Test;
1563
			defaultTestTask.task.groupType = Tasks.GroupType.user;
E
Erich Gamma 已提交
1564
		}
1565 1566

		return result;
E
Erich Gamma 已提交
1567 1568
	}

1569
	export function assignTasks(target: Tasks.CustomTask[], source: Tasks.CustomTask[]): Tasks.CustomTask[] {
1570
		if (source === void 0 || source.length === 0) {
1571 1572
			return target;
		}
1573
		if (target === void 0 || target.length === 0) {
1574 1575 1576
			return source;
		}

1577
		if (source) {
1578
			// Tasks are keyed by ID but we need to merge by name
1579
			let map: IStringDictionary<Tasks.CustomTask> = Object.create(null);
1580 1581
			target.forEach((task) => {
				map[task.name] = task;
1582 1583
			});

1584 1585
			source.forEach((task) => {
				map[task.name] = task;
1586
			});
1587
			let newTarget: Tasks.CustomTask[] = [];
1588 1589 1590
			target.forEach(task => {
				newTarget.push(map[task.name]);
				delete map[task.name];
E
Erich Gamma 已提交
1591
			});
1592 1593
			Object.keys(map).forEach(key => newTarget.push(map[key]));
			target = newTarget;
E
Erich Gamma 已提交
1594
		}
1595 1596 1597 1598 1599
		return target;
	}
}

interface Globals {
1600
	command?: Tasks.CommandConfiguration;
1601
	problemMatcher?: ProblemMatcher[];
1602 1603 1604 1605 1606
	promptOnClose?: boolean;
	suppressTaskName?: boolean;
}

namespace Globals {
1607 1608 1609

	export function from(config: ExternalTaskRunnerConfiguration, context: ParseContext): Globals {
		let result = fromBase(config, context);
1610
		let osGlobals: Globals | undefined = undefined;
1611
		if (config.windows && context.platform === Platform.Windows) {
1612
			osGlobals = fromBase(config.windows, context);
1613
		} else if (config.osx && context.platform === Platform.Mac) {
1614
			osGlobals = fromBase(config.osx, context);
1615
		} else if (config.linux && context.platform === Platform.Linux) {
1616 1617 1618
			osGlobals = fromBase(config.linux, context);
		}
		if (osGlobals) {
1619
			result = Globals.assignProperties(result, osGlobals);
1620 1621 1622 1623 1624
		}
		let command = CommandConfiguration.from(config, context);
		if (command) {
			result.command = command;
		}
1625
		Globals.fillDefaults(result, context);
1626 1627 1628 1629 1630
		Globals.freeze(result);
		return result;
	}

	export function fromBase(this: void, config: BaseTaskRunnerConfiguration, context: ParseContext): Globals {
1631 1632 1633 1634 1635 1636 1637
		let result: Globals = {};
		if (config.suppressTaskName !== void 0) {
			result.suppressTaskName = !!config.suppressTaskName;
		}
		if (config.promptOnClose !== void 0) {
			result.promptOnClose = !!config.promptOnClose;
		}
1638 1639 1640
		if (config.problemMatcher) {
			result.problemMatcher = ProblemMatcherConverter.from(config.problemMatcher, context);
		}
E
Erich Gamma 已提交
1641 1642 1643
		return result;
	}

1644
	export function isEmpty(value: Globals): boolean {
D
Dirk Baeumer 已提交
1645
		return !value || value.command === void 0 && value.promptOnClose === void 0 && value.suppressTaskName === void 0;
1646 1647
	}

1648
	export function assignProperties(target: Globals, source: Globals): Globals {
1649 1650
		if (isEmpty(source)) {
			return target;
E
Erich Gamma 已提交
1651
		}
1652 1653 1654
		if (isEmpty(target)) {
			return source;
		}
1655 1656
		assignProperty(target, source, 'promptOnClose');
		assignProperty(target, source, 'suppressTaskName');
1657
		return target;
E
Erich Gamma 已提交
1658 1659
	}

1660
	export function fillDefaults(value: Globals, context: ParseContext): void {
1661 1662 1663
		if (!value) {
			return;
		}
1664
		CommandConfiguration.fillDefaults(value.command, context);
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
		if (value.suppressTaskName === void 0) {
			value.suppressTaskName = false;
		}
		if (value.promptOnClose === void 0) {
			value.promptOnClose = true;
		}
	}

	export function freeze(value: Globals): void {
		Object.freeze(value);
		if (value.command) {
			CommandConfiguration.freeze(value.command);
		}
	}
}

1681 1682
export namespace ExecutionEngine {

1683
	export function from(config: ExternalTaskRunnerConfiguration): Tasks.ExecutionEngine {
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
		let runner = config.runner || config._runner;
		let result: Tasks.ExecutionEngine;
		if (runner) {
			switch (runner) {
				case 'terminal':
					result = Tasks.ExecutionEngine.Terminal;
					break;
				case 'process':
					result = Tasks.ExecutionEngine.Process;
					break;
			}
		}
		let schemaVersion = JsonSchemaVersion.from(config);
		if (schemaVersion === Tasks.JsonSchemaVersion.V0_1_0) {
			return result || Tasks.ExecutionEngine.Process;
		} else if (schemaVersion === Tasks.JsonSchemaVersion.V2_0_0) {
			return Tasks.ExecutionEngine.Terminal;
		} else {
			throw new Error('Shouldn\'t happen.');
		}
1704
	}
1705
}
1706

1707 1708
export namespace JsonSchemaVersion {

1709
	const _default: Tasks.JsonSchemaVersion = Tasks.JsonSchemaVersion.V2_0_0;
1710

1711 1712 1713
	export function from(config: ExternalTaskRunnerConfiguration): Tasks.JsonSchemaVersion {
		let version = config.version;
		if (!version) {
1714
			return _default;
1715 1716 1717 1718
		}
		switch (version) {
			case '0.1.0':
				return Tasks.JsonSchemaVersion.V0_1_0;
1719
			case '2.0.0':
1720
				return Tasks.JsonSchemaVersion.V2_0_0;
1721 1722
			default:
				return _default;
1723
		}
1724 1725 1726
	}
}

1727 1728
export interface ParseResult {
	validationStatus: ValidationStatus;
1729 1730
	custom: Tasks.CustomTask[];
	configured: Tasks.ConfiguringTask[];
1731
	engine: Tasks.ExecutionEngine;
1732 1733
}

1734
export interface IProblemReporter extends IProblemReporterBase {
1735 1736
}

1737 1738 1739 1740 1741
class UUIDMap {

	private last: IStringDictionary<string | string[]>;
	private current: IStringDictionary<string | string[]>;

1742
	constructor(other?: UUIDMap) {
1743
		this.current = Object.create(null);
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
		if (other) {
			for (let key of Object.keys(other.current)) {
				let value = other.current[key];
				if (Array.isArray(value)) {
					this.current[key] = value.slice();
				} else {
					this.current[key] = value;
				}
			}
		}
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
	}

	public start(): void {
		this.last = this.current;
		this.current = Object.create(null);
	}

	public getUUID(identifier: string): string {
		let lastValue = this.last[identifier];
		let result: string;
		if (lastValue !== void 0) {
			if (Array.isArray(lastValue)) {
				result = lastValue.shift();
				if (lastValue.length === 0) {
					delete this.last[identifier];
				}
			} else {
				result = lastValue;
				delete this.last[identifier];
			}
		}
		if (result === void 0) {
			result = UUID.generateUuid();
		}
		let currentValue = this.current[identifier];
		if (currentValue === void 0) {
			this.current[identifier] = result;
		} else {
			if (Array.isArray(currentValue)) {
				currentValue.push(result);
			} else {
				let arrayValue: string[] = [currentValue];
				arrayValue.push(result);
				this.current[identifier] = arrayValue;
			}
		}
		return result;
	}

	public finish(): void {
		this.last = undefined;
	}
}

1798 1799
class ConfigurationParser {

S
Sandeep Somavarapu 已提交
1800
	private workspaceFolder: IWorkspaceFolder;
1801
	private problemReporter: IProblemReporter;
1802
	private uuidMap: UUIDMap;
1803
	private platform: Platform;
1804

1805
	constructor(workspaceFolder: IWorkspaceFolder, platform: Platform, problemReporter: IProblemReporter, uuidMap: UUIDMap) {
1806
		this.workspaceFolder = workspaceFolder;
1807
		this.platform = platform;
1808
		this.problemReporter = problemReporter;
1809
		this.uuidMap = uuidMap;
1810 1811 1812
	}

	public run(fileConfig: ExternalTaskRunnerConfiguration): ParseResult {
1813
		let engine = ExecutionEngine.from(fileConfig);
1814
		let schemaVersion = JsonSchemaVersion.from(fileConfig);
1815
		let context: ParseContext = {
1816
			workspaceFolder: this.workspaceFolder,
1817
			problemReporter: this.problemReporter,
1818
			uuidMap: this.uuidMap,
1819
			namedProblemMatchers: undefined,
1820
			engine,
1821 1822
			schemaVersion,
			platform: this.platform
1823 1824
		};
		let taskParseResult = this.createTaskRunnerConfiguration(fileConfig, context);
1825
		return {
1826
			validationStatus: this.problemReporter.status,
1827 1828
			custom: taskParseResult.custom,
			configured: taskParseResult.configured,
1829
			engine
1830 1831 1832
		};
	}

1833
	private createTaskRunnerConfiguration(fileConfig: ExternalTaskRunnerConfiguration, context: ParseContext): TaskParseResult {
1834
		let globals = Globals.from(fileConfig, context);
1835
		if (this.problemReporter.status.isFatal()) {
1836
			return { custom: [], configured: [] };
1837 1838
		}
		context.namedProblemMatchers = ProblemMatcherConverter.namedFrom(fileConfig.declares, context);
1839
		let globalTasks: Tasks.CustomTask[];
1840
		let externalGlobalTasks: (ConfiguringTask | CustomTask)[];
1841
		if (fileConfig.windows && context.platform === Platform.Windows) {
1842
			globalTasks = TaskParser.from(fileConfig.windows.tasks, globals, context).custom;
1843
			externalGlobalTasks = fileConfig.windows.tasks;
1844
		} else if (fileConfig.osx && context.platform === Platform.Mac) {
1845
			globalTasks = TaskParser.from(fileConfig.osx.tasks, globals, context).custom;
1846
			externalGlobalTasks = fileConfig.osx.tasks;
1847
		} else if (fileConfig.linux && context.platform === Platform.Linux) {
1848
			globalTasks = TaskParser.from(fileConfig.linux.tasks, globals, context).custom;
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
			externalGlobalTasks = fileConfig.linux.tasks;
		}
		if (context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0 && globalTasks && globalTasks.length > 0 && externalGlobalTasks && externalGlobalTasks.length > 0) {
			let taskContent: string[] = [];
			for (let task of externalGlobalTasks) {
				taskContent.push(JSON.stringify(task, null, 4));
			}
			context.problemReporter.error(
				nls.localize(
					'TaskParse.noOsSpecificGlobalTasks',
1859
					'Task version 2.0.0 doesn\'t support global OS specific tasks. Convert them to a task with a OS specific command. Affected tasks are:\n{0}', taskContent.join('\n'))
1860
			);
1861 1862
		}

1863
		let result: TaskParseResult = { custom: undefined, configured: undefined };
1864
		if (fileConfig.tasks) {
1865
			result = TaskParser.from(fileConfig.tasks, globals, context);
1866
		}
1867
		if (globalTasks) {
1868
			result.custom = TaskParser.assignTasks(result.custom, globalTasks);
1869 1870
		}

1871
		if ((!result.custom || result.custom.length === 0) && (globals.command && globals.command.name)) {
1872
			let matchers: ProblemMatcher[] = ProblemMatcherConverter.from(fileConfig.problemMatcher, context);
1873
			let isBackground = fileConfig.isBackground ? !!fileConfig.isBackground : fileConfig.isWatching ? !!fileConfig.isWatching : undefined;
1874
			let name = Tasks.CommandString.value(globals.command.name);
1875
			let task: Tasks.CustomTask = {
1876
				_id: context.uuidMap.getUUID(name),
M
Matt Bierner 已提交
1877
				_source: Objects.assign({} as Tasks.WorkspaceTaskSource, source, { config: { index: -1, element: fileConfig, workspaceFolder: context.workspaceFolder } }),
1878
				_label: name,
1879
				type: Tasks.CUSTOMIZED_TASK_TYPE,
1880 1881
				name: name,
				identifier: name,
1882
				group: Tasks.TaskGroup.Build,
1883 1884
				command: {
					name: undefined,
1885
					runtime: undefined,
1886
					presentation: undefined,
1887 1888
					suppressTaskName: true
				},
1889
				isBackground: isBackground,
1890 1891
				problemMatchers: matchers,
				hasDefinedMatchers: false,
1892
				runOptions: { reevaluateOnRerun: true },
1893
			};
1894 1895 1896
			let value = GroupKind.from(fileConfig.group);
			if (value) {
				task.group = value[0];
1897
				task.groupType = value[1];
1898 1899 1900
			} else if (fileConfig.group === 'none') {
				task.group = undefined;
			}
1901 1902 1903
			CustomTask.fillGlobals(task, globals);
			CustomTask.fillDefaults(task, context);
			result.custom = [task];
1904
		}
1905 1906
		result.custom = result.custom || [];
		result.configured = result.configured || [];
1907
		return result;
1908
	}
E
Erich Gamma 已提交
1909 1910
}

1911
let uuidMaps: Map<string, UUIDMap> = new Map();
1912
export function parse(workspaceFolder: IWorkspaceFolder, platform: Platform, configuration: ExternalTaskRunnerConfiguration, logger: IProblemReporter): ParseResult {
1913 1914 1915 1916 1917
	let uuidMap = uuidMaps.get(workspaceFolder.uri.toString());
	if (!uuidMap) {
		uuidMap = new UUIDMap();
		uuidMaps.set(workspaceFolder.uri.toString(), uuidMap);
	}
1918 1919
	try {
		uuidMap.start();
1920
		return (new ConfigurationParser(workspaceFolder, platform, logger, uuidMap)).run(configuration);
1921 1922 1923
	} finally {
		uuidMap.finish();
	}
1924 1925
}

1926
export function createCustomTask(contributedTask: Tasks.ContributedTask, configuredProps: Tasks.ConfigurationProperties & { _id: string; _source: Tasks.WorkspaceTaskSource }): Tasks.CustomTask {
1927 1928 1929
	return CustomTask.createCustomTask(contributedTask, configuredProps);
}

D
Dirk Baeumer 已提交
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
/*
class VersionConverter {
	constructor(private problemReporter: IProblemReporter) {
	}

	public convert(fromConfig: ExternalTaskRunnerConfiguration): ExternalTaskRunnerConfiguration {
		let result: ExternalTaskRunnerConfiguration;
		result.version = '2.0.0';
		if (Array.isArray(fromConfig.tasks)) {

		} else {
			result.tasks = [];
		}


		return result;
	}

	private convertGlobalTask(fromConfig: ExternalTaskRunnerConfiguration): TaskDescription {
		let command: string = this.getGlobalCommand(fromConfig);
		if (!command) {
			this.problemReporter.error(nls.localize('Converter.noGlobalName', 'No global command specified. Can\'t convert to 2.0.0 version.'));
			return undefined;
		}
		let result: TaskDescription = {
			taskName: command
		};
		if (fromConfig.isShellCommand) {
			result.type = 'shell';
		} else {
			result.type = 'process';
			result.args = fromConfig.args;
		}
		if (fromConfig.)

		return result;
	}

	private getGlobalCommand(fromConfig: ExternalTaskRunnerConfiguration): string {
		if (fromConfig.command) {
			return fromConfig.command;
		} else if (fromConfig.windows && fromConfig.windows.command) {
			return fromConfig.windows.command;
		} else if (fromConfig.osx && fromConfig.osx.command) {
			return fromConfig.osx.command;
		} else if (fromConfig.linux && fromConfig.linux.command) {
			return fromConfig.linux.command;
		} else {
			return undefined;
		}
	}

	private createCommandLine(command: string, args: string[], isWindows: boolean): string {
		let result: string[];
		let commandHasSpace = false;
		let argHasSpace = false;
		if (TaskDescription.hasUnescapedSpaces(command)) {
			result.push(`"${command}"`);
			commandHasSpace = true;
		} else {
			result.push(command);
		}
		if (args) {
			for (let arg of args) {
				if (TaskDescription.hasUnescapedSpaces(arg)) {
					result.push(`"${arg}"`);
					argHasSpace= true;
				} else {
					result.push(arg);
				}
			}
		}
		return result.join(' ');
	}

}
J
Johannes Rieken 已提交
2006
*/