diff --git a/example/hxformat.json b/example/hxformat.json new file mode 100644 index 0000000..d1dca53 --- /dev/null +++ b/example/hxformat.json @@ -0,0 +1,8 @@ +{ + "indentation": { + "character": " " + }, + "wrapping": { + "maxLineLength": 120 + } +} diff --git a/example/src/Main.hx b/example/src/Main.hx index 5fc3433..a5821d8 100644 --- a/example/src/Main.hx +++ b/example/src/Main.hx @@ -2,13 +2,12 @@ import flash.display.Shape; import flash.Lib; class Main { - - public static function main() { - trace("Hello world!"); - var shape = new Shape(); - shape.graphics.beginFill(0x00ff00); - shape.graphics.drawCircle(50, 50, 20); - shape.graphics.endFill(); - Lib.current.addChild(shape); - } -} \ No newline at end of file + public static function main() { + trace("Hello world!"); + var shape = new Shape(); + shape.graphics.beginFill(0x00ff00); + shape.graphics.drawCircle(50, 50, 20); + shape.graphics.endFill(); + Lib.current.addChild(shape); + } +} diff --git a/src/cli.ts b/src/cli.ts index 2ef7108..4c589ed 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import gulp from "gulp"; -import { Preset } from "."; +import { Preset, Task } from "."; import { name, version } from "../package.json" with { type: "json" }; console.log(`[${name} ${version}]`); @@ -27,7 +27,7 @@ export const main = async () => { const preset = Preset.loader(); Preset.validator(preset); const projects = Preset.compiler(preset); - Preset.register(projects); + Task.register(Task.compiler(projects)); if (process.argv.length > 2) { await runTask(process.argv[2]); diff --git a/src/core/config.ts b/src/core/config.ts index cccae16..c7dcedd 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -24,7 +24,6 @@ export const DEFAULT_CONFIG: Config = { }, key: null, android: [], - haxe: null, }; export const absolutePath = (file: string): string => { @@ -36,7 +35,7 @@ export const compile = (source: ConfigSource): Config => { return branch(DEFAULT_CONFIG, source); }; -export const branch = (config: Config, source: ConfigSource): Config => { +export const branch = (config: Config, source: ConfigSource | Config): Config => { const result = structuredClone(config); if (source.buildSystem !== undefined) { @@ -85,8 +84,8 @@ export const branch = (config: Config, source: ConfigSource): Config => { if (source.haxe !== undefined) { result.haxe = source.haxe; } - if (result.meta.icon) { - result.icon = absolutePath(result.meta.icon); + if (result.icon) { + result.icon = absolutePath(result.icon); } if (!result.meta.mobileWidth) { @@ -97,3 +96,12 @@ export const branch = (config: Config, source: ConfigSource): Config => { } return result; }; + +export const hasLib = (config: Config, name: string): boolean => { + for (const lib of config.libs) { + if (lib.name === name) { + return true; + } + } + return false; +}; diff --git a/src/core/sdk.ts b/src/core/sdk.ts index 6dd4ec7..079e0be 100755 --- a/src/core/sdk.ts +++ b/src/core/sdk.ts @@ -60,8 +60,9 @@ export class Sdk { if (this.prepared) { return Promise.resolve(); } else { - this.log.i("download: *%s*", this.link); - return download(resolveLink(this.link, this), this.path, { strip }); + const url = resolveLink(this.link, { name: this.name, version: this.version }); + this.log.i("download: *%s*", url); + return download(url, this.path, { strip }); } } } diff --git a/src/core/type.ts b/src/core/type.ts index f40bc73..d955408 100644 --- a/src/core/type.ts +++ b/src/core/type.ts @@ -24,7 +24,6 @@ export interface LibDef { export interface ConfigMeta { title?: string; filename?: string; - icon?: string; version?: string; pack?: string; author?: string; @@ -74,11 +73,11 @@ export interface Config { libs: LibDef[]; macros: string[]; flags: string[]; - icon: string | null; + icon: string; meta: ConfigMeta; key: SignKey | null; android: AndroidPreset[]; - haxe: string | null; + haxe?: string; } export type PackageJson = Record; diff --git a/src/impl/haxe.ts b/src/impl/haxe.ts index b70bd60..aef4a90 100755 --- a/src/impl/haxe.ts +++ b/src/impl/haxe.ts @@ -100,11 +100,13 @@ export class Haxe extends Sdk { haxe(args: any) { this.activate(); + this.log.d("_haxe_ *%s*", args.join(" ")); return exec(".", [this.haxeBin].concat(args).join(" ")); } haxelib(args: any) { this.activate(); + this.log.d("_haxelib_ *%s*", args.join(" ")); return exec(".", [this.haxelibBin].concat(args).join(" ")); } @@ -201,17 +203,17 @@ export class Haxe extends Sdk { } install(packages: Record | LibDef[]) { - this.activate(); - let promise = this.haxelib(["setup", path.join(this.path, "lib")]); - const next = (args: any) => () => { - this.log.i("_haxelib_ _install_ *%s:%s*", args[1], args[2]); - return this.haxelib(args); - }; - if (!Array.isArray(packages)) { packages = Object.entries(packages).map(([k, v]) => ({ name: k, version: v })); } + this.log.i("_install_ *%s*", packages.map((item) => `${item.name}:${item.version}`).join(" ")); + this.activate(); + let promise = this.haxelib(["setup", path.join(this.path, "lib")]); + const next = (args: any) => () => { + return this.haxelib(args); + }; + for (let pack of packages) { const args = []; let version = null; diff --git a/src/index.ts b/src/index.ts index b650afb..a27b505 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,3 +16,4 @@ export * from "./impl/ruffle"; export * from "./preset"; export * from "./project"; export * from "./project/impl"; +export * from "./task"; diff --git a/src/preset/compiler.ts b/src/preset/compiler.ts index d2bb150..3ffaa9f 100644 --- a/src/preset/compiler.ts +++ b/src/preset/compiler.ts @@ -1,16 +1,14 @@ import { branch, compile } from "../core/config"; import type { Config, PresetJson } from "../core/type"; -import { Project } from "../project"; -export default (preset: PresetJson): Project[] => { +export default (preset: PresetJson): Config[] => { const config: Config = compile(preset["default"] || {}); - const projects: Project[] = []; + const result: Config[] = []; for (const [k, v] of Object.entries(preset)) { if (k == "default") { continue; } - const projectConfig = branch(config, { ...v, name: k }); - projects.push(new Project(projectConfig)); + result.push(branch(config, { ...v, name: k })); } - return projects; + return result; }; diff --git a/src/preset/index.ts b/src/preset/index.ts index c6b565b..d77b444 100644 --- a/src/preset/index.ts +++ b/src/preset/index.ts @@ -1,12 +1,10 @@ import compiler from "./compiler"; import loader from "./loader"; -import register from "./register"; import validator from "./validator"; export const Preset = { compiler, loader, - register, validator, }; diff --git a/src/preset/loader.ts b/src/preset/loader.ts index 4e4c602..da3cbf0 100644 --- a/src/preset/loader.ts +++ b/src/preset/loader.ts @@ -4,9 +4,9 @@ import os from "os"; import path from "path"; import template from "../common/template"; import { Sdk } from "../core/sdk"; -import type { ConfigJson, ConfigSource, PackageJson, PresetJson } from "../core/type"; +import { Platform, type ConfigJson, type ConfigSource, type PackageJson, type PresetJson } from "../core/type"; import { Haxe } from "../impl/haxe"; -import { Project } from "../project"; +import { RuffleRunner, Runner } from "../project"; export const DEFAULT_CONFIG_JSON: ConfigJson = { SdkDir: "$HOME/sdk", @@ -15,8 +15,9 @@ export const DEFAULT_CONFIG_JSON: ConfigJson = { export const resolvePath = (path: string) => path.replace("$HOME", os.homedir()); export const applyConfig = (config: ConfigJson) => { + // TODO: ruffle kludge if (config.UseRuffle) { - Project.useRuffle(); + Runner.register(Platform.FLASH, RuffleRunner); } if (config.SdkDir) { Sdk.dir = resolvePath(config.SdkDir); diff --git a/src/preset/register.ts b/src/preset/register.ts deleted file mode 100644 index 5653fbf..0000000 --- a/src/preset/register.ts +++ /dev/null @@ -1,91 +0,0 @@ -import gulp from "gulp"; -import gulpClean from "gulp-clean"; -import type { Task } from "undertaker"; -import { System } from "../common/system"; -import { Platform } from "../core/type"; -import { Haxe } from "../impl/haxe"; -import { Builder, type Project } from "../project"; - -export default (projects: Project[]): Task => { - gulp.task("clean", () => { - return gulp.src("target/*", { read: false }).pipe(gulpClean()); - }); - - const project = projects[0]; - - gulp.task("setup", () => { - return Promise.all( - project.config.platforms.map((platform) => - Builder.new(project.config, platform, project.config.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: Task[] = [gulp.task("clean")!, gulp.task("setup")!]; - - if (protohx) { - defaultSeries.push(gulp.task("protohx:generate")!); - } - - for (const project of projects) { - for (const platform of project.config.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; - } - } - } - - const defaultTask = gulp.series(defaultSeries); - gulp.task("default", defaultTask); - return defaultTask; -}; diff --git a/src/project/core/runner.ts b/src/project/core/runner.ts index 47ca90b..360dbe7 100644 --- a/src/project/core/runner.ts +++ b/src/project/core/runner.ts @@ -5,16 +5,16 @@ import { tail } from "../../common/tail"; import { type Config, Platform } from "../../core/type"; import { Target } from "./target"; -export type RunnerFactory = new (config: Config, debug: boolean) => Runner; +export type RunnerFactory = new (config: Config, debug?: boolean) => Runner; export class Runner extends Target { static factory: Record = {}; public debug: boolean; - constructor(config: Config, platform: Platform, debug: boolean) { + constructor(config: Config, platform: Platform, debug?: boolean) { super(config, platform); - this.debug = debug; + this.debug = debug || false; } log(stream: any) { @@ -25,7 +25,7 @@ export class Runner extends Target { Runner.factory[platform] = runner; } - static new(config: Config, platform: string, debug: boolean): 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 index 156f477..88ce419 100644 --- a/src/project/core/target.ts +++ b/src/project/core/target.ts @@ -12,7 +12,7 @@ export class Target { this.target = "target"; } - prepare(): Promise { + setup(): Promise { return Promise.resolve(); } diff --git a/src/project/impl/builder/haxe.ts b/src/project/impl/builder/haxe.ts index df37ee3..8dea5d3 100644 --- a/src/project/impl/builder/haxe.ts +++ b/src/project/impl/builder/haxe.ts @@ -16,7 +16,7 @@ export class HaxeBuilder extends Builder { this.android = new Android(); } - prepare() { + setup() { let result: Promise = this.haxe.prepare().then(() => this.haxe.install(this.config.libs)); if (this.buildSystem === BuildSystem.OPENFL) { if (this.platform === Platform.ANDROID) { @@ -25,13 +25,10 @@ export class HaxeBuilder extends Builder { .then(() => this.android.activate()) .then(() => this.android.sdkmanager([ - // ToDo: android sdk dependencies config? - //'tools', + // TODO: android sdk dependencies config? "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', + "platforms;android-28", "cmake;3.6.4111459", ]), ); diff --git a/src/project/impl/packer/linux/deb.ts b/src/project/impl/packer/linux/deb.ts index c5bedf9..419449a 100644 --- a/src/project/impl/packer/linux/deb.ts +++ b/src/project/impl/packer/linux/deb.ts @@ -8,6 +8,12 @@ import template from "../../../../common/template"; import { type Config, Platform } from "../../../../core/type"; import { Packer } from "../../../core/packer"; +const UPDATE_MENUS = ` +if [ "$1" = "configure" ] && [ -x "\`which update-menus 2>/dev/null\`" ] ; then + update-menus +fi +`; + export class LinuxDEBPacker extends Packer { static NAME = "deb"; @@ -18,7 +24,7 @@ export class LinuxDEBPacker extends Packer { call() { const target = this.targetPath; const buildDir = path.join(os.tmpdir(), "build", this.config.name, "debian"); - const icon = this.config.meta.icon!; + const icon = this.config.icon; const iconfile = `${this.config.meta.filename}${path.extname(icon)}`; const desktop = template.render("linux/app.desktop", { iconfile: iconfile, ...this.config }); fse.ensureDirSync(`${buildDir}/usr/share/applications`); @@ -35,11 +41,7 @@ export class LinuxDEBPacker extends Packer { 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", - ], + postinst: [UPDATE_MENUS], _target: "/", _out: this.resolveTargetPath("debian"), _clean: true, diff --git a/src/project/impl/packer/windows/installer.ts b/src/project/impl/packer/windows/installer.ts index 81a4941..8c49cfc 100644 --- a/src/project/impl/packer/windows/installer.ts +++ b/src/project/impl/packer/windows/installer.ts @@ -12,7 +12,7 @@ export class WindowsInstallerPacker extends Packer { this.innosetup = new InnoSetup(); } - prepare() { + setup() { return this.innosetup.prepare(); } diff --git a/src/project/impl/runner/android.ts b/src/project/impl/runner/android.ts index d007d62..c4186d5 100644 --- a/src/project/impl/runner/android.ts +++ b/src/project/impl/runner/android.ts @@ -6,12 +6,12 @@ import { Runner } from "../../core/runner"; export class AndroidRunner extends Runner { protected android: Android; - constructor(config: Config, debug: boolean) { + constructor(config: Config, debug?: boolean) { super(config, Platform.ANDROID, debug); this.android = new Android(); } - prepare() { + setup() { return this.android.prepare(); } diff --git a/src/project/impl/runner/flash.ts b/src/project/impl/runner/flash.ts index 8075e8b..7d20858 100644 --- a/src/project/impl/runner/flash.ts +++ b/src/project/impl/runner/flash.ts @@ -6,12 +6,12 @@ import { Runner } from "../../core/runner"; export class FlashRunner extends Runner { public player: FlashPlayer; - constructor(config: Config, debug: boolean, player: typeof FlashPlayer = FlashPlayer) { + constructor(config: Config, debug?: boolean, player: typeof FlashPlayer = FlashPlayer) { super(config, Platform.FLASH, debug); this.player = new player(debug); } - prepare() { + setup() { return this.player.prepare(); } diff --git a/src/project/impl/runner/html5.ts b/src/project/impl/runner/html5.ts index 09dd4ac..a1275b1 100644 --- a/src/project/impl/runner/html5.ts +++ b/src/project/impl/runner/html5.ts @@ -3,7 +3,7 @@ import { type Config, Platform } from "../../../core/type"; import { Runner } from "../../core/runner"; export class Html5Runner extends Runner { - constructor(config: Config, debug: boolean) { + constructor(config: Config, debug?: boolean) { super(config, Platform.HTML5, debug); } diff --git a/src/project/impl/runner/linux.ts b/src/project/impl/runner/linux.ts index 9b06a48..38a8cca 100644 --- a/src/project/impl/runner/linux.ts +++ b/src/project/impl/runner/linux.ts @@ -5,7 +5,7 @@ import run from "../../../run"; import { Runner } from "../../core/runner"; export class LinuxRunner extends Runner { - constructor(config: Config, debug: boolean) { + constructor(config: Config, debug?: boolean) { super(config, Platform.LINUX, debug); } @@ -20,7 +20,7 @@ export class LinuxRunner extends Runner { Runner.register(Platform.LINUX, LinuxRunner); export class CPPRunner extends LinuxRunner { - constructor(config: Config, debug: boolean) { + constructor(config: Config, debug?: boolean) { super(config, debug); this.platform = Platform.CPP; } diff --git a/src/project/impl/runner/neko.ts b/src/project/impl/runner/neko.ts index 4f7ec04..da2496c 100644 --- a/src/project/impl/runner/neko.ts +++ b/src/project/impl/runner/neko.ts @@ -6,12 +6,12 @@ import { Runner } from "../../core/runner"; export class NekoRunner extends Runner { public neko: Neko; - constructor(config: Config, debug: boolean) { + constructor(config: Config, debug?: boolean) { super(config, Platform.NEKO, debug); this.neko = new Neko(); } - prepare() { + setup() { return this.neko.prepare(); } diff --git a/src/project/impl/runner/ruffle.ts b/src/project/impl/runner/ruffle.ts index 8bc818a..4fc02be 100644 --- a/src/project/impl/runner/ruffle.ts +++ b/src/project/impl/runner/ruffle.ts @@ -3,7 +3,7 @@ import { RufflePlayer } from "../../../impl/ruffle"; import { FlashRunner } from "./flash"; export class RuffleRunner extends FlashRunner { - constructor(config: Config, debug: boolean) { + 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 index 46287b7..1ede45e 100644 --- a/src/project/impl/runner/windows.ts +++ b/src/project/impl/runner/windows.ts @@ -5,7 +5,7 @@ import run from "../../../run"; import { Runner } from "../../core/runner"; export class WindowsRunner extends Runner { - constructor(config: Config, debug: boolean) { + constructor(config: Config, debug?: boolean) { super(config, Platform.WINDOWS, debug); } diff --git a/src/project/index.ts b/src/project/index.ts index 20233c0..d9661cb 100644 --- a/src/project/index.ts +++ b/src/project/index.ts @@ -1,77 +1,2 @@ -import gulp from "gulp"; -import type { Task, TaskFunction } from "undertaker"; -import { Platform, type Config } 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 * from "./impl"; - -export class Project { - public config: Config; - - constructor(config: Config) { - this.config = config; - } - - hasLib(name: string): boolean { - for (const lib of this.config.libs) { - if (lib.name === name) { - return true; - } - } - return false; - } - - build(platform: Platform, debug: boolean = false): Task[] { - const builder = Builder.new(this.config, platform, this.config.buildSystem, debug); - const tasks: Task[] = [builder.prepare.bind(builder)]; - tasks.push(builder.call.bind(builder)); - return tasks; - } - - run(platform: Platform, debug: boolean = false): Task[] { - const runner = Runner.new(this.config, platform, debug); - return [runner.prepare.bind(runner), runner.call.bind(runner)]; - } - - test(platform: Platform, debug: boolean = true): Task[] { - return this.build(platform, debug).concat(this.run(platform, debug)); - } - - pack(platform: Platform, name: string) { - const packer = Packer.new(this.config, platform, name); - return [packer.prepare.bind(packer), packer.call.bind(packer)]; - } - - compile(external_gulp?: gulp.Gulp): Record { - const result: Record = {}; - const _gulp = external_gulp || gulp; - for (const platform of this.config.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?: gulp.Gulp) { - 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/task/compiler.ts b/src/task/compiler.ts new file mode 100644 index 0000000..5e01f24 --- /dev/null +++ b/src/task/compiler.ts @@ -0,0 +1,23 @@ +import { branch, compile } from "../core/config"; +import type { Config } from "../core/type"; +import type { TaskCompilerType, TaskDef } from "./core"; +import { CleanTaskCompiler } from "./impl/clean"; +import { FormatTaskCompiler } from "./impl/format"; +import { ProjectTaskCompiler } from "./impl/project"; +import { ProtoTaskCompiler } from "./impl/proto"; +import { SetupTaskCompiler } from "./impl/setup"; + +const COMPILERS: TaskCompilerType[] = [FormatTaskCompiler, CleanTaskCompiler, SetupTaskCompiler, ProtoTaskCompiler]; +const PROJECT_COMPILERS: TaskCompilerType[] = [ProjectTaskCompiler]; + +export default function* (projects: Config[]): Iterable { + const config = projects.reduce((config, project) => branch(config, project), compile({})); + for (const compiler of COMPILERS) { + yield* new compiler(config).compile(); + } + for (const compiler of PROJECT_COMPILERS) { + for (const project of projects) { + yield* new compiler(project).compile(); + } + } +} diff --git a/src/task/core.ts b/src/task/core.ts new file mode 100644 index 0000000..885cc00 --- /dev/null +++ b/src/task/core.ts @@ -0,0 +1,22 @@ +import type { TaskFunction } from "gulp"; +import type { Config } from "../core/type"; + +export interface TaskCompiler { + config: Config; + debug?: boolean; + + compile(): Iterable; +} + +export type TaskDef = readonly [string, TaskFunction] | readonly [string, TaskFunction, boolean]; + +export abstract class TaskCompilerImpl implements TaskCompiler { + constructor( + public config: Config, + public debug?: boolean, + ) {} + + abstract compile(): Iterable; +} + +export type TaskCompilerType = new (config: Config, debug?: boolean) => TaskCompiler; diff --git a/src/task/impl/clean.ts b/src/task/impl/clean.ts new file mode 100644 index 0000000..aa0691b --- /dev/null +++ b/src/task/impl/clean.ts @@ -0,0 +1,9 @@ +import gulp from "gulp"; +import gulpClean from "gulp-clean"; +import { TaskCompilerImpl, type TaskDef } from "../core"; + +export class CleanTaskCompiler extends TaskCompilerImpl { + *compile(): Iterable { + yield ["clean", () => gulp.src("target/*", { read: false }).pipe(gulpClean()), true]; + } +} diff --git a/src/task/impl/format.ts b/src/task/impl/format.ts new file mode 100644 index 0000000..1c64390 --- /dev/null +++ b/src/task/impl/format.ts @@ -0,0 +1,36 @@ +import fs from "fs"; +import path from "path"; +import { Haxe } from "../../impl/haxe"; +import { TaskCompilerImpl, type TaskDef } from "../core"; + +export class FormatTaskCompiler extends TaskCompilerImpl { + *compile(): Iterable { + const hxformatFile = path.join(".", "hxformat.json"); + if (fs.existsSync(hxformatFile)) { + yield [ + "check", + async () => { + const haxe = new Haxe(this.config.haxe); + await haxe.install({ formatter: "" }); + await haxe.haxelib( + ["run", "formatter", "--check"].concat( + this.config.sources.reduce((sources, source) => sources.concat(["-s", source]), []), + ), + ); + }, + ]; + yield [ + "format", + async () => { + const haxe = new Haxe(this.config.haxe); + await haxe.install({ formatter: "" }); + await haxe.haxelib( + ["run", "formatter"].concat( + this.config.sources.reduce((sources, source) => sources.concat(["-s", source]), []), + ), + ); + }, + ]; + } + } +} diff --git a/src/task/impl/project.ts b/src/task/impl/project.ts new file mode 100644 index 0000000..3f6a4d1 --- /dev/null +++ b/src/task/impl/project.ts @@ -0,0 +1,42 @@ +import gulp from "gulp"; +import os from "os"; +import { Platform } from "../../core/type"; +import { Builder, Packer, Runner } from "../../project"; +import { TaskCompilerImpl, type TaskDef } from "../core"; + +const isPlatformSupported = (platform: Platform): boolean => { + switch (platform) { + case Platform.LINUX: + return os.type() === "Linux"; + case Platform.WINDOWS: + return os.type() === "Windows_NT"; + default: + return true; + } +}; + +export class ProjectTaskCompiler extends TaskCompilerImpl { + *compile(): Iterable { + for (const platform of this.config.platforms.filter(isPlatformSupported)) { + const builder = Builder.new(this.config, platform, this.config.buildSystem, this.debug); + const setupKey = `${this.config.name}:${platform}:setup`; + const buildKey = `${this.config.name}:${platform}:build`; + yield [setupKey, builder.setup.bind(builder)]; + yield [buildKey, gulp.series(builder.setup.bind(builder), builder.call.bind(builder)), true]; + + if (Runner.factory[platform]) { + const testKey = `${this.config.name}:${platform}:test`; + const runner = Runner.new(this.config, platform, this.debug); + yield [testKey, gulp.series(builder.setup.bind(builder), runner.setup.bind(runner), runner.call.bind(runner))]; + } + + if (Packer.factory[platform]) { + for (const name of Object.keys(Packer.factory[platform])) { + const packKey = `${this.config.name}:${platform}:${name}`; + const packer = Packer.new(this.config, platform, name); + yield [packKey, gulp.series(packer.setup.bind(packer), packer.call.bind(packer)), true]; + } + } + } + } +} diff --git a/src/task/impl/proto.ts b/src/task/impl/proto.ts new file mode 100644 index 0000000..87440cc --- /dev/null +++ b/src/task/impl/proto.ts @@ -0,0 +1,23 @@ +import fs from "fs"; +import path from "path"; +import { Haxe } from "../../impl/haxe"; +import { TaskCompilerImpl, type TaskDef } from "../core"; + +export class ProtoTaskCompiler extends TaskCompilerImpl { + *compile(): Iterable { + const protohxFile = path.join(".", "protohx.json"); + if (fs.existsSync(protohxFile)) { + yield [ + "protohx:generate", + async () => { + const haxe = new Haxe(this.config.haxe); + const result = await haxe.haxelib(["run", "protohx", "generate", "protohx.json"]); + if (result.stdout.indexOf("FAIL") > -1) { + throw result.stdout; + } + }, + true, + ]; + } + } +} diff --git a/src/task/impl/setup.ts b/src/task/impl/setup.ts new file mode 100644 index 0000000..4d7c56c --- /dev/null +++ b/src/task/impl/setup.ts @@ -0,0 +1,14 @@ +import { Builder } from "../../project"; +import { TaskCompilerImpl, type TaskDef } from "../core"; + +export class SetupTaskCompiler extends TaskCompilerImpl { + *compile(): Iterable { + yield [ + "setup", + () => + Promise.all( + this.config.platforms.map((platform) => Builder.new(this.config, platform, this.config.buildSystem).setup()), + ), + ]; + } +} diff --git a/src/task/index.ts b/src/task/index.ts new file mode 100644 index 0000000..3f75eb1 --- /dev/null +++ b/src/task/index.ts @@ -0,0 +1,12 @@ +import compiler from "./compiler"; +import register from "./register"; +export * from "./core"; +export * from "./impl/format"; +export * from "./impl/project"; + +export const Task = { + compiler, + register, +}; + +export default Task; diff --git a/src/task/register.ts b/src/task/register.ts new file mode 100644 index 0000000..a05af81 --- /dev/null +++ b/src/task/register.ts @@ -0,0 +1,15 @@ +import gulp from "gulp"; +import type { TaskDef } from "./core"; + +export default (tasks: Iterable) => { + const defaultTasks = []; + for (const item of tasks) { + gulp.task(item[0], item[1]); + if (item[2]) { + defaultTasks.push(item[0]); + } + } + if (defaultTasks.length > 0) { + gulp.task("default", gulp.series(defaultTasks)); + } +};