-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
392 lines (355 loc) · 10.5 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* This works like "electron-builder", but only builds the base
* Then, you can package the "unpacked" folder to whatever you want~
*/
import packageJson from "./package.json" assert { type: "json" };
import fs from "fs-extra";
import path from "path";
import childProcess from "child_process";
import {
makeCopy,
makeCopyFromTo,
patchFile,
getLinuxDistro,
} from "./build/utils.js";
import yaargs from "yargs";
import ora from "ora";
import { hideBin } from "yargs/helpers";
import { fileURLToPath } from "url";
const yargs = yaargs(hideBin(process.argv));
let DEST_DIR = "/";
let PREFIX = "/usr";
let INSTALL_ZSH_COMPLETION = true;
let INSTALL_BASH_COMPLETION = true;
let ARCH = process.arch;
let INSTALL_ROOT = fileURLToPath(new URL("./build/unpacked/", import.meta.url));
let ASAR_ROOT = fileURLToPath(new URL("./build/nody-asar/", import.meta.url));
yargs.parserConfiguration({
"short-option-groups": true,
"camel-case-expansion": false,
"dot-notation": true,
"parse-numbers": true,
"parse-positional-numbers": true,
"boolean-negation": true,
"deep-merge-config": false,
});
let argv = yargs
.scriptName("build")
.option("DEST_DIR", {
type: "string",
describe: "Where to install nody-greeter",
default: DEST_DIR,
})
.option("PREFIX", {
type: "string",
describe: "Prefix to install at",
default: PREFIX,
})
.option("ARCH", {
type: "string",
describe: "Architecture to build for",
default: ARCH,
})
.option("INSTALL_ZSH_COMPLETION", {
type: "boolean",
describe: "Wheter to install zsh completion",
default: INSTALL_ZSH_COMPLETION,
})
.option("INSTALL_BASH_COMPLETION", {
type: "boolean",
describe: "Wheter to install bash completion",
default: INSTALL_BASH_COMPLETION,
})
.help("h")
.alias("h", "help")
.version(false).argv;
DEST_DIR = argv.DEST_DIR;
PREFIX = argv.PREFIX;
ARCH = argv.ARCH;
INSTALL_ZSH_COMPLETION = argv.INSTALL_ZSH_COMPLETION;
INSTALL_BASH_COMPLETION = argv.INSTALL_BASH_COMPLETION;
// Some global variables
let nody_path = path.join(INSTALL_ROOT, "opt/nody-greeter");
// let bin_path = path.join(INSTALL_ROOT, PREFIX, "bin");
let lightdm_path = path.join(INSTALL_ROOT, "etc/lightdm");
let webg_path = path.join(INSTALL_ROOT, PREFIX, "share/web-greeter");
let xgreeters_path = path.join(INSTALL_ROOT, PREFIX, "share/xgreeters");
let applications_path = path.join(INSTALL_ROOT, PREFIX, "share/applications");
let icons_path = path.join(INSTALL_ROOT, PREFIX, "share/icons/hicolor");
let xdg_ldm_path = path.join(INSTALL_ROOT, "etc/xdg/lightdm/lightdm.conf.d/");
let bash_c_path = path.join(
INSTALL_ROOT,
PREFIX,
"share/bash-completion/completions"
);
let zsh_c_path = path.join(
INSTALL_ROOT,
PREFIX,
"share/zsh/vendor-completions/"
);
// Functions
let copies_binding = [
{
from: "./src/main/bindings/screensaver.cc",
to: "./out/main/bindings/screensaver.cc",
},
{
from: "./src/main/bindings/binding.gyp",
to: "./out/main/bindings/binding.gyp",
},
{
from: "./src/main/bindings/package.json",
to: "./out/main/bindings/package.json",
},
];
async function compile_bindings() {
fs.ensureDirSync("./out/main/bindings/");
await makeCopyFromTo(copies_binding);
console.log("Bindings copied");
let spinner = ora({
text: `Compiling bindings with electron-rebuild for ${ARCH}...`,
spinner: "dots",
});
spinner.start();
await new Promise((resolve) => {
childProcess.exec(
`npx electron-rebuild -m . --arch ${ARCH}`,
{
cwd: "./out/main/bindings",
encoding: "utf-8",
stdio: "ignore",
},
(error) => {
if (error) {
console.error(error);
spinner.fail("electron-rebuild failed");
process.exit(1);
}
resolve();
}
);
});
spinner.succeed("Bindings compiled succesfully");
}
let copies = [
{ from: "./out", to: path.join(ASAR_ROOT, "out") },
{ from: "./package.json", to: path.join(ASAR_ROOT, "package.json") },
{
from: "./package-lock.json",
to: path.join(ASAR_ROOT, "package-lock.json"),
},
];
function check_program(program) {
let res = childProcess.spawnSync("which", [program], {
encoding: "utf-8",
});
if (res.status == 0) return true;
else return false;
}
async function create_build() {
fs.mkdirSync(ASAR_ROOT, { recursive: true });
fs.mkdirSync(INSTALL_ROOT, { recursive: true });
await makeCopyFromTo(copies);
console.log("Resources copied");
try {
console.log(
"Installing packages with 'npm ci --omit=dev --ignore-scripts'"
);
childProcess.execSync("npm ci --omit=dev --ignore-scripts", {
cwd: "./build/nody-asar",
encoding: "utf-8",
stdio: "inherit",
});
console.log("Packages installed");
} catch (err) {
console.error(err);
}
}
function find_electron_binding() {
const electronMatch = packageJson.devDependencies.electron.match(
/^\^*(?<major>\d+)(?:\.(?<minor>\d+))?/
);
let electronVersion = ".*";
if (electronMatch && electronMatch.groups) {
const major = electronMatch.groups.major;
const minor = electronMatch.groups.minor ? electronMatch.groups.minor : "0";
electronVersion = `${major}.${minor}`;
}
let binding_exists = fs.pathExistsSync(
"./node_modules/node-gtk/lib/binding/"
);
if (!binding_exists) {
console.error(
"Node-gtk bindings not found, be sure to install npm dependencies"
);
process.exit(1);
}
fs.removeSync("./build/nody-asar/node_modules/node-gtk/lib/binding/");
let bindings = fs.readdirSync("./node_modules/node-gtk/lib/binding/");
let re = new RegExp(`electron-v${electronVersion}-linux-${ARCH}`);
let electron_binding = bindings.find((v) => v.match(re));
return electron_binding;
}
async function ensure_electron_binding() {
let electron_binding = find_electron_binding();
if (electron_binding) {
console.log("Node-gtk binding for electron found!");
} else {
try {
let spinner = ora({
text: `Node-gtk binding for electron not found. Compiling for ${ARCH}...`,
spinner: "dots",
});
spinner.start();
await new Promise((resolve) => {
childProcess.exec(
`npx electron-rebuild -w node-gtk --build-from-source --arch ${ARCH}`,
{
encoding: "utf-8",
stdio: "ignore",
},
(error) => {
if (error) {
console.error(error);
spinner.fail("electron-rebuild failed");
process.exit(1);
}
resolve();
}
);
});
spinner.succeed("Node-gtk compiled");
electron_binding = find_electron_binding();
} catch (err) {
console.error(err);
process.exit(1);
}
}
if (!electron_binding) {
console.error("Electron binding couldn't be found");
process.exit(1);
}
// Remove node-gtk build files
fs.removeSync("./build/nody-asar/node_modules/node-gtk/build/");
return electron_binding;
}
async function copy_electron_binding() {
let electron_binding = await ensure_electron_binding();
let path_to_binding = path.join(
"node_modules/node-gtk/lib/binding",
electron_binding
);
await makeCopy(
path.resolve(path_to_binding),
path.resolve(ASAR_ROOT, path_to_binding)
);
console.log("Binding copied");
}
function create_install_root() {
fs.mkdirsSync(nody_path, { recursive: true });
fs.mkdirsSync(lightdm_path, { recursive: true });
fs.mkdirsSync(webg_path, { recursive: true });
fs.mkdirsSync(xdg_ldm_path, { recursive: true });
fs.mkdirsSync(xgreeters_path, { recursive: true });
fs.mkdirsSync(applications_path, { recursive: true });
fs.mkdirsSync(icons_path, { recursive: true });
fs.mkdirsSync(path.join(icons_path, "scalable/apps"), { recursive: true });
if (check_program("bash") && INSTALL_BASH_COMPLETION)
fs.mkdirsSync(bash_c_path, { recursive: true });
if (check_program("zsh") && INSTALL_ZSH_COMPLETION)
fs.mkdirsSync(zsh_c_path, { recursive: true });
}
let copies_prepare = [
{
from: "./dist/web-greeter.yml",
to: path.join(lightdm_path, "web-greeter.yml"),
},
{
from: "./dist/nody-xgreeter.desktop",
to: path.join(xgreeters_path, "nody-greeter.desktop"),
},
{
from: "./dist/nody-greeter.desktop",
to: path.join(applications_path, "nody-greeter.desktop"),
},
{
from: "./dist/com.github.jezerm.nody-greeter.svg",
to: path.join(
icons_path,
"scalable/apps/com.github.jezerm.nody-greeter.svg"
),
},
{
from: "./dist/90-greeter-wrapper.conf",
to: path.join(xdg_ldm_path, "90-greeter-wrapper.conf"),
},
{
from: "./dist/Xgreeter",
to: path.join(lightdm_path, "Xgreeter"),
},
{
from: "./themes/themes/",
to: path.join(webg_path, "themes/"),
},
{
from: "./node_modules/electron/dist/",
to: nody_path,
},
];
async function prepare_install() {
create_install_root();
if (check_program("bash") && INSTALL_BASH_COMPLETION)
copies_prepare.push({
from: "./dist/nody-greeter-bash",
to: path.join(bash_c_path, "nody-greeter"),
});
if (check_program("zsh") && INSTALL_ZSH_COMPLETION)
copies_prepare.push({
from: "./dist/nody-greeter-zsh",
to: path.join(zsh_c_path, "_nody-greeter"),
});
await makeCopyFromTo(copies_prepare);
const distro = getLinuxDistro();
switch (distro) {
case "fedora":
console.log(
"The current distro has some issues with sandboxed browsers in the LightDM environment. Patching..."
);
patchFile(path.join(lightdm_path, "Xgreeter"), "./build/Xgreeter.patch");
break;
}
fs.removeSync(path.join(nody_path, "resources"));
fs.renameSync(
path.join(nody_path, "electron"),
path.join(nody_path, "nody-greeter")
);
fs.moveSync(
path.join(webg_path, "themes/_vendor"),
path.join(webg_path, "_vendor/"),
{
overwrite: true,
}
);
console.log("INSTALL_ROOT (build/unpacked) prepared");
}
async function build_asar() {
let asar_dest = path.join(nody_path, "resources/app.asar");
console.log(`Creating 'asar' package in '${asar_dest}'`);
let spinner = ora({
text: `Creating package...`,
spinner: "dots",
});
spinner.start();
const asar = await import("@electron/asar");
await asar.createPackage(ASAR_ROOT, asar_dest);
spinner.succeed('"asar" package created');
}
export async function build() {
console.log("Building with prefix:", PREFIX);
await compile_bindings();
await create_build();
await copy_electron_binding();
await prepare_install();
await build_asar();
console.log("\x1b[92mSUCCESS!\x1b[0m");
}