diff --git a/package.json b/package.json index 493df309b..d0630729a 100644 --- a/package.json +++ b/package.json @@ -132,7 +132,6 @@ }, "pnpm": { "onlyBuiltDependencies": [ - "electron", "electron-winstaller", "esbuild", "node-pty" diff --git a/scripts/afterPack.mjs b/scripts/afterPack.mjs index 48719ba9a..19ee452b0 100644 --- a/scripts/afterPack.mjs +++ b/scripts/afterPack.mjs @@ -91,14 +91,55 @@ function unpackedNodeModulesDir(context) { return path.join(resourcesDir, 'app.asar.unpacked', 'node_modules') } +const DARWIN_MAJOR_BY_MACOS_MAJOR = new Map([ + [11, 20], + [12, 21], + [13, 22], + [14, 23], + [15, 24], + [26, 25], + [27, 27] +]) + +export function darwinVersionForMacOS(macOSVersion) { + const darwinMajor = DARWIN_MAJOR_BY_MACOS_MAJOR.get(Number.parseInt(macOSVersion, 10)) + if (darwinMajor === undefined) { + throw new Error( + `afterPack: no Darwin version known for macOS ${macOSVersion}; add it to DARWIN_MAJOR_BY_MACOS_MAJOR` + ) + } + return `${darwinMajor}.0.0` +} + +export function minimumMacOSVersionOfApp(appContentsDir) { + const infoPlistPath = path.join(appContentsDir, 'Info.plist') + const match = /LSMinimumSystemVersion<\/key>\s*([^<]+)<\/string>/.exec( + fs.readFileSync(infoPlistPath, 'utf8') + ) + if (!match) { + throw new Error(`afterPack: LSMinimumSystemVersion not found in ${infoPlistPath}`) + } + return match[1] +} + +function requireTheAppsMinimumMacOSToUpdate(context) { + const { packager } = context + const appContentsDir = path.dirname(packager.getResourcesDir(context.appOutDir)) + const minimumMacOSVersion = minimumMacOSVersionOfApp(appContentsDir) + const minimumSystemVersion = darwinVersionForMacOS(minimumMacOSVersion) + packager.platformSpecificBuildOptions.releaseInfo = { + ...packager.config.releaseInfo, + ...packager.platformSpecificBuildOptions.releaseInfo, + minimumSystemVersion + } + console.log( + `afterPack: updates require macOS ${minimumMacOSVersion} (Darwin ${minimumSystemVersion})` + ) +} + export default async function afterPack(context) { if (context.electronPlatformName === 'darwin') { - const { packager } = context - packager.platformSpecificBuildOptions.releaseInfo = { - ...packager.config.releaseInfo, - ...packager.platformSpecificBuildOptions.releaseInfo, - minimumSystemVersion: '22.0.0' - } + requireTheAppsMinimumMacOSToUpdate(context) } if (context.electronPlatformName === 'win32') { diff --git a/src/main/lib/macUpdateFloor.test.ts b/src/main/lib/macUpdateFloor.test.ts new file mode 100644 index 000000000..0b1ed2f83 --- /dev/null +++ b/src/main/lib/macUpdateFloor.test.ts @@ -0,0 +1,114 @@ +import fs from 'fs' +import os from 'os' +import path from 'path' +import { pathToFileURL } from 'url' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +interface ReleaseInfo { + releaseNotes?: string + minimumSystemVersion?: string +} + +interface PackContext { + electronPlatformName: string + appOutDir: string + packager: { + config: { releaseInfo?: ReleaseInfo } + platformSpecificBuildOptions: { releaseInfo?: ReleaseInfo } + getResourcesDir: (appOutDir: string) => string + } +} + +interface AfterPackModule { + default: (context: PackContext) => Promise +} + +const APP_BUNDLE = 'Comfy Desktop.app' +const afterPackUrl = pathToFileURL(path.join(process.cwd(), 'scripts', 'afterPack.mjs')).href +const packedApps: string[] = [] + +function packedMacApp(minimumMacOS: string | null): string { + const appOutDir = fs.mkdtempSync(path.join(os.tmpdir(), 'packed-mac-app-')) + packedApps.push(appOutDir) + const contentsDir = path.join(appOutDir, APP_BUNDLE, 'Contents') + fs.mkdirSync(contentsDir, { recursive: true }) + const minimumSystemEntry = + minimumMacOS === null + ? '' + : `\tLSMinimumSystemVersion\n\t${minimumMacOS}\n` + fs.writeFileSync( + path.join(contentsDir, 'Info.plist'), + [ + '', + '', + '', + '\tCFBundleName', + '\tComfy Desktop', + `${minimumSystemEntry}`, + '', + '' + ].join('\n') + ) + return appOutDir +} + +function macPackContext(appOutDir: string, releaseInfo?: ReleaseInfo): PackContext { + return { + electronPlatformName: 'darwin', + appOutDir, + packager: { + config: { releaseInfo }, + platformSpecificBuildOptions: {}, + getResourcesDir: (dir) => path.join(dir, APP_BUNDLE, 'Contents', 'Resources') + } + } +} + +async function runAfterPack(context: PackContext): Promise { + const hook = (await import(afterPackUrl)) as AfterPackModule + await hook.default(context) +} + +describe('the macOS update manifest', () => { + beforeEach(() => { + vi.spyOn(console, 'log').mockImplementation(() => {}) + }) + + afterEach(() => { + vi.restoreAllMocks() + for (const dir of packedApps.splice(0)) fs.rmSync(dir, { recursive: true, force: true }) + }) + + it('holds an update back from a Mac older than the packaged app itself supports', async () => { + const context = macPackContext(packedMacApp('13.0'), { releaseNotes: 'What changed' }) + + await runAfterPack(context) + + expect(context.packager.platformSpecificBuildOptions.releaseInfo).toEqual({ + releaseNotes: 'What changed', + minimumSystemVersion: '22.0.0' + }) + }) + + it('follows the app when a later Electron raises the macOS it needs', async () => { + const context = macPackContext(packedMacApp('26.0')) + + await runAfterPack(context) + + expect(context.packager.platformSpecificBuildOptions.releaseInfo?.minimumSystemVersion).toBe( + '25.0.0' + ) + }) + + it('fails the build when the app needs a macOS whose Darwin version is not known', async () => { + const context = macPackContext(packedMacApp('99.0')) + + await expect(runAfterPack(context)).rejects.toThrow('no Darwin version known for macOS 99.0') + }) + + it('fails the build when the app does not say which macOS it needs', async () => { + const context = macPackContext(packedMacApp(null)) + + await expect(runAfterPack(context)).rejects.toThrow('LSMinimumSystemVersion not found') + }) +})