diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-05-05 01:06:13 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-05-16 00:55:03 +0200 |
commit | a9d9b6cc1b29e424ade426d82db2fc54f0ea8988 (patch) | |
tree | 84da5ec1c544863ab7731aefbac657cf2d80ead5 /config | |
parent | feat: use wayland when available (diff) | |
download | sophie-a9d9b6cc1b29e424ade426d82db2fc54f0ea8988.tar.gz sophie-a9d9b6cc1b29e424ade426d82db2fc54f0ea8988.tar.zst sophie-a9d9b6cc1b29e424ade426d82db2fc54f0ea8988.zip |
refactor: electron-builder config
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'config')
-rw-r--r-- | config/burnFuses.cjs | 47 | ||||
-rw-r--r-- | config/enableWaylandAutoDetection.cjs | 86 |
2 files changed, 133 insertions, 0 deletions
diff --git a/config/burnFuses.cjs b/config/burnFuses.cjs new file mode 100644 index 0000000..a6ea197 --- /dev/null +++ b/config/burnFuses.cjs | |||
@@ -0,0 +1,47 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | const path = require('node:path'); | ||
22 | |||
23 | const { flipFuses } = require('@electron/fuses'); | ||
24 | |||
25 | /** | ||
26 | * Hardens the shipped electron binary by burning some electron fuses. | ||
27 | * | ||
28 | * | ||
29 | * @param {import('electron-builder').AfterPackContext} context The `electron-builder` context. | ||
30 | * @param {import('@electron/fuses').FuseConfig<boolean>} config The fuses to burn. | ||
31 | * @return {Promise<void>} The promise to flip the fuses. | ||
32 | * @see https://github.com/electron/fuses | ||
33 | */ | ||
34 | module.exports = async function burnFuses(context, config) { | ||
35 | /** @type {string} */ | ||
36 | const ext = | ||
37 | { | ||
38 | darwin: '.app', | ||
39 | win32: '.exe', | ||
40 | }[context.electronPlatformName] || ''; | ||
41 | const electronBinaryPath = path.join( | ||
42 | context.appOutDir, | ||
43 | `${context.packager.appInfo.productFilename}${ext}`, | ||
44 | ); | ||
45 | /** @type {import('@electron/fuses').FuseConfig<boolean>} */ | ||
46 | return flipFuses(electronBinaryPath, config); | ||
47 | }; | ||
diff --git a/config/enableWaylandAutoDetection.cjs b/config/enableWaylandAutoDetection.cjs new file mode 100644 index 0000000..393ec91 --- /dev/null +++ b/config/enableWaylandAutoDetection.cjs | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | const { readFile, rename, writeFile } = require('node:fs/promises'); | ||
22 | const path = require('node:path'); | ||
23 | |||
24 | const templatePath = path.join(__dirname, '../build-helpers/detect_wayland.sh'); | ||
25 | |||
26 | /** | ||
27 | * Replaces the `{{handlebars}}` in the template with the replacements. | ||
28 | * | ||
29 | * Each `{{VARIABLE}}` will be repalced by `value` in the replacement `['VARIABLE', value]`. | ||
30 | * | ||
31 | * @param {string} template The template. | ||
32 | * @param {[string, string][]} replacements The replacements to apply. | ||
33 | * @returns | ||
34 | */ | ||
35 | function executeTemplate(template, ...replacements) { | ||
36 | const replacementsMap = new Map(replacements); | ||
37 | return template.replaceAll( | ||
38 | /{{([^}]+)}}/g, | ||
39 | (_match, /** @type {string} */ variable) => { | ||
40 | const replacement = replacementsMap.get(variable); | ||
41 | if (replacement === undefined) { | ||
42 | throw new Error(`Unknown variable: ${variable}`); | ||
43 | } | ||
44 | return replacement; | ||
45 | }, | ||
46 | ); | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Adds a wrapper scripts that detects in wayland is in use and enabled it in chromium. | ||
51 | * | ||
52 | * The script in `build-heleprs/detect_wayland.sh` uses the `WAYLAND_DISPLAY` environmental | ||
53 | * variable to detect whether wayland is in use. | ||
54 | * | ||
55 | * If wayland is in use, the script enables the wayland ozone backed for chromium | ||
56 | * and pipewire screen sharing. Otherwise, the x11 ozone backend will be used. | ||
57 | * | ||
58 | * @param {import('electron-builder').AfterPackContext} context The `electron-builder` context. | ||
59 | * @return {Promise<void>} The promise to add the wrapper script. | ||
60 | * @see https://stackoverflow.com/a/45537237 | ||
61 | */ | ||
62 | module.exports = async function enableWaylandAutoDetection(context) { | ||
63 | const { | ||
64 | appOutDir, | ||
65 | packager: { | ||
66 | appInfo: { productName, productFilename }, | ||
67 | }, | ||
68 | } = context; | ||
69 | |||
70 | const electronBinaryPath = path.join(appOutDir, productFilename); | ||
71 | const newFilename = `${productFilename}-bin`; | ||
72 | const newElectronBinaryPath = path.join(appOutDir, newFilename); | ||
73 | |||
74 | await rename(electronBinaryPath, newElectronBinaryPath); | ||
75 | |||
76 | const wrapperScriptTempate = await readFile(templatePath, 'utf8'); | ||
77 | const wrapperScript = executeTemplate( | ||
78 | wrapperScriptTempate, | ||
79 | ['PRODUCT_NAME', productName], | ||
80 | ['REAL_BINARY_NAME', newFilename], | ||
81 | ); | ||
82 | await writeFile(electronBinaryPath, wrapperScript, { | ||
83 | encoding: 'utf8', | ||
84 | mode: 0o755, | ||
85 | }); | ||
86 | }; | ||