-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.js
91 lines (83 loc) · 2.71 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
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const fsExtra = require('fs-extra');
const debugBuild = false;
class MissingDotNetSDKException extends Error {
constructor() {
super('Missing .NET SDK - Install a .NET SDK from https://dotnet.microsoft.com/en-us/download/dotnet/6.0');
this.name = 'MissingDotNetSDKException';
}
}
const ERROR_CODE_HANDLER = {
2147516561: {
genError: () => new MissingDotNetSDKException(),
},
}
function spawnAsync(exe, args, options, out) {
if (options === undefined) {
options = {};
}
if (out === undefined) {
out = console;
}
return new Promise((resolve, reject) => {
let desc = `${options.cwd || '.'}/${exe} ${args.join(' ')}`;
out.log('started: ' + desc);
const outBufs = [];
try {
let proc = cp.spawn(exe, args, options);
proc.stdout.on('data', (data) => outBufs.push(data));
proc.stderr.on('data', (data) => out.error(data.toString()));
proc.on('error', (err) => {
out.log(Buffer.concat(outBufs).toString());
reject(err);
});
proc.on('close', (code) => {
out.log('done: ' + desc + ': ' + code);
if (code === 0) {
resolve();
} else {
out.log(Buffer.concat(outBufs).toString());
reject(new Error(`${desc} failed with code ${code}`));
}
});
} catch (err) {
out.error(`failed to spawn ${desc}: ${err.message}`);
reject(err);
}
});
}
async function dotnet(args) {
return spawnAsync('dotnet', args, {}, console);
}
function sign(filePath) {
if (process.env['SIGN_TOOL'] !== undefined) {
return spawnAsync(process.env['SIGN_TOOL'], ['sign', '/sha1', process.env['SIGN_THUMBPRINT'], '/td', 'sha256', '/fd', 'sha256', '/tr', 'http://timestamp.comodoca.com', filePath]);
}
}
async function main() {
try {
const buildType = debugBuild ? 'Debug' : 'Release';
await fsExtra.remove(path.join(__dirname, 'dist'));
const args = ['publish', 'ModInstallerIPC', '-c', buildType, '--no-self-contained', '-r', 'win-x64', '-o', 'dist'];
if (!debugBuild) {
args.push('/p:DebugType=None', '/p:DebugSymbols=false');
}
try {
await dotnet(args);
} catch (err) {
// the build may fail because of locked files (sigh) so just try again...
await new Promise((resolve) => setTimeout(resolve, 500));
await dotnet(args);
}
await sign('dist\\ModInstallerIPC.exe');
} catch (err) {
const error = ERROR_CODE_HANDLER[err?.code] !== undefined
? ERROR_CODE_HANDLER[err.code].genError() : err;
console.error('build failed:', error.message);
const exitCode = !err.code ? -1 : err.code;
process.exit(exitCode);
}
}
main();