mirror of
https://github.com/debpalash/VoiceStudio.git
synced 2026-12-06 05:16:16 +08:00
fix(electron): harden macOS bundle branding
This commit is contained in:
+35
-11
@@ -3,6 +3,7 @@ import { createRequire } from 'node:module';
|
||||
import {
|
||||
copyFileSync,
|
||||
existsSync,
|
||||
mkdtempSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
@@ -10,7 +11,6 @@ import {
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { basename, dirname, join, resolve } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
@@ -19,16 +19,20 @@ const here = dirname(fileURLToPath(import.meta.url));
|
||||
const electronRoot = resolve(here, '..');
|
||||
const repoRoot = resolve(electronRoot, '..');
|
||||
const require = createRequire(import.meta.url);
|
||||
const defaultCacheRoot = join(electronRoot, 'node_modules', '.cache', 'voicestudio-electron-dev');
|
||||
|
||||
export function createMacDevBundlePlan({
|
||||
electronExecutable,
|
||||
electronVersion,
|
||||
appVersion,
|
||||
architecture = process.arch,
|
||||
cacheRoot = join(tmpdir(), 'voicestudio-electron-dev'),
|
||||
cacheFingerprint = '',
|
||||
cacheRoot = defaultCacheRoot,
|
||||
}) {
|
||||
const sourceBundle = resolve(dirname(electronExecutable), '../..');
|
||||
const cacheKey = `${electronVersion}-${appVersion}-${architecture}`;
|
||||
const cacheKey = [electronVersion, appVersion, architecture, cacheFingerprint]
|
||||
.filter(Boolean)
|
||||
.join('-');
|
||||
const destinationRoot = join(cacheRoot, cacheKey);
|
||||
const destinationBundle = join(destinationRoot, `${APP_NAME}.app`);
|
||||
|
||||
@@ -67,20 +71,23 @@ export function prepareMacDevElectron({
|
||||
electronVersion,
|
||||
appVersion,
|
||||
iconPath,
|
||||
cacheRoot = join(tmpdir(), 'voicestudio-electron-dev'),
|
||||
cacheRoot = defaultCacheRoot,
|
||||
}) {
|
||||
const icon = statSync(iconPath);
|
||||
const cacheFingerprint = `${icon.size}-${String(icon.mtimeMs).replace('.', '_')}`;
|
||||
const plan = createMacDevBundlePlan({
|
||||
electronExecutable,
|
||||
electronVersion,
|
||||
appVersion,
|
||||
cacheFingerprint,
|
||||
cacheRoot,
|
||||
});
|
||||
const expectedManifest = JSON.stringify({
|
||||
electronVersion,
|
||||
appVersion,
|
||||
architecture: process.arch,
|
||||
iconSize: statSync(iconPath).size,
|
||||
iconModified: statSync(iconPath).mtimeMs,
|
||||
iconSize: icon.size,
|
||||
iconModified: icon.mtimeMs,
|
||||
});
|
||||
|
||||
if (
|
||||
@@ -92,7 +99,7 @@ export function prepareMacDevElectron({
|
||||
}
|
||||
|
||||
mkdirSync(cacheRoot, { recursive: true });
|
||||
const stagingRoot = join(cacheRoot, `.staging-${process.pid}`);
|
||||
const stagingRoot = mkdtempSync(join(cacheRoot, '.staging-'));
|
||||
const stagingBundle = join(stagingRoot, `${APP_NAME}.app`);
|
||||
rmSync(stagingRoot, { recursive: true, force: true });
|
||||
mkdirSync(stagingRoot);
|
||||
@@ -110,10 +117,28 @@ export function prepareMacDevElectron({
|
||||
replacePlistValue(infoPlist, 'CFBundleVersion', appVersion);
|
||||
copyFileSync(iconPath, join(stagingBundle, 'Contents', 'Resources', `${APP_NAME}.icns`));
|
||||
run('codesign', ['--force', '--deep', '--sign', '-', stagingBundle]);
|
||||
writeFileSync(join(stagingRoot, 'brand-manifest.json'), expectedManifest);
|
||||
|
||||
rmSync(plan.destinationRoot, { recursive: true, force: true });
|
||||
renameSync(stagingRoot, plan.destinationRoot);
|
||||
writeFileSync(plan.manifest, expectedManifest);
|
||||
try {
|
||||
renameSync(stagingRoot, plan.destinationRoot);
|
||||
} catch (error) {
|
||||
if (
|
||||
!(
|
||||
error instanceof Error &&
|
||||
'code' in error &&
|
||||
(error.code === 'EEXIST' || error.code === 'ENOTEMPTY')
|
||||
)
|
||||
) {
|
||||
throw error;
|
||||
}
|
||||
if (
|
||||
!existsSync(plan.destinationExecutable) ||
|
||||
!existsSync(plan.manifest) ||
|
||||
readFileSync(plan.manifest, 'utf8') !== expectedManifest
|
||||
) {
|
||||
throw new Error('Concurrent macOS development bundle creation produced an invalid cache');
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rmSync(stagingRoot, { recursive: true, force: true });
|
||||
}
|
||||
@@ -138,7 +163,6 @@ function launchElectronVite() {
|
||||
electronVersion,
|
||||
appVersion,
|
||||
iconPath: join(repoRoot, 'frontend', 'src-tauri', 'icons', 'icon.icns'),
|
||||
cacheRoot: join(tmpdir(), 'voicestudio-electron-dev'),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
createMacApplicationMenuTemplate,
|
||||
@@ -20,11 +22,7 @@ describe('installAppIdentity', () => {
|
||||
|
||||
expect(template[0]).toMatchObject({ label: 'VoiceStudio' });
|
||||
expect(template[0]?.submenu).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ role: 'about', label: 'About VoiceStudio' }),
|
||||
expect.objectContaining({ role: 'hide', label: 'Hide VoiceStudio' }),
|
||||
expect.objectContaining({ role: 'quit', label: 'Quit VoiceStudio' }),
|
||||
]),
|
||||
expect.arrayContaining([{ role: 'about' }, { role: 'hide' }, { role: 'quit' }]),
|
||||
);
|
||||
expect(template.slice(1)).toEqual([
|
||||
{ role: 'fileMenu' },
|
||||
@@ -34,6 +32,15 @@ describe('installAppIdentity', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('installs the identity before Electron begins ready initialization', () => {
|
||||
const entrypoint = readFileSync(resolve(process.cwd(), 'src/main/index.ts'), 'utf8');
|
||||
const identityInstall = entrypoint.indexOf('installAppIdentity(app);');
|
||||
const readyInitialization = entrypoint.indexOf('.whenReady()');
|
||||
|
||||
expect(identityInstall).toBeGreaterThanOrEqual(0);
|
||||
expect(readyInitialization).toBeGreaterThan(identityInstall);
|
||||
});
|
||||
|
||||
it('brands the About panel and installs the macOS menu', () => {
|
||||
const application = {
|
||||
setAboutPanelOptions: vi.fn(),
|
||||
|
||||
@@ -12,15 +12,15 @@ export function createMacApplicationMenuTemplate(): MenuItemConstructorOptions[]
|
||||
{
|
||||
label: DESKTOP_APP_NAME,
|
||||
submenu: [
|
||||
{ role: 'about', label: `About ${DESKTOP_APP_NAME}` },
|
||||
{ role: 'about' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'services' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'hide', label: `Hide ${DESKTOP_APP_NAME}` },
|
||||
{ role: 'hide' },
|
||||
{ role: 'hideOthers' },
|
||||
{ role: 'unhide' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'quit', label: `Quit ${DESKTOP_APP_NAME}` },
|
||||
{ role: 'quit' },
|
||||
],
|
||||
},
|
||||
{ role: 'fileMenu' },
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { basename } from 'node:path';
|
||||
import { basename, join, resolve } from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
// This is a Node launcher shared with the package script, so it intentionally
|
||||
// remains plain ESM rather than being compiled into Electron's main process.
|
||||
@@ -12,12 +12,17 @@ describe('macOS development bundle branding', () => {
|
||||
electronVersion: '44.3.0',
|
||||
appVersion: '0.5.6',
|
||||
architecture: 'arm64',
|
||||
cacheFingerprint: '1024-123_5',
|
||||
cacheRoot: '/cache',
|
||||
});
|
||||
|
||||
expect(plan.sourceBundle).toBe('/source/Electron.app');
|
||||
expect(plan.destinationBundle).toBe('/cache/44.3.0-0.5.6-arm64/VoiceStudio.app');
|
||||
expect(plan.sourceBundle).toBe(resolve('/source/Electron.app'));
|
||||
expect(plan.destinationBundle).toBe(
|
||||
join(resolve('/cache'), '44.3.0-0.5.6-arm64-1024-123_5', 'VoiceStudio.app'),
|
||||
);
|
||||
expect(basename(plan.destinationExecutable)).toBe('Electron');
|
||||
expect(plan.destinationExecutable).toContain('/VoiceStudio.app/Contents/MacOS/Electron');
|
||||
expect(plan.destinationExecutable).toContain(
|
||||
join('VoiceStudio.app', 'Contents', 'MacOS', 'Electron'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user