taskConfiguration.ts 67.3 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 { IJSONSchemaMap } from 'vs/base/common/jsonSchema';
11
import { Platform } from 'vs/base/common/platform';
E
Erich Gamma 已提交
12 13 14
import * as Types from 'vs/base/common/types';
import * as UUID from 'vs/base/common/uuid';

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

S
Sandeep Somavarapu 已提交
21
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
22 23
import * as Tasks from './tasks';
import { TaskDefinitionRegistry } from './taskDefinitionRegistry';
A
Alex Ross 已提交
24
import { ConfiguredInput } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
25

26

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
	/**
	 * Controls whether the problems panel is revealed when running this task or not.
	 * Defaults to `RevealKind.Never`.
	 */
100
	revealProblems?: string;
101

D
Dirk Baeumer 已提交
102
	/**
103 104 105 106 107 108 109 110 111 112 113
	 * 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 已提交
114
	 */
115
	panel?: string;
116 117 118 119

	/**
	 * Controls whether to show the "Terminal will be reused by tasks, press any key to close it" message.
	 */
D
Dirk Baeumer 已提交
120
	showReuseMessage?: boolean;
121 122 123 124

	/**
	 * Controls whether the terminal should be cleared before running the task.
	 */
125
	clear?: boolean;
126 127 128 129 130

	/**
	 * Controls whether the task is executed in a specific terminal group using split panes.
	 */
	group?: string;
131 132
}

133
export interface RunOptionsConfig {
134
	reevaluateOnRerun?: boolean;
A
Alex Ross 已提交
135
	runOn?: string;
A
Alex Ross 已提交
136 137
}

138
export interface TaskIdentifier {
D
Dirk Baeumer 已提交
139
	type?: string;
140 141 142 143 144 145
	[name: string]: any;
}

export namespace TaskIdentifier {
	export function is(value: any): value is TaskIdentifier {
		let candidate: TaskIdentifier = value;
R
Rob Lourens 已提交
146
		return candidate !== undefined && Types.isString(value.type);
147
	}
148
}
D
Dirk Baeumer 已提交
149

150
export interface LegacyTaskProperties {
E
Erich Gamma 已提交
151
	/**
152 153
	 * @deprecated Use `isBackground` instead.
	 * Whether the executed command is kept alive and is watching the file system.
154
	 */
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	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;

190 191 192
	/**
	 * @deprecated Use presentation instead
	 */
193
	terminal?: PresentationOptionsConfig;
194

195 196 197 198 199 200 201 202 203 204 205 206
	/**
	 * @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;
207 208

	/**
D
Dirk Baeumer 已提交
209
	 * @deprecated use the task type instead.
210 211 212 213 214
	 * 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 已提交
215
	isShellCommand?: boolean | ShellConfiguration;
216 217
}

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

220 221 222 223
export namespace CommandString {
	export function value(value: CommandString): string {
		if (Types.isString(value)) {
			return value;
224 225
		} else if (Types.isStringArray(value)) {
			return value.join(' ');
226
		} else {
227 228 229 230 231
			if (Types.isString(value.value)) {
				return value.value;
			} else {
				return value.value.join(' ');
			}
232 233 234 235 236
		}
	}
}

export interface BaseCommandProperties {
237 238 239 240 241

	/**
	 * The command to be executed. Can be an external program or a shell
	 * command.
	 */
242
	command?: CommandString;
243 244 245 246

	/**
	 * The command options used when the command is executed. Can be omitted.
	 */
247
	options?: CommandOptionsConfig;
248 249 250 251

	/**
	 * The arguments passed to the command or additional arguments passed to the
	 * command when using a global command.
E
Erich Gamma 已提交
252
	 */
253
	args?: CommandString[];
254 255 256
}


257
export interface CommandProperties extends BaseCommandProperties {
D
Dirk Baeumer 已提交
258

259
	/**
260
	 * Windows specific command properties
261
	 */
262
	windows?: BaseCommandProperties;
263

264
	/**
265
	 * OSX specific command properties
266
	 */
267
	osx?: BaseCommandProperties;
268 269

	/**
270
	 * linux specific command properties
271
	 */
272 273
	linux?: BaseCommandProperties;
}
274

D
Dirk Baeumer 已提交
275 276
export interface GroupKind {
	kind?: string;
277
	isDefault?: boolean;
D
Dirk Baeumer 已提交
278 279
}

280
export interface ConfigurationProperties {
281
	/**
282
	 * The task's name
283
	 */
284
	taskName?: string;
E
Erich Gamma 已提交
285

286 287 288 289 290
	/**
	 * The UI label used for the task.
	 */
	label?: string;

E
Erich Gamma 已提交
291
	/**
A
Alex Ross 已提交
292
	 * An optional identifier which can be used to reference a task
293
	 * in a dependsOn or other attributes.
E
Erich Gamma 已提交
294
	 */
295
	identifier?: string;
E
Erich Gamma 已提交
296

297 298 299 300 301
	/**
	 * Whether the executed command is kept alive and runs in the background.
	 */
	isBackground?: boolean;

D
Dirk Baeumer 已提交
302 303 304 305 306
	/**
	 * Whether the task should prompt on close for confirmation if running.
	 */
	promptOnClose?: boolean;

E
Erich Gamma 已提交
307
	/**
308 309
	 * Defines the group the task belongs too.
	 */
D
Dirk Baeumer 已提交
310
	group?: string | GroupKind;
311 312

	/**
313
	 * The other tasks the task depend on
E
Erich Gamma 已提交
314
	 */
315
	dependsOn?: string | TaskIdentifier | Array<string | TaskIdentifier>;
E
Erich Gamma 已提交
316

317 318 319 320 321
	/**
	 * The order the dependsOn tasks should be executed in.
	 */
	dependsOrder?: string;

E
Erich Gamma 已提交
322
	/**
323
	 * Controls the behavior of the used terminal
E
Erich Gamma 已提交
324
	 */
325
	presentation?: PresentationOptionsConfig;
E
Erich Gamma 已提交
326

327 328 329
	/**
	 * Controls shell options.
	 */
330
	options?: CommandOptionsConfig;
331

E
Erich Gamma 已提交
332
	/**
333 334
	 * The problem matcher(s) to use to capture problems in the tasks
	 * output.
E
Erich Gamma 已提交
335
	 */
336
	problemMatcher?: ProblemMatcherConfig.ProblemMatcherType;
A
Alex Ross 已提交
337 338 339 340

	/**
	 * Task run options. Control run related properties.
	 */
341
	runOptions?: RunOptionsConfig;
342
}
E
Erich Gamma 已提交
343

344
export interface CustomTask extends CommandProperties, ConfigurationProperties {
E
Erich Gamma 已提交
345
	/**
346
	 * Custom tasks have the type CUSTOMIZED_TASK_TYPE
E
Erich Gamma 已提交
347
	 */
348
	type?: string;
E
Erich Gamma 已提交
349

350
}
351

352
export interface ConfiguringTask extends ConfigurationProperties {
E
Erich Gamma 已提交
353
	/**
354
	 * The contributed type of the task
E
Erich Gamma 已提交
355
	 */
356
	type?: string;
E
Erich Gamma 已提交
357 358 359 360 361
}

/**
 * The base task runner configuration
 */
362
export interface BaseTaskRunnerConfiguration {
E
Erich Gamma 已提交
363 364 365 366 367

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

	/**
371 372
	 * @deprecated Use type instead
	 *
E
Erich Gamma 已提交
373 374 375 376 377 378 379
	 * 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;

380 381 382 383 384
	/**
	 * The task type
	 */
	type?: string;

E
Erich Gamma 已提交
385 386 387
	/**
	 * The command options used when the command is executed. Can be omitted.
	 */
388
	options?: CommandOptionsConfig;
E
Erich Gamma 已提交
389 390 391 392

	/**
	 * The arguments passed to the command. Can be omitted.
	 */
393
	args?: CommandString[];
E
Erich Gamma 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

	/**
	 * 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;

411 412 413 414
	/**
	 * The group
	 */
	group?: string | GroupKind;
D
Dirk Baeumer 已提交
415 416 417
	/**
	 * Controls the behavior of the used terminal
	 */
418
	presentation?: PresentationOptionsConfig;
D
Dirk Baeumer 已提交
419

E
Erich Gamma 已提交
420 421 422 423 424 425 426 427 428 429 430 431
	/**
	 * 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 已提交
432
	taskSelector?: string;
E
Erich Gamma 已提交
433 434

	/**
A
Alex Ross 已提交
435
	 * The problem matcher(s) to used if a global command is executed (e.g. no tasks
E
Erich Gamma 已提交
436 437 438 439 440 441
	 * are defined). A tasks.json file can either contain a global problemMatcher
	 * property or a tasks property but not both.
	 */
	problemMatcher?: ProblemMatcherConfig.ProblemMatcherType;

	/**
442 443
	 * @deprecated Use `isBackground` instead.
	 *
E
Erich Gamma 已提交
444
	 * Specifies whether a global command is a watching the filesystem. A task.json
445
	 * file can either contain a global isWatching property or a tasks property
E
Erich Gamma 已提交
446 447 448 449
	 * but not both.
	 */
	isWatching?: boolean;

450 451 452 453 454
	/**
	 * Specifies whether a global command is a background task.
	 */
	isBackground?: boolean;

D
Dirk Baeumer 已提交
455 456 457 458 459
	/**
	 * Whether the task should prompt on close for confirmation if running.
	 */
	promptOnClose?: boolean;

E
Erich Gamma 已提交
460 461 462 463
	/**
	 * The configuration of the available tasks. A tasks.json file can either
	 * contain a global problemMatcher property or a tasks property but not both.
	 */
464
	tasks?: Array<CustomTask | ConfiguringTask>;
E
Erich Gamma 已提交
465 466

