From da1715a3277ec04a908b62ce82d59281e54158e7 Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 13 Oct 2026 22:56:25 +0300 Subject: [PATCH] refactor(project): refactor project modules --- package-lock.json | 140 ++++- package.json | 2 + src/common/util.ts | 6 + src/impl/ruffle.ts | 9 +- src/index.ts | 2 + src/project.ts | 524 ------------------- src/project/core/builder.ts | 23 + src/project/core/index.ts | 4 + src/project/core/packer.ts | 21 + src/project/core/runner.ts | 31 ++ src/project/core/target.ts | 30 ++ src/project/impl/builder/haxe.ts | 69 +++ src/project/impl/index.ts | 13 + src/project/impl/packer/flash.ts | 27 + src/project/impl/packer/linux/archive.ts | 26 + src/project/impl/packer/linux/deb.ts | 55 ++ src/project/impl/packer/windows/archive.ts | 24 + src/project/impl/packer/windows/installer.ts | 25 + src/project/impl/runner/android.ts | 32 ++ src/project/impl/runner/flash.ts | 27 + src/project/impl/runner/html5.ts | 25 + src/project/impl/runner/linux.ts | 30 ++ src/project/impl/runner/neko.ts | 27 + src/project/impl/runner/ruffle.ts | 9 + src/project/impl/runner/windows.ts | 21 + src/project/index.ts | 88 ++++ src/project/preset.ts | 174 ++++++ 27 files changed, 934 insertions(+), 530 deletions(-) create mode 100644 src/common/util.ts delete mode 100755 src/project.ts create mode 100644 src/project/core/builder.ts create mode 100644 src/project/core/index.ts create mode 100644 src/project/core/packer.ts create mode 100644 src/project/core/runner.ts create mode 100644 src/project/core/target.ts create mode 100644 src/project/impl/builder/haxe.ts create mode 100644 src/project/impl/index.ts create mode 100644 src/project/impl/packer/flash.ts create mode 100644 src/project/impl/packer/linux/archive.ts create mode 100644 src/project/impl/packer/linux/deb.ts create mode 100644 src/project/impl/packer/windows/archive.ts create mode 100644 src/project/impl/packer/windows/installer.ts create mode 100644 src/project/impl/runner/android.ts create mode 100644 src/project/impl/runner/flash.ts create mode 100644 src/project/impl/runner/html5.ts create mode 100644 src/project/impl/runner/linux.ts create mode 100644 src/project/impl/runner/neko.ts create mode 100644 src/project/impl/runner/ruffle.ts create mode 100644 src/project/impl/runner/windows.ts create mode 100644 src/project/index.ts create mode 100644 src/project/preset.ts diff --git a/package-lock.json b/package-lock.json index fc61717..6a331ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,10 +12,12 @@ "ansi-colors": "^1.1.0", "async": "^2.6.0", "cli-progress": "^3.0.0", + "dateformat": "^5.0.4", "fancy-log": "^1.3.2", "fs-extra": "^5.0.0", "got": "^9.6.0", "gulp": "^4.0.0", + "gulp-clean": "^0.4.0", "gulp-debian": "^0.1.9", "gulp-gzip": "^1.4.2", "gulp-rename": "^1.2.2", @@ -1624,6 +1626,18 @@ "node": ">=0.10.0" } }, + "node_modules/ansi-cyan": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/ansi-cyan/-/ansi-cyan-0.1.1.tgz", + "integrity": "sha512-eCjan3AVo/SxZ0/MyIYRtkpxIu/H3xZN7URr1vXVrISxeyz8fUFz0FJziamK4sS8I+t35y4rHg1b2PklyBe/7A==", + "license": "MIT", + "dependencies": { + "ansi-wrap": "0.1.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/ansi-gray": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/ansi-gray/-/ansi-gray-0.1.1.tgz", @@ -1636,6 +1650,18 @@ "node": ">=0.10.0" } }, + "node_modules/ansi-red": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/ansi-red/-/ansi-red-0.1.1.tgz", + "integrity": "sha512-ewaIr5y+9CUTGFwZfpECUbFlGcC0GCw1oqR9RI6h1gQCd9Aj2GxSckCnPsVJnmfMZbwFYE+leZGASgkWl06Jow==", + "license": "MIT", + "dependencies": { + "ansi-wrap": "0.1.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", @@ -3040,12 +3066,12 @@ } }, "node_modules/dateformat": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-2.2.0.tgz", - "integrity": "sha512-GODcnWq3YGoTnygPfi02ygEiRxqUxpJwuRHjdhJYuxpcZmDq4rjBiXYmbCCzStxo176ixfLT6i4NPwQooRySnw==", + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-5.0.4.tgz", + "integrity": "sha512-DnMqBsxDYMXnJ1c6TY8AgalunEVV5LBm1INpkod2A60Ldw/bopC/MGH0bEoK0S+I66Juezz36snGuWrEp9FmNg==", "license": "MIT", "engines": { - "node": "*" + "node": ">=12.20" } }, "node_modules/debug": { @@ -4691,6 +4717,90 @@ "node": ">= 0.10" } }, + "node_modules/gulp-clean": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/gulp-clean/-/gulp-clean-0.4.0.tgz", + "integrity": "sha512-DARK8rNMo4lHOFLGTiHEJdf19GuoBDHqGUaypz+fOhrvOs3iFO7ntdYtdpNxv+AzSJBx/JfypF0yEj9ks1IStQ==", + "license": "MIT", + "dependencies": { + "fancy-log": "^1.3.2", + "plugin-error": "^0.1.2", + "rimraf": "^2.6.2", + "through2": "^2.0.3", + "vinyl": "^2.1.0" + }, + "engines": { + "node": ">=0.9" + } + }, + "node_modules/gulp-clean/node_modules/arr-diff": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/arr-diff/-/arr-diff-1.1.0.tgz", + "integrity": "sha512-OQwDZUqYaQwyyhDJHThmzId8daf4/RFNLaeh3AevmSeZ5Y7ug4Ga/yKc6l6kTZOBW781rCj103ZuTh8GAsB3+Q==", + "license": "MIT", + "dependencies": { + "arr-flatten": "^1.0.1", + "array-slice": "^0.2.3" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/gulp-clean/node_modules/arr-union": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/arr-union/-/arr-union-2.1.0.tgz", + "integrity": "sha512-t5db90jq+qdgk8aFnxEkjqta0B/GHrM1pxzuuZz2zWsOXc5nKu3t+76s/PQBA8FTcM/ipspIH9jWG4OxCBc2eA==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/gulp-clean/node_modules/array-slice": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/array-slice/-/array-slice-0.2.3.tgz", + "integrity": "sha512-rlVfZW/1Ph2SNySXwR9QYkChp8EkOEiTMO5Vwx60usw04i4nWemkm9RXmQqgkQFaLHsqLuADvjp6IfgL9l2M8Q==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/gulp-clean/node_modules/extend-shallow": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-1.1.4.tgz", + "integrity": "sha512-L7AGmkO6jhDkEBBGWlLtftA80Xq8DipnrRPr0pyi7GQLXkaq9JYA4xF4z6qnadIC6euiTDKco0cGSU9muw+WTw==", + "license": "MIT", + "dependencies": { + "kind-of": "^1.1.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/gulp-clean/node_modules/kind-of": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-1.1.0.tgz", + "integrity": "sha512-aUH6ElPnMGon2/YkxRIigV32MOpTVcoXQ1Oo8aYn40s+sJ3j+0gFZsT8HKDcxNy7Fi9zuquWtGaGAahOdv5p/g==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/gulp-clean/node_modules/plugin-error": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/plugin-error/-/plugin-error-0.1.2.tgz", + "integrity": "sha512-WzZHcm4+GO34sjFMxQMqZbsz3xiNEgonCskQ9v+IroMmYgk/tas8dG+Hr2D6IbRPybZ12oWpzE/w3cGJ6FJzOw==", + "license": "MIT", + "dependencies": { + "ansi-cyan": "^0.1.1", + "ansi-red": "^0.1.1", + "arr-diff": "^1.0.1", + "arr-union": "^2.0.1", + "extend-shallow": "^1.1.2" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/gulp-cli": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/gulp-cli/-/gulp-cli-2.3.0.tgz", @@ -4851,6 +4961,15 @@ "integrity": "sha512-dhUqc57gSMCo6TX85FLfe51eC/s+Im2MLkAgJwfaRRexR2tA4dd3eLEW4L6efzHc2iNorrRRXITifnDLlRrhaA==", "license": "MIT" }, + "node_modules/gulp-util/node_modules/dateformat": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-2.2.0.tgz", + "integrity": "sha512-GODcnWq3YGoTnygPfi02ygEiRxqUxpJwuRHjdhJYuxpcZmDq4rjBiXYmbCCzStxo176ixfLT6i4NPwQooRySnw==", + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/gulp-util/node_modules/lodash.template": { "version": "3.6.2", "resolved": "https://registry.npmjs.org/lodash.template/-/lodash.template-3.6.2.tgz", @@ -8309,6 +8428,19 @@ "node": ">=0.12" } }, + "node_modules/rimraf": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", + "deprecated": "Rimraf versions prior to v4 are no longer supported", + "license": "ISC", + "dependencies": { + "glob": "^7.1.3" + }, + "bin": { + "rimraf": "bin.js" + } + }, "node_modules/rolldown": { "version": "1.2.13", "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.13.tgz", diff --git a/package.json b/package.json index 63baac8..5a98f2d 100755 --- a/package.json +++ b/package.json @@ -30,10 +30,12 @@ "ansi-colors": "^1.1.0", "async": "^2.6.0", "cli-progress": "^3.0.0", + "dateformat": "^5.0.4", "fancy-log": "^1.3.2", "fs-extra": "^5.0.0", "got": "^9.6.0", "gulp": "^4.0.0", + "gulp-clean": "^0.4.0", "gulp-debian": "^0.1.9", "gulp-gzip": "^1.4.2", "gulp-rename": "^1.2.2", diff --git a/src/common/util.ts b/src/common/util.ts new file mode 100644 index 0000000..7a0a7c4 --- /dev/null +++ b/src/common/util.ts @@ -0,0 +1,6 @@ +export const streamToPromise = (stream: any) => { + return new Promise((resolve, reject) => { + stream.on("end", resolve); + stream.on("error", reject); + }); +}; diff --git a/src/impl/ruffle.ts b/src/impl/ruffle.ts index 5dd6dc3..f98e215 100755 --- a/src/impl/ruffle.ts +++ b/src/impl/ruffle.ts @@ -11,7 +11,11 @@ export class RufflePlayer extends FlashPlayer { static ID = "ruffle"; static VERSION_2026_05_15 = "nightly-2026_05_15"; - static VERSION = RufflePlayer.VERSION_2026_05_15; + static VERSION_2026_10_13 = "nightly-2026_10_13"; + static VERSION_0_5_0 = "0.5.0"; + static VERSION_0_6_0 = "0.6.0"; + static VERSION_0_7_1 = "0.7.1"; + static VERSION = RufflePlayer.VERSION_2026_10_13; BASE_URL = "https://github.com/ruffle-rs/ruffle/releases/download"; @@ -21,7 +25,8 @@ export class RufflePlayer extends FlashPlayer { } get link(): string { - const baseUrl = `${this.baseUrl}/${this.version.replaceAll("_", "-")}`; + const nightly = this.version.includes("nightly"); + const baseUrl = `${this.baseUrl}/${nightly ? "" : "v"}${this.version.replaceAll("_", "-")}`; if (System.isWindows) { return baseUrl + `/ruffle-${this.version}-windows-x86_64.zip`; } else if (System.isLinux) { diff --git a/src/index.ts b/src/index.ts index ef7c866..7dacdc9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,3 +9,5 @@ export * from "./impl/innosetup"; export * from "./impl/neko"; export * from "./impl/ruffle"; export * from "./project"; +export * from "./project/impl"; +export * from "./project/preset"; diff --git a/src/project.ts b/src/project.ts deleted file mode 100755 index d91d185..0000000 --- a/src/project.ts +++ /dev/null @@ -1,524 +0,0 @@ -import fs from "fs"; -import fse from "fs-extra"; -import gulp from "gulp"; -import deb from "gulp-debian"; -import gzip from "gulp-gzip"; -import rename from "gulp-rename"; -import tar from "gulp-tar"; -import webserver from "gulp-webserver"; -import zip from "gulp-zip"; -import template from "lodash.template"; -import os from "os"; -import path from "path"; -import vfs from "vinyl-fs"; -import { DEBUG } from "./common/debug"; -import exec from "./common/exec"; -import { tail } from "./common/tail"; -import { Config } from "./core/config"; -import { BuildSystem, Platform } from "./core/type"; -import { Android } from "./impl/android"; -import { FlashPlayer } from "./impl/flashplayer"; -import { Haxe } from "./impl/haxe"; -import { InnoSetup } from "./impl/innosetup"; -import { Neko } from "./impl/neko"; -import { RufflePlayer } from "./impl/ruffle"; -import run from "./run"; - -const streamToPromise = (stream: any) => { - return new Promise((resolve, reject) => { - stream.on("end", resolve); - stream.on("error", reject); - }); -}; - -export class Target { - public config: Config; - public platform: string; - public target: string; - - constructor(config: Config, platform: string) { - this.config = config; - this.platform = platform; - this.target = "target"; - } - - prepare(): Promise { - return Promise.resolve(); - } - - call() { - throw "Not Implemented"; - } - - resolveTargetPath(platform = this.platform): string { - return path.resolve(this.target, this.config.name, platform); - } - - get targetPath(): string { - return this.resolveTargetPath(); - } -} - -export class Builder extends Target { - static factory: Record = {}; - - public buildSystem: string; - public debug: boolean; - - constructor(config: Config, platform: string, buildSystem: string, debug: boolean = false) { - super(config, platform); - this.buildSystem = buildSystem; - this.debug = debug; - } - - static register(buildSystem: string, builder: typeof Builder) { - Builder.factory[buildSystem] = builder; - } - - static new(config: Config, platform: string, buildSystem: string, debug: boolean = false): Builder { - return new Builder.factory[buildSystem](config, platform, buildSystem, debug); - } -} - -export class HaxeBuilder extends Builder { - public haxe: Haxe; - public android: Android; - - constructor(config: Config, platform: string, buildSystem: string, debug: boolean = false) { - super(config, platform, buildSystem, debug); - this.haxe = new Haxe(); - this.android = new Android(); - } - - prepare() { - let result = this.haxe.prepare().then(() => this.haxe.install(this.config.libs)); - if (this.buildSystem === BuildSystem.OPENFL) { - if (this.platform === Platform.ANDROID) { - result = result - .then(() => this.android.prepare()) - .then(() => this.android.activate()) - .then(() => - this.android.sdkmanager([ - // ToDo: android sdk dependencies config? - //'tools', - "platform-tools", - "build-tools;27.0.3", - "platforms;android-28", //ToDo: lime version 7.6.0 -> 28; 7.0.0 -> 26 - //'ndk-bundle', - //'lldb;3.1', - "cmake;3.6.4111459", - ]), - ); - const AndroidSDK = this.android.android_home; - const AndroidNDK = this.android.ndk_home; - const answer = `echo "${[AndroidSDK, AndroidNDK].join("\n")}"`; - result = result.then(() => { - return exec(".", [answer, "|", this.haxe.haxelibBin, "run", "openfl", "setup", this.platform].join(" ")); - }); - } - } - return result; - } - - call() { - const target = this.targetPath; - switch (this.buildSystem) { - case BuildSystem.OPENFL: - return this.haxe.openfl("build", this.platform, this.config, this.debug).then((result) => { - if (this.platform === Platform.ANDROID && this.config.key) { - result = result.pipe(this.android.sign(this.config.key.store, this.config.key.pass)); - } - return streamToPromise(result.pipe(vfs.dest(target))); - }); - case BuildSystem.HAXE: - return this.haxe - .build(this.platform, this.config, this.debug) - .then((result) => streamToPromise(result.pipe(vfs.dest(target)))); - } - } -} - -Builder.register(BuildSystem.HAXE, HaxeBuilder); -Builder.register(BuildSystem.OPENFL, HaxeBuilder); - -export type PackerFactoty = new (config: Config) => Packer; - -export class Packer extends Target { - static factory: Record> = {}; - - call() { - throw "Not Implemented"; - } - - static register(platform: string, name: string, packer: PackerFactoty) { - if (!Packer.factory[platform]) Packer.factory[platform] = {}; - Packer.factory[platform][name] = packer; - } - - static new(config: Config, platform: string, name: string): Packer { - return new Packer.factory[platform][name](config); - } -} - -export class FlashHTMLPacker extends Packer { - static NAME = "html"; - - constructor(config: Config) { - super(config, Platform.FLASH); - } - - call() { - const target = this.targetPath; - const indexTemplate = template( - fs.readFileSync(path.resolve(import.meta.dirname, "..", "template/flash/index.html")), - ); - const index = indexTemplate(this.config); - fs.writeFileSync(path.resolve(target, "index.html"), index); - fs.copyFileSync(path.resolve(import.meta.dirname, "..", "template/flash/swf.js"), path.resolve(target, "swf.js")); - return Promise.resolve(); - } -} - -Packer.register(Platform.FLASH, FlashHTMLPacker.NAME, FlashHTMLPacker); - -export class LinuxDEBPacker extends Packer { - static NAME = "deb"; - - constructor(config: Config) { - super(config, Platform.LINUX); - } - - call() { - const target = this.targetPath; - const buildDir = path.join(os.tmpdir(), "build", this.config.name, "debian"); - const iconfile = `${this.config.meta.filename}${path.extname(this.config.meta.icon!)}`; - const desktopTemplate = template( - fs.readFileSync(path.resolve(import.meta.dirname, "..", "template/linux/app.desktop")), - ); - const desktop = desktopTemplate(Object.assign({ iconfile: iconfile }, this.config)); - fse.ensureDirSync(`${buildDir}/usr/share/applications`); - fs.writeFileSync(`${buildDir}/usr/share/applications/${this.config.meta.filename}.desktop`, desktop); - fse.copySync(`${target}`, `${buildDir}/usr/share/${this.config.meta.filename}/`); - fse.copySync(this.config.meta.icon!, `${buildDir}/usr/share/${this.config.meta.filename}/${iconfile}`); - return gulp.src(`${buildDir}/*`).pipe( - deb({ - package: this.config.meta.filename, - version: this.config.meta.version, - section: "base", - priority: "optional", - architecture: "all", - maintainer: this.config.meta.author, - description: this.config.meta.title, - changelog: [], - postinst: [ - 'if [ "$1" = "configure" ] && [ -x "`which update-menus 2>/dev/null`" ] ; then\n' + - " update-menus\n" + - "fi", - ], - _target: "/", - _out: this.resolveTargetPath("debian"), - _clean: true, - _verbose: false, - }), - ); - } -} - -Packer.register(Platform.LINUX, LinuxDEBPacker.NAME, LinuxDEBPacker); - -export class LinuxArchivePacker extends Packer { - static NAME = "archive"; - - constructor(config: Config) { - super(config, Platform.LINUX); - } - - call() { - const target = this.targetPath; - const archiveName = `${this.config.meta.filename}_${this.config.meta.version}_linux.tar`; - return gulp - .src(`${target}/**/*`) - .pipe(tar(archiveName)) - .pipe(gzip()) - .pipe(gulp.dest(this.resolveTargetPath("archive"))); - } -} - -Packer.register(Platform.LINUX, LinuxArchivePacker.NAME, LinuxArchivePacker); - -export class WindowsInstallerPacker extends Packer { - static NAME = "installer"; - - public innosetup: InnoSetup; - - constructor(config: Config) { - super(config, Platform.WINDOWS); - this.innosetup = new InnoSetup(); - } - - prepare() { - return this.innosetup.prepare(); - } - - call() { - return this.innosetup.pack(this.config, this.targetPath, this.resolveTargetPath("installer")); - } -} - -Packer.register(Platform.WINDOWS, WindowsInstallerPacker.NAME, WindowsInstallerPacker); - -export class WindowsArchivePacker extends Packer { - static NAME = "archive"; - - constructor(config: Config) { - super(config, Platform.WINDOWS); - } - - call() { - const target = this.targetPath; - const archiveName = `${this.config.meta.filename}_${this.config.meta.version}_win.zip`; - return gulp - .src(`${target}/**/*`) - .pipe(zip(archiveName)) - .pipe(gulp.dest(this.resolveTargetPath("archive"))); - } -} - -Packer.register(Platform.WINDOWS, WindowsArchivePacker.NAME, WindowsArchivePacker); - -export type RunnerFactory = new (config: Config, debug: boolean) => Runner; - -export class Runner extends Target { - static factory: Record = {}; - - public debug: boolean; - - constructor(config: Config, platform: string, debug: boolean) { - super(config, platform); - this.debug = debug; - } - - log(stream: any) { - return stream.pipe(tail(DEBUG.log)).pipe(rename("out.log")).pipe(gulp.dest(this.targetPath)); - } - - static register(platform: string, runner: RunnerFactory) { - Runner.factory[platform] = runner; - } - - static new(config: Config, platform: string, debug: boolean): Runner { - return new Runner.factory[platform](config, debug); - } -} - -export class FlashRunner extends Runner { - public player: FlashPlayer; - - constructor(config: Config, debug: boolean, player: typeof FlashPlayer = FlashPlayer) { - super(config, Platform.FLASH, debug); - this.player = new player(debug); - } - - prepare() { - return this.player.prepare(); - } - - call() { - const target = this.targetPath; - const filename = path.resolve(target, this.config.meta.filename + ".swf"); - const result = this.player.run(filename, { cwd: target, verbosity: 0 }); - return this.log(result); - } -} - -class RuffleRunner extends FlashRunner { - constructor(config: Config, debug: boolean) { - super(config, debug, RufflePlayer); - } -} - -Runner.register(Platform.FLASH, FlashRunner); - -export class Html5Runner extends Runner { - constructor(config: Config, debug: boolean) { - super(config, Platform.HTML5, debug); - } - - call() { - return gulp.src(this.targetPath).pipe( - webserver({ - // ToDo: config? - host: "0.0.0.0", - port: 3000, - open: true, - fallback: "index.html", - }), - ); - } -} - -Runner.register(Platform.HTML5, Html5Runner); - -export class LinuxRunner extends Runner { - constructor(config: Config, debug: boolean) { - super(config, Platform.LINUX, debug); - } - - call() { - const target = this.targetPath; - const filename = path.resolve(target, this.config.meta.filename!); - const result = gulp.src(filename).pipe(run("./<%=file.basename%>", { cwd: target, verbosity: 0 })); - return this.log(result); - } -} - -Runner.register(Platform.LINUX, LinuxRunner); - -export class CPPRunner extends LinuxRunner { - constructor(config: Config, debug: boolean) { - super(config, debug); - this.platform = Platform.CPP; - } -} - -Runner.register(Platform.CPP, CPPRunner); - -export class WindowsRunner extends Runner { - constructor(config: Config, debug: boolean) { - super(config, Platform.WINDOWS, debug); - } - - call() { - const target = this.targetPath; - const filename = path.resolve(target, this.config.meta.filename + ".exe"); - const result = gulp.src(filename).pipe(run("<%=file.basename%>", { cwd: target, verbosity: 0 })); - return this.log(result); - } -} - -Runner.register(Platform.WINDOWS, WindowsRunner); - -export class NekoRunner extends Runner { - protected neko: Neko; - - constructor(config: Config, debug: boolean) { - super(config, Platform.NEKO, debug); - this.neko = new Neko(); - } - - prepare() { - return this.neko.prepare(); - } - - call() { - const target = this.targetPath; - const filename = path.resolve(target, this.config.meta.filename + ".n"); - const result = this.neko.run(filename, { cwd: target, verbosity: 0 }); - return this.log(result); - } -} - -Runner.register(Platform.NEKO, NekoRunner); - -/** - * - */ -export class AndroidRunner extends Runner { - protected android: Android; - - constructor(config: Config, debug: boolean) { - super(config, Platform.ANDROID, debug); - this.android = new Android(); - } - - prepare() { - return this.android.prepare(); - } - - call() { - const target = this.targetPath; - return this.log( - gulp - .src(`${target}/${this.config.meta.filename}_${this.config.meta.version}.apk`) - .pipe(this.android.apk()) - .pipe(this.android.install()) - .pipe(this.android.start()) - .pipe(this.android.logcat()), - ); - } -} - -Runner.register(Platform.ANDROID, AndroidRunner); - -export type Task = any; // TODO: gulp task type - -export class Project { - // TODO: compatibility - static Config = Config; - static BuildSystem = BuildSystem; - static Platform = Platform; - static Builder = Builder; - - public buildSystem: string; - public platforms: string[]; - public config: Config; - public prepare: (() => Task) | null; - - constructor(buildSystem: string, platforms: string[] | string, config: Config, prepare: (() => Task) | null = null) { - this.buildSystem = buildSystem; - this.platforms = Array.isArray(platforms) ? platforms : [platforms]; - this.config = config; - this.prepare = prepare; - } - - build(platform: string, debug: boolean = false): Task[] { - const builder = Builder.new(this.config, platform, this.buildSystem, debug); - const tasks: Task[] = [builder.prepare.bind(builder)]; - if (this.prepare) tasks.push(this.prepare); - tasks.push(builder.call.bind(builder)); - return tasks; - } - - run(platform: string, debug: boolean = false): Task[] { - const runner = Runner.new(this.config, platform, debug); - return [runner.prepare.bind(runner), runner.call.bind(runner)]; - } - - test(platform: string, debug: boolean = true): Task[] { - return this.build(platform, debug).concat(this.run(platform, debug)); - } - - pack(platform: string, name: string) { - const packer = Packer.new(this.config, platform, name); - return [packer.prepare.bind(packer), packer.call.bind(packer)]; - } - - compile(external_gulp?: any): Record { - const result: Record = {}; - const _gulp = external_gulp || gulp; - for (const platform of this.platforms) { - result[`${this.config.name}:${platform}:build`] = _gulp.series(this.build(platform)); - if (Runner.factory[platform]) { - result[`${this.config.name}:${platform}:run`] = _gulp.series(this.run(platform)); - result[`${this.config.name}:${platform}:test`] = _gulp.series(this.test(platform)); - } - if (Packer.factory[platform]) { - for (const name of Object.keys(Packer.factory[platform])) { - result[`${this.config.name}:${platform}:${name}`] = _gulp.series(this.pack(platform, name)); - } - } - } - return result; - } - - register(external_gulp?: any) { - const _gulp = external_gulp || gulp; - for (const [k, v] of Object.entries(this.compile(_gulp))) { - _gulp.task(k, v); - } - } - - static useRuffle() { - Runner.register(Platform.FLASH, RuffleRunner); - } -} diff --git a/src/project/core/builder.ts b/src/project/core/builder.ts new file mode 100644 index 0000000..12c6bf9 --- /dev/null +++ b/src/project/core/builder.ts @@ -0,0 +1,23 @@ +import type { Config } from "../../core/config"; +import { Target } from "./target"; + +export class Builder extends Target { + static factory: Record = {}; + + public buildSystem: string; + public debug: boolean; + + constructor(config: Config, platform: string, buildSystem: string, debug: boolean = false) { + super(config, platform); + this.buildSystem = buildSystem; + this.debug = debug; + } + + static register(buildSystem: string, builder: typeof Builder) { + Builder.factory[buildSystem] = builder; + } + + static new(config: Config, platform: string, buildSystem: string, debug: boolean = false): Builder { + return new Builder.factory[buildSystem](config, platform, buildSystem, debug); + } +} diff --git a/src/project/core/index.ts b/src/project/core/index.ts new file mode 100644 index 0000000..4bb9905 --- /dev/null +++ b/src/project/core/index.ts @@ -0,0 +1,4 @@ +export * from "./builder"; +export * from "./packer"; +export * from "./runner"; +export * from "./target"; diff --git a/src/project/core/packer.ts b/src/project/core/packer.ts new file mode 100644 index 0000000..41f0e70 --- /dev/null +++ b/src/project/core/packer.ts @@ -0,0 +1,21 @@ +import type { Config } from "../../core/config"; +import { Target } from "./target"; + +export type PackerFactory = new (config: Config) => Packer; + +export class Packer extends Target { + static factory: Record> = {}; + + call() { + throw "Not Implemented"; + } + + static register(platform: string, name: string, packer: PackerFactory) { + if (!Packer.factory[platform]) Packer.factory[platform] = {}; + Packer.factory[platform][name] = packer; + } + + static new(config: Config, platform: string, name: string): Packer { + return new Packer.factory[platform][name](config); + } +} diff --git a/src/project/core/runner.ts b/src/project/core/runner.ts new file mode 100644 index 0000000..b5c7293 --- /dev/null +++ b/src/project/core/runner.ts @@ -0,0 +1,31 @@ +import gulp from "gulp"; +import rename from "gulp-rename"; +import { DEBUG } from "../../common/debug"; +import { tail } from "../../common/tail"; +import { Config } from "../../core/config"; +import { Target } from "./target"; + +export type RunnerFactory = new (config: Config, debug: boolean) => Runner; + +export class Runner extends Target { + static factory: Record = {}; + + public debug: boolean; + + constructor(config: Config, platform: string, debug: boolean) { + super(config, platform); + this.debug = debug; + } + + log(stream: any) { + return stream.pipe(tail(DEBUG.log)).pipe(rename("out.log")).pipe(gulp.dest(this.targetPath)); + } + + static register(platform: string, runner: RunnerFactory) { + Runner.factory[platform] = runner; + } + + static new(config: Config, platform: string, debug: boolean): Runner { + return new Runner.factory[platform](config, debug); + } +} diff --git a/src/project/core/target.ts b/src/project/core/target.ts new file mode 100644 index 0000000..23b2c8c --- /dev/null +++ b/src/project/core/target.ts @@ -0,0 +1,30 @@ +import path from "path"; +import type { Config } from "../../core/config"; + +export class Target { + public config: Config; + public platform: string; + public target: string; + + constructor(config: Config, platform: string) { + this.config = config; + this.platform = platform; + this.target = "target"; + } + + prepare(): Promise { + return Promise.resolve(); + } + + call() { + throw "Not Implemented"; + } + + resolveTargetPath(platform = this.platform): string { + return path.resolve(this.target, this.config.name, platform); + } + + get targetPath(): string { + return this.resolveTargetPath(); + } +} diff --git a/src/project/impl/builder/haxe.ts b/src/project/impl/builder/haxe.ts new file mode 100644 index 0000000..331c98e --- /dev/null +++ b/src/project/impl/builder/haxe.ts @@ -0,0 +1,69 @@ +import vfs from "vinyl-fs"; +import exec from "../../../common/exec"; +import { streamToPromise } from "../../../common/util"; +import { Config } from "../../../core/config"; +import { BuildSystem, Platform } from "../../../core/type"; +import { Android } from "../../../impl/android"; +import { Haxe } from "../../../impl/haxe"; +import { Builder } from "../../core/builder"; + +export class HaxeBuilder extends Builder { + public haxe: Haxe; + public android: Android; + + constructor(config: Config, platform: string, buildSystem: string, debug: boolean = false) { + super(config, platform, buildSystem, debug); + this.haxe = new Haxe(); + this.android = new Android(); + } + + prepare() { + let result = this.haxe.prepare().then(() => this.haxe.install(this.config.libs)); + if (this.buildSystem === BuildSystem.OPENFL) { + if (this.platform === Platform.ANDROID) { + result = result + .then(() => this.android.prepare()) + .then(() => this.android.activate()) + .then(() => + this.android.sdkmanager([ + // ToDo: android sdk dependencies config? + //'tools', + "platform-tools", + "build-tools;27.0.3", + "platforms;android-28", //ToDo: lime version 7.6.0 -> 28; 7.0.0 -> 26 + //'ndk-bundle', + //'lldb;3.1', + "cmake;3.6.4111459", + ]), + ); + const AndroidSDK = this.android.android_home; + const AndroidNDK = this.android.ndk_home; + const answer = `echo "${[AndroidSDK, AndroidNDK].join("\n")}"`; + result = result.then(() => { + return exec(".", [answer, "|", this.haxe.haxelibBin, "run", "openfl", "setup", this.platform].join(" ")); + }); + } + } + return result; + } + + call() { + const target = this.targetPath; + switch (this.buildSystem) { + case BuildSystem.OPENFL: + return this.haxe.openfl("build", this.platform, this.config, this.debug).then((result) => { + if (this.platform === Platform.ANDROID && this.config.key) { + result = result.pipe(this.android.sign(this.config.key.store, this.config.key.pass)); + } + return streamToPromise(result.pipe(vfs.dest(target))); + }); + case BuildSystem.HAXE: + return this.haxe + .build(this.platform, this.config, this.debug) + .then((result) => streamToPromise(result.pipe(vfs.dest(target)))); + } + } +} + +Builder.register(BuildSystem.HAXE, HaxeBuilder); +Builder.register(BuildSystem.OPENFL, HaxeBuilder); diff --git a/src/project/impl/index.ts b/src/project/impl/index.ts new file mode 100644 index 0000000..0fd2b48 --- /dev/null +++ b/src/project/impl/index.ts @@ -0,0 +1,13 @@ +export * from "./builder/haxe"; +export * from "./packer/flash"; +export * from "./packer/linux/archive"; +export * from "./packer/linux/deb"; +export * from "./packer/windows/archive"; +export * from "./packer/windows/installer"; +export * from "./runner/android"; +export * from "./runner/flash"; +export * from "./runner/html5"; +export * from "./runner/linux"; +export * from "./runner/neko"; +export * from "./runner/ruffle"; +export * from "./runner/windows"; diff --git a/src/project/impl/packer/flash.ts b/src/project/impl/packer/flash.ts new file mode 100644 index 0000000..a12cee9 --- /dev/null +++ b/src/project/impl/packer/flash.ts @@ -0,0 +1,27 @@ +import fs from "fs"; +import template from "lodash.template"; +import path from "path"; +import type { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import { Packer } from "../../core/packer"; + +export class FlashHTMLPacker extends Packer { + static NAME = "html"; + + constructor(config: Config) { + super(config, Platform.FLASH); + } + + call() { + const target = this.targetPath; + const indexTemplate = template( + fs.readFileSync(path.resolve(import.meta.dirname, "..", "template/flash/index.html")), + ); + const index = indexTemplate(this.config); + fs.writeFileSync(path.resolve(target, "index.html"), index); + fs.copyFileSync(path.resolve(import.meta.dirname, "..", "template/flash/swf.js"), path.resolve(target, "swf.js")); + return Promise.resolve(); + } +} + +Packer.register(Platform.FLASH, FlashHTMLPacker.NAME, FlashHTMLPacker); diff --git a/src/project/impl/packer/linux/archive.ts b/src/project/impl/packer/linux/archive.ts new file mode 100644 index 0000000..0b1693d --- /dev/null +++ b/src/project/impl/packer/linux/archive.ts @@ -0,0 +1,26 @@ +import gulp from "gulp"; +import gzip from "gulp-gzip"; +import tar from "gulp-tar"; +import type { Config } from "../../../../core/config"; +import { Platform } from "../../../../core/type"; +import { Packer } from "../../../core/packer"; + +export class LinuxArchivePacker extends Packer { + static NAME = "archive"; + + constructor(config: Config) { + super(config, Platform.LINUX); + } + + call() { + const target = this.targetPath; + const archiveName = `${this.config.meta.filename}_${this.config.meta.version}_linux.tar`; + return gulp + .src(`${target}/**/*`) + .pipe(tar(archiveName)) + .pipe(gzip()) + .pipe(gulp.dest(this.resolveTargetPath("archive"))); + } +} + +Packer.register(Platform.LINUX, LinuxArchivePacker.NAME, LinuxArchivePacker); diff --git a/src/project/impl/packer/linux/deb.ts b/src/project/impl/packer/linux/deb.ts new file mode 100644 index 0000000..10b2617 --- /dev/null +++ b/src/project/impl/packer/linux/deb.ts @@ -0,0 +1,55 @@ +import fs from "fs"; +import fse from "fs-extra"; +import gulp from "gulp"; +import deb from "gulp-debian"; +import template from "lodash.template"; +import os from "os"; +import path from "path"; +import { Config } from "../../../../core/config"; +import { Platform } from "../../../../core/type"; +import { Packer } from "../../../core/packer"; + +export class LinuxDEBPacker extends Packer { + static NAME = "deb"; + + constructor(config: Config) { + super(config, Platform.LINUX); + } + + call() { + const target = this.targetPath; + const buildDir = path.join(os.tmpdir(), "build", this.config.name, "debian"); + const iconfile = `${this.config.meta.filename}${path.extname(this.config.meta.icon!)}`; + const desktopTemplate = template( + fs.readFileSync(path.resolve(import.meta.dirname, "..", "template/linux/app.desktop")), + ); + const desktop = desktopTemplate(Object.assign({ iconfile: iconfile }, this.config)); + fse.ensureDirSync(`${buildDir}/usr/share/applications`); + fs.writeFileSync(`${buildDir}/usr/share/applications/${this.config.meta.filename}.desktop`, desktop); + fse.copySync(`${target}`, `${buildDir}/usr/share/${this.config.meta.filename}/`); + fse.copySync(this.config.meta.icon!, `${buildDir}/usr/share/${this.config.meta.filename}/${iconfile}`); + return gulp.src(`${buildDir}/*`).pipe( + deb({ + package: this.config.meta.filename, + version: this.config.meta.version, + section: "base", + priority: "optional", + architecture: "all", + maintainer: this.config.meta.author, + description: this.config.meta.title, + changelog: [], + postinst: [ + 'if [ "$1" = "configure" ] && [ -x "`which update-menus 2>/dev/null`" ] ; then\n' + + " update-menus\n" + + "fi", + ], + _target: "/", + _out: this.resolveTargetPath("debian"), + _clean: true, + _verbose: false, + }), + ); + } +} + +Packer.register(Platform.LINUX, LinuxDEBPacker.NAME, LinuxDEBPacker); diff --git a/src/project/impl/packer/windows/archive.ts b/src/project/impl/packer/windows/archive.ts new file mode 100644 index 0000000..44cd9d4 --- /dev/null +++ b/src/project/impl/packer/windows/archive.ts @@ -0,0 +1,24 @@ +import gulp from "gulp"; +import zip from "gulp-zip"; +import { Config } from "../../../../core/config"; +import { Platform } from "../../../../core/type"; +import { Packer } from "../../../core/packer"; + +export class WindowsArchivePacker extends Packer { + static NAME = "archive"; + + constructor(config: Config) { + super(config, Platform.WINDOWS); + } + + call() { + const target = this.targetPath; + const archiveName = `${this.config.meta.filename}_${this.config.meta.version}_win.zip`; + return gulp + .src(`${target}/**/*`) + .pipe(zip(archiveName)) + .pipe(gulp.dest(this.resolveTargetPath("archive"))); + } +} + +Packer.register(Platform.WINDOWS, WindowsArchivePacker.NAME, WindowsArchivePacker); diff --git a/src/project/impl/packer/windows/installer.ts b/src/project/impl/packer/windows/installer.ts new file mode 100644 index 0000000..85841fa --- /dev/null +++ b/src/project/impl/packer/windows/installer.ts @@ -0,0 +1,25 @@ +import { Config } from "../../../../core/config"; +import { Platform } from "../../../../core/type"; +import { InnoSetup } from "../../../../impl/innosetup"; +import { Packer } from "../../../core/packer"; + +export class WindowsInstallerPacker extends Packer { + static NAME = "installer"; + + public innosetup: InnoSetup; + + constructor(config: Config) { + super(config, Platform.WINDOWS); + this.innosetup = new InnoSetup(); + } + + prepare() { + return this.innosetup.prepare(); + } + + call() { + return this.innosetup.pack(this.config, this.targetPath, this.resolveTargetPath("installer")); + } +} + +Packer.register(Platform.WINDOWS, WindowsInstallerPacker.NAME, WindowsInstallerPacker); diff --git a/src/project/impl/runner/android.ts b/src/project/impl/runner/android.ts new file mode 100644 index 0000000..08b5cbc --- /dev/null +++ b/src/project/impl/runner/android.ts @@ -0,0 +1,32 @@ +import gulp from "gulp"; +import type { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import { Android } from "../../../impl/android"; +import { Runner } from "../../core/runner"; + +export class AndroidRunner extends Runner { + protected android: Android; + + constructor(config: Config, debug: boolean) { + super(config, Platform.ANDROID, debug); + this.android = new Android(); + } + + prepare() { + return this.android.prepare(); + } + + call() { + const target = this.targetPath; + return this.log( + gulp + .src(`${target}/${this.config.meta.filename}_${this.config.meta.version}.apk`) + .pipe(this.android.apk()) + .pipe(this.android.install()) + .pipe(this.android.start()) + .pipe(this.android.logcat()), + ); + } +} + +Runner.register(Platform.ANDROID, AndroidRunner); diff --git a/src/project/impl/runner/flash.ts b/src/project/impl/runner/flash.ts new file mode 100644 index 0000000..bd1311d --- /dev/null +++ b/src/project/impl/runner/flash.ts @@ -0,0 +1,27 @@ +import path from "path"; +import { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import { FlashPlayer } from "../../../impl/flashplayer"; +import { Runner } from "../../core/runner"; + +export class FlashRunner extends Runner { + public player: FlashPlayer; + + constructor(config: Config, debug: boolean, player: typeof FlashPlayer = FlashPlayer) { + super(config, Platform.FLASH, debug); + this.player = new player(debug); + } + + prepare() { + return this.player.prepare(); + } + + call() { + const target = this.targetPath; + const filename = path.resolve(target, this.config.meta.filename + ".swf"); + const result = this.player.run(filename, { cwd: target, verbosity: 0 }); + return this.log(result); + } +} + +Runner.register(Platform.FLASH, FlashRunner); diff --git a/src/project/impl/runner/html5.ts b/src/project/impl/runner/html5.ts new file mode 100644 index 0000000..4033111 --- /dev/null +++ b/src/project/impl/runner/html5.ts @@ -0,0 +1,25 @@ +import gulp from "gulp"; +import webserver from "gulp-webserver"; +import { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import { Runner } from "../../core/runner"; + +export class Html5Runner extends Runner { + constructor(config: Config, debug: boolean) { + super(config, Platform.HTML5, debug); + } + + call() { + return gulp.src(this.targetPath).pipe( + webserver({ + // TODO: config? + host: "0.0.0.0", + port: 3000, + open: true, + fallback: "index.html", + }), + ); + } +} + +Runner.register(Platform.HTML5, Html5Runner); diff --git a/src/project/impl/runner/linux.ts b/src/project/impl/runner/linux.ts new file mode 100644 index 0000000..a90c1a7 --- /dev/null +++ b/src/project/impl/runner/linux.ts @@ -0,0 +1,30 @@ +import gulp from "gulp"; +import path from "path"; +import { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import run from "../../../run"; +import { Runner } from "../../core/runner"; + +export class LinuxRunner extends Runner { + constructor(config: Config, debug: boolean) { + super(config, Platform.LINUX, debug); + } + + call() { + const target = this.targetPath; + const filename = path.resolve(target, this.config.meta.filename!); + const result = gulp.src(filename).pipe(run("./<%=file.basename%>", { cwd: target, verbosity: 0 })); + return this.log(result); + } +} + +Runner.register(Platform.LINUX, LinuxRunner); + +export class CPPRunner extends LinuxRunner { + constructor(config: Config, debug: boolean) { + super(config, debug); + this.platform = Platform.CPP; + } +} + +Runner.register(Platform.CPP, CPPRunner); diff --git a/src/project/impl/runner/neko.ts b/src/project/impl/runner/neko.ts new file mode 100644 index 0000000..dc337b9 --- /dev/null +++ b/src/project/impl/runner/neko.ts @@ -0,0 +1,27 @@ +import path from "path"; +import { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import { Neko } from "../../../impl/neko"; +import { Runner } from "../../core/runner"; + +export class NekoRunner extends Runner { + public neko: Neko; + + constructor(config: Config, debug: boolean) { + super(config, Platform.NEKO, debug); + this.neko = new Neko(); + } + + prepare() { + return this.neko.prepare(); + } + + call() { + const target = this.targetPath; + const filename = path.resolve(target, this.config.meta.filename + ".n"); + const result = this.neko.run(filename, { cwd: target, verbosity: 0 }); + return this.log(result); + } +} + +Runner.register(Platform.NEKO, NekoRunner); diff --git a/src/project/impl/runner/ruffle.ts b/src/project/impl/runner/ruffle.ts new file mode 100644 index 0000000..dab441a --- /dev/null +++ b/src/project/impl/runner/ruffle.ts @@ -0,0 +1,9 @@ +import type { Config } from "../../../core/config"; +import { RufflePlayer } from "../../../impl/ruffle"; +import { FlashRunner } from "./flash"; + +export class RuffleRunner extends FlashRunner { + constructor(config: Config, debug: boolean) { + super(config, debug, RufflePlayer); + } +} diff --git a/src/project/impl/runner/windows.ts b/src/project/impl/runner/windows.ts new file mode 100644 index 0000000..191a9d5 --- /dev/null +++ b/src/project/impl/runner/windows.ts @@ -0,0 +1,21 @@ +import gulp from "gulp"; +import path from "path"; +import { Config } from "../../../core/config"; +import { Platform } from "../../../core/type"; +import run from "../../../run"; +import { Runner } from "../../core/runner"; + +export class WindowsRunner extends Runner { + constructor(config: Config, debug: boolean) { + super(config, Platform.WINDOWS, debug); + } + + call() { + const target = this.targetPath; + const filename = path.resolve(target, this.config.meta.filename + ".exe"); + const result = gulp.src(filename).pipe(run("<%=file.basename%>", { cwd: target, verbosity: 0 })); + return this.log(result); + } +} + +Runner.register(Platform.WINDOWS, WindowsRunner); diff --git a/src/project/index.ts b/src/project/index.ts new file mode 100644 index 0000000..90be523 --- /dev/null +++ b/src/project/index.ts @@ -0,0 +1,88 @@ +import gulp from "gulp"; +import { Config } from "../core/config"; +import { BuildSystem, Platform } from "../core/type"; +import { Builder } from "./core/builder"; +import { Packer } from "./core/packer"; +import { Runner } from "./core/runner"; +import { RuffleRunner } from "./impl/runner/ruffle"; +export * from "./core"; + +export type Task = any; // TODO: gulp task type + +export class Project { + // TODO: compatibility + static Config = Config; + static BuildSystem = BuildSystem; + static Platform = Platform; + static Builder = Builder; + + public buildSystem: string; + public platforms: string[]; + public config: Config; + + constructor(buildSystem: string, platforms: string[] | string, config: Config) { + this.buildSystem = buildSystem; + this.platforms = Array.isArray(platforms) ? platforms : [platforms]; + this.config = config; + } + + hasLib(name: string): boolean { + for (const lib of this.config.libs) { + if (lib.name === name) { + return true; + } + } + return false; + } + + build(platform: string, debug: boolean = false): Task[] { + const builder = Builder.new(this.config, platform, this.buildSystem, debug); + const tasks: Task[] = [builder.prepare.bind(builder)]; + tasks.push(builder.call.bind(builder)); + return tasks; + } + + run(platform: string, debug: boolean = false): Task[] { + const runner = Runner.new(this.config, platform, debug); + return [runner.prepare.bind(runner), runner.call.bind(runner)]; + } + + test(platform: string, debug: boolean = true): Task[] { + return this.build(platform, debug).concat(this.run(platform, debug)); + } + + pack(platform: string, name: string) { + const packer = Packer.new(this.config, platform, name); + return [packer.prepare.bind(packer), packer.call.bind(packer)]; + } + + compile(external_gulp?: any): Record { + const result: Record = {}; + const _gulp = external_gulp || gulp; + for (const platform of this.platforms) { + result[`${this.config.name}:${platform}:build`] = _gulp.series(this.build(platform)); + if (Runner.factory[platform]) { + result[`${this.config.name}:${platform}:run`] = _gulp.series(this.run(platform)); + result[`${this.config.name}:${platform}:test`] = _gulp.series(this.test(platform)); + } + if (Packer.factory[platform]) { + for (const name of Object.keys(Packer.factory[platform])) { + result[`${this.config.name}:${platform}:${name}`] = _gulp.series(this.pack(platform, name)); + } + } + } + return result; + } + + register(external_gulp?: any) { + const _gulp = external_gulp || gulp; + for (const [k, v] of Object.entries(this.compile(_gulp))) { + _gulp.task(k, v); + } + } + + // TODO: + static useRuffle() { + Runner.register(Platform.FLASH, RuffleRunner); + } +} diff --git a/src/project/preset.ts b/src/project/preset.ts new file mode 100644 index 0000000..778fdb9 --- /dev/null +++ b/src/project/preset.ts @@ -0,0 +1,174 @@ +import dateformat from "dateformat"; +import gulp from "gulp"; +import gulpClean from "gulp-clean"; +import template from "lodash.template"; +import os from "os"; +import { Builder, Project } from "."; +import { System } from "../common/system"; +import { Config, type ConfigParams } from "../core/config"; +import { Sdk } from "../core/sdk"; +import { Platform } from "../core/type"; +import { Haxe } from "../impl/haxe"; + +export interface PresetConfig { + SdkDir?: string; + BuildDir?: string; + Develop?: boolean; + UseRuffle?: boolean; + Key?: any; +} + +export const DEFAULT_CONFIG: PresetConfig = { + SdkDir: "$HOME/sdk", +}; + +export interface ProjectPreset { + buildSystem?: string; + platforms?: string[]; + config: ConfigParams; +} + +export class Preset { + public options: Record; + public config: PresetConfig; + public packageInfo: Record; + + constructor(options: Record, config?: PresetConfig, packageInfo?: Record) { + this.config = Object.assign(DEFAULT_CONFIG, config || {}); + this.packageInfo = packageInfo || {}; + for (const [k, v] of Object.entries(options)) { + options[k] = this.fillPreset(v); + } + this.options = options; + } + + private fillPreset(preset: ProjectPreset): ProjectPreset { + const configTemplate = template(JSON.stringify(preset)); + return JSON.parse( + configTemplate({ config: this.config, package: this.packageInfo, env: process.env, dateformat: dateformat }), + ); + } + + compile(): Project[] { + const config = new Config(this.options["default"]?.config); + if (this.packageInfo.haxeDependencies) { + config.update({ libs: this.packageInfo.haxeDependencies }); + } + const projects: Project[] = []; + for (const [k, v] of Object.entries(this.options)) { + if (k == "default") { + continue; + } + const projectConfig = config.branch(Object.assign(v.config, { name: k })); + projects.push(new Project(v.buildSystem!, v.platforms!, projectConfig)); + } + return projects; + } + + register(external_gulp?: any, additional_tasks?: string[]) { + const _gulp = external_gulp || gulp; + + const resolvePath = (path: string) => path.replace("$HOME", os.homedir()); + + if (this.config.UseRuffle) { + Project.useRuffle(); + } + + if (this.packageInfo.haxe) { + Haxe.VERSION = this.packageInfo.haxe; + } + + if (this.config.SdkDir) { + Sdk.dir = resolvePath(this.config.SdkDir); + } + + if (this.config.BuildDir) { + Haxe.buildDir = resolvePath(this.config.BuildDir); + } + + _gulp.task("clean", () => { + return _gulp.src("target/*", { read: false }).pipe(gulpClean()); + }); + + const projects = this.compile(); + const project = projects[0]; + + _gulp.task("setup", () => { + return Promise.all( + project.platforms.map((platform) => Builder.new(project.config, platform, project.buildSystem).prepare()), + ); + }); + + const protohx = project.hasLib("protohx"); + if (protohx) { + _gulp.task("protohx:generate", () => + new Haxe().haxelib(["run", "protohx", "generate", "protohx.json"]).then((result: any) => { + if (result.stdout.indexOf("FAIL") > -1) { + throw result.stdout; + } + }), + ); + } + + for (const project of projects) { + project.register(_gulp); + } + + const defaultSeries = [_gulp.task("clean"), _gulp.task("setup")]; + + if (protohx) { + defaultSeries.push("protohx:generate"); + } + + if (additional_tasks) { + for (const task of additional_tasks) { + defaultSeries.push(_gulp.task(task)); + } + } + + for (const project of projects) { + for (const platform of project.platforms) { + switch (platform) { + case Platform.FLASH: + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:html`)); + break; + case Platform.HTML5: + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + break; + case Platform.LINUX: + if (System.isLinux) { + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:archive`)); + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:deb`)); + } + break; + case Platform.CPP: + if (System.isLinux) { + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + } + break; + case Platform.NEKO: + if (System.isLinux) { + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + } + break; + case Platform.ANDROID: + if (System.isLinux) { + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + } + break; + case Platform.WINDOWS: + if (System.isWindows) { + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:build`)); + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:archive`)); + defaultSeries.push(_gulp.task(`${project.config.name}:${platform}:installer`)); + } + break; + } + } + } + + return _gulp.series(defaultSeries); + } +}