Skip to content

Commit

Permalink
CmdLine on Linux (#4609)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple authored Jun 22, 2017
1 parent 3d06df1 commit 47678c0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"loc.input.label.workingDirectory": "Working Directory",
"loc.input.label.failOnStderr": "Fail on Standard Error",
"loc.input.help.failOnStderr": "If this is true, this task will fail if any errors are written to the StandardError stream.",
"loc.messages.CmdLineReturnCode": "%s exited with return code: %d",
"loc.messages.CmdLineFailed": "%s failed with error: %s",
"loc.messages.PS_ExitCode": "powershell exited with code '{0}'.",
"loc.messages.PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from powershell."
"loc.messages.JS_Stderr": "bash wrote one or more lines to the standard error stream.",
"loc.messages.JS_ExitCode": "bash exited with code '%s'.",
"loc.messages.PS_ExitCode": "cmd.exe exited with code '{0}'.",
"loc.messages.PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from cmd.exe."
}
67 changes: 67 additions & 0 deletions Tasks/CmdLine/cmdline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs = require('fs');
import path = require('path');
import os = require('os');
import tl = require('vsts-task-lib/task');
import tr = require('vsts-task-lib/toolrunner');
var uuidV4 = require('uuid/v4');

async function run() {
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));

// Get inputs.
let failOnStderr = tl.getBoolInput('failOnStderr', false);
let script: string = tl.getInput('script', false) || '';
let workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);

// Write the script to disk.
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.sh');
await fs.writeFileSync(
filePath,
'\ufeff' + script, // Prepend the Unicode BOM character.
{ encoding: 'utf8' }); // Since UTF8 encoding is specified, node will
// // encode the BOM into its UTF8 binary sequence.

// Create the tool runner.
let bash = tl.tool(tl.which('bash', true))
.arg('--noprofile')
.arg(`--norc`)
.arg(filePath);
let options = <tr.IExecOptions>{
cwd: workingDirectory,
failOnStdErr: false,
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
ignoreReturnCode: true
};

// Listen for stderr.
let stderrFailure = false;
if (failOnStderr) {
bash.on('stderr', (data) => {
stderrFailure = true;
});
}

// Run bash.
let exitCode: number = await bash.exec(options);

// Fail on exit code.
if (exitCode !== 0) {
tl.setResult(tl.TaskResult.Failed, tl.loc('JS_ExitCode', exitCode));
}

// Fail on stderr.
if (stderrFailure) {
tl.setResult(tl.TaskResult.Failed, tl.loc('JS_Stderr'));
}
}
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
}

run();
5 changes: 3 additions & 2 deletions Tasks/CmdLine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vsts-cmdline-task",
"version": "1.0.0",
"description": "VSTS Command Line Task",
"main": "cmdlinetask.js",
"main": "cmdline.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -17,6 +17,7 @@
},
"homepage": "https://github.com/Microsoft/vsts-tasks#readme",
"dependencies": {
"vsts-task-lib": "^0.9.20"
"uuid": "^3.0.1",
"vsts-task-lib": "2.0.5"
}
}
10 changes: 5 additions & 5 deletions Tasks/CmdLine/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@
]
},
"Node": {
"target": "cmdlinetask.js",
"target": "cmdline.js",
"argumentFormat": ""
}
},
"messages": {
"CmdLineReturnCode": "%s exited with return code: %d",
"CmdLineFailed": "%s failed with error: %s",
"PS_ExitCode": "powershell exited with code '{0}'.",
"PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from powershell."
"JS_Stderr": "bash wrote one or more lines to the standard error stream.",
"JS_ExitCode": "bash exited with code '%s'.",
"PS_ExitCode": "cmd.exe exited with code '{0}'.",
"PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from cmd.exe."
}
}
6 changes: 3 additions & 3 deletions Tasks/CmdLine/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@
]
},
"Node": {
"target": "cmdlinetask.js",
"target": "cmdline.js",
"argumentFormat": ""
}
},
"messages": {
"CmdLineReturnCode": "ms-resource:loc.messages.CmdLineReturnCode",
"CmdLineFailed": "ms-resource:loc.messages.CmdLineFailed",
"JS_Stderr": "ms-resource:loc.messages.JS_Stderr",
"JS_ExitCode": "ms-resource:loc.messages.JS_ExitCode",
"PS_ExitCode": "ms-resource:loc.messages.PS_ExitCode",
"PS_UnableToDetermineExitCode": "ms-resource:loc.messages.PS_UnableToDetermineExitCode"
}
Expand Down

0 comments on commit 47678c0

Please sign in to comment.