	/**
467
	 * Problem matcher declarations.
E
Erich Gamma 已提交
468 469
	 */
	declares?: ProblemMatcherConfig.NamedProblemMatcher[];
A
Alex Ross 已提交
470 471

	/**
472
	 * Optional user input variables.
A
Alex Ross 已提交
473 474
	 */
	inputs?: ConfiguredInput[];
E
Erich Gamma 已提交
475 476 477 478 479 480 481 482
}

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

483 484
	_runner?: string;

485 486 487 488 489
	/**
	 * Determines the runner to use
	 */
	runner?: string;

E
Erich Gamma 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	/**
	 * The config's version number
	 */
	version: string;

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

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

	/**
A
Alex Ross 已提交
506
	 * Linux specific task configuration
E
Erich Gamma 已提交
507 508 509 510 511 512 513 514 515 516 517
	 */
	linux?: BaseTaskRunnerConfiguration;
}

enum ProblemMatcherKind {
	Unknown,
	String,
	ProblemMatcher,
	Array
}

518 519
const EMPTY_ARRAY: any[] = [];
Object.freeze(EMPTY_ARRAY);
E
Erich Gamma 已提交
520

521
function assignProperty<T, K extends keyof T>(target: T, source: Partial<T>, key: K) {
A
Alex Ross 已提交
522
	const sourceAtKey = source[key];
R
Rob Lourens 已提交
523
	if (sourceAtKey !== undefined) {
A
Alex Ross 已提交
524
		target[key] = sourceAtKey!;
525
	}
E
Erich Gamma 已提交
526 527
}

528
function fillProperty<T, K extends keyof T>(target: T, source: Partial<T>, key: K) {
A
Alex Ross 已提交
529
	const sourceAtKey = source[key];
R
Rob Lourens 已提交
530
	if (target[key] === undefined && sourceAtKey !== undefined) {
A
Alex Ross 已提交
531
		target[key] = sourceAtKey!;
532 533 534 535
	}
}


536
interface ParserType<T> {
A
Alex Ross 已提交
537 538 539 540 541
	isEmpty(value: T | undefined): boolean;
	assignProperties(target: T | undefined, source: T | undefined): T | undefined;
	fillProperties(target: T | undefined, source: T | undefined): T | undefined;
	fillDefaults(value: T | undefined, context: ParseContext): T | undefined;
	freeze(value: T): Readonly<T> | undefined;
542 543 544 545 546 547 548 549
}

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


550
function _isEmpty<T>(this: void, value: T | undefined, properties: MetaData<T, any>[] | undefined): boolean {
R
Rob Lourens 已提交
551
	if (value === undefined || value === null || properties === undefined) {
552 553 554 555
		return true;
	}
	for (let meta of properties) {
		let property = value[meta.property];
R
Rob Lourens 已提交
556 557
		if (property !== undefined && property !== null) {
			if (meta.type !== undefined && !meta.type.isEmpty(property)) {
558 559 560 561 562 563 564 565 566
				return false;
			} else if (!Array.isArray(property) || property.length > 0) {
				return false;
			}
		}
	}
	return true;
}

567 568
function _assignProperties<T>(this: void, target: T | undefined, source: T | undefined, properties: MetaData<T, any>[]): T | undefined {
	if (!source || _isEmpty(source, properties)) {
569 570
		return target;
	}
571
	if (!target || _isEmpty(target, properties)) {
572 573 574 575 576
		return source;
	}
	for (let meta of properties) {
		let property = meta.property;
		let value: any;
R
Rob Lourens 已提交
577
		if (meta.type !== undefined) {
578 579 580 581
			value = meta.type.assignProperties(target[property], source[property]);
		} else {
			value = source[property];
		}
R
Rob Lourens 已提交
582
		if (value !== undefined && value !== null) {
583 584 585 586 587 588
			target[property] = value;
		}
	}
	return target;
}

589 590
function _fillProperties<T>(this: void, target: T | undefined, source: T | undefined, properties: MetaData<T, any>[] | undefined): T | undefined {
	if (!source || _isEmpty(source, properties)) {
591 592
		return target;
	}
593
	if (!target || _isEmpty(target, properties)) {
594 595
		return source;
	}
A
Alex Ross 已提交
596
	for (let meta of properties!) {
597 598 599 600
		let property = meta.property;
		let value: any;
		if (meta.type) {
			value = meta.type.fillProperties(target[property], source[property]);
R
Rob Lourens 已提交
601
		} else if (target[property] === undefined) {
602 603
			value = source[property];
		}
R
Rob Lourens 已提交
604
		if (value !== undefined && value !== null) {
605 606 607 608 609 610
			target[property] = value;
		}
	}
	return target;
}

611
function _fillDefaults<T>(this: void, target: T | undefined, defaults: T | undefined, properties: MetaData<T, any>[], context: ParseContext): T | undefined {
612 613 614
	if (target && Object.isFrozen(target)) {
		return target;
	}
615
	if (target === undefined || target === null || defaults === undefined || defaults === null) {
R
Rob Lourens 已提交
616
		if (defaults !== undefined && defaults !== null) {
J
Johannes Rieken 已提交
617
			return Objects.deepClone(defaults);
618 619 620 621 622 623
		} else {
			return undefined;
		}
	}
	for (let meta of properties) {
		let property = meta.property;
R
Rob Lourens 已提交
624
		if (target[property] !== undefined) {
625 626 627 628 629 630 631 632 633
			continue;
		}
		let value: any;
		if (meta.type) {
			value = meta.type.fillDefaults(target[property], context);
		} else {
			value = defaults[property];
		}

R
Rob Lourens 已提交
634
		if (value !== undefined && value !== null) {
635 636 637 638 639 640
			target[property] = value;
		}
	}
	return target;
}

A
Alex Ross 已提交
641
function _freeze<T>(this: void, target: T, properties: MetaData<T, any>[]): Readonly<T> | undefined {
R
Rob Lourens 已提交
642
	if (target === undefined || target === null) {
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
		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 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
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 {
678
			reevaluateOnRerun: value ? value.reevaluateOnRerun : true,
A
Alex Ross 已提交
679 680 681 682 683
			runOn: value ? RunOnOptions.fromString(value.runOn) : Tasks.RunOnOptions.default
		};
	}
}

684
interface ParseContext {
S
Sandeep Somavarapu 已提交
685
	workspaceFolder: IWorkspaceFolder;
686
	problemReporter: IProblemReporter;
687
	namedProblemMatchers: IStringDictionary<NamedProblemMatcher>;
688
	uuidMap: UUIDMap;
689 690
	engine: Tasks.ExecutionEngine;
	schemaVersion: Tasks.JsonSchemaVersion;
691
	platform: Platform;
692
	taskLoadIssues: string[];
E
Erich Gamma 已提交
693 694
}

695

696
namespace ShellConfiguration {
697

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

700 701
	export function is(value: any): value is ShellConfiguration {
		let candidate: ShellConfiguration = value;
D
Dirk Baeumer 已提交
702
		return candidate && (Types.isString(candidate.executable) || Types.isStringArray(candidate.args));
703 704
	}

A
Alex Ross 已提交
705
	export function from(this: void, config: ShellConfiguration | undefined, context: ParseContext): Tasks.ShellConfiguration | undefined {
706 707 708
		if (!is(config)) {
			return undefined;
		}
D
Dirk Baeumer 已提交
709
		let result: ShellConfiguration = {};
R
Rob Lourens 已提交
710
		if (config.executable !== undefined) {
D
Dirk Baeumer 已提交
711 712
			result.executable = config.executable;
		}
R
Rob Lourens 已提交
713
		if (config.args !== undefined) {
714 715
			result.args = config.args.slice();
		}
R
Rob Lourens 已提交
716
		if (config.quoting !== undefined) {
717 718 719
			result.quoting = Objects.deepClone(config.quoting);
		}

720 721 722
		return result;
	}

723 724
	export function isEmpty(this: void, value: Tasks.ShellConfiguration): boolean {
		return _isEmpty(value, properties);
725 726
	}

A
Alex Ross 已提交
727
	export function assignProperties(this: void, target: Tasks.ShellConfiguration | undefined, source: Tasks.ShellConfiguration | undefined): Tasks.ShellConfiguration | undefined {
728
		return _assignProperties(target, source, properties);
729 730
	}

731
	export function fillProperties(this: void, target: Tasks.ShellConfiguration, source: Tasks.ShellConfiguration): Tasks.ShellConfiguration | undefined {
732
		return _fillProperties(target, source, properties);
733 734
	}

735 736
	export function fillDefaults(this: void, value: Tasks.ShellConfiguration, context: ParseContext): Tasks.ShellConfiguration {
		return value;
737 738
	}

A
Alex Ross 已提交
739
	export function freeze(this: void, value: Tasks.ShellConfiguration): Readonly<Tasks.ShellConfiguration> | undefined {
740
		if (!value) {
741
			return undefined;
742
		}
743
		return Object.freeze(value);
744 745 746
	}
}

