254 lines
7.3 KiB
TypeScript
Executable File
254 lines
7.3 KiB
TypeScript
Executable File
import fs from "fs";
|
|
import fse from "fs-extra";
|
|
import rename from "gulp-rename";
|
|
import os from "os";
|
|
import path from "path";
|
|
import vfs from "vinyl-fs";
|
|
import env from "../common/env";
|
|
import exec from "../common/exec";
|
|
import { System } from "../common/system";
|
|
import template from "../common/template";
|
|
import { Sdk } from "../core/sdk";
|
|
import type { Config, LibDef } from "../core/type";
|
|
import { Platform } from "../core/type";
|
|
import { Neko } from "./neko";
|
|
|
|
export class Haxe extends Sdk {
|
|
static ID = "haxe";
|
|
|
|
static VERSION_3_4_0 = "3.4.0";
|
|
static VERSION_3_4_2 = "3.4.2";
|
|
static VERSION_3_4_3 = "3.4.3";
|
|
static VERSION_3_4_7 = "3.4.7";
|
|
static VERSION_3 = Haxe.VERSION_3_4_7;
|
|
static VERSION_4_0_0 = "4.0.0";
|
|
static VERSION_4_0_1 = "4.0.1";
|
|
static VERSION_4_0_2 = "4.0.2";
|
|
static VERSION_4_0_3 = "4.0.3";
|
|
static VERSION_4_0_5 = "4.0.5";
|
|
static VERSION_4_2_5 = "4.2.5";
|
|
static VERSION_4_3_7 = "4.3.7";
|
|
static VERSION_4 = Haxe.VERSION_4_2_5;
|
|
static VERSION = Haxe.VERSION_4;
|
|
|
|
static buildDir = path.join(process.cwd(), "build");
|
|
|
|
getBin(name: string) {
|
|
if (System.isWindows) {
|
|
return path.join(this.path, name + ".exe");
|
|
} else if (System.isLinux) {
|
|
const binPath = path.join(this.path, name);
|
|
if (fs.existsSync(binPath)) {
|
|
fs.chmodSync(binPath, 0o755);
|
|
}
|
|
return binPath;
|
|
}
|
|
throw `Unsupported OS: ${os.type()}`;
|
|
}
|
|
|
|
get haxeBin() {
|
|
return this.getBin("haxe");
|
|
}
|
|
|
|
get haxelibBin() {
|
|
return this.getBin("haxelib");
|
|
}
|
|
|
|
private neko: Neko;
|
|
|
|
constructor(version?: string) {
|
|
super(Haxe.ID, version || Haxe.VERSION);
|
|
this.neko = new Neko();
|
|
}
|
|
|
|
get prepared() {
|
|
try {
|
|
return fs.existsSync(this.haxeBin);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
activate() {
|
|
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() {
|
|
return Promise.all([this.neko.prepare(), super.prepare()]).then(() => {
|
|
fs.copyFileSync(
|
|
path.resolve(import.meta.dirname, "template/haxe/activate"),
|
|
path.resolve(this.path, "activate"),
|
|
);
|
|
});
|
|
}
|
|
|
|
get link(): string {
|
|
if (System.isWindows) {
|
|
return `https://github.com/HaxeFoundation/haxe/releases/download/${this.version}/haxe-${this.version}-win.zip`;
|
|
} else if (System.isLinux) {
|
|
let arch = System.archInt;
|
|
return `https://github.com/HaxeFoundation/haxe/releases/download/${this.version}/haxe-${this.version}-linux${arch}.tar.gz`;
|
|
} else {
|
|
throw `Unsupported os: ${System.os}`;
|
|
}
|
|
}
|
|
|
|
haxe(args: any) {
|
|
this.activate();
|
|
return exec(".", [this.haxeBin].concat(args).join(" "));
|
|
}
|
|
|
|
haxelib(args: any) {
|
|
this.activate();
|
|
return exec(".", [this.haxelibBin].concat(args).join(" "));
|
|
}
|
|
|
|
resolveBuildDir(config: Config) {
|
|
return path.join(Haxe.buildDir, config.name);
|
|
}
|
|
|
|
openfl(command: string, platform: Platform, config: Config, debug = false) {
|
|
this.log.i("_openfl_ _build_ *%s*", platform);
|
|
this.activate();
|
|
const buildDir = this.resolveBuildDir(config);
|
|
fse.ensureDirSync(buildDir);
|
|
|
|
const project = template.render("project.xml", { ...config, buildDir: buildDir });
|
|
fs.writeFileSync(path.resolve(buildDir, "project.xml"), project);
|
|
|
|
const args = ["-cwd", buildDir, "run", "openfl", command, platform];
|
|
if (debug) {
|
|
args.push("-debug");
|
|
}
|
|
const target = path.resolve(buildDir, platform, "bin");
|
|
fse.emptyDirSync(target);
|
|
const result =
|
|
(
|
|
{
|
|
[Platform.ANDROID]: `app/build/outputs/**/${config.meta.filename}-debug.apk`,
|
|
[Platform.FLASH]: "*.swf",
|
|
} as Record<Platform, string>
|
|
)[platform] || "**/*";
|
|
return this.haxelib(args).then(() => {
|
|
let r = vfs.src(`${target}/${result}`, { encoding: false });
|
|
if (platform === Platform.ANDROID) {
|
|
r = r.pipe(rename(`${config.meta.filename}_${config.meta.version}.apk`));
|
|
}
|
|
return r;
|
|
});
|
|
}
|
|
|
|
build(platform: Platform, config: Config, debug = false) {
|
|
this.log.i("_build_ *%s*", platform);
|
|
this.activate();
|
|
const buildDir = this.resolveBuildDir(config);
|
|
fse.ensureDirSync(buildDir);
|
|
|
|
const ext =
|
|
(
|
|
{
|
|
[Platform.FLASH]: ".swf",
|
|
[Platform.NEKO]: ".n",
|
|
} as Record<Platform, string>
|
|
)[platform] || "";
|
|
|
|
const out =
|
|
(
|
|
{
|
|
[Platform.FLASH]: "swf",
|
|
} as Record<Platform, string>
|
|
)[platform] || platform;
|
|
|
|
const project = template.render("project.hxml", {
|
|
...config,
|
|
buildDir: buildDir,
|
|
platform: platform,
|
|
ext: ext,
|
|
out: out,
|
|
});
|
|
const projectHXML = path.resolve(buildDir, "project.hxml");
|
|
fs.writeFileSync(projectHXML, project);
|
|
|
|
const args = [projectHXML];
|
|
if (debug) {
|
|
args.push("-debug");
|
|
}
|
|
const target = path.resolve(buildDir, platform, "bin");
|
|
if (platform !== "cpp") {
|
|
fse.emptyDirSync(target);
|
|
}
|
|
for (const asset of config.assets) {
|
|
fse.copySync(asset, path.join(target, asset.split("/").pop()!));
|
|
}
|
|
const result =
|
|
(
|
|
{
|
|
[Platform.CPP]: `${config.meta.filename}/${config.main.split(".").pop()}${debug ? "-debug" : ""}`,
|
|
} as Record<Platform, string>
|
|
)[platform] || "**/*";
|
|
return this.haxe(args).then(() => {
|
|
let r = vfs.src(`${target}/${result}`, { encoding: false });
|
|
if (platform === "cpp") {
|
|
r = r.pipe(rename(config.meta.filename!));
|
|
}
|
|
return r;
|
|
});
|
|
}
|
|
|
|
install(packages: Record<string, string> | 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 }));
|
|
}
|
|
|
|
for (let pack of packages) {
|
|
const args = [];
|
|
let version = null;
|
|
if (typeof pack === "string") {
|
|
args.push("install", pack);
|
|
} else if (typeof pack === "object") {
|
|
version = pack.version;
|
|
if (version.substr(0, 3) === "git") {
|
|
pack.git = version;
|
|
version = null;
|
|
}
|
|
if (pack.git) {
|
|
args.push("git", pack.name, pack.git);
|
|
if (pack.branch) args.push(pack.branch);
|
|
} else {
|
|
args.push("install", pack.name);
|
|
if (version) args.push(version);
|
|
}
|
|
args.push("--always");
|
|
}
|
|
let path = `${this.path}/lib/${args[1]}`;
|
|
if (version) {
|
|
path += `/${version.replace(/\./g, ",")}`;
|
|
} else if (pack.git) {
|
|
path += "/git";
|
|
}
|
|
if (!fs.existsSync(path)) {
|
|
promise = promise.then(next(args));
|
|
}
|
|
}
|
|
return promise;
|
|
}
|
|
|
|
upgrade() {
|
|
let promise = this.haxelib(["setup", path.join(this.path, "lib")]);
|
|
promise = promise.then(() => this.haxelib(["upgrade", "--always"]));
|
|
return promise;
|
|
}
|
|
}
|