Skip to content

Commit

Permalink
Removed unsupported lib, added async methods (#932)
Browse files Browse the repository at this point in the history
* Removed unsupported lib, added async methods

* Update 3rd party library change to async

* Format style in ThirdPartyNotice
  • Loading branch information
ismayilov-ismayil authored Sep 19, 2023
1 parent 8e26b3e commit 561944d
Show file tree
Hide file tree
Showing 11 changed files with 999 additions and 527 deletions.
5 changes: 5 additions & 0 deletions node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ Backported from ver.`3.4.0`:

- Add `getBoolFeatureFlag` [#936](https://github.com/microsoft/azure-pipelines-task-lib/pull/936)


## 4.5.0

- Added `execAsync` methods that return native promises. Marked `exec` methods that return promises from the Q library as deprecated [#905](https://github.com/microsoft/azure-pipelines-task-lib/pull/905)

## 4.6.0

- Replaced deprecated "sync-request" lib and Added new Async methods - [#932](https://github.com/microsoft/azure-pipelines-task-lib/pull/932)
8 changes: 5 additions & 3 deletions node/ThirdPartyNotice.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This Azure Pipelines extension (azure-pipelines-task-lib) is based on or incorpo
30. semver (git+https://github.com/npm/node-semver.git)
31. shelljs (git://github.com/arturadib/shelljs.git)
32. string_decoder (git://github.com/rvagg/string_decoder.git)
33. sync-request (git+https://github.com/ForbesLindesay/sync-request.git)
33. nodejs-file-downloader (git://github.com/ibrod83/nodejs-file-downloader.git)
34. then-request (git+https://github.com/then/then-request.git)
35. typedarray (git://github.com/substack/typedarray.git)
36. typescript (git+https://github.com/Microsoft/TypeScript.git)
Expand Down Expand Up @@ -889,7 +889,8 @@ IN THE SOFTWARE.
=========================================
END OF string_decoder NOTICES, INFORMATION, AND LICENSE

%% sync-request NOTICES, INFORMATION, AND LICENSE BEGIN HERE

%% nodejs-file-downloader NOTICES, INFORMATION, AND LICENSE BEGIN HERE
=========================================
Copyright (c) 2014 Forbes Lindesay

Expand All @@ -911,7 +912,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=========================================
END OF sync-request NOTICES, INFORMATION, AND LICENSE
END OF nodejs-file-downloader NOTICES, INFORMATION, AND LICENSE


%% then-request NOTICES, INFORMATION, AND LICENSE BEGIN HERE
=========================================
Expand Down
140 changes: 79 additions & 61 deletions node/buildutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var fs = require('fs');
var os = require('os');
var path = require('path');
var process = require('process');
var syncRequest = require('sync-request');
var admZip = require('adm-zip');
var deasync = require('deasync')
const Downloader = require("nodejs-file-downloader");

var downloadPath = path.join(__dirname, '_download');
var testPath = path.join(__dirname, '_test');

exports.run = function(cl) {
exports.run = function (cl) {
console.log('> ' + cl);
var rc = exec(cl).code;
if (rc !== 0) {
Expand All @@ -19,7 +21,9 @@ exports.run = function(cl) {
}
var run = exports.run;

exports.getExternals = function () {


const getExternalsAsync = async () => {
if (process.env['TF_BUILD']) {
// skip adding node 5.10.1 to the PATH. the CI definition tests against node 5 and 6.
return;
Expand All @@ -38,16 +42,16 @@ exports.getExternals = function () {
var nodeVersion = 'v16.13.0';
switch (platform) {
case 'darwin':
var nodeArchivePath = downloadArchive(nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz');
var nodeArchivePath = await downloadArchiveAsync(nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz');
addPath(path.join(nodeArchivePath, 'node-' + nodeVersion + '-darwin-x64', 'bin'));
break;
case 'linux':
var nodeArchivePath = downloadArchive(nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz');
var nodeArchivePath = await downloadArchiveAsync(nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz');
addPath(path.join(nodeArchivePath, 'node-' + nodeVersion + '-linux-x64', 'bin'));
break;
case 'win32':
var nodeExePath = downloadFile(nodeUrl + '/' + nodeVersion + '/win-x64/node.exe');
var nodeLibPath = downloadFile(nodeUrl + '/' + nodeVersion + '/win-x64/node.lib');
var nodeExePath = await downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.exe');
var nodeLibPath = await downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.lib');
var nodeDirectory = path.join(testPath, 'node');
mkdir('-p', nodeDirectory);
cp(nodeExePath, path.join(nodeDirectory, 'node.exe'));
Expand All @@ -57,83 +61,97 @@ exports.getExternals = function () {
}
}

var downloadFile = function (url) {
exports.getExternalsAsync = getExternalsAsync



/**
* @deprecated This method uses library which is not prefered to use on production
*/
exports.getExternals = function () {
var result = false;
getExternalsAsync().then(t => result = true);
deasync.loopWhile(function () { return !result });
return result;
}


var downloadFileAsync = async function (url, fileName) {
// validate parameters
if (!url) {
throw new Error('Parameter "url" must be set.');
}

// short-circuit if already downloaded
// skip if already downloaded
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
var targetPath = path.join(downloadPath, 'file', scrubbedUrl);
if (fileName == undefined) {
fileName = scrubbedUrl;
}
var targetPath = path.join(downloadPath, 'file', fileName);
var marker = targetPath + '.completed';
if (!test('-f', marker)) {
console.log('Downloading file: ' + url);
if (test('-f', marker)) {
console.log('File already exists: ' + targetPath);
return targetPath;
}

// delete any previous partial attempt
if (test('-f', targetPath)) {
rm('-f', targetPath);
}
console.log('Downloading file: ' + url);
// delete any previous partial attempt
if (test('-f', targetPath)) {
rm('-f', targetPath);
}

// download the file
mkdir('-p', path.join(downloadPath, 'file'));
var result = syncRequest('GET', url);
fs.writeFileSync(targetPath, result.getBody());
// download the file
mkdir('-p', path.join(downloadPath, 'file'));

// write the completed marker
fs.writeFileSync(marker, '');
}
const downloader = new Downloader({
url: url,
directory: path.join(downloadPath, 'file'),
fileName: fileName
});

return targetPath;
}
const { fileName: downloadedFileName } = await downloader.download(); // Downloader.download() resolves with some useful properties.
fs.writeFileSync(marker, '');
return downloadedFileName;

var downloadArchive = function (url) {
// validate platform
var platform = os.platform();
if (platform != 'darwin' && platform != 'linux') {
throw new Error('Unexpected platform: ' + platform);
}
};

// validate parameters

var downloadArchiveAsync = async function (url, fileName) {
if (!url) {
throw new Error('Parameter "url" must be set.');
}

if (!url.match(/\.tar\.gz$/)) {
throw new Error('Expected .tar.gz');
// skip if already downloaded and extracted
var scrubbedUrl = url.replace(/[\/\\:?]/g, '_');
if (fileName != undefined) {
scrubbedUrl = fileName;
}

// short-circuit if already downloaded and extracted
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
var targetPath = path.join(downloadPath, 'archive', scrubbedUrl);
var marker = targetPath + '.completed';
if (!test('-f', marker)) {
// download the archive
var archivePath = downloadFile(url);
console.log('Extracting archive: ' + url);

// delete any previously attempted extraction directory
if (test('-d', targetPath)) {
rm('-rf', targetPath);
}

// extract
mkdir('-p', targetPath);
var cwd = process.cwd();
process.chdir(targetPath);
try {
run('tar -xzf "' + archivePath + '"');
}
finally {
process.chdir(cwd);
}

// write the completed marker
fs.writeFileSync(marker, '');
if (test('-f', marker)) {
return targetPath;
}

// download the archive
var archivePath = await downloadFileAsync(url, scrubbedUrl);
console.log('Extracting archive: ' + url);

// delete any previously attempted extraction directory
if (test('-d', targetPath)) {
rm('-rf', targetPath);
}

// extract
mkdir('-p', targetPath);
var zip = new admZip(archivePath);
zip.extractAllTo(targetPath);

// write the completed marker
fs.writeFileSync(marker, '');

return targetPath;
}
};


var addPath = function (directory) {
var separator;
Expand Down
Loading

0 comments on commit 561944d

Please sign in to comment.