747
namespace CommandOptions {
748 749

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

A
Alex Ross 已提交
752
	export function from(this: void, options: CommandOptionsConfig, context: ParseContext): Tasks.CommandOptions | undefined {
753
		let result: Tasks.CommandOptions = {};
R
Rob Lourens 已提交
754
		if (options.cwd !== undefined) {
755 756 757
			if (Types.isString(options.cwd)) {
				result.cwd = options.cwd;
			} else {
758
				context.taskLoadIssues.push(nls.localize('ConfigurationParser.invalidCWD', 'Warning: options.cwd must be of type string. Ignoring value {0}\n', options.cwd));
759 760
			}
		}
R
Rob Lourens 已提交
761
		if (options.env !== undefined) {
J
Johannes Rieken 已提交
762
			result.env = Objects.deepClone(options.env);
763
		}
D
Dirk Baeumer 已提交
764
		result.shell = ShellConfiguration.from(options.shell, context);
765
		return isEmpty(result) ? undefined : result;
E
Erich Gamma 已提交
766 767
	}

A
Alex Ross 已提交
768
	export function isEmpty(value: Tasks.CommandOptions | undefined): boolean {
769
		return _isEmpty(value, properties);
E
Erich Gamma 已提交
770 771
	}

A
Alex Ross 已提交
772
	export function assignProperties(target: Tasks.CommandOptions | undefined, source: Tasks.CommandOptions | undefined): Tasks.CommandOptions | undefined {
R
Rob Lourens 已提交
773
		if ((source === undefined) || isEmpty(source)) {
774
			return target;
E
Erich Gamma 已提交
775
		}
R
Rob Lourens 已提交
776
		if ((target === undefined) || isEmpty(target)) {
777
			return source;
E
Erich Gamma 已提交
778
		}
779
		assignProperty(target, source, 'cwd');
R
Rob Lourens 已提交
780
		if (target.env === undefined) {
781
			target.env = source.env;
R
Rob Lourens 已提交
782
		} else if (source.env !== undefined) {
783
			let env: { [key: string]: string; } = Object.create(null);
R
Rob Lourens 已提交
784
			if (target.env !== undefined) {
A
Alex Ross 已提交
785 786
				Object.keys(target.env).forEach(key => env[key] = target.env![key]);
			}
R
Rob Lourens 已提交
787
			if (source.env !== undefined) {
A
Alex Ross 已提交
788 789
				Object.keys(source.env).forEach(key => env[key] = source.env![key]);
			}
790 791
			target.env = env;
		}
792 793 794 795
		target.shell = ShellConfiguration.assignProperties(target.shell, source.shell);
		return target;
	}

A
Alex Ross 已提交
796
	export function fillProperties(target: Tasks.CommandOptions | undefined, source: Tasks.CommandOptions | undefined): Tasks.CommandOptions | undefined {
797
		return _fillProperties(target, source, properties);
E
Erich Gamma 已提交
798 799
	}

A
Alex Ross 已提交
800
	export function fillDefaults(value: Tasks.CommandOptions | undefined, context: ParseContext): Tasks.CommandOptions | undefined {
801
		return _fillDefaults(value, defaults, properties, context);
802 803
	}

A
Alex Ross 已提交
804
	export function freeze(value: Tasks.CommandOptions): Readonly<Tasks.CommandOptions> | undefined {
805
		return _freeze(value, properties);
E
Erich Gamma 已提交
806
	}
807
}
E
Erich Gamma 已提交
808

809
namespace CommandConfiguration {
810

811
	export namespace PresentationOptions {
812
		const properties: MetaData<Tasks.PresentationOptions, void>[] = [{ property: 'echo' }, { property: 'reveal' }, { property: 'revealProblems' }, { property: 'focus' }, { property: 'panel' }, { property: 'showReuseMessage' }, { property: 'clear' }, { property: 'group' }];
813

814
		interface PresentationOptionsShape extends LegacyCommandProperties {
815
			presentation?: PresentationOptionsConfig;
816 817
		}

A
Alex Ross 已提交
818
		export function from(this: void, config: PresentationOptionsShape, context: ParseContext): Tasks.PresentationOptions | undefined {
819 820
			let echo: boolean;
			let reveal: Tasks.RevealKind;
821
			let revealProblems: Tasks.RevealProblemKind;
822
			let focus: boolean;
823
			let panel: Tasks.PanelKind;
D
Dirk Baeumer 已提交
824
			let showReuseMessage: boolean;
825
			let clear: boolean;
826
			let group: string | undefined;
A
Alex Ross 已提交
827
			let hasProps = false;
D
Dirk Baeumer 已提交
828 829
			if (Types.isBoolean(config.echoCommand)) {
				echo = config.echoCommand;
A
Alex Ross 已提交
830
				hasProps = true;
D
Dirk Baeumer 已提交
831 832 833
			}
			if (Types.isString(config.showOutput)) {
				reveal = Tasks.RevealKind.fromString(config.showOutput);
A
Alex Ross 已提交
834
				hasProps = true;
D
Dirk Baeumer 已提交
835
			}
836 837 838 839
			let presentation = config.presentation || config.terminal;
			if (presentation) {
				if (Types.isBoolean(presentation.echo)) {
					echo = presentation.echo;
D
Dirk Baeumer 已提交
840
				}
841 842
				if (Types.isString(presentation.reveal)) {
					reveal = Tasks.RevealKind.fromString(presentation.reveal);
D
Dirk Baeumer 已提交
843
				}
844 845
				if (Types.isString(presentation.revealProblems)) {
					revealProblems = Tasks.RevealProblemKind.fromString(presentation.revealProblems);
846
				}
847 848
				if (Types.isBoolean(presentation.focus)) {
					focus = presentation.focus;
849
				}
850 851
				if (Types.isString(presentation.panel)) {
					panel = Tasks.PanelKind.fromString(presentation.panel);
852
				}
D
Dirk Baeumer 已提交
853 854
				if (Types.isBoolean(presentation.showReuseMessage)) {
					showReuseMessage = presentation.showReuseMessage;
855
				}
856 857
				if (Types.isBoolean(presentation.clear)) {
					clear = presentation.clear;
858
				}
859 860 861
				if (Types.isString(presentation.group)) {
					group = presentation.group;
				}
A
Alex Ross 已提交
862
				hasProps = true;
D
Dirk Baeumer 已提交
863
			}
A
Alex Ross 已提交
864
			if (!hasProps) {
D
Dirk Baeumer 已提交
865 866
				return undefined;
			}
867
			return { echo: echo!, reveal: reveal!, revealProblems: revealProblems!, focus: focus!, panel: panel!, showReuseMessage: showReuseMessage!, clear: clear!, group };
D
Dirk Baeumer 已提交
868 869
		}

A
Alex Ross 已提交
870
		export function assignProperties(target: Tasks.PresentationOptions, source: Tasks.PresentationOptions | undefined): Tasks.PresentationOptions | undefined {
871
			return _assignProperties(target, source, properties);
872 873
		}

A
Alex Ross 已提交
874
		export function fillProperties(target: Tasks.PresentationOptions, source: Tasks.PresentationOptions | undefined): Tasks.PresentationOptions | undefined {
875
			return _fillProperties(target, source, properties);
D
Dirk Baeumer 已提交
876 877
		}

A
Alex Ross 已提交
878
		export function fillDefaults(value: Tasks.PresentationOptions, context: ParseContext): Tasks.PresentationOptions | undefined {
879
			let defaultEcho = context.engine === Tasks.ExecutionEngine.Terminal ? true : false;
880
			return _fillDefaults(value, { echo: defaultEcho, reveal: Tasks.RevealKind.Always, revealProblems: Tasks.RevealProblemKind.Never, focus: false, panel: Tasks.PanelKind.Shared, showReuseMessage: true, clear: false }, properties, context);
D
Dirk Baeumer 已提交
881 882
		}

A
Alex Ross 已提交
883
		export function freeze(value: Tasks.PresentationOptions): Readonly<Tasks.PresentationOptions> | undefined {
884
			return _freeze(value, properties);
D
Dirk Baeumer 已提交
885 886
		}

887
		export function isEmpty(this: void, value: Tasks.PresentationOptions): boolean {
888
			return _isEmpty(value, properties);
D
Dirk Baeumer 已提交
889 890 891
		}
	}

892
	namespace ShellString {
A
Alex Ross 已提交
893
		export function from(this: void, value: CommandString | undefined): Tasks.CommandString | undefined {
R
Rob Lourens 已提交
894
			if (value === undefined || value === null) {
895 896 897 898
				return undefined;
			}
			if (Types.isString(value)) {
				return value;
899 900 901 902 903 904 905 906 907 908 909 910 911
			} 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;
				}
912 913 914 915
			}
		}
	}

A
Alex Ross 已提交
916
	interface BaseCommandConfigurationShape extends BaseCommandProperties, LegacyCommandProperties {
917 918
	}

A
Alex Ross 已提交
919 920 921 922
	interface CommandConfigurationShape extends BaseCommandConfigurationShape {
		windows?: BaseCommandConfigurationShape;
		osx?: BaseCommandConfigurationShape;
		linux?: BaseCommandConfigurationShape;
923 924 925 926
	}

	const properties: MetaData<Tasks.CommandConfiguration, any>[] = [
		{ property: 'runtime' }, { property: 'name' }, { property: 'options', type: CommandOptions },
927
		{ property: 'args' }, { property: 'taskSelector' }, { property: 'suppressTaskName' },
928
		{ property: 'presentation', type: PresentationOptions }
929 930
	];

A
Alex Ross 已提交
931
	export function from(this: void, config: CommandConfigurationShape, context: ParseContext): Tasks.CommandConfiguration | undefined {
A
Alex Ross 已提交
932
		let result: Tasks.CommandConfiguration = fromBase(config, context)!;
933

A
Alex Ross 已提交
934
		let osConfig: Tasks.CommandConfiguration | undefined = undefined;
935
		if (config.windows && context.platform === Platform.Windows) {
936
			osConfig = fromBase(config.windows, context);
937
		} else if (config.osx && context.platform === Platform.Mac) {
938
			osConfig = fromBase(config.osx, context);
939
		} else if (config.linux && context.platform === Platform.Linux) {
940 941 942
			osConfig = fromBase(config.linux, context);
		}
		if (osConfig) {
943
			result = assignProperties(result, osConfig, context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0);
944 945 946 947
		}
		return isEmpty(result) ? undefined : result;
	}

