@theia/task 1.34.2 → 1.34.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE +641 -641
  2. package/README.md +193 -193
  3. package/lib/browser/index.d.ts +6 -6
  4. package/lib/browser/index.js +33 -33
  5. package/lib/browser/process/process-task-contribution.d.ts +6 -6
  6. package/lib/browser/process/process-task-contribution.js +43 -43
  7. package/lib/browser/process/process-task-frontend-module.d.ts +2 -2
  8. package/lib/browser/process/process-task-frontend-module.js +27 -27
  9. package/lib/browser/process/process-task-resolver.d.ts +17 -17
  10. package/lib/browser/process/process-task-resolver.js +93 -93
  11. package/lib/browser/provided-task-configurations.d.ts +32 -32
  12. package/lib/browser/provided-task-configurations.js +171 -171
  13. package/lib/browser/provided-task-configurations.spec.d.ts +1 -1
  14. package/lib/browser/provided-task-configurations.spec.js +43 -43
  15. package/lib/browser/quick-open-task.d.ts +129 -129
  16. package/lib/browser/quick-open-task.js +636 -636
  17. package/lib/browser/task-configuration-manager.d.ts +64 -64
  18. package/lib/browser/task-configuration-manager.js +261 -261
  19. package/lib/browser/task-configuration-model.d.ts +32 -32
  20. package/lib/browser/task-configuration-model.js +79 -79
  21. package/lib/browser/task-configurations.d.ts +127 -127
  22. package/lib/browser/task-configurations.js +479 -479
  23. package/lib/browser/task-contribution.d.ts +173 -173
  24. package/lib/browser/task-contribution.js +199 -199
  25. package/lib/browser/task-definition-registry.d.ts +38 -38
  26. package/lib/browser/task-definition-registry.js +131 -131
  27. package/lib/browser/task-definition-registry.spec.d.ts +1 -1
  28. package/lib/browser/task-definition-registry.spec.js +170 -170
  29. package/lib/browser/task-frontend-contribution.d.ts +56 -56
  30. package/lib/browser/task-frontend-contribution.js +353 -353
  31. package/lib/browser/task-frontend-module.d.ts +5 -5
  32. package/lib/browser/task-frontend-module.js +79 -79
  33. package/lib/browser/task-name-resolver.d.ts +13 -13
  34. package/lib/browser/task-name-resolver.js +65 -65
  35. package/lib/browser/task-node.d.ts +9 -9
  36. package/lib/browser/task-node.js +17 -17
  37. package/lib/browser/task-preferences.d.ts +4 -4
  38. package/lib/browser/task-preferences.js +40 -40
  39. package/lib/browser/task-problem-matcher-registry.d.ts +40 -40
  40. package/lib/browser/task-problem-matcher-registry.js +310 -310
  41. package/lib/browser/task-problem-pattern-registry.d.ts +23 -23
  42. package/lib/browser/task-problem-pattern-registry.js +208 -208
  43. package/lib/browser/task-schema-updater.d.ts +58 -58
  44. package/lib/browser/task-schema-updater.js +672 -672
  45. package/lib/browser/task-service.d.ts +276 -276
  46. package/lib/browser/task-service.js +1119 -1119
  47. package/lib/browser/task-source-resolver.d.ts +9 -9
  48. package/lib/browser/task-source-resolver.js +51 -51
  49. package/lib/browser/task-templates.d.ts +13 -13
  50. package/lib/browser/task-templates.js +161 -161
  51. package/lib/browser/task-terminal-widget-manager.d.ts +44 -44
  52. package/lib/browser/task-terminal-widget-manager.js +224 -224
  53. package/lib/browser/tasks-monaco-contribution.d.ts +1 -1
  54. package/lib/browser/tasks-monaco-contribution.js +27 -27
  55. package/lib/common/index.d.ts +3 -3
  56. package/lib/common/index.js +30 -30
  57. package/lib/common/problem-matcher-protocol.d.ts +124 -124
  58. package/lib/common/problem-matcher-protocol.js +132 -132
  59. package/lib/common/process/task-protocol.d.ts +68 -68
  60. package/lib/common/process/task-protocol.js +33 -33
  61. package/lib/common/task-common-module.d.ts +7 -7
  62. package/lib/common/task-common-module.js +33 -33
  63. package/lib/common/task-protocol.d.ts +190 -190
  64. package/lib/common/task-protocol.js +125 -125
  65. package/lib/common/task-watcher.d.ts +17 -17
  66. package/lib/common/task-watcher.js +86 -86
  67. package/lib/node/custom/custom-task-runner-backend-module.d.ts +2 -2
  68. package/lib/node/custom/custom-task-runner-backend-module.js +36 -36
  69. package/lib/node/custom/custom-task-runner-contribution.d.ts +6 -6
  70. package/lib/node/custom/custom-task-runner-contribution.js +42 -42
  71. package/lib/node/custom/custom-task-runner.d.ts +15 -15
  72. package/lib/node/custom/custom-task-runner.js +69 -69
  73. package/lib/node/custom/custom-task.d.ts +20 -20
  74. package/lib/node/custom/custom-task.js +78 -78
  75. package/lib/node/index.d.ts +3 -3
  76. package/lib/node/index.js +30 -30
  77. package/lib/node/process/process-task-runner-backend-module.d.ts +2 -2
  78. package/lib/node/process/process-task-runner-backend-module.js +36 -36
  79. package/lib/node/process/process-task-runner-contribution.d.ts +6 -6
  80. package/lib/node/process/process-task-runner-contribution.js +43 -43
  81. package/lib/node/process/process-task-runner.d.ts +60 -60
  82. package/lib/node/process/process-task-runner.js +352 -352
  83. package/lib/node/process/process-task.d.ts +26 -26
  84. package/lib/node/process/process-task.js +139 -139
  85. package/lib/node/process/process-task.spec.d.ts +1 -1
  86. package/lib/node/process/process-task.spec.js +29 -29
  87. package/lib/node/task-abstract-line-matcher.d.ts +49 -49
  88. package/lib/node/task-abstract-line-matcher.js +278 -278
  89. package/lib/node/task-backend-application-contribution.d.ts +8 -8
  90. package/lib/node/task-backend-application-contribution.js +48 -48
  91. package/lib/node/task-backend-module.d.ts +3 -3
  92. package/lib/node/task-backend-module.js +50 -50
  93. package/lib/node/task-line-matchers.d.ts +27 -27
  94. package/lib/node/task-line-matchers.js +121 -121
  95. package/lib/node/task-manager.d.ts +55 -55
  96. package/lib/node/task-manager.js +135 -135
  97. package/lib/node/task-problem-collector.d.ts +10 -10
  98. package/lib/node/task-problem-collector.js +57 -57
  99. package/lib/node/task-problem-collector.spec.d.ts +1 -1
  100. package/lib/node/task-problem-collector.spec.js +310 -310
  101. package/lib/node/task-runner-protocol.d.ts +16 -16
  102. package/lib/node/task-runner-protocol.js +19 -19
  103. package/lib/node/task-runner.d.ts +50 -50
  104. package/lib/node/task-runner.js +97 -97
  105. package/lib/node/task-server.d.ts +39 -39
  106. package/lib/node/task-server.js +237 -237
  107. package/lib/node/task-server.slow-spec.d.ts +1 -1
  108. package/lib/node/task-server.slow-spec.js +395 -395
  109. package/lib/node/task.d.ts +50 -50
  110. package/lib/node/task.js +77 -77
  111. package/lib/node/test/task-test-container.d.ts +2 -2
  112. package/lib/node/test/task-test-container.js +58 -58
  113. package/package.json +13 -13
  114. package/src/browser/index.ts +22 -22
  115. package/src/browser/process/process-task-contribution.ts +31 -31
  116. package/src/browser/process/process-task-frontend-module.ts +27 -27
  117. package/src/browser/process/process-task-resolver.ts +89 -89
  118. package/src/browser/provided-task-configurations.spec.ts +46 -46
  119. package/src/browser/provided-task-configurations.ts +170 -170
  120. package/src/browser/quick-open-task.ts +701 -701
  121. package/src/browser/style/index.css +19 -19
  122. package/src/browser/task-configuration-manager.ts +256 -256
  123. package/src/browser/task-configuration-model.ts +101 -101
  124. package/src/browser/task-configurations.ts +507 -507
  125. package/src/browser/task-contribution.ts +254 -254
  126. package/src/browser/task-definition-registry.spec.ts +203 -203
  127. package/src/browser/task-definition-registry.ts +128 -128
  128. package/src/browser/task-frontend-contribution.ts +396 -396
  129. package/src/browser/task-frontend-module.ts +86 -86
  130. package/src/browser/task-name-resolver.ts +55 -55
  131. package/src/browser/task-node.ts +37 -37
  132. package/src/browser/task-preferences.ts +40 -40
  133. package/src/browser/task-problem-matcher-registry.ts +308 -308
  134. package/src/browser/task-problem-pattern-registry.ts +195 -195
  135. package/src/browser/task-schema-updater.ts +703 -703
  136. package/src/browser/task-service.ts +1163 -1163
  137. package/src/browser/task-source-resolver.ts +36 -36
  138. package/src/browser/task-templates.ts +168 -168
  139. package/src/browser/task-terminal-widget-manager.ts +221 -221
  140. package/src/browser/tasks-monaco-contribution.ts +27 -27
  141. package/src/common/index.ts +19 -19
  142. package/src/common/problem-matcher-protocol.ts +235 -235
  143. package/src/common/process/task-protocol.ts +97 -97
  144. package/src/common/task-common-module.ts +34 -34
  145. package/src/common/task-protocol.ts +310 -310
  146. package/src/common/task-watcher.ts +78 -78
  147. package/src/node/custom/custom-task-runner-backend-module.ts +37 -37
  148. package/src/node/custom/custom-task-runner-contribution.ts +30 -30
  149. package/src/node/custom/custom-task-runner.ts +60 -60
  150. package/src/node/custom/custom-task.ts +73 -73
  151. package/src/node/index.ts +19 -19
  152. package/src/node/process/process-task-runner-backend-module.ts +37 -37
  153. package/src/node/process/process-task-runner-contribution.ts +31 -31
  154. package/src/node/process/process-task-runner.ts +371 -371
  155. package/src/node/process/process-task.spec.ts +30 -30
  156. package/src/node/process/process-task.ts +147 -147
  157. package/src/node/task-abstract-line-matcher.ts +317 -317
  158. package/src/node/task-backend-application-contribution.ts +36 -36
  159. package/src/node/task-backend-module.ts +57 -57
  160. package/src/node/task-line-matchers.ts +127 -127
  161. package/src/node/task-manager.ts +129 -129
  162. package/src/node/task-problem-collector.spec.ts +338 -338
  163. package/src/node/task-problem-collector.ts +62 -62
  164. package/src/node/task-runner-protocol.ts +33 -33
  165. package/src/node/task-runner.ts +96 -96
  166. package/src/node/task-server.slow-spec.ts +444 -444
  167. package/src/node/task-server.ts +263 -263
  168. package/src/node/task.ts +103 -103
  169. package/src/node/test/task-test-container.ts +63 -63
