toolrunner.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const os = require("os");
  13. const events = require("events");
  14. const child = require("child_process");
  15. /* eslint-disable @typescript-eslint/unbound-method */
  16. const IS_WINDOWS = process.platform === 'win32';
  17. /*
  18. * Class for running command line tools. Handles quoting and arg parsing in a platform agnostic way.
  19. */
  20. class ToolRunner extends events.EventEmitter {
  21. constructor(toolPath, args, options) {
  22. super();
  23. if (!toolPath) {
  24. throw new Error("Parameter 'toolPath' cannot be null or empty.");
  25. }
  26. this.toolPath = toolPath;
  27. this.args = args || [];
  28. this.options = options || {};
  29. }
  30. _debug(message) {
  31. if (this.options.listeners && this.options.listeners.debug) {
  32. this.options.listeners.debug(message);
  33. }
  34. }
  35. _getCommandString(options, noPrefix) {
  36. const toolPath = this._getSpawnFileName();
  37. const args = this._getSpawnArgs(options);
  38. let cmd = noPrefix ? '' : '[command]'; // omit prefix when piped to a second tool
  39. if (IS_WINDOWS) {
  40. // Windows + cmd file
  41. if (this._isCmdFile()) {
  42. cmd += toolPath;
  43. for (const a of args) {
  44. cmd += ` ${a}`;
  45. }
  46. }
  47. // Windows + verbatim
  48. else if (options.windowsVerbatimArguments) {
  49. cmd += `"${toolPath}"`;
  50. for (const a of args) {
  51. cmd += ` ${a}`;
  52. }
  53. }
  54. // Windows (regular)
  55. else {
  56. cmd += this._windowsQuoteCmdArg(toolPath);
  57. for (const a of args) {
  58. cmd += ` ${this._windowsQuoteCmdArg(a)}`;
  59. }
  60. }
  61. }
  62. else {
  63. // OSX/Linux - this can likely be improved with some form of quoting.
  64. // creating processes on Unix is fundamentally different than Windows.
  65. // on Unix, execvp() takes an arg array.
  66. cmd += toolPath;
  67. for (const a of args) {
  68. cmd += ` ${a}`;
  69. }
  70. }
  71. return cmd;
  72. }
  73. _processLineBuffer(data, strBuffer, onLine) {
  74. try {
  75. let s = strBuffer + data.toString();
  76. let n = s.indexOf(os.EOL);
  77. while (n > -1) {
  78. const line = s.substring(0, n);
  79. onLine(line);
  80. // the rest of the string ...
  81. s = s.substring(n + os.EOL.length);
  82. n = s.indexOf(os.EOL);
  83. }
  84. strBuffer = s;
  85. }
  86. catch (err) {
  87. // streaming lines to console is best effort. Don't fail a build.
  88. this._debug(`error processing line. Failed with error ${err}`);
  89. }
  90. }
  91. _getSpawnFileName() {
  92. if (IS_WINDOWS) {
  93. if (this._isCmdFile()) {
  94. return process.env['COMSPEC'] || 'cmd.exe';
  95. }
  96. }
  97. return this.toolPath;
  98. }
  99. _getSpawnArgs(options) {
  100. if (IS_WINDOWS) {
  101. if (this._isCmdFile()) {
  102. let argline = `/D /S /C "${this._windowsQuoteCmdArg(this.toolPath)}`;
  103. for (const a of this.args) {
  104. argline += ' ';
  105. argline += options.windowsVerbatimArguments
  106. ? a
  107. : this._windowsQuoteCmdArg(a);
  108. }
  109. argline += '"';
  110. return [argline];
  111. }
  112. }
  113. return this.args;
  114. }
  115. _endsWith(str, end) {
  116. return str.endsWith(end);
  117. }
  118. _isCmdFile() {
  119. const upperToolPath = this.toolPath.toUpperCase();
  120. return (this._endsWith(upperToolPath, '.CMD') ||
  121. this._endsWith(upperToolPath, '.BAT'));
  122. }
  123. _windowsQuoteCmdArg(arg) {
  124. // for .exe, apply the normal quoting rules that libuv applies
  125. if (!this._isCmdFile()) {
  126. return this._uvQuoteCmdArg(arg);
  127. }
  128. // otherwise apply quoting rules specific to the cmd.exe command line parser.
  129. // the libuv rules are generic and are not designed specifically for cmd.exe
  130. // command line parser.
  131. //
  132. // for a detailed description of the cmd.exe command line parser, refer to
  133. // http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/7970912#7970912
  134. // need quotes for empty arg
  135. if (!arg) {
  136. return '""';
  137. }
  138. // determine whether the arg needs to be quoted
  139. const cmdSpecialChars = [
  140. ' ',
  141. '\t',
  142. '&',
  143. '(',
  144. ')',
  145. '[',
  146. ']',
  147. '{',
  148. '}',
  149. '^',
  150. '=',
  151. ';',
  152. '!',
  153. "'",
  154. '+',
  155. ',',
  156. '`',
  157. '~',
  158. '|',
  159. '<',
  160. '>',
  161. '"'
  162. ];
  163. let needsQuotes = false;
  164. for (const char of arg) {
  165. if (cmdSpecialChars.some(x => x === char)) {
  166. needsQuotes = true;
  167. break;
  168. }
  169. }
  170. // short-circuit if quotes not needed
  171. if (!needsQuotes) {
  172. return arg;
  173. }
  174. // the following quoting rules are very similar to the rules that by libuv applies.
  175. //
  176. // 1) wrap the string in quotes
  177. //
  178. // 2) double-up quotes - i.e. " => ""
  179. //
  180. // this is different from the libuv quoting rules. libuv replaces " with \", which unfortunately
  181. // doesn't work well with a cmd.exe command line.
  182. //
  183. // note, replacing " with "" also works well if the arg is passed to a downstream .NET console app.
  184. // for example, the command line:
  185. // foo.exe "myarg:""my val"""
  186. // is parsed by a .NET console app into an arg array:
  187. // [ "myarg:\"my val\"" ]
  188. // which is the same end result when applying libuv quoting rules. although the actual
  189. // command line from libuv quoting rules would look like:
  190. // foo.exe "myarg:\"my val\""
  191. //
  192. // 3) double-up slashes that precede a quote,
  193. // e.g. hello \world => "hello \world"
  194. // hello\"world => "hello\\""world"
  195. // hello\\"world => "hello\\\\""world"
  196. // hello world\ => "hello world\\"
  197. //
  198. // technically this is not required for a cmd.exe command line, or the batch argument parser.
  199. // the reasons for including this as a .cmd quoting rule are:
  200. //
  201. // a) this is optimized for the scenario where the argument is passed from the .cmd file to an
  202. // external program. many programs (e.g. .NET console apps) rely on the slash-doubling rule.
  203. //
  204. // b) it's what we've been doing previously (by deferring to node default behavior) and we
  205. // haven't heard any complaints about that aspect.
  206. //
  207. // note, a weakness of the quoting rules chosen here, is that % is not escaped. in fact, % cannot be
  208. // escaped when used on the command line directly - even though within a .cmd file % can be escaped
  209. // by using %%.
  210. //
  211. // the saving grace is, on the command line, %var% is left as-is if var is not defined. this contrasts
  212. // the line parsing rules within a .cmd file, where if var is not defined it is replaced with nothing.
  213. //
  214. // one option that was explored was replacing % with ^% - i.e. %var% => ^%var^%. this hack would
  215. // often work, since it is unlikely that var^ would exist, and the ^ character is removed when the
  216. // variable is used. the problem, however, is that ^ is not removed when %* is used to pass the args
  217. // to an external program.
  218. //
  219. // an unexplored potential solution for the % escaping problem, is to create a wrapper .cmd file.
  220. // % can be escaped within a .cmd file.
  221. let reverse = '"';
  222. let quoteHit = true;
  223. for (let i = arg.length; i > 0; i--) {
  224. // walk the string in reverse
  225. reverse += arg[i - 1];
  226. if (quoteHit && arg[i - 1] === '\\') {
  227. reverse += '\\'; // double the slash
  228. }
  229. else if (arg[i - 1] === '"') {
  230. quoteHit = true;
  231. reverse += '"'; // double the quote
  232. }
  233. else {
  234. quoteHit = false;
  235. }
  236. }
  237. reverse += '"';
  238. return reverse
  239. .split('')
  240. .reverse()
  241. .join('');
  242. }
  243. _uvQuoteCmdArg(arg) {
  244. // Tool runner wraps child_process.spawn() and needs to apply the same quoting as
  245. // Node in certain cases where the undocumented spawn option windowsVerbatimArguments
  246. // is used.
  247. //
  248. // Since this function is a port of quote_cmd_arg from Node 4.x (technically, lib UV,
  249. // see https://github.com/nodejs/node/blob/v4.x/deps/uv/src/win/process.c for details),
  250. // pasting copyright notice from Node within this function:
  251. //
  252. // Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  253. //
  254. // Permission is hereby granted, free of charge, to any person obtaining a copy
  255. // of this software and associated documentation files (the "Software"), to
  256. // deal in the Software without restriction, including without limitation the
  257. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  258. // sell copies of the Software, and to permit persons to whom the Software is
  259. // furnished to do so, subject to the following conditions:
  260. //
  261. // The above copyright notice and this permission notice shall be included in
  262. // all copies or substantial portions of the Software.
  263. //
  264. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  265. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  266. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  267. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  268. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  269. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  270. // IN THE SOFTWARE.
  271. if (!arg) {
  272. // Need double quotation for empty argument
  273. return '""';
  274. }
  275. if (!arg.includes(' ') && !arg.includes('\t') && !arg.includes('"')) {
  276. // No quotation needed
  277. return arg;
  278. }
  279. if (!arg.includes('"') && !arg.includes('\\')) {
  280. // No embedded double quotes or backslashes, so I can just wrap
  281. // quote marks around the whole thing.
  282. return `"${arg}"`;
  283. }
  284. // Expected input/output:
  285. // input : hello"world
  286. // output: "hello\"world"
  287. // input : hello""world
  288. // output: "hello\"\"world"
  289. // input : hello\world
  290. // output: hello\world
  291. // input : hello\\world
  292. // output: hello\\world
  293. // input : hello\"world
  294. // output: "hello\\\"world"
  295. // input : hello\\"world
  296. // output: "hello\\\\\"world"
  297. // input : hello world\
  298. // output: "hello world\\" - note the comment in libuv actually reads "hello world\"
  299. // but it appears the comment is wrong, it should be "hello world\\"
  300. let reverse = '"';
  301. let quoteHit = true;
  302. for (let i = arg.length; i > 0; i--) {
  303. // walk the string in reverse
  304. reverse += arg[i - 1];
  305. if (quoteHit && arg[i - 1] === '\\') {
  306. reverse += '\\';
  307. }
  308. else if (arg[i - 1] === '"') {
  309. quoteHit = true;
  310. reverse += '\\';
  311. }
  312. else {
  313. quoteHit = false;
  314. }
  315. }
  316. reverse += '"';
  317. return reverse
  318. .split('')
  319. .reverse()
  320. .join('');
  321. }
  322. _cloneExecOptions(options) {
  323. options = options || {};
  324. const result = {
  325. cwd: options.cwd || process.cwd(),
  326. env: options.env || process.env,
  327. silent: options.silent || false,
  328. windowsVerbatimArguments: options.windowsVerbatimArguments || false,
  329. failOnStdErr: options.failOnStdErr || false,
  330. ignoreReturnCode: options.ignoreReturnCode || false,
  331. delay: options.delay || 10000
  332. };
  333. result.outStream = options.outStream || process.stdout;
  334. result.errStream = options.errStream || process.stderr;
  335. return result;
  336. }
  337. _getSpawnOptions(options, toolPath) {
  338. options = options || {};
  339. const result = {};
  340. result.cwd = options.cwd;
  341. result.env = options.env;
  342. result['windowsVerbatimArguments'] =
  343. options.windowsVerbatimArguments || this._isCmdFile();
  344. if (options.windowsVerbatimArguments) {
  345. result.argv0 = `"${toolPath}"`;
  346. }
  347. return result;
  348. }
  349. /**
  350. * Exec a tool.
  351. * Output will be streamed to the live console.
  352. * Returns promise with return code
  353. *
  354. * @param tool path to tool to exec
  355. * @param options optional exec options. See ExecOptions
  356. * @returns number
  357. */
  358. exec() {
  359. return __awaiter(this, void 0, void 0, function* () {
  360. return new Promise((resolve, reject) => {
  361. this._debug(`exec tool: ${this.toolPath}`);
  362. this._debug('arguments:');
  363. for (const arg of this.args) {
  364. this._debug(` ${arg}`);
  365. }
  366. const optionsNonNull = this._cloneExecOptions(this.options);
  367. if (!optionsNonNull.silent && optionsNonNull.outStream) {
  368. optionsNonNull.outStream.write(this._getCommandString(optionsNonNull) + os.EOL);
  369. }
  370. const state = new ExecState(optionsNonNull, this.toolPath);
  371. state.on('debug', (message) => {
  372. this._debug(message);
  373. });
  374. const fileName = this._getSpawnFileName();
  375. const cp = child.spawn(fileName, this._getSpawnArgs(optionsNonNull), this._getSpawnOptions(this.options, fileName));
  376. const stdbuffer = '';
  377. if (cp.stdout) {
  378. cp.stdout.on('data', (data) => {
  379. if (this.options.listeners && this.options.listeners.stdout) {
  380. this.options.listeners.stdout(data);
  381. }
  382. if (!optionsNonNull.silent && optionsNonNull.outStream) {
  383. optionsNonNull.outStream.write(data);
  384. }
  385. this._processLineBuffer(data, stdbuffer, (line) => {
  386. if (this.options.listeners && this.options.listeners.stdline) {
  387. this.options.listeners.stdline(line);
  388. }
  389. });
  390. });
  391. }
  392. const errbuffer = '';
  393. if (cp.stderr) {
  394. cp.stderr.on('data', (data) => {
  395. state.processStderr = true;
  396. if (this.options.listeners && this.options.listeners.stderr) {
  397. this.options.listeners.stderr(data);
  398. }
  399. if (!optionsNonNull.silent &&
  400. optionsNonNull.errStream &&
  401. optionsNonNull.outStream) {
  402. const s = optionsNonNull.failOnStdErr
  403. ? optionsNonNull.errStream
  404. : optionsNonNull.outStream;
  405. s.write(data);
  406. }
  407. this._processLineBuffer(data, errbuffer, (line) => {
  408. if (this.options.listeners && this.options.listeners.errline) {
  409. this.options.listeners.errline(line);
  410. }
  411. });
  412. });
  413. }
  414. cp.on('error', (err) => {
  415. state.processError = err.message;
  416. state.processExited = true;
  417. state.processClosed = true;
  418. state.CheckComplete();
  419. });
  420. cp.on('exit', (code) => {
  421. state.processExitCode = code;
  422. state.processExited = true;
  423. this._debug(`Exit code ${code} received from tool '${this.toolPath}'`);
  424. state.CheckComplete();
  425. });
  426. cp.on('close', (code) => {
  427. state.processExitCode = code;
  428. state.processExited = true;
  429. state.processClosed = true;
  430. this._debug(`STDIO streams have closed for tool '${this.toolPath}'`);
  431. state.CheckComplete();
  432. });
  433. state.on('done', (error, exitCode) => {
  434. if (stdbuffer.length > 0) {
  435. this.emit('stdline', stdbuffer);
  436. }
  437. if (errbuffer.length > 0) {
  438. this.emit('errline', errbuffer);
  439. }
  440. cp.removeAllListeners();
  441. if (error) {
  442. reject(error);
  443. }
  444. else {
  445. resolve(exitCode);
  446. }
  447. });
  448. });
  449. });
  450. }
  451. }
  452. exports.ToolRunner = ToolRunner;
  453. /**
  454. * Convert an arg string to an array of args. Handles escaping
  455. *
  456. * @param argString string of arguments
  457. * @returns string[] array of arguments
  458. */
  459. function argStringToArray(argString) {
  460. const args = [];
  461. let inQuotes = false;
  462. let escaped = false;
  463. let arg = '';
  464. function append(c) {
  465. // we only escape double quotes.
  466. if (escaped && c !== '"') {
  467. arg += '\\';
  468. }
  469. arg += c;
  470. escaped = false;
  471. }
  472. for (let i = 0; i < argString.length; i++) {
  473. const c = argString.charAt(i);
  474. if (c === '"') {
  475. if (!escaped) {
  476. inQuotes = !inQuotes;
  477. }
  478. else {
  479. append(c);
  480. }
  481. continue;
  482. }
  483. if (c === '\\' && escaped) {
  484. append(c);
  485. continue;
  486. }
  487. if (c === '\\' && inQuotes) {
  488. escaped = true;
  489. continue;
  490. }
  491. if (c === ' ' && !inQuotes) {
  492. if (arg.length > 0) {
  493. args.push(arg);
  494. arg = '';
  495. }
  496. continue;
  497. }
  498. append(c);
  499. }
  500. if (arg.length > 0) {
  501. args.push(arg.trim());
  502. }
  503. return args;
  504. }
  505. exports.argStringToArray = argStringToArray;
  506. class ExecState extends events.EventEmitter {
  507. constructor(options, toolPath) {
  508. super();
  509. this.processClosed = false; // tracks whether the process has exited and stdio is closed
  510. this.processError = '';
  511. this.processExitCode = 0;
  512. this.processExited = false; // tracks whether the process has exited
  513. this.processStderr = false; // tracks whether stderr was written to
  514. this.delay = 10000; // 10 seconds
  515. this.done = false;
  516. this.timeout = null;
  517. if (!toolPath) {
  518. throw new Error('toolPath must not be empty');
  519. }
  520. this.options = options;
  521. this.toolPath = toolPath;
  522. if (options.delay) {
  523. this.delay = options.delay;
  524. }
  525. }
  526. CheckComplete() {
  527. if (this.done) {
  528. return;
  529. }
  530. if (this.processClosed) {
  531. this._setResult();
  532. }
  533. else if (this.processExited) {
  534. this.timeout = setTimeout(ExecState.HandleTimeout, this.delay, this);
  535. }
  536. }
  537. _debug(message) {
  538. this.emit('debug', message);
  539. }
  540. _setResult() {
  541. // determine whether there is an error
  542. let error;
  543. if (this.processExited) {
  544. if (this.processError) {
  545. error = new Error(`There was an error when attempting to execute the process '${this.toolPath}'. This may indicate the process failed to start. Error: ${this.processError}`);
  546. }
  547. else if (this.processExitCode !== 0 && !this.options.ignoreReturnCode) {
  548. error = new Error(`The process '${this.toolPath}' failed with exit code ${this.processExitCode}`);
  549. }
  550. else if (this.processStderr && this.options.failOnStdErr) {
  551. error = new Error(`The process '${this.toolPath}' failed because one or more lines were written to the STDERR stream`);
  552. }
  553. }
  554. // clear the timeout
  555. if (this.timeout) {
  556. clearTimeout(this.timeout);
  557. this.timeout = null;
  558. }
  559. this.done = true;
  560. this.emit('done', error, this.processExitCode);
  561. }
  562. static HandleTimeout(state) {
  563. if (state.done) {
  564. return;
  565. }
  566. if (!state.processClosed && state.processExited) {
  567. const message = `The STDIO streams did not close within ${state.delay /
  568. 1000} seconds of the exit event from process '${state.toolPath}'. This may indicate a child process inherited the STDIO streams and has not yet exited.`;
  569. state._debug(message);
  570. }
  571. state._setResult();
  572. }
  573. }
  574. //# sourceMappingURL=toolrunner.js.map