A
Alex Ross 已提交
948
	function fromBase(this: void, config: BaseCommandConfigurationShape, context: ParseContext): Tasks.CommandConfiguration | undefined {
949
		let name: Tasks.CommandString | undefined = ShellString.from(config.command);
A
Alex Ross 已提交
950
		let runtime: Tasks.RuntimeType;
D
Dirk Baeumer 已提交
951
		if (Types.isString(config.type)) {
952
			if (config.type === 'shell' || config.type === 'process') {
A
Alex Ross 已提交
953
				runtime = Tasks.RuntimeType.fromString(config.type);
954
			}
D
Dirk Baeumer 已提交
955 956 957
		}
		let isShellConfiguration = ShellConfiguration.is(config.isShellCommand);
		if (Types.isBoolean(config.isShellCommand) || isShellConfiguration) {
A
Alex Ross 已提交
958
			runtime = Tasks.RuntimeType.Shell;
R
Rob Lourens 已提交
959
		} else if (config.isShellCommand !== undefined) {
A
Alex Ross 已提交
960
			runtime = !!config.isShellCommand ? Tasks.RuntimeType.Shell : Tasks.RuntimeType.Process;
961
		}
962

A
Alex Ross 已提交
963
		let result: Tasks.CommandConfiguration = {
964
			name: name,
A
Alex Ross 已提交
965 966 967 968
			runtime: runtime!,
			presentation: PresentationOptions.from(config, context)!
		};

R
Rob Lourens 已提交
969
		if (config.args !== undefined) {
970 971 972
			result.args = [];
			for (let arg of config.args) {
				let converted = ShellString.from(arg);
R
Rob Lourens 已提交
973
				if (converted !== undefined) {
974 975
					result.args.push(converted);
				} else {
976
					context.taskLoadIssues.push(
977 978 979 980 981
						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'
						));
982
				}
983
			}
E
Erich Gamma 已提交
984
		}
R
Rob Lourens 已提交
985
		if (config.options !== undefined) {
986
			result.options = CommandOptions.from(config.options, context);
R
Rob Lourens 已提交
987
			if (result.options && result.options.shell === undefined && isShellConfiguration) {
D
Dirk Baeumer 已提交
988
				result.options.shell = ShellConfiguration.from(config.isShellCommand as ShellConfiguration, context);
989
				if (context.engine !== Tasks.ExecutionEngine.Terminal) {
990
					context.taskLoadIssues.push(nls.localize('ConfigurationParser.noShell', 'Warning: shell configuration is only supported when executing tasks in the terminal.'));
D
Dirk Baeumer 已提交
991 992
				}
			}
E
Erich Gamma 已提交
993
		}
A
Alex Ross 已提交
994

995 996
		if (Types.isString(config.taskSelector)) {
			result.taskSelector = config.taskSelector;
E
Erich Gamma 已提交
997
		}
998 999 1000
		if (Types.isBoolean(config.suppressTaskName)) {
			result.suppressTaskName = config.suppressTaskName;
		}
A
Alex Ross 已提交
1001

1002
		return isEmpty(result) ? undefined : result;
E
Erich Gamma 已提交
1003 1004
	}

1005 1006 1007 1008
	export function hasCommand(value: Tasks.CommandConfiguration): boolean {
		return value && !!value.name;
	}

A
Alex Ross 已提交
1009
	export function isEmpty(value: Tasks.CommandConfiguration | undefined): boolean {
1010
		return _isEmpty(value, properties);
1011 1012
	}

1013
	export function assignProperties(target: Tasks.CommandConfiguration, source: Tasks.CommandConfiguration, overwriteArgs: boolean): Tasks.CommandConfiguration {
1014 1015
		if (isEmpty(source)) {
			return target;
E
Erich Gamma 已提交
1016
		}
1017 1018
		if (isEmpty(target)) {
			return source;
E
Erich Gamma 已提交
1019
		}
1020
		assignProperty(target, source, 'name');
1021
		assignProperty(target, source, 'runtime');
1022 1023
		assignProperty(target, source, 'taskSelector');
		assignProperty(target, source, 'suppressTaskName');
R
Rob Lourens 已提交
1024 1025
		if (source.args !== undefined) {
			if (target.args === undefined || overwriteArgs) {
1026 1027 1028
				target.args = source.args;
			} else {
				target.args = target.args.concat(source.args);
E
Erich Gamma 已提交
1029 1030
			}
		}
A
Alex Ross 已提交
1031
		target.presentation = PresentationOptions.assignProperties(target.presentation!, source.presentation)!;
1032 1033 1034 1035
		target.options = CommandOptions.assignProperties(target.options, source.options);
		return target;
	}

1036
	export function fillProperties(target: Tasks.CommandConfiguration, source: Tasks.CommandConfiguration): Tasks.CommandConfiguration | undefined {
1037 1038 1039
		return _fillProperties(target, source, properties);
	}

A
Alex Ross 已提交
1040 1041
	export function fillGlobals(target: Tasks.CommandConfiguration, source: Tasks.CommandConfiguration | undefined, taskName: string | undefined): Tasks.CommandConfiguration {
		if ((source === undefined) || isEmpty(source)) {
1042 1043 1044 1045
			return target;
		}
		target = target || {
			name: undefined,
1046
			runtime: undefined,
1047
			presentation: undefined
1048
		};
R
Rob Lourens 已提交
1049
		if (target.name === undefined) {
1050 1051 1052
			fillProperty(target, source, 'name');
			fillProperty(target, source, 'taskSelector');
			fillProperty(target, source, 'suppressTaskName');
1053
			let args: Tasks.CommandString[] = source.args ? source.args.slice() : [];
A
Alex Ross 已提交
1054
			if (!target.suppressTaskName && taskName) {
R
Rob Lourens 已提交
1055
				if (target.taskSelector !== undefined) {
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
					args.push(target.taskSelector + taskName);
				} else {
					args.push(taskName);
				}
			}
			if (target.args) {
				args = args.concat(target.args);
			}
			target.args = args;
		}
1066
		fillProperty(target, source, 'runtime');
1067

A
Alex Ross 已提交
1068
		target.presentation = PresentationOptions.fillProperties(target.presentation!, source.presentation)!;
1069 1070
		target.options = CommandOptions.fillProperties(target.options, source.options);

1071
		return target;
E
Erich Gamma 已提交
1072 1073
	}

A
Alex Ross 已提交
1074
	export function fillDefaults(value: Tasks.CommandConfiguration | undefined, context: ParseContext): void {
1075 1076 1077
		if (!value || Object.isFrozen(value)) {
			return;
		}
R
Rob Lourens 已提交
1078
		if (value.name !== undefined && value.runtime === undefined) {
1079
			value.runtime = Tasks.RuntimeType.Process;
1080
		}
A
Alex Ross 已提交
1081
		value.presentation = PresentationOptions.fillDefaults(value.presentation!, context)!;
1082
		if (!isEmpty(value)) {
1083
			value.options = CommandOptions.fillDefaults(value.options, context);
1084
		}
R
Rob Lourens 已提交
1085
		if (value.args === undefined) {
1086 1087
			value.args = EMPTY_ARRAY;
		}
R
Rob Lourens 已提交
1088
		if (value.suppressTaskName === undefined) {
1089
			value.suppressTaskName = (context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0);
E
Erich Gamma 已提交
1090 1091 1092
		}
	}

A
Alex Ross 已提交
1093
	export function freeze(value: Tasks.CommandConfiguration): Readonly<Tasks.CommandConfiguration> | undefined {
1094
		return _freeze(value, properties);
E
Erich Gamma 已提交
1095
	}
1096
}
E
Erich Gamma 已提交
1097

1098 1099
namespace ProblemMatcherConverter {

A
Alex Ross 已提交
1100
	export function namedFrom(this: void, declares: ProblemMatcherConfig.NamedProblemMatcher[] | undefined, context: ParseContext): IStringDictionary<NamedProblemMatcher> {
J
Johannes Rieken 已提交
1101
		let result: IStringDictionary<NamedProblemMatcher> = Object.create(null);
1102 1103

		if (!Types.isArray(declares)) {
E
Erich Gamma 已提交
1104 1105
			return result;
		}
1106
		(<ProblemMatcherConfig.NamedProblemMatcher[]>declares).forEach((value) => {
1107
			let namedProblemMatcher = (new ProblemMatcherParser(context.problemReporter)).parse(value);
1108
			if (isNamedProblemMatcher(namedProblemMatcher)) {
E
Erich Gamma 已提交
1109
				result[namedProblemMatcher.name] = namedProblemMatcher;
1110
			} else {
1111
				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 已提交
1112 1113 1114 1115 1116
			}
		});
		return result;
	}

A
Alex Ross 已提交
1117
	export function from(this: void, config: ProblemMatcherConfig.ProblemMatcherType | undefined, context: ParseContext): ProblemMatcher[] {
1118
		let result: ProblemMatcher[] = [];
R
Rob Lourens 已提交
1119
		if (config === undefined) {
1120 1121 1122 1123
			return result;
		}
		let kind = getProblemMatcherKind(config);
		if (kind === ProblemMatcherKind.Unknown) {
1124
			context.problemReporter.warn(nls.localize(
1125
				'ConfigurationParser.unknownMatcherKind',
1126
				'Warning: the defined problem matcher is unknown. Supported types are string | ProblemMatcher | Array<string | ProblemMatcher>.\n{0}\n',
1127
				JSON.stringify(config, null, 4)));
E
Erich Gamma 已提交
1128
			return result;
1129
		} else if (kind === ProblemMatcherKind.String || kind === ProblemMatcherKind.ProblemMatcher) {
1130
			let matcher = resolveProblemMatcher(config as ProblemMatcherConfig.ProblemMatcher, context);
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
			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 已提交
1153
		} else {
1154
			return ProblemMatcherKind.Unknown;
E
Erich Gamma 已提交
1155 1156 1157
		}
	}

A
Alex Ross 已提交
1158
	function resolveProblemMatcher(this: void, value: string | ProblemMatcherConfig.ProblemMatcher, context: ParseContext): ProblemMatcher | undefined {
1159 1160 1161 1162 1163 1164
		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 已提交
1165
					return Objects.deepClone(global);
1166 1167 1168
				}
				let localProblemMatcher = context.namedProblemMatchers[variableName];
				if (localProblemMatcher) {
J
Johannes Rieken 已提交
1169
					localProblemMatcher = Objects.deepClone(localProblemMatcher);
1170 1171 1172 1173 1174
					// remove the name
					delete localProblemMatcher.name;
					return localProblemMatcher;
				}
			}
