winPackager.js
16 KB
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.WinPackager = void 0;
function _bluebirdLst() {
const data = _interopRequireDefault(require("bluebird-lst"));
_bluebirdLst = function () {
return data;
};
return data;
}
function _builderUtil() {
const data = require("builder-util");
_builderUtil = function () {
return data;
};
return data;
}
function _builderUtilRuntime() {
const data = require("builder-util-runtime");
_builderUtilRuntime = function () {
return data;
};
return data;
}
function _fs() {
const data = require("builder-util/out/fs");
_fs = function () {
return data;
};
return data;
}
function _crypto() {
const data = require("crypto");
_crypto = function () {
return data;
};
return data;
}
function _fsExtra() {
const data = require("fs-extra");
_fsExtra = function () {
return data;
};
return data;
}
function _isCi() {
const data = _interopRequireDefault(require("is-ci"));
_isCi = function () {
return data;
};
return data;
}
function _lazyVal() {
const data = require("lazy-val");
_lazyVal = function () {
return data;
};
return data;
}
var path = _interopRequireWildcard(require("path"));
function _codesign() {
const data = require("./codeSign/codesign");
_codesign = function () {
return data;
};
return data;
}
function _windowsCodeSign() {
const data = require("./codeSign/windowsCodeSign");
_windowsCodeSign = function () {
return data;
};
return data;
}
function _core() {
const data = require("./core");
_core = function () {
return data;
};
return data;
}
function _platformPackager() {
const data = require("./platformPackager");
_platformPackager = function () {
return data;
};
return data;
}
function _NsisTarget() {
const data = require("./targets/nsis/NsisTarget");
_NsisTarget = function () {
return data;
};
return data;
}
function _nsisUtil() {
const data = require("./targets/nsis/nsisUtil");
_nsisUtil = function () {
return data;
};
return data;
}
function _WebInstallerTarget() {
const data = require("./targets/nsis/WebInstallerTarget");
_WebInstallerTarget = function () {
return data;
};
return data;
}
function _targetFactory() {
const data = require("./targets/targetFactory");
_targetFactory = function () {
return data;
};
return data;
}
function _cacheManager() {
const data = require("./util/cacheManager");
_cacheManager = function () {
return data;
};
return data;
}
function _flags() {
const data = require("./util/flags");
_flags = function () {
return data;
};
return data;
}
function _timer() {
const data = require("./util/timer");
_timer = function () {
return data;
};
return data;
}
function _vm() {
const data = require("./vm/vm");
_vm = function () {
return data;
};
return data;
}
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class WinPackager extends _platformPackager().PlatformPackager {
constructor(info) {
super(info, _core().Platform.WINDOWS);
this.cscInfo = new (_lazyVal().Lazy)(() => {
const platformSpecificBuildOptions = this.platformSpecificBuildOptions;
if (platformSpecificBuildOptions.certificateSubjectName != null || platformSpecificBuildOptions.certificateSha1 != null) {
return this.vm.value.then(vm => (0, _windowsCodeSign().getCertificateFromStoreInfo)(platformSpecificBuildOptions, vm)).catch(e => {
// https://github.com/electron-userland/electron-builder/pull/2397
if (platformSpecificBuildOptions.sign == null) {
throw e;
} else {
_builderUtil().log.debug({
error: e
}, "getCertificateFromStoreInfo error");
return null;
}
});
}
const certificateFile = platformSpecificBuildOptions.certificateFile;
if (certificateFile != null) {
const certificatePassword = this.getCscPassword();
return Promise.resolve({
file: certificateFile,
password: certificatePassword == null ? null : certificatePassword.trim()
});
}
const cscLink = this.getCscLink("WIN_CSC_LINK");
if (cscLink == null) {
return Promise.resolve(null);
}
return (0, _codesign().downloadCertificate)(cscLink, this.info.tempDirManager, this.projectDir) // before then
.catch(e => {
if (e instanceof _builderUtil().InvalidConfigurationError) {
throw new (_builderUtil().InvalidConfigurationError)(`Env WIN_CSC_LINK is not correct, cannot resolve: ${e.message}`);
} else {
throw e;
}
}).then(path => {
return {
file: path,
password: this.getCscPassword()
};
});
});
this._iconPath = new (_lazyVal().Lazy)(() => this.getOrConvertIcon("ico"));
this.vm = new (_lazyVal().Lazy)(() => process.platform === "win32" ? Promise.resolve(new (_vm().VmManager)()) : (0, _vm().getWindowsVm)(this.debugLogger));
this.computedPublisherName = new (_lazyVal().Lazy)(async () => {
const publisherName = this.platformSpecificBuildOptions.publisherName;
if (publisherName === null) {
return null;
} else if (publisherName != null) {
return (0, _builderUtil().asArray)(publisherName);
}
const certInfo = await this.lazyCertInfo.value;
return certInfo == null ? null : [certInfo.commonName];
});
this.lazyCertInfo = new (_lazyVal().Lazy)(async () => {
const cscInfo = await this.cscInfo.value;
if (cscInfo == null) {
return null;
}
if ("subject" in cscInfo) {
const bloodyMicrosoftSubjectDn = cscInfo.subject;
return {
commonName: (0, _builderUtilRuntime().parseDn)(bloodyMicrosoftSubjectDn).get("CN"),
bloodyMicrosoftSubjectDn
};
}
const cscFile = cscInfo.file;
if (cscFile == null) {
return null;
}
return await (0, _windowsCodeSign().getCertInfo)(cscFile, cscInfo.password || "");
});
}
get isForceCodeSigningVerification() {
return this.platformSpecificBuildOptions.verifyUpdateCodeSignature !== false;
}
get defaultTarget() {
return ["nsis"];
}
doGetCscPassword() {
return (0, _platformPackager().chooseNotNull)((0, _platformPackager().chooseNotNull)(this.platformSpecificBuildOptions.certificatePassword, process.env.WIN_CSC_KEY_PASSWORD), super.doGetCscPassword());
}
createTargets(targets, mapper) {
let copyElevateHelper;
const getCopyElevateHelper = () => {
if (copyElevateHelper == null) {
copyElevateHelper = new (_nsisUtil().CopyElevateHelper)();
}
return copyElevateHelper;
};
let helper;
const getHelper = () => {
if (helper == null) {
helper = new (_nsisUtil().AppPackageHelper)(getCopyElevateHelper());
}
return helper;
};
for (const name of targets) {
if (name === _core().DIR_TARGET) {
continue;
}
if (name === "nsis" || name === "portable") {
mapper(name, outDir => new (_NsisTarget().NsisTarget)(this, outDir, name, getHelper()));
} else if (name === "nsis-web") {
// package file format differs from nsis target
mapper(name, outDir => new (_WebInstallerTarget().WebInstallerTarget)(this, path.join(outDir, name), name, new (_nsisUtil().AppPackageHelper)(getCopyElevateHelper())));
} else {
const targetClass = (() => {
switch (name) {
case "squirrel":
try {
return require("electron-builder-squirrel-windows").default;
} catch (e) {
throw new (_builderUtil().InvalidConfigurationError)(`Module electron-builder-squirrel-windows must be installed in addition to build Squirrel.Windows: ${e.stack || e}`);
}
case "appx":
return require("./targets/AppxTarget").default;
case "msi":
return require("./targets/MsiTarget").default;
default:
return null;
}
})();
mapper(name, outDir => targetClass === null ? (0, _targetFactory().createCommonTarget)(name, outDir, this) : new targetClass(this, outDir, name));
}
}
}
getIconPath() {
return this._iconPath.value;
}
async sign(file, logMessagePrefix) {
const signOptions = {
path: file,
name: this.appInfo.productName,
site: await this.appInfo.computePackageUrl(),
options: this.platformSpecificBuildOptions
};
const cscInfo = await this.cscInfo.value;
if (cscInfo == null) {
if (this.platformSpecificBuildOptions.sign != null) {
await (0, _windowsCodeSign().sign)(signOptions, this);
} else if (this.forceCodeSigning) {
throw new (_builderUtil().InvalidConfigurationError)(`App is not signed and "forceCodeSigning" is set to true, please ensure that code signing configuration is correct, please see https://electron.build/code-signing`);
}
return;
}
if (logMessagePrefix == null) {
logMessagePrefix = "signing";
}
if ("file" in cscInfo) {
_builderUtil().log.info({
file: _builderUtil().log.filePath(file),
certificateFile: cscInfo.file
}, logMessagePrefix);
} else {
const info = cscInfo;
_builderUtil().log.info({
file: _builderUtil().log.filePath(file),
subject: info.subject,
thumbprint: info.thumbprint,
store: info.store,
user: info.isLocalMachineStore ? "local machine" : "current user"
}, logMessagePrefix);
}
await this.doSign({ ...signOptions,
cscInfo,
options: { ...this.platformSpecificBuildOptions
}
});
}
async doSign(options) {
for (let i = 0; i < 3; i++) {
try {
await (0, _windowsCodeSign().sign)(options, this);
break;
} catch (e) {
// https://github.com/electron-userland/electron-builder/issues/1414
const message = e.message;
if (message != null && message.includes("Couldn't resolve host name")) {
_builderUtil().log.warn({
error: message,
attempt: i + 1
}, `cannot sign`);
continue;
}
throw e;
}
}
}
async signAndEditResources(file, arch, outDir, internalName, requestedExecutionLevel) {
const appInfo = this.appInfo;
const files = [];
const args = [file, "--set-version-string", "FileDescription", appInfo.productName, "--set-version-string", "ProductName", appInfo.productName, "--set-version-string", "LegalCopyright", appInfo.copyright, "--set-file-version", appInfo.shortVersion || appInfo.buildVersion, "--set-product-version", appInfo.shortVersionWindows || appInfo.getVersionInWeirdWindowsForm()];
if (internalName != null) {
args.push("--set-version-string", "InternalName", internalName, "--set-version-string", "OriginalFilename", "");
}
if (requestedExecutionLevel != null && requestedExecutionLevel !== "asInvoker") {
args.push("--set-requested-execution-level", requestedExecutionLevel);
}
(0, _builderUtil().use)(appInfo.companyName, it => args.push("--set-version-string", "CompanyName", it));
(0, _builderUtil().use)(this.platformSpecificBuildOptions.legalTrademarks, it => args.push("--set-version-string", "LegalTrademarks", it));
const iconPath = await this.getIconPath();
(0, _builderUtil().use)(iconPath, it => {
files.push(it);
args.push("--set-icon", it);
});
const config = this.config;
const cscInfoForCacheDigest = !(0, _flags().isBuildCacheEnabled)() || _isCi().default || config.electronDist != null ? null : await this.cscInfo.value;
let buildCacheManager = null; // resources editing doesn't change executable for the same input and executed quickly - no need to complicate
if (cscInfoForCacheDigest != null) {
const cscFile = cscInfoForCacheDigest.file;
if (cscFile != null) {
files.push(cscFile);
}
const timer = (0, _timer().time)("executable cache");
const hash = (0, _crypto().createHash)("sha512");
hash.update(config.electronVersion || "no electronVersion");
hash.update(JSON.stringify(this.platformSpecificBuildOptions));
hash.update(JSON.stringify(args));
hash.update(this.platformSpecificBuildOptions.certificateSha1 || "no certificateSha1");
hash.update(this.platformSpecificBuildOptions.certificateSubjectName || "no subjectName");
buildCacheManager = new (_cacheManager().BuildCacheManager)(outDir, file, arch);
if (await buildCacheManager.copyIfValid(await (0, _cacheManager().digest)(hash, files))) {
timer.end();
return;
}
timer.end();
}
const timer = (0, _timer().time)("wine&sign"); // rcedit crashed of executed using wine, resourcehacker works
if (process.platform === "win32" || this.info.framework.name === "electron") {
await (0, _builderUtil().executeAppBuilder)(["rcedit", "--args", JSON.stringify(args)], undefined
/* child-process */
, {}, 3
/* retry five times */
);
}
await this.sign(file);
timer.end();
if (buildCacheManager != null) {
await buildCacheManager.save();
}
}
isSignDlls() {
return this.platformSpecificBuildOptions.signDlls === true;
}
createTransformerForExtraFiles(packContext) {
if (this.platformSpecificBuildOptions.signAndEditExecutable === false) {
return null;
}
return file => {
if (file.endsWith(".exe") || this.isSignDlls() && file.endsWith(".dll")) {
const parentDir = path.dirname(file);
if (parentDir !== packContext.appOutDir) {
return new (_fs().CopyFileTransformer)(file => this.sign(file));
}
}
return null;
};
}
async signApp(packContext, isAsar) {
const exeFileName = `${this.appInfo.productFilename}.exe`;
if (this.platformSpecificBuildOptions.signAndEditExecutable === false) {
return;
}
await _bluebirdLst().default.map((0, _fsExtra().readdir)(packContext.appOutDir), file => {
if (file === exeFileName) {
return this.signAndEditResources(path.join(packContext.appOutDir, exeFileName), packContext.arch, packContext.outDir, path.basename(exeFileName, ".exe"), this.platformSpecificBuildOptions.requestedExecutionLevel);
} else if (file.endsWith(".exe") || this.isSignDlls() && file.endsWith(".dll")) {
return this.sign(path.join(packContext.appOutDir, file));
}
return null;
});
if (!isAsar) {
return;
}
const outResourcesDir = path.join(packContext.appOutDir, "resources", "app.asar.unpacked"); // noinspection JSUnusedLocalSymbols
const fileToSign = await (0, _fs().walk)(outResourcesDir, (file, stat) => stat.isDirectory() || file.endsWith(".exe") || this.isSignDlls() && file.endsWith(".dll"));
await _bluebirdLst().default.map(fileToSign, file => this.sign(file), {
concurrency: 4
});
}
} exports.WinPackager = WinPackager;
// __ts-babel@6.0.4
//# sourceMappingURL=winPackager.js.map