hpmo
Version:
hybirdApp package manager
214 lines (186 loc) • 6.35 kB
JavaScript
/**
*
* hpm build
* @namespace hpm
* @author 仈爪 <haibin.zhb@alipay.com>
* @version 1.0.0
*
* */
var fs = require('fs-extra'),
fs2 = require('fs'),
http = require('http'),
path = require('path'),
file = require('./sdk/file'),
map = require('map-stream'),
crypto = require('crypto'),
XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest,
spawn = require('child_process').spawn;
require('colors');
var gulp = require("gulp"),
zip = require("gulp-zip"),
tar = require('gulp-tar'),
gulpif = require('gulp-if'),
uncss = require('gulp-uncss'),
minifycss = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
ignore = require('gulp-ignore');
var ROOT_PATH = process.cwd();
var DIST_DIR = './_dist';
var DIST_PATH = path.join(ROOT_PATH, DIST_DIR);
var PACKAGE_DIR = './_package';
var PACKAGE_PATH = path.join(ROOT_PATH, PACKAGE_DIR);
var OPTIONS, HPMFILE, isCBed;
var CALLBACK;
module.exports = function (options, cb) {
OPTIONS = options;
CALLBACK = cb;
var hpmfile = file.readJSON('hpmfile.json');
if (!hpmfile) {
console.log("Fail: Can't find hpmfile.json !\r\n".red);
return;
}
HPMFILE = hpmfile;
// 如果存在prebuild.sh文件,则先调用shell执行该文件后再进行build
var prebuild = hpmfile.preBuild || 'prebuild.sh';
var prebuildPath = path.join(ROOT_PATH, prebuild);
if (fs.existsSync(prebuildPath)) {
console.log('\r\n========= start prebuild ========================='.green);
var ls = spawn('sh', [prebuild]);
ls.stdout.on('data', function (data) {
var msg = data.toString().replace(/\n$/i, "") || "";
console.log(msg);
});
ls.stderr.on('data', function (data) {
console.error(data.toString().red);
});
ls.on('exit', function (code) {
console.log('========= prebuild end ========================='.green);
setTimeout(function () {
main(options, hpmfile);
}, 500);
});
} else {
main(options, hpmfile);
}
};
function main(options, hpmfile) {
if (checkHpmfile(hpmfile)) {
hpmfile.version = options.version || hpmfile.version || "1.0.0.0";
file.writeJSON('hpmfile.json', hpmfile);
archiving(options, hpmfile);
}
}
function checkHpmfile(hpmfile) {
var rtv = true;
if (!hpmfile.appid) {
console.log("Fail: " + ("\"appid\"").yellow + " Can't be empty in hpmfile.json!\r\n".red);
rtv = false;
} else if (!hpmfile.host) {
rtv = false;
console.log("Fail: " + ("\"host\"").yellow + " Can't be empty in hpmfile.json!\r\n".red);
}
return rtv;
}
function archiving(options, hpmfile) {
console.log(('# start archiving amr...').yellow);
if (fs.existsSync(DIST_PATH)) {
fs.removeSync(DIST_PATH);
}
var distSrc = [];
if (!!hpmfile.build && !!hpmfile.build.include) {
hpmfile.build.include.forEach(function (v, i) {
distSrc.push(v);
});
}
distSrc.push('!' + PACKAGE_DIR, '!' + '!' + PACKAGE_DIR + '/**', './hpmfile.json');
if (!!hpmfile.build && !!hpmfile.build.ignore) {
hpmfile.build.ignore.forEach(function (v, i) {
distSrc.push('!' + v);
});
}
var files84 = [];
gulp.task('dist84', function () {
// 8.4+
var src = gulp.src(distSrc)
.on('end', function () {
//转换为Manifest.xml
buildManifestSync(hpmfile, DIST_PATH, true);
setTimeout(function () {
gulp.start('tar');
}, 500);
});
src.pipe(gulp.dest(function (file) {
var distDir = DIST_DIR + '/84/_tar';
if (file.base.indexOf(ROOT_PATH) > -1) {
distDir += file.base.replace(ROOT_PATH, "");
}
return distDir;
}));
});
gulp.task('tar', function () {
return gulp.src(DIST_DIR + '/84/_tar/**/*')
.on('end', function () {
var _tarPath = DIST_PATH + '/84/_tar';
if (fs.existsSync(_tarPath)) {
fs.removeSync(_tarPath);
}
setTimeout(function () {
gulpPkg(options, hpmfile);
}, 500);
})
.pipe(tar(hpmfile.appid + '.tar'))
.pipe(gulp.dest(DIST_DIR + '/84'))
});
gulp.start('dist84');
}
function gulpPkg(options, hpmfile, cb) {
options.tar = true;
var amrFilename = hpmfile.appid +
'-' + hpmfile.version.replace(/\./g, "_") +
(options.environ ? '-' + options.environ : "") +
(!!options.tar ? "+TAR" : "") +
'.amr';
var packageDir = PACKAGE_DIR;
if (options.isDebug === true) {
amrFilename = hpmfile.appid + (!options.tar ? "" : "+TAR") + '.amr';
} else {
packageDir += '/' + hpmfile.version;
}
var amrPath = path.join(ROOT_PATH, packageDir + '/' + amrFilename);
var srcPath = DIST_DIR + (!options.tar ? "/83" : "/84") + '/**/*';
gulp.task('zip', function () {
return gulp.src(srcPath)
.on('end', function () {
console.log('# packed at ' + amrPath.green);
cb && cb(!!options.tar);
})
.pipe(zip(amrFilename))
.pipe(gulp.dest(packageDir));
});
gulp.start('zip');
}
function scanPipe(files) {
function fn(file, cb) {
!file.isDirectory() && files.push(file.relative);
}
return map(fn);
}
function buildManifestSync(hpmfile, manifestDir, is84) {
try {
var out = [];
out.push('<?xml version="1.0" encoding="utf-8"?>');
out.push('<package>');
out.push(' <uid>' + hpmfile.appid + '</uid>');
out.push(' <name>' + hpmfile.name + '</name>');
out.push(' <version>' + hpmfile.version + '</version>');
out.push('</package>');
var outStr = out.join('\n');
if (is84) {
fs.writeFileSync(manifestDir + '/84/Manifest.xml', outStr);
} else {
fs.writeFileSync(manifestDir + '/83/Manifest.xml', outStr);
}
} catch (e) {
console.error('# write Manifest.xml error:'.red, e);
}
}