big update

This commit is contained in:
2018-04-06 16:29:05 +03:00
parent 294fab3279
commit 60589db248
13 changed files with 412 additions and 201 deletions

View File

@@ -27,6 +27,7 @@ class Config {
this.sources = [];
this.assets = [];
this.libs = [];
this.macros = [];
this.meta = {
title: null,
version: null,
@@ -46,6 +47,7 @@ class Config {
if (params.sources !== undefined) this.sources = this.sources.concat(params.sources.map(item => path.resolve(cwd, item)));
if (params.assets !== undefined) this.assets = this.assets.concat(params.assets.map(item => path.resolve(cwd, item)));
if (params.libs !== undefined) this.libs = this.libs.concat(Array.isArray(params.libs) ? params.libs : Object.entries(params.libs).map(([k, v]) => ({name: k, version: v})));
if (params.macros !== undefined) this.macros = this.macros.concat(params.macros);
if (params.meta !== undefined) this.meta = {...this.meta, ...params.meta};
}

View File

@@ -1,13 +1,11 @@
const net = require('net');
const through = require('through2');
const colors = require('ansi-colors');
const log = require('fancy-log');
const _colors = {
'[DEBUG]': colors.white,
'[INFO]': colors.cyan,
'[ERROR]': colors.red,
'Called from ': colors.red,
'[WARNING]': colors.yellow,
};
@@ -23,58 +21,20 @@ const getColor = (line) => {
class Debug {
static log (line, color) {
if (color === undefined) {
color = getColor(line) || colors.white;
}
if (line[0] === '\t') {
console.log(color(line));
} else {
const result = line.split(' ');
console.log(colors.gray(result.slice(0, 4).join(' ')) + ' ' + color(result.slice(4).join(' ')));
}
};
constructor() {
this.host = 'localhost';
this.port = 6000 + Math.floor(Math.random() * 1000);
this.color = colors.white;
this.log = this.log.bind(this);
}
macro() {
return [
`CompilationOption.set('debug.address','${this.host}')`,
`CompilationOption.set('debug.port','${this.port}')`,
];
};
run() {
const debug = this;
return through.obj(function (file, enc, callback) {
if (this.disabled) {
this.emit('end');
callback();
return;
}
let color = colors.white;
const server = net.createServer((socket) => {
socket.on("data", (data) => {
const lines = data.toString().split('\n');
for (let line of lines) if (line.length > 2) {
const newColor = getColor(line);
if (newColor != null) color = newColor;
Debug.log(line, color);
}
});
socket.on("close", () => {
socket.destroy();
server.close();
this.emit('end');
callback();
});
});
log(colors.green('[debug]'), colors.cyan('listen on'), colors.magenta(`${debug.host}:${debug.port}`));
server.listen(debug.port, debug.host);
})
log(line) {
const newColor = getColor(line);
if (newColor) this.color = newColor;
if (line[0] === '\t' || line.startsWith('Called from ')) {
console.log(this.color(line));
} else {
const result = line.split(' ');
console.log(colors.gray(result.slice(0, 4).join(' ')) + ' ' + this.color(result.slice(4).join(' ')));
}
}
}

View File

@@ -9,6 +9,7 @@ const PluginError = require('plugin-error');
const colors = require('ansi-colors');
const log = require('fancy-log');
const tar = require('tar');
const Vinyl = require('vinyl');
class FlashPlayer extends Sdk {
@@ -129,10 +130,10 @@ class FlashPlayer extends Sdk {
//stream.push(file);
// ToDo: watch when file is exists
// or create log file in FlashPlayer.enableLog()?
/*stream.push(new Vinyl({
stream.push(new Vinyl({
path: FlashPlayer.log
}));*/
stream.push(file);
}));
//stream.push(file);
};
return stream = through.obj(bufferContents);

View File

@@ -56,7 +56,9 @@ class Haxe extends Sdk {
process.env.HAXE_VERSION = this.version;
process.env.HAXE_STD_PATH = `${this.binPath}/std`;
process.env.HAXE_HOME = this.binPath;
process.env.PATH = `${process.env.PATH}:${this.binPath}`;
if (process.env.PATH.indexOf(this.binPath) === -1) {
process.env.PATH = `${process.env.PATH}:${this.binPath}`;
}
}
prepare() {
@@ -81,7 +83,7 @@ class Haxe extends Sdk {
}
openfl(command, platform, config, debug=false) {
log(this.tag, colors.cyan('openfl', platform));
log(this.tag, colors.cyan(`openfl build ${platform}`));
const buildDir = path.join(os.tmpdir(), 'build', config.name);
mkdirp.sync(buildDir);
@@ -98,8 +100,33 @@ class Haxe extends Sdk {
return this.haxelib(args).then(() => vfs.src(`${target}/**/*`));
}
build(platform, config) {
build(platform, config, debug=false) {
log(this.tag, colors.cyan(`build ${platform}`));
const buildDir = path.join(os.tmpdir(), 'build', config.name);
mkdirp.sync(buildDir);
const projectTemplate = template(fs.readFileSync(path.resolve(__dirname, '..', 'template/project.hxml')));
const ext = {
flash: '.swf',
neko: '.n',
}[platform] || '';
const out = {
flash: 'swf'
}[platform] || platform;
const project = projectTemplate({...config, buildDir: buildDir, platform: platform, ext: ext, out: out});
const projectHXML = path.resolve(buildDir, 'project.hxml');
fs.writeFileSync(projectHXML, project);
const args = [projectHXML];
if (debug) {
args.push('-debug');
}
const target = path.resolve(buildDir, platform, 'bin');
rmdir(target);
return this.haxe(args).then(() => vfs.src(`${target}/**/*`));
}
install(packages) {
@@ -151,77 +178,6 @@ class Haxe extends Sdk {
promise = promise.then(() => this.haxelib(['upgrade', '--always']));
return promise;
}
_build(params) {
params = Object.assign({
version: null,
values: {},
lib: [],
macro: [],
debug: false,
}, params);
const files = [];
let stream = null;
const bufferContents = (file, enc, callback) => {
// ToDo: check file not stream
files.push(file);
callback();
};
const endStream = (callback) => {
log(this.tag, colors.cyan("haxe", params.platform), '=>', colors.magenta(params.outputFile));
const args = [];
// main
args.push('-main', params.main);
// lib
let lib = params.lib;
if (!Array.isArray(lib)) {
lib = Object.entries(lib).map(([k, v]) => `${k}:${v.split('@')[0]}`);
}
for (const item of lib) {
args.push('-lib', item);
}
// cp
for (const cp of params.cp) {
args.push('-cp', cp);
}
// macro
for (const macro of params.macro) {
args.push('--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 tmpFile = tmp.generateFile();
args.push(`-${params.platform}`, tmpFile.path);
if (params.debug) {
args.push('-debug');
}
//console.log('haxe', args.join(' '));
this.haxe(args).then(() => {
const out = new Vinyl({
path: params.outputFile,
//contents: fs.createReadStream(tmpFile.path),
contents: fs.readFileSync(tmpFile.path),
});
stream.push(out);
callback();
}).catch((error) => {
stream.emit('error', new PluginError({plugin: this.name, message: error}));
callback();
});
};
return stream = through.obj(bufferContents, endStream);
}
}
Haxe.ID = 'haxe';

View File

@@ -1,35 +0,0 @@
const exec = require('./exec');
const through = require('through2');
const PluginError = require('plugin-error');
const colors = require('ansi-colors');
const log = require('fancy-log');
class Neko {
constructor() {
this.tag = 'Neko';
}
run(...args) {
let stream = null;
const bufferContents = (file, enc, callback) => {
log(this.tag, colors.cyan("run"), colors.magenta(file.path));
exec('.', ['neko', file.path].concat(args).join(' '))
.then(() => {
stream.emit('end');
callback();
})
.catch((error) => {
stream.emit('error', new PluginError({plugin: this.tag, message: error}));
callback();
});
stream.push(file);
};
return stream = through.obj(bufferContents);
}
}
module.exports = Neko;

View File

@@ -1,28 +1,52 @@
const gulp = require('gulp');
const path = require('path');
const fs = require('fs');
//const concat = require('gulp-concat');
//const uglify = require('gulp-uglify');
//const babel = require('gulp-babel');
//const template = require('gulp-template');
const Haxe = require('./haxe');
const FlashPlayer = require('./flashplayer');
const Neko = require('./neko');
const Debug = require('./debug');
const webserver = require('gulp-webserver');
const run = require('gulp-run');
const run = require('../run/index');
const tail = require('./tail');
//const deb = require('gulp-debian');
const {BuildSystem, Platform, Config} = require('./core');
const vfs = require('vinyl-fs');
const rename = require('gulp-rename');
const streamToPromise = (stream) => {
return new Promise((resolve, reject) => {
stream.on("end", resolve);
stream.on("error", reject);
});
};
/**
*
*/
class Builder {
class Target {
constructor() {
this.target = 'target';
}
targetPath(name, platform) {
return path.resolve(this.target, name, platform);
}
}
/**
*
*/
class Builder extends Target {
constructor(buildSystem) {
super();
this.buildSystem = buildSystem;
this.target = 'target';
}
prepare() {
@@ -59,10 +83,14 @@ class HaxeBuilder extends Builder {
}
call(platform, config, debug) {
const target = this.targetPath(config.name, platform);
switch (this.buildSystem) {
case BuildSystem.OPENFL:
return this.haxe.openfl('build', platform, config, debug)
.then(result => result.pipe(vfs.dest(`${this.target}/${platform}`)));
.then(result => streamToPromise(result.pipe(vfs.dest(target))));
case BuildSystem.HAXE:
return this.haxe.build(platform, config, debug)
.then(result => streamToPromise(result.pipe(vfs.dest(target))));
}
}
}
@@ -73,12 +101,12 @@ Builder.register(BuildSystem.OPENFL, HaxeBuilder);
/**
*
*/
class Runner {
class Runner extends Target {
constructor(platform, name) {
super();
this.platform = platform;
this.name = name;
this.target = 'target';
}
prepare() {
@@ -89,6 +117,17 @@ class Runner {
throw 'Not Implemented';
}
targetPath() {
return super.targetPath(this.name, this.platform);
}
log(stream) {
stream
.pipe(tail(new Debug().log))
.pipe(rename('out.log'))
.pipe(gulp.dest(this.targetPath()));
}
static register(platform, builder) {
Runner.factory[platform] = builder;
}
@@ -115,9 +154,13 @@ class FlashRunner extends Runner {
}
call(debug) {
return gulp.src(`${this.target}/${this.platform}/${this.name}.swf`)
.pipe(this.player.run(true))
.pipe(debug.run());
const target = this.targetPath();
const filename = path.resolve(target, this.name+'.swf');
const player = this.player.flashPlayerBin(debug);
FlashPlayer.trust(filename);
fs.writeFileSync(FlashPlayer.log, '');
const result = gulp.src(filename).pipe(run(player + " <%=file.basename%>", {cwd: target}));
return this.log(gulp.src(FlashPlayer.log));
}
}
@@ -129,7 +172,7 @@ Runner.register(Platform.FLASH, FlashRunner);
class Html5Runner extends Runner {
call(debug) {
return gulp.src(`${this.target}/${this.platform}`)
return gulp.src(this.targetPath())
.pipe(webserver({
host: 'localhost', port: 3000,
open: true,
@@ -146,9 +189,10 @@ 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));
const target = this.targetPath();
const filename = path.resolve(target, this.name);
const result = gulp.src(filename).pipe(run("./<%=file.basename%>", {cwd: target}));
return this.log(result);
}
}
@@ -160,9 +204,10 @@ 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());
const target = this.targetPath();
const filename = path.resolve(target, this.name+'.n');
const result = gulp.src(filename).pipe(run("neko <%=file.path%>", {cwd: target}));
return this.log(result);
}
}
@@ -200,10 +245,10 @@ class Project {
];
}
bind(module, gulp /* ToDo: spike */) {
bind(module, external_gulp /* ToDo: spike */) {
for (const platform of this.platforms) {
module.exports[`${this.config.name}:${platform}:build`] = gulp.series(this.build(platform));
module.exports[`${this.config.name}:${platform}:run`] = gulp.series(this.run(platform));
module.exports[`${this.config.name}:${platform}:build`] = (external_gulp || gulp).series(this.build(platform));
module.exports[`${this.config.name}:${platform}:run`] = (external_gulp || gulp).series(this.run(platform));
}
return this;
}

View File

@@ -1,15 +1,11 @@
const through = require('through2');
const colors = require('ansi-colors');
const log = require('fancy-log');
const TAG = colors.green('[tail]');
const { Writable } = require('stream');
const { StringDecoder } = require('string_decoder');
const {Tail} = require('tail');
const {Writable} = require('stream');
const {StringDecoder} = require('string_decoder');
class StringWritable extends Writable {
constructor(handler, options) {
super(options);
this.handler = handler;
@@ -17,6 +13,7 @@ class StringWritable extends Writable {
this._decoder = new StringDecoder(state.defaultEncoding);
this.data = '';
}
_write(chunk, encoding, callback) {
if (encoding === 'buffer') {
chunk = this._decoder.write(chunk);
@@ -27,6 +24,7 @@ class StringWritable extends Writable {
this.data += chunk;
callback();
}
_final(callback) {
this.data += this._decoder.end();
callback();
@@ -36,7 +34,13 @@ class StringWritable extends Writable {
module.exports = (handler) => {
return through.obj(function (file, enc, callback) {
file.contents.pipe(new StringWritable(handler));
if (file.contents && file.contents.pipe) {
file.contents.pipe(new StringWritable(handler));
} else {
const tail = new Tail(file.path);
tail.on("line", data => handler(data));
tail.on("error", error => handler('[ERROR]: ', error));
}
this.push(file);
callback();
});