[haxe] generate project.xml for openfl build

This commit is contained in:
2018-04-05 11:33:29 +03:00
parent 99a9f5638c
commit f94eb8745c
3 changed files with 47 additions and 35 deletions

View File

@@ -11,6 +11,8 @@ const colors = require('ansi-colors');
const log = require('fancy-log');
const vfs = require('vinyl-fs');
const rmdir = require('rmdir');
const template = require('lodash.template');
const mkdirp = require('mkdirp');
class Haxe extends Sdk {
@@ -143,17 +145,23 @@ class Haxe extends Sdk {
* values: {},
* macro: [],
* outputFile: 'out.swf',
* debug: false
* }
*/
openfl(params) {
params = Object.assign({
title: null,
pack: null,
company: null,
version: null,
lib: [],
cp: [],
asset: [],
main: null,
values: {},
macro: [],
debug: false,
outputFile: null,
debug: false
}, params);
const files = [];
@@ -167,45 +175,30 @@ class Haxe extends Sdk {
const endStream = (callback) => {
log(this.tag, colors.cyan(`openfl ${params.command} ${params.platform}`));
const args = ['-cwd', files[0].path, 'run', 'openfl', params.command, params.platform];
// lib
let lib = params.lib;
if (!Array.isArray(lib)) {
lib = Object.entries(lib).map(([k, v]) => `${k}:${v.split('@')[0]}`);
if (!Array.isArray(params.lib)) {
params.lib = Object.entries(params.lib).map(([k, v]) => ({name: k, version: v.split('@')[0]}));
}
for (const item of lib) {
args.push(`--haxelib="${item}"`);
}
// cp
for (const item of params.cp) {
args.push(`--source="${item}"`);
}
if (params.main) {
args.push(`--app-main="${params.main}"`);
}
// macro
for (const macro of params.macro) {
args.push(`--haxeflag="--macro ${macro}"`);
}
for (let key of Object.keys(params.values)) {
const value = params.values[key];
if (value === true) {
args.push(`-D${key}`);
} else if (value) {
args.push(`-D${key}="${value}"`);
}
}
const buildDir = path.join(os.tmpdir(), 'build');
args.push(`--app-path="${buildDir}"`);
if (params.outputFile) {
args.push(`--app-file="${params.outputFile}"`);
}
if (params.version) args.push(`--meta-version=${params.version}`);
params.cp = params.cp.map(item => path.resolve(files[0].path, item));
params.asset = params.asset.map(item => path.resolve(files[0].path, item));
const buildDir = path.join(os.tmpdir(), 'build', params.outputFile);
params.buildDir = buildDir;
mkdirp.sync(params.buildDir);
const projectTemplate = template(fs.readFileSync(path.resolve(__dirname, '..', 'template/project.xml')));
const project = projectTemplate(params);
fs.writeFileSync(path.resolve(buildDir, 'project.xml'), project);
const args = ['-cwd', params.buildDir, 'run', 'openfl', params.command, params.platform];
if (params.debug) {
args.push('-debug');
}
const target = `${buildDir}/${params.platform}/bin`;
rmdir(target);
this.haxelib(args).then(() => {
vfs.src(`${target}/**/*`).pipe(through.obj((file, enc, cb) => {
stream.push(file);