Files
tankz/build/project.js

295 lines
6.7 KiB
JavaScript

const gulp = require('gulp');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const template = require('gulp-template');
const {Haxe, FlashPlayer, Neko} = require('gulp-haxetool');
const Debug = require('./debug');
const webserver = require('gulp-webserver');
const run = require('gulp-run');
const tail = require('./tail');
const deb = require('gulp-debian');
/**
*
*/
const Platform = {
FLASH: 'flash',
HTML5: 'html5',
LINUX: 'linux',
NEKO: 'neko',
};
class Config {
constructor({title, pack, company, name, version, lib=[], cp=[], asset=[], main=null, values={}, macro=[]}) {
this.title = title;
this.pack = pack;
this.company = company;
this.name = name;
this.version = version;
this.lib = lib;
this.cp = cp;
this.asset = asset;
this.main = main;
this.values = values;
this.macro = macro;
}
update({name, version, lib=[], cp=[], asset=[], main=null, values={}, macro=[]}) {
return new Config({
title: this.title,
pack: this.pack,
company: this.company,
name: name || this.name,
version: version || this.version,
//lib: this.lib.concat(lib), //ToDo: check if object
lib: this.lib,
cp: this.cp.concat(cp),
asset: this.asset.concat(asset),
main: main || this.main,
values: {...this.values, ...values},
macro: this.macro.concat(macro),
})
}
}
/**
*
*/
class Builder {
constructor(platform, config) {
this.platform = platform;
this.config = config;
this.haxe = new Haxe();
this.target = 'target';
}
prepare() {
return this.haxe.prepare();
}
macro(debug) {
let macro = [];
if (debug) {
macro = macro.concat(debug.macro());
}
return macro.concat(this.config.macro);
}
call(debug) {
throw 'Not Implemented';
}
static register(platform, builder) {
Builder.factory[platform] = builder;
}
static new(platform, config) {
return new Builder.factory[platform](platform, config);
}
}
Builder.factory = {};
/**
*
*/
class OpenFLBuilder extends Builder {
call(debug) {
return gulp.src('.')
.pipe(this.haxe.openfl({
command: 'build',
platform: this.platform,
title: this.config.title,
pack: this.config.pack,
company: this.config.company,
version: this.config.version,
lib: this.config.lib,
cp: this.config.cp,
asset: this.config.asset,
main: this.config.main,
debug: debug,
values: this.config.values,
macro: this.macro(debug),
outputFile: this.config.name,
}))
.pipe(gulp.dest(`${this.target}/${this.platform}`));
}
}
Builder.register(Platform.FLASH, OpenFLBuilder);
Builder.register(Platform.HTML5, OpenFLBuilder);
Builder.register(Platform.LINUX, OpenFLBuilder);
/**
*
*/
class HaxeBuilder extends Builder {
call(debug) {
return gulp.src('.')
.pipe(new Haxe().build({
platform: this.platform,
version: this.config.version,
lib: this.config.lib,
cp: this.config.cp,
main: this.config.main,
debug: debug,
values: this.config.values,
macro: this.macro(debug),
outputFile: this.config.name + '.n', // ToDo: for neko only
}))
.pipe(gulp.dest(`${this.target}/${this.platform}`));
}
}
Builder.register(Platform.NEKO, HaxeBuilder);
/**
*
*/
class Runner {
constructor(platform, name) {
this.platform = platform;
this.name = name;
this.target = 'target';
}
prepare() {
return Promise.resolve();
}
call(debug) {
throw 'Not Implemented';
}
static register(platform, builder) {
Runner.factory[platform] = builder;
}
static new(platform, name) {
return new Runner.factory[platform](platform, name);
}
}
Runner.factory = {};
/**
*
*/
class FlashRunner extends Runner {
constructor(platform, name) {
super(platform, name);
this.player = new FlashPlayer();
}
prepare() {
return this.player.prepare();
}
call(debug) {
return gulp.src(`${this.target}/${this.platform}/${this.name}.swf`)
.pipe(this.player.run(true))
.pipe(debug.run());
}
}
Runner.register(Platform.FLASH, FlashRunner);
/**
*
*/
class Html5Runner extends Runner {
call(debug) {
return gulp.src(`${this.target}/${this.platform}`)
.pipe(webserver({
host: 'localhost', port: 3000,
open: true,
fallback: 'index.html'
}));
}
}
Runner.register(Platform.HTML5, Html5Runner);
/**
*
*/
class LinuxRunner extends Runner {
call(debug) {
return gulp.src(`${this.target}/${this.platform}/${this.name}`)
.pipe(run(`./${this.name}`, {cwd: `target/${this.platform}`, verbosity: 1}))
.pipe(tail(Debug.log));
}
}
Runner.register(Platform.LINUX, LinuxRunner);
/**
*
*/
class NekoRunner extends Runner {
call(debug) {
return gulp.src(`${this.target}/${this.platform}/${this.name}.n`)
.pipe(new Neko().run())
.pipe(debug.run());
}
}
Runner.register(Platform.NEKO, NekoRunner);
/**
*
*/
class Project {
constructor(config, platforms=[]) {
this.config = config;
this.platforms = platforms;
}
build(platform) {
const builder = Builder.new(platform, this.config);
return gulp.series(
() => builder.prepare(),
() => builder.call()
);
}
run(platform) {
const builder = Builder.new(platform, this.config);
const runner = Runner.new(platform, this.config.name);
const debug = new Debug();
return gulp.series(
() => builder.prepare(),
() => builder.call(debug),
() => runner.prepare(),
() => runner.call(debug)
);
}
bind(module) {
for (const platform of this.platforms) {
module.exports[`${this.config.name}:${platform}:build`] = this.build(platform);
module.exports[`${this.config.name}:${platform}:run`] = this.run(platform);
}
return this;
}
}
Project.Platform = Platform;
Project.Config = Config;
Project.Builder = Builder;
Project.Runner = Runner;
module.exports = Project;