Skip to content

Commit

Permalink
Update MTA & MBT publish NPMJS process to remove binwrap dependency (#…
Browse files Browse the repository at this point in the history
…1063)

* https://jira.tools.sap/browse/MBT-75
[research] Update MTA & MBT publish NPMJS process to remove binwrap dependency

* replace ci with test
  • Loading branch information
wangshen-sap committed Aug 9, 2023
1 parent 5d2d3f7 commit 9a8407f
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 748 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
steps:
- checkout
- run: npm install
- run: npm run ci
- run: npm run test

build:
docker:
Expand Down Expand Up @@ -565,7 +565,7 @@ jobs:
command: |
echo "//registry.npmjs.org/:_authToken=$CLOUD_MTA_BOT_NPM_TOKEN" > .npmrc
npm install
npm run ci
npm run test
npm publish
workflows:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Dockerfile

integration/testdata/mta_demo/mta_archives/
docs/site
bin
.npmrc
unpacked_bin
test/goos/goos.yaml
33 changes: 33 additions & 0 deletions bin/mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node
var path = require("path");
var spawn = require("child_process").spawn;
var fs = require("fs");

var os = process.env.BINWRAP_PLATFORM || process.platform;
var arch = process.env.BINWRAP_ARCH || process.arch;

var requested = os + "-" + arch;
var current = process.platform + "-" + process.arch;
if (requested !== current ) {
console.error("WARNING: Using binaries for the requested platform (" + requested + ") instead of for the actual platform (" + current + ").")
}

var binExt = "";
if (os == "win32") {
binExt = ".exe";
}

var unpackedBinPath = path.join(__dirname, "..", "unpacked_bin");
var binPath = path.join(unpackedBinPath, "mbt" + binExt);

function execBin() {
spawn(
binPath,
process.argv.slice(2),
{stdio: 'inherit'}
).on('exit', process.exit);
}

if (fs.existsSync(binPath)) {
execBin();
}
21 changes: 1 addition & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
var binwrap = require('binwrap');
var path = require('path');

var packageInfo = require(path.join(__dirname, 'package.json'));
var version = packageInfo.version;
var root = (process.env.XMAKE_IMPORT_COMMON_0 ? `${process.env.XMAKE_IMPORT_COMMON_0}/com/github/sap/cloud-mta-build-tool/${version}/cloud-mta-build-tool-${version}-` : `https://github.com/SAP/cloud-mta-build-tool/releases/download/v${version}/cloud-mta-build-tool_${version}_`);

module.exports = binwrap({
dirname: __dirname,
binaries: [
'mbt'
],
urls: {
'darwin-arm64': root + 'Darwin_arm64.tar.gz',
'darwin-x64': root + 'Darwin_amd64.tar.gz',
'linux-x64': root + 'Linux_amd64.tar.gz',
'linux-arm64': root + 'Linux_arm64.tar.gz',
'win32-x64': root + 'Windows_amd64.tar.gz'
}
});
module.exports = {};
189 changes: 189 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
var fs = require("fs");
var axios = require("axios");
var tar = require("tar");
var zlib = require("zlib");
var unzip = require("unzip-stream");
var path = require("path");


var packageInfo = require(path.join(process.cwd(), "package.json"));
var version = packageInfo.version;

var binName = process.argv[2];
var os = process.argv[3] || process.platform;
var arch = process.argv[4] || process.arch;
var root = `https://github.com/SAP/${binName}/releases/download/v${version}/${binName}_${version}_`;


var requested = os + "-" + arch;
var current = process.platform + "-" + process.arch;
if (requested !== current ) {
console.error("WARNING: Installing binaries for the requested platform (" + requested + ") instead of for the actual platform (" + current + ").")
}

var unpackedBinPath = path.join(process.cwd(), "unpacked_bin");
var config = {
dirname: __dirname,
binaries: [
'mbt'
],
urls: {
'darwin-arm64': root + 'Darwin_arm64.tar.gz',
'darwin-x64': root + 'Darwin_amd64.tar.gz',
'linux-x64': root + 'Linux_amd64.tar.gz',
'win32-x64': root + 'Windows_amd64.tar.gz'
}
};
if (!fs.existsSync("bin")) {
fs.mkdirSync("bin");
}

var binExt = "";
if (os == "win32") {
binExt = ".exe";
}

var buildId = os + "-" + arch;
var url = config.urls[buildId];
if (!url) {
throw new Error("No binaries are available for your platform: " + buildId);
}
function binstall(url, path, options) {
if (url.endsWith(".zip")) {
return unzipUrl(url, path, options);
} else {
return untgz(url, path, options);
}
}

function untgz(url, path, options) {
options = options || {};

var verbose = options.verbose;
var verify = options.verify;

return new Promise(function (resolve, reject) {
var untar = tar
.x({ cwd: path })
.on("error", function (error) {
reject("Error extracting " + url + " - " + error);
})
.on("end", function () {
var successMessage = "Successfully downloaded and processed " + url;

if (verify) {
verifyContents(verify)
.then(function () {
resolve(successMessage);
})
.catch(reject);
} else {
resolve(successMessage);
}
});

var gunzip = zlib.createGunzip().on("error", function (error) {
reject("Error decompressing " + url + " " + error);
});

try {
fs.mkdirSync(path);
} catch (error) {
if (error.code !== "EEXIST") throw error;
}

if (verbose) {
console.log("Downloading binaries from " + url);
}

axios
.get(url, { responseType: "stream" })
.then((response) => {
response.data.pipe(gunzip).pipe(untar);
})
.catch((error) => {
if (verbose) {
console.error(error);
} else {
console.error(error.message);
}
});
});
}

function unzipUrl(url, path, options) {
options = options || {};

var verbose = options.verbose;
var verify = options.verify;

return new Promise(function (resolve, reject) {
var writeStream = unzip
.Extract({ path: path })
.on("error", function (error) {
reject("Error extracting " + url + " - " + error);
})
.on("entry", function (entry) {
console.log("Entry: " + entry.path);
})
.on("close", function () {
var successMessage = "Successfully downloaded and processed " + url;

if (verify) {
verifyContents(verify)
.then(function () {
resolve(successMessage);
})
.catch(reject);
} else {
resolve(successMessage);
}
});

if (verbose) {
console.log("Downloading binaries from " + url);
}

axios
.get(url, { responseType: "stream" })
.then((response) => {
response.data.pipe(writeStream);
})
.catch((error) => {
if (verbose) {
console.error(error);
} else {
console.error(error.message);
}
});
});
}

function verifyContents(files) {
return Promise.all(
files.map(function (filePath) {
return new Promise(function (resolve, reject) {
fs.stat(filePath, function (err, stats) {
if (err) {
reject(filePath + " was not found.");
} else if (!stats.isFile()) {
reject(filePath + " was not a file.");
} else {
resolve();
}
});
});
})
);
}

binstall(url, unpackedBinPath).then(function() {
config.binaries.forEach(function(bin) {
fs.chmodSync(path.join(unpackedBinPath, bin + binExt), "755");
});
}).then(function(result) {
process.exit(0);
}, function(result) {
console.error("ERR", result);
process.exit(1);
});

0 comments on commit 9a8407f

Please sign in to comment.