A
Alex Ross 已提交
1175
			context.taskLoadIssues.push(nls.localize('ConfigurationParser.invalidVariableReference', 'Error: Invalid problemMatcher reference: {0}\n', value));
1176 1177 1178
			return undefined;
		} else {
			let json = <ProblemMatcherConfig.ProblemMatcher>value;
1179
			return new ProblemMatcherParser(context.problemReporter).parse(json);
1180 1181 1182 1183
		}
	}
}

A
Alex Ross 已提交
1184
const source: Partial<Tasks.TaskSource> = {
1185 1186
	kind: Tasks.TaskSourceKind.Workspace,
	label: 'Workspace',
1187
	config: undefined
1188
};
1189

1190
namespace GroupKind {
A
Alex Ross 已提交
1191
	export function from(this: void, external: string | GroupKind | undefined): [string, Tasks.GroupType] | undefined {
R
Rob Lourens 已提交
1192
		if (external === undefined) {
1193 1194 1195 1196
			return undefined;
		}
		if (Types.isString(external)) {
			if (Tasks.TaskGroup.is(external)) {
1197
				return [external, Tasks.GroupType.user];
1198
			} else {
D
Dirk Baeumer 已提交
1199 1200 1201
				return undefined;
			}
		}
1202 1203 1204 1205
		if (!Types.isString(external.kind) || !Tasks.TaskGroup.is(external.kind)) {
			return undefined;
		}
		let group: string = external.kind;
1206
		let isDefault: boolean = !!external.isDefault;
1207

1208
		return [group, isDefault ? Tasks.GroupType.default : Tasks.GroupType.user];
D
Dirk Baeumer 已提交
1209
	}
1210 1211
}

1212
namespace TaskDependency {
A
Alex Ross 已提交
1213
	export function from(this: void, external: string | TaskIdentifier, context: ParseContext): Tasks.TaskDependency | undefined {
1214 1215 1216
		if (Types.isString(external)) {
			return { workspaceFolder: context.workspaceFolder, task: external };
		} else if (TaskIdentifier.is(external)) {
1217
			return { workspaceFolder: context.workspaceFolder, task: Tasks.TaskDefinition.createTaskIdentifier(external as Tasks.TaskIdentifier, context.problemReporter) };
1218 1219 1220 1221 1222 1223
		} else {
			return undefined;
		}
	}
}

1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
namespace DependsOrder {
	export function from(order: string | undefined): Tasks.DependsOrder {
		switch (order) {
			case Tasks.DependsOrder.sequence:
				return Tasks.DependsOrder.sequence;
			case Tasks.DependsOrder.parallel:
			default:
				return Tasks.DependsOrder.parallel;
		}
	}
}

1236
namespace ConfigurationProperties {
D
Dirk Baeumer 已提交
1237

1238
	const properties: MetaData<Tasks.ConfigurationProperties, any>[] = [
1239

1240 1241 1242 1243
		{ property: 'name' }, { property: 'identifier' }, { property: 'group' }, { property: 'isBackground' },
		{ property: 'promptOnClose' }, { property: 'dependsOn' },
		{ property: 'presentation', type: CommandConfiguration.PresentationOptions }, { property: 'problemMatchers' }
	];
1244

1245
	export function from(this: void, external: ConfigurationProperties & { [key: string]: any; }, context: ParseContext, includeCommandOptions: boolean, properties?: IJSONSchemaMap): Tasks.ConfigurationProperties | undefined {
1246
		if (!external) {
1247
			return undefined;
E
Erich Gamma 已提交
1248
		}
1249
		let result: Tasks.ConfigurationProperties & { [key: string]: any; } = {};
1250 1251 1252 1253 1254 1255 1256 1257 1258

		if (properties) {
			for (const propertyName of Object.keys(properties)) {
				if (external[propertyName] !== undefined) {
					result[propertyName] = Objects.deepClone(external[propertyName]);
				}
			}
		}

1259 1260 1261
		if (Types.isString(external.taskName)) {
			result.name = external.taskName;
		}
1262 1263 1264
		if (Types.isString(external.label) && context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0) {
			result.name = external.label;
		}
1265 1266 1267
		if (Types.isString(external.identifier)) {
			result.identifier = external.identifier;
		}
R
Rob Lourens 已提交
1268
		if (external.isBackground !== undefined) {
1269 1270
			result.isBackground = !!external.isBackground;
		}
R
Rob Lourens 已提交
1271
		if (external.promptOnClose !== undefined) {
1272 1273
			result.promptOnClose = !!external.promptOnClose;
		}
R
Rob Lourens 已提交
1274
		if (external.group !== undefined) {
D
Dirk Baeumer 已提交
1275 1276
			if (Types.isString(external.group) && Tasks.TaskGroup.is(external.group)) {
				result.group = external.group;
1277
				result.groupType = Tasks.GroupType.user;
D
Dirk Baeumer 已提交
1278 1279 1280 1281
			} else {
				let values = GroupKind.from(external.group);
				if (values) {
					result.group = values[0];
1282
					result.groupType = values[1];
D
Dirk Baeumer 已提交
1283 1284
				}
			}
1285
		}
R
Rob Lourens 已提交
1286
		if (external.dependsOn !== undefined) {
1287
			if (Types.isArray(external.dependsOn)) {
A
Alex Ross 已提交
1288 1289 1290 1291 1292 1293 1294
				result.dependsOn = external.dependsOn.reduce((dependencies: Tasks.TaskDependency[], item): Tasks.TaskDependency[] => {
					const dependency = TaskDependency.from(item, context);
					if (dependency) {
						dependencies.push(dependency);
					}
					return dependencies;
				}, []);
1295
			} else {
A
Alex Ross 已提交
1296 1297
				const dependsOnValue = TaskDependency.from(external.dependsOn, context);
				result.dependsOn = dependsOnValue ? [dependsOnValue] : undefined;
1298
			}
1299
		}
1300
		result.dependsOrder = DependsOrder.from(external.dependsOrder);
R
Rob Lourens 已提交
1301
		if (includeCommandOptions && (external.presentation !== undefined || (external as LegacyCommandProperties).terminal !== undefined)) {
1302 1303
			result.presentation = CommandConfiguration.PresentationOptions.from(external, context);
		}
R
Rob Lourens 已提交
1304
		if (includeCommandOptions && (external.options !== undefined)) {
1305 1306
			result.options = CommandOptions.from(external.options, context);
		}
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
		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 {

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	const grunt = 'grunt.';
	const jake = 'jake.';
	const gulp = 'gulp.';
	const npm = 'vscode.npm.';
	const typescript = 'vscode.typescript.';

	interface CustomizeShape {
		customize: string;
	}

A
Alex Ross 已提交
1330
	export function from(this: void, external: ConfiguringTask, context: ParseContext, index: number): Tasks.ConfiguringTask | undefined {
1331 1332 1333 1334
		if (!external) {
			return undefined;
		}
		let type = external.type;
1335 1336
		let customize = (external as CustomizeShape).customize;
		if (!type && !customize) {
1337
			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)));
1338 1339
			return undefined;
		}
A
Alex Ross 已提交
1340
		let typeDeclaration = type ? TaskDefinitionRegistry.get(type) : undefined;
1341
		if (!typeDeclaration) {
1342
			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);
1343 1344 1345
			context.problemReporter.error(message);
			return undefined;
		}
A
Alex Ross 已提交
1346
		let identifier: Tasks.TaskIdentifier | undefined;
1347 1348
		if (Types.isString(customize)) {
			if (customize.indexOf(grunt) === 0) {
1349
				identifier = { type: 'grunt', task: customize.substring(grunt.length) };
1350
			} else if (customize.indexOf(jake) === 0) {
1351
				identifier = { type: 'jake', task: customize.substring(jake.length) };
1352
			} else if (customize.indexOf(gulp) === 0) {
1353
				identifier = { type: 'gulp', task: customize.substring(gulp.length) };
1354
			} else if (customize.indexOf(npm) === 0) {
1355
				identifier = { type: 'npm', script: customize.substring(npm.length + 4) };
1356
			} else if (customize.indexOf(typescript) === 0) {
1357
				identifier = { type: 'typescript', tsconfig: customize.substring(typescript.length + 6) };
1358
			}
1359
		} else {
1360 1361 1362 1363
			if (Types.isString(external.type)) {
				identifier = external as Tasks.TaskIdentifier;
			}
		}
