|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | +const crypto = require('node:crypto'); |
| 4 | +const { spawnSync } = require('node:child_process'); |
| 5 | +const { |
| 6 | + findWindowsSdkTool, |
| 7 | + isWindows, |
| 8 | + resolveProjectPath, |
| 9 | + runTool |
| 10 | +} = require('./win-signing-tools.cjs'); |
| 11 | +const { createSigningArgs } = require('./package-win-after-pack.cjs'); |
| 12 | + |
| 13 | +const stageOnly = process.argv.includes('--stage-only'); |
| 14 | +const skipSign = process.argv.includes('--skip-sign'); |
| 15 | +const appProjectPath = resolveProjectPath('src-dotnet', 'SwitchifyPc.App', 'SwitchifyPc.App.csproj'); |
| 16 | +const version = readDotnetAppVersion(appProjectPath); |
| 17 | +const publishDir = resolveProjectPath( |
| 18 | + 'src-dotnet', |
| 19 | + 'SwitchifyPc.App', |
| 20 | + 'bin', |
| 21 | + 'Release', |
| 22 | + 'net8.0-windows10.0.19041.0', |
| 23 | + 'win-x64', |
| 24 | + 'publish' |
| 25 | +); |
| 26 | +const distDir = resolveProjectPath('dist-dotnet'); |
| 27 | +const stageDir = path.join(distDir, 'win-unpacked'); |
| 28 | +const installerName = `Switchify-PC-Setup-${version}-x64.exe`; |
| 29 | +const installerPath = path.join(distDir, installerName); |
| 30 | + |
| 31 | +runDotnetPublish(); |
| 32 | +resetPackageArtifacts(); |
| 33 | +resetDirectory(stageDir); |
| 34 | +copyDirectory(publishDir, stageDir); |
| 35 | +verifyStagedApp(); |
| 36 | +writeBuilderDebug(); |
| 37 | + |
| 38 | +const appExe = path.join(stageDir, 'Switchify PC.exe'); |
| 39 | +if (isWindows() && !skipSign) { |
| 40 | + embedUiAccessManifest(appExe); |
| 41 | + signFile(appExe, { requireSigning: true }); |
| 42 | +} |
| 43 | + |
| 44 | +if (!stageOnly) { |
| 45 | + buildInstaller(); |
| 46 | + if (isWindows() && !skipSign) { |
| 47 | + signFile(installerPath, { requireSigning: true }); |
| 48 | + } |
| 49 | + writeLatestYml(); |
| 50 | +} |
| 51 | + |
| 52 | +console.log(stageOnly ? `Staged C# app in ${stageDir}` : `Packaged C# installer at ${installerPath}`); |
| 53 | + |
| 54 | +function runDotnetPublish() { |
| 55 | + const result = spawnSync( |
| 56 | + 'dotnet', |
| 57 | + [ |
| 58 | + 'publish', |
| 59 | + appProjectPath, |
| 60 | + '-c', |
| 61 | + 'Release', |
| 62 | + '-r', |
| 63 | + 'win-x64', |
| 64 | + '--self-contained', |
| 65 | + 'true' |
| 66 | + ], |
| 67 | + { stdio: 'inherit' } |
| 68 | + ); |
| 69 | + if (result.status !== 0) { |
| 70 | + throw new Error(`dotnet publish failed with exit code ${result.status ?? 'unknown'}.`); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function resetDirectory(directory) { |
| 75 | + fs.rmSync(directory, { recursive: true, force: true }); |
| 76 | + fs.mkdirSync(directory, { recursive: true }); |
| 77 | +} |
| 78 | + |
| 79 | +function resetPackageArtifacts() { |
| 80 | + fs.mkdirSync(distDir, { recursive: true }); |
| 81 | + fs.rmSync(path.join(distDir, 'latest.yml'), { force: true }); |
| 82 | + for (const entry of fs.readdirSync(distDir)) { |
| 83 | + if (/^Switchify-PC-Setup-.+-x64\.exe$/i.test(entry)) { |
| 84 | + fs.rmSync(path.join(distDir, entry), { force: true }); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function copyDirectory(from, to) { |
| 90 | + if (!fs.existsSync(from)) { |
| 91 | + throw new Error(`Publish directory was not found: ${from}`); |
| 92 | + } |
| 93 | + |
| 94 | + for (const entry of fs.readdirSync(from, { withFileTypes: true })) { |
| 95 | + const source = path.join(from, entry.name); |
| 96 | + const target = path.join(to, entry.name); |
| 97 | + if (entry.isDirectory()) { |
| 98 | + fs.mkdirSync(target, { recursive: true }); |
| 99 | + copyDirectory(source, target); |
| 100 | + } else { |
| 101 | + fs.copyFileSync(source, target); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function verifyStagedApp() { |
| 107 | + const executable = path.join(stageDir, 'Switchify PC.exe'); |
| 108 | + if (!fs.existsSync(executable)) { |
| 109 | + throw new Error(`Staged C# app executable is missing: ${executable}`); |
| 110 | + } |
| 111 | + |
| 112 | + const electronMarkers = ['resources', 'chrome_100_percent.pak', 'ffmpeg.dll']; |
| 113 | + for (const marker of electronMarkers) { |
| 114 | + if (fs.existsSync(path.join(stageDir, marker))) { |
| 115 | + throw new Error(`Staged C# app unexpectedly contains Electron marker: ${marker}`); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function embedUiAccessManifest(executablePath) { |
| 121 | + const mtExe = findWindowsSdkTool('mt.exe'); |
| 122 | + const manifestPath = resolveProjectPath('build', 'win-uiaccess.manifest'); |
| 123 | + runTool(mtExe, ['-nologo', '-manifest', manifestPath, `-outputresource:${executablePath};#1`]); |
| 124 | +} |
| 125 | + |
| 126 | +function signFile(filePath, { requireSigning }) { |
| 127 | + const signingArgs = createSigningArgs(filePath, { requireSigning }); |
| 128 | + if (!signingArgs) return; |
| 129 | + |
| 130 | + const signtoolExe = findWindowsSdkTool('signtool.exe'); |
| 131 | + runTool(signtoolExe, signingArgs); |
| 132 | +} |
| 133 | + |
| 134 | +function buildInstaller() { |
| 135 | + const makensis = findMakensis(); |
| 136 | + fs.rmSync(installerPath, { force: true }); |
| 137 | + runTool(makensis, [ |
| 138 | + `/DVERSION=${version}`, |
| 139 | + `/DSOURCE_DIR=${stageDir}`, |
| 140 | + `/DOUTPUT_EXE=${installerPath}`, |
| 141 | + resolveProjectPath('installer', 'SwitchifyPc.DotNet.nsi') |
| 142 | + ]); |
| 143 | + if (!fs.existsSync(installerPath)) { |
| 144 | + throw new Error(`NSIS did not create expected installer: ${installerPath}`); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function findMakensis() { |
| 149 | + const override = process.env.SWITCHIFY_MAKENSIS_EXE; |
| 150 | + if (override) { |
| 151 | + if (!fs.existsSync(override)) { |
| 152 | + throw new Error(`SWITCHIFY_MAKENSIS_EXE does not exist: ${override}`); |
| 153 | + } |
| 154 | + return override; |
| 155 | + } |
| 156 | + |
| 157 | + const candidates = [ |
| 158 | + 'C:\\Program Files (x86)\\NSIS\\makensis.exe', |
| 159 | + 'C:\\Program Files\\NSIS\\makensis.exe' |
| 160 | + ]; |
| 161 | + for (const candidate of candidates) { |
| 162 | + if (fs.existsSync(candidate)) return candidate; |
| 163 | + } |
| 164 | + |
| 165 | + const result = spawnSync('where.exe', ['makensis.exe'], { encoding: 'utf8' }); |
| 166 | + if (result.status === 0) { |
| 167 | + const [first] = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean); |
| 168 | + if (first) return first; |
| 169 | + } |
| 170 | + |
| 171 | + throw new Error('Unable to find makensis.exe. Install NSIS or set SWITCHIFY_MAKENSIS_EXE.'); |
| 172 | +} |
| 173 | + |
| 174 | +function writeLatestYml() { |
| 175 | + const sha512 = createSha512Base64(installerPath); |
| 176 | + const size = fs.statSync(installerPath).size; |
| 177 | + const releaseDate = new Date().toISOString(); |
| 178 | + const latest = [ |
| 179 | + `version: ${version}`, |
| 180 | + 'files:', |
| 181 | + ` - url: ${installerName}`, |
| 182 | + ` sha512: ${sha512}`, |
| 183 | + ` size: ${size}`, |
| 184 | + ' isAdminRightsRequired: true', |
| 185 | + `path: ${installerName}`, |
| 186 | + `sha512: ${sha512}`, |
| 187 | + 'isAdminRightsRequired: true', |
| 188 | + `releaseDate: '${releaseDate}'`, |
| 189 | + '' |
| 190 | + ].join('\n'); |
| 191 | + |
| 192 | + fs.writeFileSync(path.join(distDir, 'latest.yml'), latest); |
| 193 | +} |
| 194 | + |
| 195 | +function writeBuilderDebug() { |
| 196 | + const content = [ |
| 197 | + `version: ${version}`, |
| 198 | + 'packager: dotnet-wpf-nsis', |
| 199 | + `stageDir: ${toYamlPath(stageDir)}`, |
| 200 | + `publishDir: ${toYamlPath(publishDir)}`, |
| 201 | + '' |
| 202 | + ].join('\n'); |
| 203 | + fs.writeFileSync(path.join(distDir, 'builder-debug.yml'), content); |
| 204 | +} |
| 205 | + |
| 206 | +function createSha512Base64(filePath) { |
| 207 | + return crypto.createHash('sha512').update(fs.readFileSync(filePath)).digest('base64'); |
| 208 | +} |
| 209 | + |
| 210 | +function toYamlPath(value) { |
| 211 | + return JSON.stringify(value.replace(/\\/g, '/')); |
| 212 | +} |
| 213 | + |
| 214 | +function readDotnetAppVersion(projectPath) { |
| 215 | + const content = fs.readFileSync(projectPath, 'utf8'); |
| 216 | + const match = content.match(/<Version>([^<]+)<\/Version>/); |
| 217 | + if (!match || !match[1].trim()) { |
| 218 | + throw new Error(`Could not read C# app <Version> from ${projectPath}.`); |
| 219 | + } |
| 220 | + |
| 221 | + return match[1].trim(); |
| 222 | +} |
0 commit comments