2020-02-14 00:52:16 +01:00
|
|
|
/*
|
2024-09-06 18:56:18 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2024-09-06 18:56:18 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-02-14 00:52:16 +01:00
|
|
|
*/
|
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
import path from "node:path";
|
|
|
|
import os from "node:os";
|
2024-04-25 11:41:35 +02:00
|
|
|
import nodePreGypVersioning from "@mapbox/node-pre-gyp/lib/util/versioning";
|
2024-10-30 19:06:03 +01:00
|
|
|
import { getElectronVersionFromInstalled } from "app-builder-lib/out/electron/electronVersion.js";
|
|
|
|
import childProcess, { SpawnOptions } from "node:child_process";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
import { Arch, Target, TARGETS, getHost, isHostId, TargetId } from "./target.js";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
async function getRuntime(projectRoot: string): Promise<string> {
|
2024-04-08 12:38:54 +02:00
|
|
|
const electronVersion = await getElectronVersionFromInstalled(projectRoot);
|
2022-12-15 12:00:58 +01:00
|
|
|
return electronVersion ? "electron" : "node-webkit";
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
async function getRuntimeVersion(projectRoot: string): Promise<string> {
|
2024-04-08 12:38:54 +02:00
|
|
|
const electronVersion = await getElectronVersionFromInstalled(projectRoot);
|
2020-02-14 00:52:16 +01:00
|
|
|
if (electronVersion) {
|
|
|
|
return electronVersion;
|
|
|
|
} else {
|
|
|
|
return process.version.substr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
export type Tool = [cmd: string, ...args: string[]];
|
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
export default class HakEnv {
|
2022-05-27 00:05:59 +02:00
|
|
|
public readonly target: Target;
|
2022-12-05 12:50:49 +01:00
|
|
|
public runtime?: string;
|
|
|
|
public runtimeVersion?: string;
|
2021-12-14 15:32:27 +01:00
|
|
|
public dotHakDir: string;
|
|
|
|
|
2024-01-02 19:12:16 +01:00
|
|
|
public constructor(
|
|
|
|
public readonly projectRoot: string,
|
|
|
|
targetId: TargetId | null,
|
|
|
|
) {
|
2022-12-05 12:50:49 +01:00
|
|
|
const target = targetId ? TARGETS[targetId] : getHost();
|
2021-06-18 18:38:22 +02:00
|
|
|
|
2022-12-05 12:50:49 +01:00
|
|
|
if (!target) {
|
2021-06-18 18:38:22 +02:00
|
|
|
throw new Error(`Unknown target ${targetId}!`);
|
2020-02-18 12:01:34 +01:00
|
|
|
}
|
2022-12-05 12:50:49 +01:00
|
|
|
this.target = target;
|
2022-12-15 12:00:58 +01:00
|
|
|
this.dotHakDir = path.join(this.projectRoot, ".hak");
|
2021-12-13 22:15:17 +01:00
|
|
|
}
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-01 08:20:55 +01:00
|
|
|
public async init(): Promise<void> {
|
2021-12-14 15:32:27 +01:00
|
|
|
this.runtime = await getRuntime(this.projectRoot);
|
|
|
|
this.runtimeVersion = await getRuntimeVersion(this.projectRoot);
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public getRuntimeAbi(): string {
|
2022-12-05 12:50:49 +01:00
|
|
|
return nodePreGypVersioning.get_runtime_abi(this.runtime!, this.runtimeVersion!);
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// {node_abi}-{platform}-{arch}
|
2022-05-27 10:15:47 +02:00
|
|
|
public getNodeTriple(): string {
|
2022-12-15 12:00:58 +01:00
|
|
|
return this.getRuntimeAbi() + "-" + this.target.platform + "-" + this.target.arch;
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public getTargetId(): TargetId {
|
2021-06-22 14:37:33 +02:00
|
|
|
return this.target.id;
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public isWin(): boolean {
|
2022-12-15 12:00:58 +01:00
|
|
|
return this.target.platform === "win32";
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public isMac(): boolean {
|
2022-12-15 12:00:58 +01:00
|
|
|
return this.target.platform === "darwin";
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public isLinux(): boolean {
|
2022-12-15 12:00:58 +01:00
|
|
|
return this.target.platform === "linux";
|
2021-06-18 18:38:22 +02:00
|
|
|
}
|
|
|
|
|
2023-08-09 11:11:27 +02:00
|
|
|
public isFreeBSD(): boolean {
|
|
|
|
return this.target.platform === "freebsd";
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public getTargetArch(): Arch {
|
2021-06-25 14:10:08 +02:00
|
|
|
return this.target.arch;
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public isHost(): boolean {
|
2021-06-18 18:38:22 +02:00
|
|
|
return isHostId(this.target.id);
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-05 12:50:49 +01:00
|
|
|
public makeGypEnv(): Record<string, string | undefined> {
|
2024-07-31 16:08:06 +02:00
|
|
|
return {
|
|
|
|
...process.env,
|
2021-06-18 18:38:22 +02:00
|
|
|
npm_config_arch: this.target.arch,
|
|
|
|
npm_config_target_arch: this.target.arch,
|
2022-12-15 12:00:58 +01:00
|
|
|
npm_config_disturl: "https://electronjs.org/headers",
|
2020-02-14 00:52:16 +01:00
|
|
|
npm_config_runtime: this.runtime,
|
2021-06-18 17:40:00 +02:00
|
|
|
npm_config_target: this.runtimeVersion,
|
2024-07-31 16:08:06 +02:00
|
|
|
npm_config_build_from_source: "true",
|
2020-02-14 00:52:16 +01:00
|
|
|
npm_config_devdir: path.join(os.homedir(), ".electron-gyp"),
|
2024-07-31 16:08:06 +02:00
|
|
|
};
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public wantsStaticSqlCipher(): boolean {
|
2023-08-09 11:11:27 +02:00
|
|
|
return !(this.isLinux() || this.isFreeBSD()) || process.env.SQLCIPHER_BUNDLED == "1";
|
2022-05-27 10:15:47 +02:00
|
|
|
}
|
2024-10-30 19:06:03 +01:00
|
|
|
|
|
|
|
public spawn(
|
|
|
|
cmd: string,
|
|
|
|
args: string[],
|
|
|
|
{ ignoreWinCmdlet, ...options }: SpawnOptions & { ignoreWinCmdlet?: boolean } = {},
|
|
|
|
): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const proc = childProcess.spawn(cmd + (!ignoreWinCmdlet && this.isWin() ? ".cmd" : ""), args, {
|
|
|
|
stdio: "inherit",
|
|
|
|
// We need shell mode on Windows to be able to launch `.cmd` executables
|
|
|
|
// See https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
|
|
|
|
shell: this.isWin(),
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
proc.on("exit", (code) => {
|
|
|
|
if (code) {
|
|
|
|
reject(code);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async checkTools(tools: Tool[]): Promise<void> {
|
|
|
|
for (const [tool, ...args] of tools) {
|
|
|
|
try {
|
|
|
|
await this.spawn(tool, args, {
|
|
|
|
ignoreWinCmdlet: true,
|
|
|
|
stdio: ["ignore"],
|
|
|
|
shell: false,
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
throw new Error(`Can't find ${tool}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 15:32:27 +01:00
|
|
|
}
|