R
Rob Lourens 已提交
1364
		if (identifier === undefined) {
1365 1366 1367 1368 1369
			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;
1370
		}
1371
		let taskIdentifier: Tasks.KeyedTaskIdentifier | undefined = Tasks.TaskDefinition.createTaskIdentifier(identifier, context.problemReporter);
R
Rob Lourens 已提交
1372
		if (taskIdentifier === undefined) {
1373 1374
			context.problemReporter.error(nls.localize(
				'ConfigurationParser.incorrectType',
D
Dániel Tar 已提交
1375
				'Error: the task configuration \'{0}\' is using an unknown type. The task configuration will be ignored.', JSON.stringify(external, undefined, 0)
1376
			));
1377
			return undefined;
1378
		}
1379
		let configElement: Tasks.TaskSourceConfigElement = {
1380
			workspaceFolder: context.workspaceFolder,
1381
			file: '.vscode/tasks.json',
1382 1383 1384
			index,
			element: external
		};
A
Alex Ross 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393
		let result: Tasks.ConfiguringTask = new Tasks.ConfiguringTask(
			`${typeDeclaration.extensionId}.${taskIdentifier._key}`,
			Objects.assign({} as Tasks.WorkspaceTaskSource, source, { config: configElement }),
			undefined,
			type,
			taskIdentifier,
			RunOptions.fromConfiguration(external.runOptions),
			{}
		);
1394
		let configuration = ConfigurationProperties.from(external, context, true, typeDeclaration.properties);
1395
		if (configuration) {
1396
			result.configurationProperties = Objects.assign(result.configurationProperties, configuration);
A
Alex Ross 已提交
1397 1398
			if (result.configurationProperties.name) {
				result._label = result.configurationProperties.name;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
			} 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 已提交
1409
				}
1410
				result._label = label;
1411
			}
A
Alex Ross 已提交
1412 1413
			if (!result.configurationProperties.identifier) {
				result.configurationProperties.identifier = taskIdentifier._key;
E
Erich Gamma 已提交
1414
			}
1415 1416 1417 1418 1419 1420
		}
		return result;
	}
}

namespace CustomTask {
A
Alex Ross 已提交
1421
	export function from(this: void, external: CustomTask, context: ParseContext, index: number): Tasks.CustomTask | undefined {
1422 1423 1424 1425
		if (!external) {
			return undefined;
		}
		let type = external.type;
R
Rob Lourens 已提交
1426
		if (type === undefined || type === null) {
1427
			type = Tasks.CUSTOMIZED_TASK_TYPE;
1428
		}
1429
		if (type !== Tasks.CUSTOMIZED_TASK_TYPE && type !== 'shell' && type !== 'process') {
1430
			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)));
1431 1432 1433
			return undefined;
		}
		let taskName = external.taskName;
1434 1435 1436
		if (Types.isString(external.label) && context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0) {
			taskName = external.label;
		}
1437
		if (!taskName) {
D
Dirk Baeumer 已提交
1438
			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)));
1439 1440 1441
			return undefined;
		}

A
Alex Ross 已提交
1442 1443
		let result: Tasks.CustomTask = new Tasks.CustomTask(
			context.uuidMap.getUUID(taskName),
1444
			Objects.assign({} as Tasks.WorkspaceTaskSource, source, { config: { index, element: external, file: '.vscode/tasks.json', workspaceFolder: context.workspaceFolder } }),
A
Alex Ross 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
			taskName,
			Tasks.CUSTOMIZED_TASK_TYPE,
			undefined,
			false,
			RunOptions.fromConfiguration(external.runOptions),
			{
				name: taskName,
				identifier: taskName,
			}
		);
1455 1456
		let configuration = ConfigurationProperties.from(external, context, false);
		if (configuration) {
1457
			result.configurationProperties = Objects.assign(result.configurationProperties, configuration);
1458 1459 1460 1461
		}
		let supportLegacy: boolean = true; //context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0;
		if (supportLegacy) {
			let legacy: LegacyTaskProperties = external as LegacyTaskProperties;
R
Rob Lourens 已提交
1462
			if (result.configurationProperties.isBackground === undefined && legacy.isWatching !== undefined) {
A
Alex Ross 已提交
1463
				result.configurationProperties.isBackground = !!legacy.isWatching;
1464
			}
R
Rob Lourens 已提交
1465
			if (result.configurationProperties.group === undefined) {
1466
				if (legacy.isBuildCommand === true) {
A
Alex Ross 已提交
1467
					result.configurationProperties.group = Tasks.TaskGroup.Build;
1468
				} else if (legacy.isTestCommand === true) {
A
Alex Ross 已提交
1469
					result.configurationProperties.group = Tasks.TaskGroup.Test;
1470
				}
1471
			}
1472
		}
A
Alex Ross 已提交
1473
		let command: Tasks.CommandConfiguration = CommandConfiguration.from(external, context)!;
1474 1475 1476
		if (command) {
			result.command = command;
		}
R
Rob Lourens 已提交
1477
		if (external.command !== undefined) {
1478 1479 1480 1481 1482 1483 1484 1485 1486
			// 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
1487
		// or there is a dependsOn and a defined command.
R
Rob Lourens 已提交
1488
		if (CommandConfiguration.hasCommand(task.command) || task.configurationProperties.dependsOn === undefined) {
A
Alex Ross 已提交
1489
			task.command = CommandConfiguration.fillGlobals(task.command, globals.command, task.configurationProperties.name);
1490
		}
R
Rob Lourens 已提交
1491
		if (task.configurationProperties.problemMatchers === undefined && globals.problemMatcher !== undefined) {
A
Alex Ross 已提交
1492
			task.configurationProperties.problemMatchers = Objects.deepClone(globals.problemMatcher);
1493 1494
			task.hasDefinedMatchers = true;
		}
1495
		// promptOnClose is inferred from isBackground if available
R
Rob Lourens 已提交
1496
		if (task.configurationProperties.promptOnClose === undefined && task.configurationProperties.isBackground === undefined && globals.promptOnClose !== undefined) {
A
Alex Ross 已提交
1497
			task.configurationProperties.promptOnClose = globals.promptOnClose;
1498 1499 1500 1501 1502
		}
	}

	export function fillDefaults(task: Tasks.CustomTask, context: ParseContext): void {
		CommandConfiguration.fillDefaults(task.command, context);
R
Rob Lourens 已提交
1503 1504
		if (task.configurationProperties.promptOnClose === undefined) {
			task.configurationProperties.promptOnClose = task.configurationProperties.isBackground !== undefined ? !task.configurationProperties.isBackground : true;
1505
		}
R
Rob Lourens 已提交
1506
		if (task.configurationProperties.isBackground === undefined) {
A
Alex Ross 已提交
1507
			task.configurationProperties.isBackground = false;
1508
		}
R
Rob Lourens 已提交
1509
		if (task.configurationProperties.problemMatchers === undefined) {
A
Alex Ross 已提交
1510
			task.configurationProperties.problemMatchers = EMPTY_ARRAY;
1511
		}
R
Rob Lourens 已提交
1512
		if (task.configurationProperties.group !== undefined && task.configurationProperties.groupType === undefined) {
A
Alex Ross 已提交
1513
			task.configurationProperties.groupType = Tasks.GroupType.user;
1514
		}
1515 1516
	}

1517
	export function createCustomTask(contributedTask: Tasks.ContributedTask, configuredProps: Tasks.ConfiguringTask | Tasks.CustomTask): Tasks.CustomTask {
A
Alex Ross 已提交
1518 1519 1520
		let result: Tasks.CustomTask = new Tasks.CustomTask(
			configuredProps._id,
			Objects.assign({}, configuredProps._source, { customizes: contributedTask.defines }),
1521
			configuredProps.configurationProperties.name || contributedTask._label,
A
Alex Ross 已提交
1522 1523 1524 1525 1526
			Tasks.CUSTOMIZED_TASK_TYPE,
			contributedTask.command,
			false,
			contributedTask.runOptions,
			{
1527 1528
				name: configuredProps.configurationProperties.name || contributedTask.configurationProperties.name,
				identifier: configuredProps.configurationProperties.identifier || contributedTask.configurationProperties.identifier,
A
Alex Ross 已提交
1529 1530
			}
		);
1531
		result.addTaskLoadMessages(configuredProps.taskLoadMessages);
A
Alex Ross 已提交
1532
		let resultConfigProps: Tasks.ConfigurationProperties = result.configurationProperties;
1533

1534 1535 1536 1537 1538 1539
		assignProperty(resultConfigProps, configuredProps.configurationProperties, 'group');
		assignProperty(resultConfigProps, configuredProps.configurationProperties, 'groupType');
		assignProperty(resultConfigProps, configuredProps.configurationProperties, 'isBackground');
		assignProperty(resultConfigProps, configuredProps.configurationProperties, 'dependsOn');
		assignProperty(resultConfigProps, configuredProps.configurationProperties, 'problemMatchers');
		assignProperty(resultConfigProps, configuredProps.configurationProperties, 'promptOnClose');
1540
		result.command.presentation = CommandConfiguration.PresentationOptions.assignProperties(
1541 1542
			result.command.presentation!, configuredProps.configurationProperties.presentation)!;
		result.command.options = CommandOptions.assignProperties(result.command.options, configuredProps.configurationProperties.options);
1543

A
Alex Ross 已提交
1544
		let contributedConfigProps: Tasks.ConfigurationProperties = contributedTask.configurationProperties;
1545
		fillProperty(resultConfigProps, contributedConfigProps, 'group');
1546
		fillProperty(resultConfigProps, contributedConfigProps, 'groupType');
1547 1548 1549 1550 1551
		fillProperty(resultConfigProps, contributedConfigProps, 'isBackground');
		fillProperty(resultConfigProps, contributedConfigProps, 'dependsOn');
		fillProperty(resultConfigProps, contributedConfigProps, 'problemMatchers');
		fillProperty(resultConfigProps, contributedConfigProps, 'promptOnClose');
		result.command.presentation = CommandConfiguration.PresentationOptions.fillProperties(
A
Alex Ross 已提交
1552
			result.command.presentation!, contributedConfigProps.presentation)!;
1553
		result.command.options = CommandOptions.fillProperties(result.command.options, contributedConfigProps.options);
1554

1555 1556 1557 1558
		if (contributedTask.hasDefinedMatchers === true) {
			result.hasDefinedMatchers = true;
		}

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
		return result;
	}
}

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

