feat(project): add preset

This commit is contained in:
2026-10-14 15:53:44 +03:00
parent 78804036ab
commit dba24a2c68
27 changed files with 934 additions and 530 deletions
+174
View File
@@ -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<string, ProjectPreset>;
public config: PresetConfig;
public packageInfo: Record<string, any>;
constructor(options: Record<string, ProjectPreset>, config?: PresetConfig, packageInfo?: Record<string, any>) {
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);
}
}