-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage-dotnet-win.cjs
More file actions
222 lines (195 loc) · 6.38 KB
/
Copy pathpackage-dotnet-win.cjs
File metadata and controls
222 lines (195 loc) · 6.38 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
const fs = require('node:fs');
const path = require('node:path');
const crypto = require('node:crypto');
const { spawnSync } = require('node:child_process');
const {
findWindowsSdkTool,
isWindows,
resolveProjectPath,
runTool
} = require('./win-signing-tools.cjs');
const { createSigningArgs } = require('./package-win-after-pack.cjs');
const stageOnly = process.argv.includes('--stage-only');
const skipSign = process.argv.includes('--skip-sign');
const appProjectPath = resolveProjectPath('src-dotnet', 'SwitchifyPc.App', 'SwitchifyPc.App.csproj');
const version = readDotnetAppVersion(appProjectPath);
const publishDir = resolveProjectPath(
'src-dotnet',
'SwitchifyPc.App',
'bin',
'Release',
'net8.0-windows10.0.19041.0',
'win-x64',
'publish'
);
const distDir = resolveProjectPath('dist-dotnet');
const stageDir = path.join(distDir, 'win-unpacked');
const installerName = `Switchify-PC-Setup-${version}-x64.exe`;
const installerPath = path.join(distDir, installerName);
runDotnetPublish();
resetPackageArtifacts();
resetDirectory(stageDir);
copyDirectory(publishDir, stageDir);
verifyStagedApp();
writeBuilderDebug();
const appExe = path.join(stageDir, 'Switchify PC.exe');
if (isWindows() && !skipSign) {
embedUiAccessManifest(appExe);
signFile(appExe, { requireSigning: true });
}
if (!stageOnly) {
buildInstaller();
if (isWindows() && !skipSign) {
signFile(installerPath, { requireSigning: true });
}
writeLatestYml();
}
console.log(stageOnly ? `Staged C# app in ${stageDir}` : `Packaged C# installer at ${installerPath}`);
function runDotnetPublish() {
const result = spawnSync(
'dotnet',
[
'publish',
appProjectPath,
'-c',
'Release',
'-r',
'win-x64',
'--self-contained',
'true'
],
{ stdio: 'inherit' }
);
if (result.status !== 0) {
throw new Error(`dotnet publish failed with exit code ${result.status ?? 'unknown'}.`);
}
}
function resetDirectory(directory) {
fs.rmSync(directory, { recursive: true, force: true });
fs.mkdirSync(directory, { recursive: true });
}
function resetPackageArtifacts() {
fs.mkdirSync(distDir, { recursive: true });
fs.rmSync(path.join(distDir, 'latest.yml'), { force: true });
for (const entry of fs.readdirSync(distDir)) {
if (/^Switchify-PC-Setup-.+-x64\.exe$/i.test(entry)) {
fs.rmSync(path.join(distDir, entry), { force: true });
}
}
}
function copyDirectory(from, to) {
if (!fs.existsSync(from)) {
throw new Error(`Publish directory was not found: ${from}`);
}
for (const entry of fs.readdirSync(from, { withFileTypes: true })) {
const source = path.join(from, entry.name);
const target = path.join(to, entry.name);
if (entry.isDirectory()) {
fs.mkdirSync(target, { recursive: true });
copyDirectory(source, target);
} else {
fs.copyFileSync(source, target);
}
}
}
function verifyStagedApp() {
const executable = path.join(stageDir, 'Switchify PC.exe');
if (!fs.existsSync(executable)) {
throw new Error(`Staged C# app executable is missing: ${executable}`);
}
const electronMarkers = ['resources', 'chrome_100_percent.pak', 'ffmpeg.dll'];
for (const marker of electronMarkers) {
if (fs.existsSync(path.join(stageDir, marker))) {
throw new Error(`Staged C# app unexpectedly contains Electron marker: ${marker}`);
}
}
}
function embedUiAccessManifest(executablePath) {
const mtExe = findWindowsSdkTool('mt.exe');
const manifestPath = resolveProjectPath('build', 'win-uiaccess.manifest');
runTool(mtExe, ['-nologo', '-manifest', manifestPath, `-outputresource:${executablePath};#1`]);
}
function signFile(filePath, { requireSigning }) {
const signingArgs = createSigningArgs(filePath, { requireSigning });
if (!signingArgs) return;
const signtoolExe = findWindowsSdkTool('signtool.exe');
runTool(signtoolExe, signingArgs);
}
function buildInstaller() {
const makensis = findMakensis();
fs.rmSync(installerPath, { force: true });
runTool(makensis, [
`/DVERSION=${version}`,
`/DSOURCE_DIR=${stageDir}`,
`/DOUTPUT_EXE=${installerPath}`,
resolveProjectPath('installer', 'SwitchifyPc.DotNet.nsi')
]);
if (!fs.existsSync(installerPath)) {
throw new Error(`NSIS did not create expected installer: ${installerPath}`);
}
}
function findMakensis() {
const override = process.env.SWITCHIFY_MAKENSIS_EXE;
if (override) {
if (!fs.existsSync(override)) {
throw new Error(`SWITCHIFY_MAKENSIS_EXE does not exist: ${override}`);
}
return override;
}
const candidates = [
'C:\\Program Files (x86)\\NSIS\\makensis.exe',
'C:\\Program Files\\NSIS\\makensis.exe'
];
for (const candidate of candidates) {
if (fs.existsSync(candidate)) return candidate;
}
const result = spawnSync('where.exe', ['makensis.exe'], { encoding: 'utf8' });
if (result.status === 0) {
const [first] = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
if (first) return first;
}
throw new Error('Unable to find makensis.exe. Install NSIS or set SWITCHIFY_MAKENSIS_EXE.');
}
function writeLatestYml() {
const sha512 = createSha512Base64(installerPath);
const size = fs.statSync(installerPath).size;
const releaseDate = new Date().toISOString();
const latest = [
`version: ${version}`,
'files:',
` - url: ${installerName}`,
` sha512: ${sha512}`,
` size: ${size}`,
' isAdminRightsRequired: true',
`path: ${installerName}`,
`sha512: ${sha512}`,
'isAdminRightsRequired: true',
`releaseDate: '${releaseDate}'`,
''
].join('\n');
fs.writeFileSync(path.join(distDir, 'latest.yml'), latest);
}
function writeBuilderDebug() {
const content = [
`version: ${version}`,
'packager: dotnet-wpf-nsis',
`stageDir: ${toYamlPath(stageDir)}`,
`publishDir: ${toYamlPath(publishDir)}`,
''
].join('\n');
fs.writeFileSync(path.join(distDir, 'builder-debug.yml'), content);
}
function createSha512Base64(filePath) {
return crypto.createHash('sha512').update(fs.readFileSync(filePath)).digest('base64');
}
function toYamlPath(value) {
return JSON.stringify(value.replace(/\\/g, '/'));
}
function readDotnetAppVersion(projectPath) {
const content = fs.readFileSync(projectPath, 'utf8');
const match = content.match(/<Version>([^<]+)<\/Version>/);
if (!match || !match[1].trim()) {
throw new Error(`Could not read C# app <Version> from ${projectPath}.`);
}
return match[1].trim();
}