namespace TaskParser {

	function isCustomTask(value: CustomTask | ConfiguringTask): value is CustomTask {
		let type = value.type;
1572
		let customize = (value as any).customize;
R
Rob Lourens 已提交
1573
		return customize === undefined && (type === undefined || type === null || type === Tasks.CUSTOMIZED_TASK_TYPE || type === 'shell' || type === 'process');
1574 1575
	}

A
Alex Ross 已提交
1576
	export function from(this: void, externals: Array<CustomTask | ConfiguringTask> | undefined, globals: Globals, context: ParseContext): TaskParseResult {
1577 1578 1579 1580
		let result: TaskParseResult = { custom: [], configured: [] };
		if (!externals) {
			return result;
		}
A
Alex Ross 已提交
1581 1582
		let defaultBuildTask: { task: Tasks.Task | undefined; rank: number; } = { task: undefined, rank: -1 };
		let defaultTestTask: { task: Tasks.Task | undefined; rank: number; } = { task: undefined, rank: -1 };
1583
		let schema2_0_0: boolean = context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0;
1584
		const baseLoadIssues = Objects.deepClone(context.taskLoadIssues);
1585 1586
		for (let index = 0; index < externals.length; index++) {
			let external = externals[index];
1587
			if (isCustomTask(external)) {
1588
				let customTask = CustomTask.from(external, context, index);
1589 1590 1591 1592
				if (customTask) {
					CustomTask.fillGlobals(customTask, globals);
					CustomTask.fillDefaults(customTask, context);
					if (schema2_0_0) {
R
Rob Lourens 已提交
1593
						if ((customTask.command === undefined || customTask.command.name === undefined) && (customTask.configurationProperties.dependsOn === undefined || customTask.configurationProperties.dependsOn.length === 0)) {
1594 1595
							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}',
A
Alex Ross 已提交
1596
								customTask.configurationProperties.name, JSON.stringify(external, undefined, 4)
1597 1598 1599 1600
							));
							continue;
						}
					} else {
R
Rob Lourens 已提交
1601
						if (customTask.command === undefined || customTask.command.name === undefined) {
1602 1603
							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}',
A
Alex Ross 已提交
1604
								customTask.configurationProperties.name, JSON.stringify(external, undefined, 4)
1605 1606 1607 1608
							));
							continue;
						}
					}
A
Alex Ross 已提交
1609
					if (customTask.configurationProperties.group === Tasks.TaskGroup.Build && defaultBuildTask.rank < 2) {
1610 1611
						defaultBuildTask.task = customTask;
						defaultBuildTask.rank = 2;
A
Alex Ross 已提交
1612
					} else if (customTask.configurationProperties.group === Tasks.TaskGroup.Test && defaultTestTask.rank < 2) {
1613 1614
						defaultTestTask.task = customTask;
						defaultTestTask.rank = 2;
A
Alex Ross 已提交
1615
					} else if (customTask.configurationProperties.name === 'build' && defaultBuildTask.rank < 1) {
1616 1617
						defaultBuildTask.task = customTask;
						defaultBuildTask.rank = 1;
A
Alex Ross 已提交
1618
					} else if (customTask.configurationProperties.name === 'test' && defaultTestTask.rank < 1) {
1619 1620 1621
						defaultTestTask.task = customTask;
						defaultTestTask.rank = 1;
					}
1622
					customTask.addTaskLoadMessages(context.taskLoadIssues);
1623
					result.custom.push(customTask);
D
Dirk Baeumer 已提交
1624 1625
				}
			} else {
1626
				let configuredTask = ConfiguringTask.from(external, context, index);
1627
				if (configuredTask) {
1628
					configuredTask.addTaskLoadMessages(context.taskLoadIssues);
1629
					result.configured.push(configuredTask);
D
Dirk Baeumer 已提交
1630
				}
E
Erich Gamma 已提交
1631
			}
1632
			context.taskLoadIssues = Objects.deepClone(baseLoadIssues);
1633
		}
A
Alex Ross 已提交
1634
		if ((defaultBuildTask.rank > -1) && (defaultBuildTask.rank < 2) && defaultBuildTask.task) {
A
Alex Ross 已提交
1635 1636
			defaultBuildTask.task.configurationProperties.group = Tasks.TaskGroup.Build;
			defaultBuildTask.task.configurationProperties.groupType = Tasks.GroupType.user;
A
Alex Ross 已提交
1637
		} else if ((defaultTestTask.rank > -1) && (defaultTestTask.rank < 2) && defaultTestTask.task) {
A
Alex Ross 已提交
1638 1639
			defaultTestTask.task.configurationProperties.group = Tasks.TaskGroup.Test;
			defaultTestTask.task.configurationProperties.groupType = Tasks.GroupType.user;
E
Erich Gamma 已提交
1640
		}
1641 1642

		return result;
E
Erich Gamma 已提交
1643 1644
	}

1645
	export function assignTasks(target: Tasks.CustomTask[], source: Tasks.CustomTask[]): Tasks.CustomTask[] {
R
Rob Lourens 已提交
1646
		if (source === undefined || source.length === 0) {
1647 1648
			return target;
		}
R
Rob Lourens 已提交
1649
		if (target === undefined || target.length === 0) {
1650 1651 1652
			return source;
		}

1653
		if (source) {
1654
			// Tasks are keyed by ID but we need to merge by name
1655
			let map: IStringDictionary<Tasks.CustomTask> = Object.create(null);
1656
			target.forEach((task) => {
A
Alex Ross 已提交
1657
				map[task.configurationProperties.name!] = task;
1658 1659
			});

1660
			source.forEach((task) => {
A
Alex Ross 已提交
1661
				map[task.configurationProperties.name!] = task;
1662
			});
1663
			let newTarget: Tasks.CustomTask[] = [];
1664
			target.forEach(task => {
A
Alex Ross 已提交
1665 1666
				newTarget.push(map[task.configurationProperties.name!]);
				delete map[task.configurationProperties.name!];
E
Erich Gamma 已提交
1667
			});
1668 1669
			Object.keys(map).forEach(key => newTarget.push(map[key]));
			target = newTarget;
E
Erich Gamma 已提交
1670
		}
1671 1672 1673 1674 1675
		return target;
	}
}

interface Globals {
1676
	command?: Tasks.CommandConfiguration;
1677
	problemMatcher?: ProblemMatcher[];
1678 1679 1680 1681 1682
	promptOnClose?: boolean;
	suppressTaskName?: boolean;
}

namespace Globals {
1683 1684 1685

	export function from(config: ExternalTaskRunnerConfiguration, context: ParseContext): Globals {
		let result = fromBase(config, context);
1686
		let osGlobals: Globals | undefined = undefined;
1687
		if (config.windows && context.platform === Platform.Windows) {
1688
			osGlobals = fromBase(config.windows, context);
1689
		} else if (config.osx && context.platform === Platform.Mac) {
1690
			osGlobals = fromBase(config.osx, context);
1691
		} else if (config.linux && context.platform === Platform.Linux) {
1692 1693 1694
			osGlobals = fromBase(config.linux, context);
		}
		if (osGlobals) {
1695
			result = Globals.assignProperties(result, osGlobals);
1696 1697 1698 1699 1700
		}
		let command = CommandConfiguration.from(config, context);
		if (command) {
			result.command = command;
		}
1701
		Globals.fillDefaults(result, context);
1702 1703 1704 1705 1706
		Globals.freeze(result);
		return result;
	}

	export function fromBase(this: void, config: BaseTaskRunnerConfiguration, context: ParseContext): Globals {
1707
		let result: Globals = {};
R
Rob Lourens 已提交
1708
		if (config.suppressTaskName !== undefined) {
1709 1710
			result.suppressTaskName = !!config.suppressTaskName;
		}
R
Rob Lourens 已提交
1711
		if (config.promptOnClose !== undefined) {
1712 1713
			result.promptOnClose = !!config.promptOnClose;
		}
1714 1715 1716
		if (config.problemMatcher) {
			result.problemMatcher = ProblemMatcherConverter.from(config.problemMatcher, context);
		}
E
Erich Gamma 已提交
1717 1718 1719
		return result;
	}

1720
	export function isEmpty(value: Globals): boolean {
R
Rob Lourens 已提交
1721
		return !value || value.command === undefined && value.promptOnClose === undefined && value.suppressTaskName === undefined;
1722 1723
	}

1724
	export function assignProperties(target: Globals, source: Globals): Globals {
1725 1726
		if (isEmpty(source)) {
			return target;
E
Erich Gamma 已提交
1727
		}
1728 1729 1730
		if (isEmpty(target)) {
			return source;
		}
1731 1732
		assignProperty(target, source, 'promptOnClose');
		assignProperty(target, source, 'suppressTaskName');
1733
		return target;
E
Erich Gamma 已提交
1734 1735
	}

1736
	export function fillDefaults(value: Globals, context: ParseContext): void {
1737 1738 1739
		if (!value) {
			return;
		}
1740
		CommandConfiguration.fillDefaults(value.command, context);
R
Rob Lourens 已提交
1741
		if (value.suppressTaskName === undefined) {
1742
			value.suppressTaskName = (context.schemaVersion === Tasks.JsonSchemaVersion.V2_0_0);
1743
		}
R
Rob Lourens 已提交
1744
		if (value.promptOnClose === undefined) {
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
			value.promptOnClose = true;
		}
	}

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

1757 1758
export namespace ExecutionEngine {

1759
	export function from(config: ExternalTaskRunnerConfiguration): Tasks.ExecutionEngine {
1760
		let runner = config.runner || config._runner;
A
Alex Ross 已提交
1761
		let result: Tasks.ExecutionEngine | undefined;
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
		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.');
		}
1780
	}
1781
}
1782

