const gulp = require("gulp"); const fs = require("fs"); const fse = require("fs-extra"); const path = require("path"); const through = require("through2"); class Package { constructor(platform, type, version, versionInt) { this.platform = platform; this.type = type; this.version = version; this.versionInt = versionInt; } get key() { return [this.platform, this.type].join(":"); } static getPlatform(filename) { for (const [platform, r] of [ ["android", /^.*?\.apk$/], ["linux", /^.*?\.deb$/], ["linux", /^.*?linux\.tar\.gz$/], ["windows", /^.*?win\.zip$/], ["windows", /^.*?\.exe$/], ]) { if (r.test(filename)) { return platform; } } return null; } static getType(filename) { for (const [type, r] of [ ["apk", /^.*?\.apk$/], ["deb", /^.*?\.deb$/], ["archive", /^.*?\.tar\.gz$/], ["archive", /^.*?\.zip$/], ["exe", /^.*?\.exe$/], ]) { if (r.test(filename)) { return type; } } return null; } static getVersion(filename) { const m = /(\d+)\.(\d+)\.(\d+)/.exec(filename); if (m) { return [ m[0], parseInt(m[1]) * 10000 + parseInt(m[2]) * 100 + parseInt(m[3]), ]; } return null; } static parseFilename(filename) { const platform = this.getPlatform(filename); const type = this.getType(filename); const version = this.getVersion(filename); if (platform && type && version) { return new Package(platform, type, version[0], version[1]); } return null; } } module.exports = (name, version, publishDir, publishUrl) => function publish(cb) { const packages = {}; fse.ensureDirSync(publishDir); return gulp.series([ function copy() { return gulp .src("target/app/@(android|archive|debian|installer)/*") .pipe( through.obj(function (file, enc, cb) { const pack = Package.parseFilename(file.path); if (pack) { this.push(file); } cb(null); }) ) .pipe(gulp.dest(publishDir)); }, function generate() { return gulp .src(`${publishDir}/*/*`) .pipe( through.obj(function (file, enc, cb) { const basepath = file.path.replace(file.base + path.sep, ""); const pack = Package.parseFilename(file.path); if (pack) { if ( !packages[pack.key] || packages[pack.key].versionInt < pack.versionInt ) { packages[pack.key] = { platform: pack.platform, type: pack.type, version: pack.version, versionInt: pack.versionInt, path: basepath, filename: basepath.split(path.sep).pop(), url: `${publishUrl}/${basepath.replace(path.sep, "/")}`, }; } } cb(null); }) ) .on("end", function () { fs.writeFileSync( path.join(publishDir, "packages.json"), JSON.stringify( { name: name, version: version, packages: Object.values(packages), }, null, 4 ) ); }); }, ])(cb); }; module.exports.Package = Package;