Skip to content

Commit

Permalink
Escape backslashes in filespecs on windows #5
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Aug 9, 2018
1 parent 5a702e3 commit 6a99fb0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
7 changes: 4 additions & 3 deletions ArtifactoryGenericDownload/downloadArtifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ function RunTaskCbk(cliPath) {
let fileSpec = tl.getInput("fileSpec", false);
let collectBuildInfo = tl.getBoolInput("collectBuildInfo");

console.log("Using file spec:");
console.log(fileSpec);
// Write provided fileSpec to file
try {
fileSpec = utils.fixWindowsPaths(fileSpec);
console.log("Using file spec:");
console.log(fileSpec);
// Write provided fileSpec to file
tl.writeFile(specPath, fileSpec);
} catch (ex) {
tl.setResult(tl.TaskResult.Failed, ex);
Expand Down
7 changes: 4 additions & 3 deletions ArtifactoryGenericUpload/uploadArtifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ function RunTaskCbk(cliPath) {
let fileSpec = tl.getInput("fileSpec", false);
let collectBuildInfo = tl.getBoolInput("collectBuildInfo");

console.log("Using file spec:");
console.log(fileSpec);
// Write provided fileSpec to file
try {
fileSpec = utils.fixWindowsPaths(fileSpec);
console.log("Using file spec:");
console.log(fileSpec);
// Write provided fileSpec to file
tl.writeFile(specPath, fileSpec);
} catch (ex) {
tl.setResult(tl.TaskResult.Failed, ex);
Expand Down
42 changes: 35 additions & 7 deletions jfrog-utils/jutils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const os = require('os');
const fs = require('fs-extra');
const tl = require('vsts-task-lib/task');
const checksumStream = require('checksum-stream');
const path = require('path');
Expand Down Expand Up @@ -26,7 +27,8 @@ module.exports = {
quote: quote,
addArtifactoryCredentials: addArtifactoryCredentials,
addStringParam: addStringParam,
addBoolParam: addBoolParam
addBoolParam: addBoolParam,
fixWindowsPaths: fixWindowsPaths
};

function executeCliTask(runTaskFunc) {
Expand Down Expand Up @@ -150,14 +152,13 @@ function downloadCli(attemptNumber) {
}).on('error', handleError)
.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
fs.copyFile(cliTmpPath, versionedCliPath, () => {
fs.move(cliTmpPath, versionedCliPath).then(() => {
if (!process.platform.startsWith("win")) {
fs.chmodSync(versionedCliPath, 0o555)
fs.chmodSync(versionedCliPath, 0o555);
}
console.log("Finished downloading jfrog cli");
fs.unlinkSync(cliTmpPath);
resolve();
});
})
}
})
).pipe(
Expand Down Expand Up @@ -190,4 +191,31 @@ function getFileName() {
executable += ".exe"
}
return executable
}
}

/**
* Escape single backslashes in fileSpec.
* / -> //
* // -> //
* @param fileSpec (String) - The file spec to escape
* @returns fileSpec (String) - The file spec after escaping
*/
function fixWindowsPaths(fileSpec) {
if (os.type() === "Windows_NT") {
fileSpec = fileSpec.replace(/([^\\])\\(?!\\)/g, '$1\\\\');
validateSpecWithoutRegex(fileSpec);
return fileSpec
}
return fileSpec;
}

function validateSpecWithoutRegex(fileSpec) {
let files = JSON.parse(fileSpec)["files"];
for (const file of Object.keys(files)) {
let values = files[file];
let regexp = values["regexp"];
if (regexp && regexp.toLowerCase() === "true") {
throw ("The File Spec includes 'regexp: true' which is currently not supported.");
}
}
}
1 change: 1 addition & 0 deletions jfrog-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "jutils.js",
"dependencies": {
"vsts-task-lib": "^2.6.0",
"fs-extra": "^7.0.0",
"checksum-stream": "^1.0.3",
"request": "^2.87.0",
"request-promise": "^4.2.2"
Expand Down

0 comments on commit 6a99fb0

Please sign in to comment.