1783 1784
export namespace JsonSchemaVersion {

1785
	const _default: Tasks.JsonSchemaVersion = Tasks.JsonSchemaVersion.V2_0_0;
1786

1787 1788 1789
	export function from(config: ExternalTaskRunnerConfiguration): Tasks.JsonSchemaVersion {
		let version = config.version;
		if (!version) {
1790
			return _default;
1791 1792 1793 1794
		}
		switch (version) {
			case '0.1.0':
				return Tasks.JsonSchemaVersion.V0_1_0;
1795
			case '2.0.0':
1796
				return Tasks.JsonSchemaVersion.V2_0_0;
1797 1798
			default:
				return _default;
1799
		}
1800 1801 1802
	}
}

1803 1804
export interface ParseResult {
	validationStatus: ValidationStatus;
1805 1806
	custom: Tasks.CustomTask[];
	configured: Tasks.ConfiguringTask[];
1807
	engine: Tasks.ExecutionEngine;
1808 1809
}

1810
export interface IProblemReporter extends IProblemReporterBase {
1811 1812
}

1813 1814
class UUIDMap {

A
Alex Ross 已提交
1815
	private last: IStringDictionary<string | string[]> | undefined;
1816 1817
	private current: IStringDictionary<string | string[]>;

1818
	constructor(other?: UUIDMap) {
1819
		this.current = Object.create(null);
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
		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;
				}
			}
		}
1830 1831 1832 1833 1834 1835 1836 1837
	}

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

	public getUUID(identifier: string): string {
A
Alex Ross 已提交
1838 1839
		let lastValue = this.last ? this.last[identifier] : undefined;
		let result: string | undefined = undefined;
R
Rob Lourens 已提交
1840
		if (lastValue !== undefined) {
1841 1842 1843
			if (Array.isArray(lastValue)) {
				result = lastValue.shift();
				if (lastValue.length === 0) {
A
Alex Ross 已提交
1844
					delete this.last![identifier];
1845 1846 1847
				}
			} else {
				result = lastValue;
A
Alex Ross 已提交
1848
				delete this.last![identifier];
1849 1850
			}
		}
R
Rob Lourens 已提交
1851
		if (result === undefined) {
1852 1853 1854
			result = UUID.generateUuid();
		}
		let currentValue = this.current[identifier];
R
Rob Lourens 已提交
1855
		if (currentValue === undefined) {
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
			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;
	}
}

1874 1875
class ConfigurationParser {

S
Sandeep Somavarapu 已提交
1876
	private workspaceFolder: IWorkspaceFolder;
1877
	private problemReporter: IProblemReporter;
1878
	private uuidMap: UUIDMap;
1879
	private platform: Platform;
1880

1881
	constructor(workspaceFolder: IWorkspaceFolder, platform: Platform, problemReporter: IProblemReporter, uuidMap: UUIDMap) {
1882
		this.workspaceFolder = workspaceFolder;
1883
		this.platform = platform;
1884
		this.problemReporter = problemReporter;
1885
		this.uuidMap = uuidMap;
1886 1887 1888
	}

	public run(fileConfig: ExternalTaskRunnerConfiguration): ParseResult {
1889
		let engine = ExecutionEngine.from(fileConfig);
1890
		let schemaVersion = JsonSchemaVersion.from(fileConfig);
1891
		let context: ParseContext = {
1892
			workspaceFolder: this.workspaceFolder,
1893
			problemReporter: this.problemReporter,
1894
			uuidMap: this.uuidMap,
A
Alex Ross 已提交
1895
			namedProblemMatchers: {},
1896
			engine,
1897
			schemaVersion,
1898 1899
			platform: this.platform,
			taskLoadIssues: []
1900 1901
		};
		let taskParseResult = this.createTaskRunnerConfiguration(fileConfig, context);
1902
		return {
1903
			validationStatus: this.problemReporter.status,
1904 1905
			custom: taskParseResult.custom,
			configured: taskParseResult.configured,
1906
			engine
1907 1908 1909
		};
	}

1910
	private createTaskRunnerConfiguration(fileConfig: ExternalTaskRunnerConfiguration, context: ParseContext): TaskParseResult {
1911
		let globals = Globals.from(fileConfig, context);
1912
		if (this.problemReporter.status.isFatal()) {
1913
			return { custom: [], configured: [] };
1914 1915
		}
		context.namedProblemMatchers = ProblemMatcherConverter.namedFrom(fileConfig.declares, context);
A
Alex Ross 已提交
1916 1917
		let globalTasks: Tasks.CustomTask[] | undefined = undefined;
		let externalGlobalTasks: Array<ConfiguringTask | CustomTask> | undefined = undefined;
1918
		if (fileConfig.windows && context.platform === Platform.Windows) {
1919
			globalTasks = TaskParser.from(fileConfig.windows.tasks, globals, context).custom;
1920
			externalGlobalTasks = fileConfig.windows.tasks;
1921
		} else if (fileConfig.osx && context.platform === Platform.Mac) {
1922
			globalTasks = TaskParser.from(fileConfig.osx.tasks, globals, context).custom;
1923
			externalGlobalTasks = fileConfig.osx.tasks;
1924
		} else if (fileConfig.linux && context.platform === Platform.Linux) {
1925
			globalTasks = TaskParser.from(fileConfig.linux.tasks, globals, context).custom;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
			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',
1936
					'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'))
1937
			);
1938 1939
		}

A
Alex Ross 已提交
1940
		let result: TaskParseResult = { custom: [], configured: [] };
1941
		if (fileConfig.tasks) {
1942
			result = TaskParser.from(fileConfig.tasks, globals, context);
1943
		}
1944
		if (globalTasks) {
1945
			result.custom = TaskParser.assignTasks(result.custom, globalTasks);
1946 1947
		}

1948
		if ((!result.custom || result.custom.length === 0) && (globals.command && globals.command.name)) {
1949
			let matchers: ProblemMatcher[] = ProblemMatcherConverter.from(fileConfig.problemMatcher, context);
1950
			let isBackground = fileConfig.isBackground ? !!fileConfig.isBackground : fileConfig.isWatching ? !!fileConfig.isWatching : undefined;
1951
			let name = Tasks.CommandString.value(globals.command.name);
A
Alex Ross 已提交
1952 1953 1954 1955 1956 1957
			let task: Tasks.CustomTask = new Tasks.CustomTask(
				context.uuidMap.getUUID(name),
				Objects.assign({} as Tasks.WorkspaceTaskSource, source, { config: { index: -1, element: fileConfig, workspaceFolder: context.workspaceFolder } }),
				name,
				Tasks.CUSTOMIZED_TASK_TYPE,
				{
1958
					name: undefined,
1959
					runtime: undefined,
1960
					presentation: undefined,
1961 1962
					suppressTaskName: true
				},
A
Alex Ross 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
				false,
				{ reevaluateOnRerun: true },
				{
					name: name,
					identifier: name,
					group: Tasks.TaskGroup.Build,
					isBackground: isBackground,
					problemMatchers: matchers,
				}
			);
1973 1974
			let value = GroupKind.from(fileConfig.group);
			if (value) {
A
Alex Ross 已提交
1975 1976
				task.configurationProperties.group = value[0];
				task.configurationProperties.groupType = value[1];
1977
			} else if (fileConfig.group === 'none') {
A
Alex Ross 已提交
1978
				task.configurationProperties.group = undefined;
1979
			}
1980 1981 1982
			CustomTask.fillGlobals(task, globals);
			CustomTask.fillDefaults(task, context);
			result.custom = [task];
1983
		}
1984 1985
		result.custom = result.custom || [];
		result.configured = result.configured || [];
1986
		return result;
1987
	}
E
Erich Gamma 已提交
1988 1989
}

1990
let uuidMaps: Map<string, UUIDMap> = new Map();
1991
export function parse(workspaceFolder: IWorkspaceFolder, platform: Platform, configuration: ExternalTaskRunnerConfiguration, logger: IProblemReporter): ParseResult {
1992 1993 1994 1995 1996
	let uuidMap = uuidMaps.get(workspaceFolder.uri.toString());
	if (!uuidMap) {
		uuidMap = new UUIDMap();
		uuidMaps.set(workspaceFolder.uri.toString(), uuidMap);
	}
1997 1998
	try {
		uuidMap.start();
1999
		return (new ConfigurationParser(workspaceFolder, platform, logger, uuidMap)).run(configuration);
2000 2001 2002
	} finally {
		uuidMap.finish();
	}
2003 2004
}

2005
export function createCustomTask(contributedTask: Tasks.ContributedTask, configuredProps: Tasks.ConfiguringTask | Tasks.CustomTask): Tasks.CustomTask {
2006 2007 2008
	return CustomTask.createCustomTask(contributedTask, configuredProps);
}

D
Dirk Baeumer 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
/*
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 已提交
2085
*/