diff --git a/haxetool/env.js b/haxetool/env.js new file mode 100644 index 0000000..bbcd251 --- /dev/null +++ b/haxetool/env.js @@ -0,0 +1,20 @@ +const path = require('path'); + + +class Env { + + static set(key, value) { + process.env[key] = value; + } + + static addPath(value, key='PATH') { + if (!process.env[key]) { + process.env[key] = value; + } else if (process.env[key].split(path.delimiter).indexOf(value) === -1) { + process.env[key] = [process.env[key], value].join(path.delimiter); + } + } +} + + +module.exports = Env; \ No newline at end of file diff --git a/haxetool/exec.js b/haxetool/exec.js index ed28584..7024117 100755 --- a/haxetool/exec.js +++ b/haxetool/exec.js @@ -1,19 +1,17 @@ const child_process = require('child_process'); const async = require('async'); -const colors = require('ansi-colors'); -const log = require('fancy-log'); +const log = require('./log')('exec'); -const TAG = colors.green('[exec]'); -const verbose = process.argv.indexOf('--verbose') > -1; const queue = async.queue((task, done) => { - if (verbose) log(TAG, colors.magenta(task.command)); + log.d('*%s*', task.command); //process.chdir(task.dir); child_process.exec(task.command, {cwd: task.dir, maxBuffer: 1024 * 5000}, (err, stdout, stderr) => { - if (verbose) log(TAG, err ? colors.red(err) : '', stdout, colors.red(stderr)); if (err) { + log.v('!%s!', err); task.failure(stderr || stdout || err); } else { + log.v('%s', stdout); task.success({stdout: stdout, stderr: stderr}); } done(); diff --git a/haxetool/flashplayer.js b/haxetool/flashplayer.js index 1da0945..d7d484e 100755 --- a/haxetool/flashplayer.js +++ b/haxetool/flashplayer.js @@ -4,11 +4,8 @@ const fse = require('fs-extra'); const os = require('os'); const through = require('through2'); const Sdk = require('./sdk'); -const exec = require('./exec'); -const PluginError = require('plugin-error'); -const colors = require('ansi-colors'); -const log = require('fancy-log'); const Vinyl = require('vinyl'); +const run = require('../run/index'); class FlashPlayer extends Sdk { @@ -78,6 +75,7 @@ class FlashPlayer extends Sdk { } else { fs.writeFileSync(filename, `${value}\n`); } + fs.writeFileSync(FlashPlayer.log, ''); } static trust(value) { @@ -95,29 +93,15 @@ class FlashPlayer extends Sdk { } } - run() { - let stream = null; - const bufferContents = (file, enc, callback) => { - log(this.tag, colors.cyan("run"), colors.magenta(file.path)); - FlashPlayer.trust(file.path); - FlashPlayer.enableLog(); - exec('.', [this.flashPlayerBin, file.path].join(' ')).then(() => { - stream.emit('end'); + run(filename, params) { + this.log.i('_run_ *%s*', filename); + FlashPlayer.trust(filename); + FlashPlayer.enableLog(); + return run(`${this.flashPlayerBin} ${filename}`, params).exec() + .pipe(through.obj(function (file, enc, callback) { + this.push(new Vinyl({path: FlashPlayer.log})); callback(); - }).catch((error) => { - stream.emit('error', new PluginError({plugin: this.tag, message: error})); - callback(); - }); - //stream.push(file); - // ToDo: watch when file is exists - // or create log file in FlashPlayer.enableLog()? - stream.push(new Vinyl({ - path: FlashPlayer.log })); - //stream.push(file); - }; - - return stream = through.obj(bufferContents); } } diff --git a/haxetool/haxe.js b/haxetool/haxe.js index 551ddf7..a336978 100755 --- a/haxetool/haxe.js +++ b/haxetool/haxe.js @@ -4,9 +4,8 @@ const fse = require('fs-extra'); const path = require('path'); const exec = require('./exec'); const Sdk = require('./sdk'); +const Env = require('./env'); const Neko = require('./neko'); -const colors = require('ansi-colors'); -const log = require('fancy-log'); const vfs = require('vinyl-fs'); const template = require('lodash.template'); @@ -15,9 +14,9 @@ class Haxe extends Sdk { getBin(name) { if (Sdk.System.isWindows) { - return path.join(this.binPath, name + '.exe'); + return path.join(this.path, name + '.exe'); } else if (Sdk.System.isLinux) { - const binPath = path.join(this.binPath, name); + const binPath = path.join(this.path, name); if (fs.existsSync(binPath)) { fs.chmodSync(binPath, 0o755); } @@ -26,10 +25,6 @@ class Haxe extends Sdk { throw `Unsupported OS: ${os.type()}`; } - get binPath() { - return `${this.path}`; - } - get haxeBin() { return this.getBin('haxe'); } @@ -52,13 +47,12 @@ class Haxe extends Sdk { } activate() { - this.neko.activate() - process.env.HAXE_VERSION = this.version; - process.env.HAXE_STD_PATH = path.join(this.binPath, 'std'); - process.env.HAXE_HOME = this.binPath; - if (process.env.PATH.split(path.delimiter).indexOf(this.binPath) === -1) { - process.env.PATH = [process.env.PATH, this.binPath].join(path.delimiter); - } + this.neko.activate(); + Env.set('HAXE_VERSION', this.version); + Env.set('HAXE_STD_PATH', path.join(this.path, 'std')); + Env.set('HAXE_HOME', this.path); + Env.addPath(this.path); + Env.addPath(this.path, 'LD_LIBRARY_PATH'); } prepare() { @@ -83,8 +77,8 @@ class Haxe extends Sdk { } openfl(command, platform, config, debug=false) { - log(this.tag, colors.cyan(`openfl build ${platform}`)); - this.activate() + this.log.i('_openfl_ _build_ *%s*', platform); + this.activate(); const buildDir = path.join(os.tmpdir(), 'build', config.name); fse.ensureDirSync(buildDir); @@ -102,8 +96,8 @@ class Haxe extends Sdk { } build(platform, config, debug=false) { - log(this.tag, colors.cyan(`build ${platform}`)); - this.activate() + this.log.i('_build_ *%s*', platform); + this.activate(); const buildDir = path.join(os.tmpdir(), 'build', config.name); fse.ensureDirSync(buildDir); @@ -135,10 +129,10 @@ class Haxe extends Sdk { } install(packages) { - this.activate() + this.activate(); let promise = this.haxelib(['setup', path.join(this.path, 'lib')]); const next = (args) => () => { - log(this.tag, colors.cyan('haxelib', 'install'), colors.magenta(args[1])); + this.log.i('_haxelib_ _install_ *%s*', args[1]); return this.haxelib(args); }; diff --git a/haxetool/log.js b/haxetool/log.js new file mode 100644 index 0000000..bea8965 --- /dev/null +++ b/haxetool/log.js @@ -0,0 +1,64 @@ +const colors = require('ansi-colors'); +const timestamp = require('time-stamp'); + + +const Level = { + VERBOSE: -1, + DEBUG: 0, + INFO: 1, + WARN: 2, + ERROR: 3, +}; + +const level = process.argv.indexOf('--verbose') > -1 ? Level.VERBOSE : Level.INFO; + + +class Log { + + static colorize(pattern) { + for (const item of [ + ['_', colors.cyan], + ['\\*', colors.magenta], + ['!', colors.red], + ]) { + const r = new RegExp(item[0] + "(.*?)" + item[0], 'g'); + pattern = pattern.replace(r, item[1]('$1')); + } + return pattern + } + + static write(level, tag, pattern, ...args) { + if (level >= this.level) { + const message = `[${colors.gray(timestamp('HH:mm:ss'))}] ${colors.green(tag)} ${this.colorize(pattern)}`; + console.log(message, ...args) + } + } + + constructor(tag) { + this.tag = tag; + } + + v(...args) { + Log.write(-1, this.tag, ...args); + } + + d(...args) { + Log.write(0, this.tag, ...args); + } + + i(...args) { + Log.write(1, this.tag, ...args); + } + + w(...args) { + Log.write(2, this.tag, ...args); + } + + e(...args) { + Log.write(3, this.tag, ...args); + } +} + +Log.level = level; + +module.exports = (tag) => new Log(tag); diff --git a/haxetool/neko.js b/haxetool/neko.js index 06520ce..58bfe0e 100644 --- a/haxetool/neko.js +++ b/haxetool/neko.js @@ -3,7 +3,7 @@ const fs = require('fs'); const path = require('path'); const run = require('../run/index'); const Sdk = require('./sdk'); -const System = Sdk.System; +const Env = require('./env'); class NekoLink { @@ -56,26 +56,25 @@ class Neko extends Sdk { } activate() { - process.env.NEKO_INSTPATH = this.path; - if (process.env.PATH.split(path.delimiter).indexOf(this.path) === -1) { - process.env.PATH = [process.env.PATH, this.path].join(path.delimiter); - } + Env.set('NEKO_INSTPATH', this.path); + Env.addPath(this.path); + Env.addPath(this.path, 'LD_LIBRARY_PATH'); } get nekoBin() { let binname = 'neko'; - if (os.type() == 'Windows_NT') binname += '.exe'; + if (os.type() === 'Windows_NT') binname += '.exe'; return path.join(this.path, binname); } run(filename, params) { + this.log.i('_run_ *%s*', filename); this.activate(); - console.log(`${this.nekoBin} ${filename}`); return run(`${this.nekoBin} ${filename}`, params).exec(); } } -Neko.ID = 'neko' +Neko.ID = 'neko'; Neko.VERSION_2_2_0 = '2.2.0'; Neko.VERSION = Neko.VERSION_2_2_0; diff --git a/haxetool/project.js b/haxetool/project.js index 435f2e9..a642542 100644 --- a/haxetool/project.js +++ b/haxetool/project.js @@ -15,8 +15,6 @@ const {BuildSystem, Platform, Config} = require('./core'); const vfs = require('vinyl-fs'); const rename = require('gulp-rename'); const template = require('lodash.template'); -const through = require('through2'); -const Vinyl = require('vinyl'); const streamToPromise = (stream) => { @@ -249,15 +247,7 @@ class FlashRunner extends Runner { call() { const target = this.targetPath; const filename = path.resolve(target, this.config.meta.filename+'.swf'); - const player = this.player.flashPlayerBin; - FlashPlayer.trust(filename); - fs.writeFileSync(FlashPlayer.log, ''); - let result = gulp.src(filename) - .pipe(run(player + " <%=file.basename%>", {cwd: target, verbosity: 0})) - .pipe(through.obj(function (file, enc, callback) { - this.push(new Vinyl({path: FlashPlayer.log})); - callback(); - })); + const result = this.player.run(filename, {cwd: target, verbosity: 0}); return this.log(result); } } diff --git a/haxetool/sdk.js b/haxetool/sdk.js index 775f447..088a35c 100755 --- a/haxetool/sdk.js +++ b/haxetool/sdk.js @@ -6,9 +6,8 @@ const got = require('got'); const unzip = require('unzip-stream'); const tar = require('tar'); const ProgressBar = require('progress'); -const colors = require('ansi-colors'); -const log = require('fancy-log'); const fse = require('fs-extra'); +const Log = require('./log'); class System { @@ -51,7 +50,7 @@ class Sdk { constructor(name, version) { this.name = name; - this.tag = colors.green(`[${name}]`); + this.log = Log(name); this.version = version; this.path = Sdk.path(name, version); } @@ -76,8 +75,7 @@ class Sdk { } prepare(strip=1) { - log(this.tag, `version: ${colors.magenta(this.version)}`); - + this.log.i('version: *%s*', this.version); if (this.prepared) { return Promise.resolve(); } else { diff --git a/package.json b/package.json index 42ab04f..a6d15c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-haxetool", - "version": "0.0.9", + "version": "0.0.10", "description": "HaXe Tool for Gulp", "main": "index.js", "dependencies": {