@@ -1,353 +1,353 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2017-2019 Ericsson and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
- return c > 3 && r && Object.defineProperty(target, key, r), r;
22
- };
23
- var __metadata = (this && this.__metadata) || function (k, v) {
24
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.ProcessTaskRunner = void 0;
28
- /*---------------------------------------------------------------------------------------------
29
- * Copyright (c) Microsoft Corporation. All rights reserved.
30
- * Licensed under the MIT License. See License.txt in the project root for license information.
31
- *--------------------------------------------------------------------------------------------*/
32
- const inversify_1 = require("@theia/core/shared/inversify");
33
- const core_1 = require("@theia/core");
34
- const node_1 = require("@theia/core/lib/node");
35
- const node_2 = require("@theia/process/lib/node");
36
- const shell_quoting_1 = require("@theia/process/lib/common/shell-quoting");
37
- const process_task_1 = require("./process-task");
38
- const task_protocol_1 = require("../../common/process/task-protocol");
39
- const fs = require("fs");
40
- const shell_process_1 = require("@theia/terminal/lib/node/shell-process");
41
- /**
42
- * Task runner that runs a task as a process or a command inside a shell.
43
- */
44
- let ProcessTaskRunner = class ProcessTaskRunner {
45
- /**
46
- * Runs a task from the given task configuration.
47
- * @param taskConfig task configuration to run a task from. The provided task configuration must have a shape of `CommandProperties`.
48
- */
49
- async run(taskConfig, ctx) {
50
- if (!taskConfig.command) {
51
- throw new Error("Process task config must have 'command' property specified");
52
- }
53
- try {
54
- // Always spawn a task in a pty, the only difference between shell/process tasks is the
55
- // way the command is passed:
56
- // - process: directly look for an executable and pass a specific set of arguments/options.
57
- // - shell: defer the spawning to a shell that will evaluate a command line with our executable.
58
- const terminalProcessOptions = this.getResolvedCommand(taskConfig);
59
- const terminal = this.taskTerminalProcessFactory(terminalProcessOptions);
60
- // Wait for the confirmation that the process is successfully started, or has failed to start.
61
- await new Promise((resolve, reject) => {
62
- terminal.onStart(resolve);
63
- terminal.onError((error) => {
64
- reject(task_protocol_1.ProcessTaskError.CouldNotRun(error.code));
65
- });
66
- });
67
- const processType = (taskConfig.taskType || taskConfig.type);
68
- return this.taskFactory({
69
- label: taskConfig.label,
70
- process: terminal,
71
- processType,
72
- context: ctx,
73
- config: taskConfig,
74
- command: this.getCommand(processType, terminalProcessOptions)
75
- });
76
- }
77
- catch (error) {
78
- this.logger.error(`Error occurred while creating task: ${error}`);
79
- throw error;
80
- }
81
- }
82
- getResolvedCommand(taskConfig) {
83
- const osSpecificCommand = this.getOsSpecificCommand(taskConfig);
84
- const options = osSpecificCommand.options;
85
- // Use task's cwd with spawned process and pass node env object to
86
- // new process, so e.g. we can re-use the system path
87
- if (options) {
88
- options.env = Object.assign(Object.assign({}, process.env), (options.env || {}));
89
- }
90
- /** Executable to actually spawn. */
91
- let command;
92
- /** List of arguments passed to `command`. */
93
- let args;
94
- /**
95
- * Only useful on Windows, has to do with how node-pty handles complex commands.
96
- * This string should not include the executable, only what comes after it (arguments).
97
- */
98
- let commandLine;
99
- if ((taskConfig.taskType || taskConfig.type) === 'shell') {
100
- // When running a shell task, we have to spawn a shell process somehow,
101
- // and tell it to run the command the user wants to run inside of it.
102
- //
103
- // E.g:
104
- // - Spawning a process:
105
- // spawn(process_exe, [...args])
106
- // - Spawning a shell and run a command:
107
- // spawn(shell_exe, [shell_exec_cmd_flag, command])
108
- //
109
- // The fun part is, the `command` to pass as an argument usually has to be
110
- // what you would type verbatim inside the shell, so escaping rules apply.
111
- //
112
- // What's even more funny is that on Windows, node-pty uses a special
113
- // mechanism to pass complex escaped arguments, via a string.
114
- //
115
- // We need to accommodate most shells, so we need to get specific.
116
- const { shell } = osSpecificCommand.options;
117
- command = (shell === null || shell === void 0 ? void 0 : shell.executable) || shell_process_1.ShellProcess.getShellExecutablePath();
118
- const { execArgs, quotingFunctions } = this.getShellSpecificOptions(command);
119
- // Allow overriding shell options from task configuration.
120
- args = (shell === null || shell === void 0 ? void 0 : shell.args) ? [...shell.args] : [...execArgs];
121
- // Check if an argument list is defined or not. Empty is ok.
122
- /** Shell command to run: */
123
- const shellCommand = this.buildShellCommand(osSpecificCommand, quotingFunctions);
124
- if (core_1.isWindows && /cmd(.exe)?$/.test(command)) {
125
- // Let's take the following command, including an argument containing whitespace:
126
- // cmd> node -p process.argv 1 2 " 3"
127
- //
128
- // We would expect the following output:
129
- // json> [ '...\\node.exe', '1', '2', ' 3' ]
130
- //
131
- // Let's run this command through `cmd.exe` using `child_process`:
132
- // js> void childprocess.spawn('cmd.exe', ['/s', '/c', 'node -p process.argv 1 2 " 3"']).stderr.on('data', console.log)
133
- //
134
- // We get the correct output, but when using node-pty:
135
- // js> void nodepty.spawn('cmd.exe', ['/s', '/c', 'node -p process.argv 1 2 " 3"']).on('data', console.log)
136
- //
137
- // Then the output looks like:
138
- // json> [ '...\\node.exe', '1', '2', '"', '3"' ]
139
- //
140
- // To fix that, we need to use a special node-pty feature and pass arguments as one string:
141
- // js> nodepty.spawn('cmd.exe', '/s /c "node -p process.argv 1 2 " 3""')
142
- //
143
- // Note the extra quotes that need to be added around the whole command.
144
- commandLine = [...args, `"${shellCommand}"`].join(' ');
145
- }
146
- args.push(shellCommand);
147
- }
148
- else {
149
- // When running process tasks, `command` is the executable to run,
150
- // and `args` are the arguments we want to pass to it.
151
- command = osSpecificCommand.command;
152
- if (Array.isArray(osSpecificCommand.args)) {
153
- // Process task doesn't handle quotation: Normalize arguments from `ShellQuotedString` to raw `string`.
154
- args = osSpecificCommand.args.map(arg => typeof arg === 'string' ? arg : arg.value);
155
- }
156
- else {
157
- args = [];
158
- }
159
- }
160
- return { command, args, commandLine, options };
161
- }
162
- buildShellCommand(systemSpecificCommand, quotingFunctions) {
163
- var _a;
164
- if (Array.isArray(systemSpecificCommand.args)) {
165
- const commandLineElements = [systemSpecificCommand.command, ...systemSpecificCommand.args].map(arg => {
166
- // We want to quote arguments only if needed.
167
- if (quotingFunctions && typeof arg === 'string' && this.argumentNeedsQuotes(arg, quotingFunctions)) {
168
- return {
169
- quoting: "strong" /* Strong */,
170
- value: arg,
171
- };
172
- }
173
- else {
174
- return arg;
175
- }
176
- });
177
- return (0, shell_quoting_1.createShellCommandLine)(commandLineElements, quotingFunctions);
178
- }
179
- else {
180
- // No arguments are provided, so `command` is actually the full command line to execute.
181
- return (_a = systemSpecificCommand.command) !== null && _a !== void 0 ? _a : '';
182
- }
183
- }
184
- getShellSpecificOptions(command) {
185
- if (/bash(.exe)?$/.test(command)) {
186
- return {
187
- quotingFunctions: shell_quoting_1.BashQuotingFunctions,
188
- execArgs: ['-c']
189
- };
190
- }
191
- else if (/wsl(.exe)?$/.test(command)) {
192
- return {
193
- quotingFunctions: shell_quoting_1.BashQuotingFunctions,
194
- execArgs: ['-e']
195
- };
196
- }
197
- else if (/cmd(.exe)?$/.test(command)) {
198
- return {
199
- quotingFunctions: shell_quoting_1.CmdQuotingFunctions,
200
- execArgs: ['/S', '/C']
201
- };
202
- }
203
- else if (/(ps|pwsh|powershell)(.exe)?/.test(command)) {
204
- return {
205
- quotingFunctions: shell_quoting_1.PowershellQuotingFunctions,
206
- execArgs: ['-c']
207
- };
208
- }
209
- else {
210
- return {
211
- quotingFunctions: shell_quoting_1.BashQuotingFunctions,
212
- execArgs: ['-l', '-c']
213
- };
214
- }
215
- }
216
- getOsSpecificCommand(taskConfig) {
217
- // on windows, windows-specific options, if available, take precedence
218
- if (core_1.isWindows && taskConfig.windows !== undefined) {
219
- return this.getSystemSpecificCommand(taskConfig, 'windows');
220
- }
221
- else if (core_1.isOSX && taskConfig.osx !== undefined) { // on macOS, mac-specific options, if available, take precedence
222
- return this.getSystemSpecificCommand(taskConfig, 'osx');
223
- }
224
- else if (!core_1.isWindows && !core_1.isOSX && taskConfig.linux !== undefined) { // on linux, linux-specific options, if available, take precedence
225
- return this.getSystemSpecificCommand(taskConfig, 'linux');
226
- }
227
- else { // system-specific options are unavailable, use the default
228
- return this.getSystemSpecificCommand(taskConfig, undefined);
229
- }
230
- }
231
- getCommand(processType, terminalProcessOptions) {
232
- if (terminalProcessOptions.args) {
233
- if (processType === 'shell') {
234
- return terminalProcessOptions.args[terminalProcessOptions.args.length - 1];
235
- }
236
- else if (processType === 'process') {
237
- return `${terminalProcessOptions.command} ${terminalProcessOptions.args.join(' ')}`;
238
- }
239
- }
240
- }
241
- /**
242
- * This is task specific, to align with VS Code's behavior.
243
- *
244
- * When parsing arguments, VS Code will try to detect if the user already
245
- * tried to quote things.
246
- *
247
- * See: https://github.com/microsoft/vscode/blob/d363b988e1e58cf49963841c498681cdc6cb55a3/src/vs/workbench/contrib/tasks/browser/terminalTaskSystem.ts#L1101-L1127
248
- *
249
- * @param value
250
- * @param shellQuotingOptions
251
- */
252
- argumentNeedsQuotes(value, shellQuotingOptions) {
253
- const { characters } = shellQuotingOptions;
254
- const needQuotes = new Set([' ', ...characters.needQuotes || []]);
255
- if (!characters) {
256
- return false;
257
- }
258
- if (value.length >= 2) {
259
- const first = value[0] === characters.strong ? characters.strong : value[0] === characters.weak ? characters.weak : undefined;
260
- if (first === value[value.length - 1]) {
261
- return false;
262
- }
263
- }
264
- let quote;
265
- for (let i = 0; i < value.length; i++) {
266
- // We found the end quote.
267
- const ch = value[i];
268
- if (ch === quote) {
269
- quote = undefined;
270
- }
271
- else if (quote !== undefined) {
272
- // skip the character. We are quoted.
273
- continue;
274
- }
275
- else if (ch === characters.escape) {
276
- // Skip the next character
277
- i++;
278
- }
279
- else if (ch === characters.strong || ch === characters.weak) {
280
- quote = ch;
281
- }
282
- else if (needQuotes.has(ch)) {
283
- return true;
284
- }
285
- }
286
- return false;
287
- }
288
- getSystemSpecificCommand(taskConfig, system) {
289
- // initialize with default values from the `taskConfig`
290
- let command = taskConfig.command;
291
- let args = taskConfig.args;
292
- let options = (0, core_1.deepClone)(taskConfig.options) || {};
293
- if (system) {
294
- if (taskConfig[system].command) {
295
- command = taskConfig[system].command;
296
- }
297
- if (taskConfig[system].args) {
298
- args = taskConfig[system].args;
299
- }
300
- if (taskConfig[system].options) {
301
- options = taskConfig[system].options;
302
- }
303
- }
304
- if (options.cwd) {
305
- options.cwd = this.asFsPath(options.cwd);
306
- }
307
- if (command === undefined) {
308
- throw new Error('The `command` field of a task cannot be undefined.');
309
- }
310
- return { command, args, options };
311
- }
312
- asFsPath(uriOrPath) {
313
- return (uriOrPath.startsWith('file:'))
314
- ? node_1.FileUri.fsPath(uriOrPath)
315
- : uriOrPath;
316
- }
317
- /**
318
- * @deprecated
319
- *
320
- * Remove ProcessTaskRunner.findCommand, introduce process "started" event
321
- * Checks for the existence of a file, at the provided path, and make sure that
322
- * it's readable and executable.
323
- */
324
- async executableFileExists(filePath) {
325
- return new Promise(async (resolve, reject) => {
326
- fs.access(filePath, fs.constants.F_OK | fs.constants.X_OK, err => {
327
- resolve(err ? false : true);
328
- });
329
- });
330
- }
331
- };
332
- __decorate([
333
- (0, inversify_1.inject)(core_1.ILogger),
334
- (0, inversify_1.named)('task'),
335
- __metadata("design:type", Object)
336
- ], ProcessTaskRunner.prototype, "logger", void 0);
337
- __decorate([
338
- (0, inversify_1.inject)(node_2.RawProcessFactory),
339
- __metadata("design:type", Function)
340
- ], ProcessTaskRunner.prototype, "rawProcessFactory", void 0);
341
- __decorate([
342
- (0, inversify_1.inject)(node_2.TaskTerminalProcessFactory),
343
- __metadata("design:type", Function)
344
- ], ProcessTaskRunner.prototype, "taskTerminalProcessFactory", void 0);
345
- __decorate([
346
- (0, inversify_1.inject)(process_task_1.TaskFactory),
347
- __metadata("design:type", Function)
348
- ], ProcessTaskRunner.prototype, "taskFactory", void 0);
349
- ProcessTaskRunner = __decorate([
350
- (0, inversify_1.injectable)()
351
- ], ProcessTaskRunner);
352
- exports.ProcessTaskRunner = ProcessTaskRunner;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2017-2019 Ericsson and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.ProcessTaskRunner = void 0;
28
+ /*---------------------------------------------------------------------------------------------
29
+ * Copyright (c) Microsoft Corporation. All rights reserved.
30
+ * Licensed under the MIT License. See License.txt in the project root for license information.
31
+ *--------------------------------------------------------------------------------------------*/
32
+ const inversify_1 = require("@theia/core/shared/inversify");
33
+ const core_1 = require("@theia/core");
34
+ const node_1 = require("@theia/core/lib/node");
35
+ const node_2 = require("@theia/process/lib/node");
36
+ const shell_quoting_1 = require("@theia/process/lib/common/shell-quoting");
37
+ const process_task_1 = require("./process-task");
38
+ const task_protocol_1 = require("../../common/process/task-protocol");
39
+ const fs = require("fs");
40
+ const shell_process_1 = require("@theia/terminal/lib/node/shell-process");
41
+ /**
42
+ * Task runner that runs a task as a process or a command inside a shell.
43
+ */
44
+ let ProcessTaskRunner = class ProcessTaskRunner {
45
+ /**
46
+ * Runs a task from the given task configuration.
47
+ * @param taskConfig task configuration to run a task from. The provided task configuration must have a shape of `CommandProperties`.
48
+ */
49
+ async run(taskConfig, ctx) {
50
+ if (!taskConfig.command) {
51
+ throw new Error("Process task config must have 'command' property specified");
52
+ }
53
+ try {
54
+ // Always spawn a task in a pty, the only difference between shell/process tasks is the
55
+ // way the command is passed:
56
+ // - process: directly look for an executable and pass a specific set of arguments/options.
57
+ // - shell: defer the spawning to a shell that will evaluate a command line with our executable.
58
+ const terminalProcessOptions = this.getResolvedCommand(taskConfig);
59
+ const terminal = this.taskTerminalProcessFactory(terminalProcessOptions);
60
+ // Wait for the confirmation that the process is successfully started, or has failed to start.
61
+ await new Promise((resolve, reject) => {
62
+ terminal.onStart(resolve);
63
+ terminal.onError((error) => {
64
+ reject(task_protocol_1.ProcessTaskError.CouldNotRun(error.code));
65
+ });
66
+ });
67
+ const processType = (taskConfig.taskType || taskConfig.type);
68
+ return this.taskFactory({
69
+ label: taskConfig.label,
70
+ process: terminal,
71
+ processType,
72
+ context: ctx,
73
+ config: taskConfig,
74
+ command: this.getCommand(processType, terminalProcessOptions)
75
+ });
76
+ }
77
+ catch (error) {
78
+ this.logger.error(`Error occurred while creating task: ${error}`);
79
+ throw error;
80
+ }
81
+ }
82
+ getResolvedCommand(taskConfig) {
83
+ const osSpecificCommand = this.getOsSpecificCommand(taskConfig);
84
+ const options = osSpecificCommand.options;
85
+ // Use task's cwd with spawned process and pass node env object to
86
+ // new process, so e.g. we can re-use the system path
87
+ if (options) {
88
+ options.env = Object.assign(Object.assign({}, process.env), (options.env || {}));
89
+ }
90
+ /** Executable to actually spawn. */
91
+ let command;
92
+ /** List of arguments passed to `command`. */
93
+ let args;
94
+ /**
95
+ * Only useful on Windows, has to do with how node-pty handles complex commands.
96
+ * This string should not include the executable, only what comes after it (arguments).
97
+ */
98
+ let commandLine;
99
+ if ((taskConfig.taskType || taskConfig.type) === 'shell') {
100
+ // When running a shell task, we have to spawn a shell process somehow,
101
+ // and tell it to run the command the user wants to run inside of it.
102
+ //
103
+ // E.g:
104
+ // - Spawning a process:
105
+ // spawn(process_exe, [...args])
106
+ // - Spawning a shell and run a command:
107
+ // spawn(shell_exe, [shell_exec_cmd_flag, command])
108
+ //
109
+ // The fun part is, the `command` to pass as an argument usually has to be
110
+ // what you would type verbatim inside the shell, so escaping rules apply.
111
+ //
112
+ // What's even more funny is that on Windows, node-pty uses a special
113
+ // mechanism to pass complex escaped arguments, via a string.
114
+ //
115
+ // We need to accommodate most shells, so we need to get specific.
116
+ const { shell } = osSpecificCommand.options;
117
+ command = (shell === null || shell === void 0 ? void 0 : shell.executable) || shell_process_1.ShellProcess.getShellExecutablePath();
118
+ const { execArgs, quotingFunctions } = this.getShellSpecificOptions(command);
119
+ // Allow overriding shell options from task configuration.
120
+ args = (shell === null || shell === void 0 ? void 0 : shell.args) ? [...shell.args] : [...execArgs];
121
+ // Check if an argument list is defined or not. Empty is ok.
122
+ /** Shell command to run: */
123
+ const shellCommand = this.buildShellCommand(osSpecificCommand, quotingFunctions);
124
+ if (core_1.isWindows && /cmd(.exe)?$/.test(command)) {
125
+ // Let's take the following command, including an argument containing whitespace:
126
+ // cmd> node -p process.argv 1 2 " 3"
127
+ //
128
+ // We would expect the following output:
129
+ // json> [ '...\\node.exe', '1', '2', ' 3' ]
130
+ //
131
+ // Let's run this command through `cmd.exe` using `child_process`:
132
+ // js> void childprocess.spawn('cmd.exe', ['/s', '/c', 'node -p process.argv 1 2 " 3"']).stderr.on('data', console.log)
133
+ //
134
+ // We get the correct output, but when using node-pty:
135
+ // js> void nodepty.spawn('cmd.exe', ['/s', '/c', 'node -p process.argv 1 2 " 3"']).on('data', console.log)
136
+ //
137
+ // Then the output looks like:
138
+ // json> [ '...\\node.exe', '1', '2', '"', '3"' ]
139
+ //
140
+ // To fix that, we need to use a special node-pty feature and pass arguments as one string:
141
+ // js> nodepty.spawn('cmd.exe', '/s /c "node -p process.argv 1 2 " 3""')
142
+ //
143
+ // Note the extra quotes that need to be added around the whole command.
144
+ commandLine = [...args, `"${shellCommand}"`].join(' ');
145
+ }
146
+ args.push(shellCommand);
147
+ }
148
+ else {
149
+ // When running process tasks, `command` is the executable to run,
150
+ // and `args` are the arguments we want to pass to it.
151
+ command = osSpecificCommand.command;
152
+ if (Array.isArray(osSpecificCommand.args)) {
153
+ // Process task doesn't handle quotation: Normalize arguments from `ShellQuotedString` to raw `string`.
154
+ args = osSpecificCommand.args.map(arg => typeof arg === 'string' ? arg : arg.value);
155
+ }
156
+ else {
157
+ args = [];
158
+ }
159
+ }
160
+ return { command, args, commandLine, options };
161
+ }
162
+ buildShellCommand(systemSpecificCommand, quotingFunctions) {
163
+ var _a;
164
+ if (Array.isArray(systemSpecificCommand.args)) {
165
+ const commandLineElements = [systemSpecificCommand.command, ...systemSpecificCommand.args].map(arg => {
166
+ // We want to quote arguments only if needed.
167
+ if (quotingFunctions && typeof arg === 'string' && this.argumentNeedsQuotes(arg, quotingFunctions)) {
168
+ return {
169
+ quoting: "strong" /* Strong */,
170
+ value: arg,
171
+ };
172
+ }
173
+ else {
174
+ return arg;
175
+ }
176
+ });
177
+ return (0, shell_quoting_1.createShellCommandLine)(commandLineElements, quotingFunctions);
178
+ }
179
+ else {
180
+ // No arguments are provided, so `command` is actually the full command line to execute.
181
+ return (_a = systemSpecificCommand.command) !== null && _a !== void 0 ? _a : '';
182
+ }
183
+ }
184
+ getShellSpecificOptions(command) {
185
+ if (/bash(.exe)?$/.test(command)) {
186
+ return {
187
+ quotingFunctions: shell_quoting_1.BashQuotingFunctions,
188
+ execArgs: ['-c']
189
+ };
190
+ }
191
+ else if (/wsl(.exe)?$/.test(command)) {
192
+ return {
193
+ quotingFunctions: shell_quoting_1.BashQuotingFunctions,
194
+ execArgs: ['-e']
195
+ };
196
+ }
197
+ else if (/cmd(.exe)?$/.test(command)) {
198
+ return {
199
+ quotingFunctions: shell_quoting_1.CmdQuotingFunctions,
200
+ execArgs: ['/S', '/C']
201
+ };
202
+ }
203
+ else if (/(ps|pwsh|powershell)(.exe)?/.test(command)) {
204
+ return {
205
+ quotingFunctions: shell_quoting_1.PowershellQuotingFunctions,
206
+ execArgs: ['-c']
207
+ };
208
+ }
209
+ else {
210
+ return {
211
+ quotingFunctions: shell_quoting_1.BashQuotingFunctions,
212
+ execArgs: ['-l', '-c']
213
+ };
214
+ }
215
+ }
216
+ getOsSpecificCommand(taskConfig) {
217
+ // on windows, windows-specific options, if available, take precedence
218
+ if (core_1.isWindows && taskConfig.windows !== undefined) {
219
+ return this.getSystemSpecificCommand(taskConfig, 'windows');
220
+ }
221
+ else if (core_1.isOSX && taskConfig.osx !== undefined) { // on macOS, mac-specific options, if available, take precedence
222
+ return this.getSystemSpecificCommand(taskConfig, 'osx');
223
+ }
224
+ else if (!core_1.isWindows && !core_1.isOSX && taskConfig.linux !== undefined) { // on linux, linux-specific options, if available, take precedence
225
+ return this.getSystemSpecificCommand(taskConfig, 'linux');
226
+ }
227
+ else { // system-specific options are unavailable, use the default
228
+ return this.getSystemSpecificCommand(taskConfig, undefined);
229
+ }
230
+ }
231
+ getCommand(processType, terminalProcessOptions) {
232
+ if (terminalProcessOptions.args) {
233
+ if (processType === 'shell') {
234
+ return terminalProcessOptions.args[terminalProcessOptions.args.length - 1];
235
+ }
236
+ else if (processType === 'process') {
237
+ return `${terminalProcessOptions.command} ${terminalProcessOptions.args.join(' ')}`;
238
+ }
239
+ }
240
+ }
241
+ /**
242
+ * This is task specific, to align with VS Code's behavior.
243
+ *
244
+ * When parsing arguments, VS Code will try to detect if the user already
245
+ * tried to quote things.
246
+ *
247
+ * See: https://github.com/microsoft/vscode/blob/d363b988e1e58cf49963841c498681cdc6cb55a3/src/vs/workbench/contrib/tasks/browser/terminalTaskSystem.ts#L1101-L1127
248
+ *
249
+ * @param value
250
+ * @param shellQuotingOptions
251
+ */
252
+ argumentNeedsQuotes(value, shellQuotingOptions) {
253
+ const { characters } = shellQuotingOptions;
254
+ const needQuotes = new Set([' ', ...characters.needQuotes || []]);
255
+ if (!characters) {
256
+ return false;
257
+ }
258
+ if (value.length >= 2) {
259
+ const first = value[0] === characters.strong ? characters.strong : value[0] === characters.weak ? characters.weak : undefined;
260
+ if (first === value[value.length - 1]) {
261
+ return false;
262
+ }
263
+ }
264
+ let quote;
265
+ for (let i = 0; i < value.length; i++) {
266
+ // We found the end quote.
267
+ const ch = value[i];
268
+ if (ch === quote) {
269
+ quote = undefined;
270
+ }
271
+ else if (quote !== undefined) {
272
+ // skip the character. We are quoted.
273
+ continue;
274
+ }
275
+ else if (ch === characters.escape) {
276
+ // Skip the next character
277
+ i++;
278
+ }
279
+ else if (ch === characters.strong || ch === characters.weak) {
280
+ quote = ch;
281
+ }
282
+ else if (needQuotes.has(ch)) {
283
+ return true;
284
+ }
285
+ }
286
+ return false;
287
+ }
288
+ getSystemSpecificCommand(taskConfig, system) {
289
+ // initialize with default values from the `taskConfig`
290
+ let command = taskConfig.command;
291
+ let args = taskConfig.args;
292
+ let options = (0, core_1.deepClone)(taskConfig.options) || {};
293
+ if (system) {
294
+ if (taskConfig[system].command) {
295
+ command = taskConfig[system].command;
296
+ }
297
+ if (taskConfig[system].args) {
298
+ args = taskConfig[system].args;
299
+ }
300
+ if (taskConfig[system].options) {
301
+ options = taskConfig[system].options;
302
+ }
303
+ }
304
+ if (options.cwd) {
305
+ options.cwd = this.asFsPath(options.cwd);
306
+ }
307
+ if (command === undefined) {
308
+ throw new Error('The `command` field of a task cannot be undefined.');
309
+ }
310
+ return { command, args, options };
311
+ }
312
+ asFsPath(uriOrPath) {
313
+ return (uriOrPath.startsWith('file:'))
314
+ ? node_1.FileUri.fsPath(uriOrPath)
315
+ : uriOrPath;
316
+ }
317
+ /**
318
+ * @deprecated
319
+ *
320
+ * Remove ProcessTaskRunner.findCommand, introduce process "started" event
321
+ * Checks for the existence of a file, at the provided path, and make sure that
322
+ * it's readable and executable.
323
+ */
324
+ async executableFileExists(filePath) {
325
+ return new Promise(async (resolve, reject) => {
326
+ fs.access(filePath, fs.constants.F_OK | fs.constants.X_OK, err => {
327
+ resolve(err ? false : true);
328
+ });
329
+ });
330
+ }
331
+ };
332
+ __decorate([
333
+ (0, inversify_1.inject)(core_1.ILogger),
334
+ (0, inversify_1.named)('task'),
335
+ __metadata("design:type", Object)
336
+ ], ProcessTaskRunner.prototype, "logger", void 0);
337
+ __decorate([
338
+ (0, inversify_1.inject)(node_2.RawProcessFactory),
339
+ __metadata("design:type", Function)
340
+ ], ProcessTaskRunner.prototype, "rawProcessFactory", void 0);
341
+ __decorate([
342
+ (0, inversify_1.inject)(node_2.TaskTerminalProcessFactory),
343
+ __metadata("design:type", Function)
344
+ ], ProcessTaskRunner.prototype, "taskTerminalProcessFactory", void 0);
345
+ __decorate([
346
+ (0, inversify_1.inject)(process_task_1.TaskFactory),
347
+ __metadata("design:type", Function)
348
+ ], ProcessTaskRunner.prototype, "taskFactory", void 0);
349
+ ProcessTaskRunner = __decorate([
350
+ (0, inversify_1.injectable)()
351
+ ], ProcessTaskRunner);
352
+ exports.ProcessTaskRunner = ProcessTaskRunner;
353
353
  //# sourceMappingURL=process-task-runner.js.map