2020-02-14 00:52:16 +01:00
|
|
|
/*
|
2021-06-18 18:38:22 +02:00
|
|
|
Copyright 2020-2021 The Matrix.org Foundation C.I.C.
|
2020-02-14 00:52:16 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
import path from 'path';
|
|
|
|
import os from 'os';
|
|
|
|
import nodePreGypVersioning from "node-pre-gyp/lib/util/versioning";
|
|
|
|
import { getElectronVersion } from "app-builder-lib/out/electron/electronVersion";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
import { Arch, Target, TARGETS, getHost, isHostId, TargetId } from './target';
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
async function getRuntime(projectRoot: string): Promise<string> {
|
|
|
|
const electronVersion = await getElectronVersion(projectRoot);
|
2020-02-14 00:52:16 +01:00
|
|
|
return electronVersion ? 'electron' : 'node-webkit';
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:32:27 +01:00
|
|
|
async function getRuntimeVersion(projectRoot: string): Promise<string> {
|
|
|
|
const electronVersion = await getElectronVersion(projectRoot);
|
2020-02-14 00:52:16 +01:00
|
|
|
if (electronVersion) {
|
|
|
|
return electronVersion;
|
|
|
|
} else {
|
|
|
|
return process.version.substr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2022-12-01 08:20:55 +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;
|
2021-12-14 15:32:27 +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 {
|
2021-06-18 18:38:22 +02: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 {
|
2021-06-18 18:38:22 +02: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 {
|
2021-06-18 18:38:22 +02: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 {
|
2021-06-18 18:38:22 +02:00
|
|
|
return this.target.platform === 'linux';
|
|
|
|
}
|
|
|
|
|
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> {
|
2020-02-14 00:52:16 +01:00
|
|
|
return Object.assign({}, process.env, {
|
2021-06-18 18:38:22 +02:00
|
|
|
npm_config_arch: this.target.arch,
|
|
|
|
npm_config_target_arch: this.target.arch,
|
2022-05-22 10:33:14 +02: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,
|
2020-02-14 00:52:16 +01:00
|
|
|
npm_config_build_from_source: true,
|
|
|
|
npm_config_devdir: path.join(os.homedir(), ".electron-gyp"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:15:47 +02:00
|
|
|
public wantsStaticSqlCipherUnix(): boolean {
|
|
|
|
return this.isMac() || process.env.SQLCIPHER_STATIC == '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
public wantsStaticSqlCipher(): boolean {
|
|
|
|
return this.isWin() || this.wantsStaticSqlCipherUnix();
|
|
|
|
}
|
2021-12-14 15:32:27 +01:00
|
|
|
}
|