-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathdistribution.js
547 lines (502 loc) · 18.3 KB
/
distribution.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
const path = require('path');
const fs = require('fs');
const builder = require('electron-builder');
const { Arch } = require('electron-builder');
const { execFileSync } = require('child_process');
const noCompression = process.argv.includes('--no-compression');
const shouldNotarize = process.argv.includes('--notarize');
const universal = process.argv.includes('--universal');
const release = process.argv.includes('--release');
const targets = getOptionValue('--targets');
const hostTargetTriple = getOptionValue('--host-target-triple');
function getOptionValue(option) {
const optionIndex = process.argv.indexOf(option);
if (optionIndex !== -1) {
return process.argv[optionIndex + 1];
}
}
const config = {
appId: 'net.mullvad.vpn',
copyright: 'Mullvad VPN AB',
productName: 'Mullvad VPN',
publish: null,
asar: true,
compression: noCompression ? 'store' : 'normal',
extraResources: [
{ from: distAssets('ca.crt'), to: '.' },
{ from: buildAssets('relays.json'), to: '.' },
{ from: root('CHANGELOG.md'), to: '.' },
],
directories: {
buildResources: root('dist-assets'),
output: root('dist'),
},
extraMetadata: {
name: 'mullvad-vpn',
// We have to stick to semver on Windows for now due to:
// https://github.com/electron-userland/electron-builder/issues/7173
version: productVersion(process.platform === 'win32' ? ['semver'] : []),
},
files: [
'package.json',
'changes.txt',
'init.js',
'build/',
'!build/src/renderer',
'build/src/renderer/index.html',
'build/src/renderer/bundle.js',
'build/src/renderer/preloadBundle.js',
'!**/*.tsbuildinfo',
'!test/',
'!playwright.config.ts',
'node_modules/',
'!node_modules/grpc-tools',
'!node_modules/@types',
'!node_modules/nseventmonitor/build/Release',
],
// Make sure that all files declared in "extraResources" exists and abort if they don't.
afterPack: (context) => {
if (context.arch !== Arch.universal) {
const resources = context.packager.platformSpecificBuildOptions.extraResources;
for (const resource of resources) {
const filePath = resource.from.replace(/\$\{env\.(.*)\}/, function (match, captureGroup) {
return process.env[captureGroup];
});
if (!fs.existsSync(filePath)) {
throw new Error(`Can't find file: ${filePath}`);
}
}
}
},
mac: {
target: {
target: 'pkg',
arch: getMacArch(),
},
singleArchFiles: 'node_modules/nseventmonitor/lib/binding/Release/**',
artifactName: 'MullvadVPN-${version}.${ext}',
category: 'public.app-category.tools',
icon: distAssets('icon-macos.icns'),
notarize: shouldNotarize,
extendInfo: {
LSUIElement: true,
NSUserNotificationAlertStyle: 'banner',
},
extraResources: [
{ from: distAssets(path.join('${env.BINARIES_PATH}', 'mullvad')), to: '.' },
{ from: distAssets(path.join('${env.BINARIES_PATH}', 'mullvad-problem-report')), to: '.' },
{ from: distAssets(path.join('${env.BINARIES_PATH}', 'mullvad-daemon')), to: '.' },
{ from: distAssets(path.join('${env.BINARIES_PATH}', 'mullvad-setup')), to: '.' },
{
from: distAssets(path.join('${env.BINARIES_PATH}', 'libtalpid_openvpn_plugin.dylib')),
to: '.',
},
{ from: distAssets(path.join('binaries', '${env.TARGET_TRIPLE}', 'openvpn')), to: '.' },
{ from: distAssets(path.join('binaries', '${env.TARGET_TRIPLE}', 'apisocks5')), to: '.' },
{ from: distAssets('uninstall_macos.sh'), to: './uninstall.sh' },
{ from: buildAssets('shell-completions/_mullvad'), to: '.' },
{ from: buildAssets('shell-completions/mullvad.fish'), to: '.' },
{ from: distAssets('maybenot_machines'), to: '.' },
],
},
pkg: {
allowAnywhere: false,
allowCurrentUserHome: false,
isRelocatable: false,
isVersionChecked: false,
},
nsis: {
guid: '2A356FD4-03B7-4F45-99B4-737BE580DC82',
oneClick: false,
perMachine: true,
allowElevation: true,
allowToChangeInstallationDirectory: false,
include: distAssets('windows/installer.nsh'),
installerSidebar: distAssets('windows/installersidebar.bmp'),
},
win: {
target: [
{
target: 'nsis',
arch: getWindowsTargetArch(),
},
],
artifactName: getWindowsArtifactName(),
publisherName: 'Mullvad VPN AB',
extraResources: [
{ from: distAssets(path.join(getWindowsDistSubdir(), 'mullvad.exe')), to: '.' },
{
from: distAssets(path.join(getWindowsDistSubdir(), 'mullvad-problem-report.exe')),
to: '.',
},
{ from: distAssets(path.join(getWindowsDistSubdir(), 'mullvad-daemon.exe')), to: '.' },
{ from: distAssets(path.join(getWindowsDistSubdir(), 'talpid_openvpn_plugin.dll')), to: '.' },
{
from: root(
path.join(
'windows',
'winfw',
'bin',
getWindowsTargetArch() + '-${env.CPP_BUILD_MODE}',
'winfw.dll',
),
),
to: '.',
},
// TODO: OpenVPN does not have an ARM64 build yet.
{ from: distAssets('binaries/x86_64-pc-windows-msvc/openvpn.exe'), to: '.' },
{
from: distAssets(path.join('binaries', getWindowsTargetSubdir(), 'apisocks5.exe')),
to: '.',
},
{
from: distAssets(path.join('binaries', getWindowsTargetSubdir(), 'wintun/wintun.dll')),
to: '.',
},
{
from: distAssets(
path.join('binaries', getWindowsTargetSubdir(), 'split-tunnel/mullvad-split-tunnel.sys'),
),
to: '.',
},
{
from: distAssets(
path.join('binaries', getWindowsTargetSubdir(), 'wireguard-nt/mullvad-wireguard.dll'),
),
to: '.',
},
{ from: distAssets('maybenot_machines'), to: '.' },
],
},
linux: {
target: [
{
target: 'deb',
arch: getLinuxTargetArch(),
},
{
target: 'rpm',
arch: getLinuxTargetArch(),
},
],
artifactName: 'MullvadVPN-${version}_${arch}.${ext}',
category: 'Network',
icon: distAssets('icon.icns'),
extraFiles: [{ from: distAssets('linux/mullvad-gui-launcher.sh'), to: '.' }],
extraResources: [
{ from: distAssets(path.join(getLinuxTargetSubdir(), 'mullvad-problem-report')), to: '.' },
{ from: distAssets(path.join(getLinuxTargetSubdir(), 'mullvad-setup')), to: '.' },
{
from: distAssets(path.join(getLinuxTargetSubdir(), 'libtalpid_openvpn_plugin.so')),
to: '.',
},
{ from: distAssets(path.join('linux', 'apparmor_mullvad')), to: '.' },
{ from: distAssets(path.join('binaries', '${env.TARGET_TRIPLE}', 'openvpn')), to: '.' },
{ from: distAssets(path.join('binaries', '${env.TARGET_TRIPLE}', 'apisocks5')), to: '.' },
{ from: distAssets('maybenot_machines'), to: '.' },
],
},
deb: {
fpm: [
'--no-depends',
'--version',
getLinuxVersion(),
'--before-install',
distAssets('linux/before-install.sh'),
'--before-remove',
distAssets('linux/before-remove.sh'),
distAssets('linux/mullvad-daemon.service') +
'=/usr/lib/systemd/system/mullvad-daemon.service',
distAssets('linux/mullvad-early-boot-blocking.service') +
'=/usr/lib/systemd/system/mullvad-early-boot-blocking.service',
distAssets(path.join(getLinuxTargetSubdir(), 'mullvad')) + '=/usr/bin/',
distAssets(path.join(getLinuxTargetSubdir(), 'mullvad-daemon')) + '=/usr/bin/',
distAssets(path.join(getLinuxTargetSubdir(), 'mullvad-exclude')) + '=/usr/bin/',
distAssets('linux/problem-report-link') + '=/usr/bin/mullvad-problem-report',
buildAssets('shell-completions/mullvad.bash') +
'=/usr/share/bash-completion/completions/mullvad',
buildAssets('shell-completions/_mullvad') + '=/usr/local/share/zsh/site-functions/_mullvad',
buildAssets('shell-completions/mullvad.fish') +
'=/usr/share/fish/vendor_completions.d/mullvad.fish',
],
afterInstall: distAssets('linux/after-install.sh'),
afterRemove: distAssets('linux/after-remove.sh'),
},
rpm: {
fpm: [
'--version',
getLinuxVersion(),
// Prevents RPM from packaging build-id metadata, some of which is the
// same across all electron-builder applications, which causes package
// conflicts
'--rpm-rpmbuild-define=_build_id_links none',
'--directories=/opt/Mullvad VPN/',
'--before-install',
distAssets('linux/before-install.sh'),
'--before-remove',
distAssets('linux/before-remove.sh'),
'--rpm-posttrans',
distAssets('linux/post-transaction.sh'),
distAssets('linux/mullvad-daemon.service') +
'=/usr/lib/systemd/system/mullvad-daemon.service',
distAssets('linux/mullvad-early-boot-blocking.service') +
'=/usr/lib/systemd/system/mullvad-early-boot-blocking.service',
distAssets(path.join(getLinuxTargetSubdir(), 'mullvad')) + '=/usr/bin/',
distAssets(path.join(getLinuxTargetSubdir(), 'mullvad-daemon')) + '=/usr/bin/',
distAssets(path.join(getLinuxTargetSubdir(), 'mullvad-exclude')) + '=/usr/bin/',
distAssets('linux/problem-report-link') + '=/usr/bin/mullvad-problem-report',
buildAssets('shell-completions/mullvad.bash') +
'=/usr/share/bash-completion/completions/mullvad',
buildAssets('shell-completions/_mullvad') + '=/usr/share/zsh/site-functions/_mullvad',
buildAssets('shell-completions/mullvad.fish') +
'=/usr/share/fish/vendor_completions.d/mullvad.fish',
],
afterInstall: distAssets('linux/after-install.sh'),
afterRemove: distAssets('linux/after-remove.sh'),
depends: ['libXScrnSaver', 'libnotify', 'dbus-libs'],
},
};
function packWin() {
return builder.build({
targets: builder.Platform.WINDOWS.createTarget(),
config: {
...config,
asarUnpack: ['build/assets/images/menubar-icons/win32/lock-*.ico'],
beforeBuild: (options) => {
process.env.CPP_BUILD_MODE = release ? 'Release' : 'Debug';
process.env.CPP_BUILD_TARGET = options.arch;
process.env.TARGET_ARCHITECTURE = options.arch;
switch (options.arch) {
case 'x64':
process.env.TARGET_TRIPLE = 'x86_64-pc-windows-msvc';
process.env.SETUP_SUBDIR = '.';
break;
case 'arm64':
process.env.TARGET_TRIPLE = 'aarch64-pc-windows-msvc';
process.env.SETUP_SUBDIR = 'aarch64-pc-windows-msvc';
break;
default:
throw new Error('Invalid or unknown target (only one may be specified)');
}
return true;
},
afterAllArtifactBuild: (buildResult) => {
// All of this is a hack to work around the limitation in:
// https://github.com/electron-userland/electron-builder/issues/7173
const productSemverVersion = productVersion(['semver']);
const productTargetVersion = productVersion([]);
// Rename the artifacts so that they don't have the .0 (semver format)
for (const artifactPath of buildResult.artifactPaths) {
const artifactDir = path.dirname(artifactPath);
const artifactSemverFilename = path.basename(artifactPath);
const artifactDesiredFilename = artifactSemverFilename.replace(
productSemverVersion,
productTargetVersion,
);
const targetArtifactPath = path.join(artifactDir, artifactDesiredFilename);
console.log('Moving', artifactSemverFilename, '=>', artifactDesiredFilename);
fs.renameSync(artifactPath, targetArtifactPath);
}
},
},
});
}
function packMac() {
const appOutDirs = [];
return builder.build({
targets: builder.Platform.MAC.createTarget(),
config: {
...config,
asarUnpack: ['**/*.node'],
beforeBuild: (options) => {
switch (options.arch) {
case 'x64':
process.env.TARGET_TRIPLE = 'x86_64-apple-darwin';
break;
case 'arm64':
process.env.TARGET_TRIPLE = 'aarch64-apple-darwin';
break;
default:
delete process.env.TARGET_TRIPLE;
break;
}
process.env.BINARIES_PATH =
hostTargetTriple !== process.env.TARGET_TRIPLE ? process.env.TARGET_TRIPLE : '';
return true;
},
beforePack: async (context) => {
try {
// `@electron/universal` tries to lipo together libraries built for the same architecture
// if they're present for both targets. So make sure we remove libraries for other archs.
// Remove the workaround once the issue has been fixed:
// https://github.com/electron/universal/issues/41#issuecomment-1496288834
await fs.promises.rm('node_modules/nseventmonitor/lib/binding/Release', {
recursive: true,
});
} catch {
// noop
}
config.beforePack?.(context);
},
afterPack: (context) => {
config.afterPack?.(context);
if (context.arch !== Arch.universal) {
delete process.env.TARGET_TRIPLE;
appOutDirs.push(context.appOutDir);
}
return Promise.resolve();
},
afterAllArtifactBuild: async (_buildResult) => {
// Remove the folder that contains the unpacked app. Electron builder cleans up some of
// these directories and it's changed between versions without a mention in the changelog.
for (const dir of appOutDirs) {
try {
await fs.promises.rm(dir, { recursive: true });
} catch {
// noop
}
}
},
afterSign: (context) => {
const appOutDir = context.appOutDir;
appOutDirs.push(appOutDir);
},
},
});
}
function packLinux() {
if (noCompression) {
config.rpm.fpm.unshift('--rpm-compression', 'none');
}
if (targets && targets === 'aarch64-unknown-linux-gnu') {
config.rpm.fpm.unshift('--architecture', 'aarch64');
}
return builder.build({
targets: builder.Platform.LINUX.createTarget(),
config: {
...config,
beforeBuild: (options) => {
switch (options.arch) {
case 'x64':
process.env.TARGET_TRIPLE = 'x86_64-unknown-linux-gnu';
break;
case 'arm64':
process.env.TARGET_TRIPLE = 'aarch64-unknown-linux-gnu';
break;
default:
delete process.env.TARGET_TRIPLE;
break;
}
return true;
},
afterPack: async (context) => {
config.afterPack?.(context);
const sourceExecutable = path.join(context.appOutDir, 'mullvad-vpn');
const targetExecutable = path.join(context.appOutDir, 'mullvad-gui');
const launcherScript = path.join(context.appOutDir, 'mullvad-gui-launcher.sh');
// rename mullvad-vpn to mullvad-gui
await fs.promises.rename(sourceExecutable, targetExecutable);
// rename launcher script to mullvad-vpn
await fs.promises.rename(launcherScript, sourceExecutable);
},
},
});
}
function buildAssets(relativePath) {
return path.join(path.resolve(__dirname, '../../build'), relativePath);
}
function distAssets(relativePath) {
return path.join(path.resolve(__dirname, '../../dist-assets'), relativePath);
}
function root(relativePath) {
return path.join(path.resolve(__dirname, '../../'), relativePath);
}
function getWindowsDistSubdir() {
if (targets === 'aarch64-pc-windows-msvc') {
return targets;
} else {
return '';
}
}
function getWindowsTargetArch() {
if (targets && process.platform === 'win32') {
if (targets === 'aarch64-pc-windows-msvc') {
return 'arm64';
}
throw new Error('Invalid or unknown target (only one may be specified)');
}
// Use host architecture (we assume this is x64 since building on Arm64 isn't supported).
return 'x64';
}
function getWindowsArtifactName() {
if (targets === 'aarch64-pc-windows-msvc') {
return 'MullvadVPN-${version}_${arch}.${ext}';
}
return 'MullvadVPN-${version}.${ext}';
}
function getWindowsTargetSubdir() {
if (targets && process.platform === 'win32') {
if (targets === 'aarch64-pc-windows-msvc') {
return targets;
}
throw new Error('Invalid or unknown target (only one may be specified)');
}
// Use host architecture (we assume this is x64 since building on Arm64 isn't supported).
return 'x86_64-pc-windows-msvc';
}
function getLinuxTargetArch() {
if (targets && process.platform === 'linux') {
if (targets === 'aarch64-unknown-linux-gnu') {
return 'arm64';
}
throw new Error('Invalid or unknown target (only one may be specified)');
}
// Use host architecture.
return undefined;
}
function getLinuxTargetSubdir() {
if (targets && process.platform === 'linux') {
if (targets === 'aarch64-unknown-linux-gnu') {
return targets;
}
throw new Error('Invalid or unknown target (only one may be specified)');
}
return '';
}
function getMacArch() {
if (universal) {
return 'universal';
} else {
// Not specifying an arch makes Electron builder build for the arch it's running on.
return undefined;
}
}
// Replace '-' with `~` (tilde) before the beta component, to make the version comparison
// understand that stable `YYYY.NN` is newer than beta `YYYY.NN-betaN`. Both Debian and
// Fedora do this where a tilde denotes a version component that must be sorted as earlier
// than a non-tilde version component
// https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_complex_versioning
function getLinuxVersion() {
const [version, ...prereleaseParts] = productVersion([]).split('-');
const [major, minor] = version.split('.');
const prerelease = prereleaseParts.join('-');
if (prerelease) {
if (prerelease.toLowerCase().startsWith('beta')) {
return `${major}.${minor}~${prerelease}`;
}
return `${major}.${minor}-${prerelease}`;
}
return `${major}.${minor}`;
}
// Returns the product version. The `args` argument is optional. Set it to `'semver'`
// to get the version in semver format.
function productVersion(extraArgs) {
const args = ['run', '-q', '--bin', 'mullvad-version', ...extraArgs];
return execFileSync('cargo', args, { encoding: 'utf-8' }).trim();
}
packWin.displayName = 'builder-win';
packMac.displayName = 'builder-mac';
packLinux.displayName = 'builder-linux';
exports.packWin = packWin;
exports.packMac = packMac;
exports.packLinux = packLinux;