Skip to content

Commit 41eec7f

Browse files
committed
chore: improve dmg cleanup diagnostics and regex maintainability
1 parent 3aaf469 commit 41eec7f

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

scripts/ci/cleanup-dmg.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ canonicalize_path() {
176176
workspace_root_canonical="$(canonicalize_path "${workspace_root}")"
177177
workspace_root_canonical="${workspace_root_canonical%/}"
178178

179+
log_cleanup_configuration() {
180+
echo "DMG cleanup configuration:" >&2
181+
echo " workspace_root=${workspace_root}" >&2
182+
echo " workspace_root_canonical=${workspace_root_canonical}" >&2
183+
echo " strict_workspace_root=${strict_workspace_root}" >&2
184+
echo " canonicalize_tool=${canonicalize_tool}" >&2
185+
echo " detach_attempts=${detach_attempts}" >&2
186+
echo " detach_sleep_seconds=${detach_sleep_seconds}" >&2
187+
echo " rw_dmg_image_prefix=${rw_dmg_image_prefix}" >&2
188+
echo " rw_dmg_image_suffix_regex=${rw_dmg_image_suffix_regex}" >&2
189+
echo " rw_dmg_mountpoint_regex=${rw_dmg_mountpoint_regex}" >&2
190+
echo " allow_global_helper_cleanup=${allow_global_helper_cleanup}" >&2
191+
}
192+
193+
log_cleanup_configuration
194+
179195
is_workspace_rw_dmg_image() {
180196
local image="$1"
181197
local normalized_image

scripts/prepare-resources.mjs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,28 @@ const patchMonacoCssNestingWarnings = async (dashboardDir) => {
209209
}
210210
};
211211

212+
const LEGACY_DESKTOP_BRIDGE_PATTERNS = {
213+
trayRestartGuard:
214+
/if\s*\(\s*!desktopBridge\?\.isElectron\s*\|\|\s*!desktopBridge\.onTrayRestartBackend\s*\)\s*\{/,
215+
typeIsElectron: /^\s+isElectron:\s*boolean;\r?\n/m,
216+
typeIsElectronRuntime: /^\s+isElectronRuntime:\s*\(\)\s*=>\s*Promise<boolean>;\r?\n/m,
217+
electronAppFlagToken: /\bisElectronApp\b/,
218+
electronAppFlagReplace: /\bisElectronApp\b/g,
219+
desktopReleaseEnvGuard:
220+
/typeof\s+window\s*!==\s*'undefined'\s*&&\s*!!window\.astrbotDesktop\?\.isElectron/,
221+
desktopReleaseRuntimeGuard:
222+
/isDesktopReleaseMode\.value\s*=\s*!!window\.astrbotDesktop\?\.isElectron\s*\|\|\s*\r?\n\s*!!\(\s*await\s+window\.astrbotDesktop\?\.isElectronRuntime\?\.\(\)\s*\)\s*;/,
223+
legacyRuntimeUsage: /window\.astrbotDesktop\?\.isElectronRuntime\?\.\(\)/,
224+
restartGuard: /if\s*\(\s*desktopBridge\?\.isElectron\s*\)\s*\{/,
225+
};
226+
227+
const MODERN_DESKTOP_BRIDGE_PATTERNS = {
228+
trayRestartGuard: /if\s*\(\s*!desktopBridge\?\.onTrayRestartBackend\s*\)\s*\{/,
229+
desktopBridgeTypeIsDesktop: /^\s+isDesktop:\s*boolean;\r?\n/m,
230+
desktopBridgeTypeRuntime: /^\s+isDesktopRuntime:\s*\(\)\s*=>\s*Promise<boolean>;\r?\n/m,
231+
restartCapabilityGuard: /const hasDesktopRestartCapability\s*=/,
232+
};
233+
212234
const patchRequiredLegacyFile = async ({ filePath, transform, patchLabel, isAlreadyModern }) => {
213235
if (!existsSync(filePath)) {
214236
throw new Error(
@@ -247,22 +269,22 @@ const patchRequiredLegacyFile = async ({ filePath, transform, patchLabel, isAlre
247269

248270
const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
249271
const hasModernTrayRestartGuard = (source) =>
250-
/if\s*\(\s*!desktopBridge\?\.onTrayRestartBackend\s*\)\s*\{/.test(source);
272+
MODERN_DESKTOP_BRIDGE_PATTERNS.trayRestartGuard.test(source);
251273
const hasModernDesktopBridgeTypes = (source) =>
252-
/^\s+isDesktop:\s*boolean;\n/m.test(source) &&
253-
/^\s+isDesktopRuntime:\s*\(\)\s*=>\s*Promise<boolean>;\n/m.test(source);
274+
MODERN_DESKTOP_BRIDGE_PATTERNS.desktopBridgeTypeIsDesktop.test(source) &&
275+
MODERN_DESKTOP_BRIDGE_PATTERNS.desktopBridgeTypeRuntime.test(source);
254276
const hasLegacyDesktopReleaseGuards = (source) =>
255-
/\bisElectronApp\b/.test(source) ||
256-
/window\.astrbotDesktop\?\.isElectron/.test(source) ||
257-
/window\.astrbotDesktop\?\.isElectronRuntime\?\.\(\)/.test(source);
277+
LEGACY_DESKTOP_BRIDGE_PATTERNS.electronAppFlagToken.test(source) ||
278+
LEGACY_DESKTOP_BRIDGE_PATTERNS.desktopReleaseEnvGuard.test(source) ||
279+
LEGACY_DESKTOP_BRIDGE_PATTERNS.legacyRuntimeUsage.test(source);
258280
const hasModernRestartCapabilityGuard = (source) =>
259-
source.includes('const hasDesktopRestartCapability =');
281+
MODERN_DESKTOP_BRIDGE_PATTERNS.restartCapabilityGuard.test(source);
260282

261283
await patchRequiredLegacyFile({
262284
filePath: path.join(dashboardDir, 'src', 'App.vue'),
263285
transform: (source) =>
264286
source.replace(
265-
/if\s*\(\s*!desktopBridge\?\.isElectron\s*\|\|\s*!desktopBridge\.onTrayRestartBackend\s*\)\s*\{/,
287+
LEGACY_DESKTOP_BRIDGE_PATTERNS.trayRestartGuard,
266288
'if (!desktopBridge?.onTrayRestartBackend) {',
267289
),
268290
patchLabel: 'tray restart desktop guard',
@@ -273,9 +295,12 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
273295
filePath: path.join(dashboardDir, 'src', 'types', 'electron-bridge.d.ts'),
274296
transform: (source) => {
275297
let patched = source;
276-
patched = patched.replace(/^\s+isElectron:\s*boolean;\n/m, ' isDesktop: boolean;\n');
277298
patched = patched.replace(
278-
/^\s+isElectronRuntime:\s*\(\)\s*=>\s*Promise<boolean>;\n/m,
299+
LEGACY_DESKTOP_BRIDGE_PATTERNS.typeIsElectron,
300+
' isDesktop: boolean;\n',
301+
);
302+
patched = patched.replace(
303+
LEGACY_DESKTOP_BRIDGE_PATTERNS.typeIsElectronRuntime,
279304
' isDesktopRuntime: () => Promise<boolean>;\n',
280305
);
281306
return patched;
@@ -287,13 +312,16 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
287312
await patchRequiredLegacyFile({
288313
filePath: path.join(dashboardDir, 'src', 'layouts', 'full', 'vertical-header', 'VerticalHeader.vue'),
289314
transform: (source) => {
290-
let patched = source.replaceAll(/\bisElectronApp\b/g, 'isDesktopReleaseMode');
315+
let patched = source.replaceAll(
316+
LEGACY_DESKTOP_BRIDGE_PATTERNS.electronAppFlagReplace,
317+
'isDesktopReleaseMode',
318+
);
291319
patched = patched.replace(
292-
/typeof window !== 'undefined'\s*&&\s*!!window\.astrbotDesktop\?\.isElectron/,
320+
LEGACY_DESKTOP_BRIDGE_PATTERNS.desktopReleaseEnvGuard,
293321
'false',
294322
);
295323
patched = patched.replace(
296-
/isDesktopReleaseMode\.value\s*=\s*!!window\.astrbotDesktop\?\.isElectron\s*\|\|\s*\n\s*!!\(await window\.astrbotDesktop\?\.isElectronRuntime\?\.\(\)\);/,
324+
LEGACY_DESKTOP_BRIDGE_PATTERNS.desktopReleaseRuntimeGuard,
297325
'isDesktopReleaseMode.value = false;',
298326
);
299327
return patched;
@@ -305,11 +333,11 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
305333
await patchRequiredLegacyFile({
306334
filePath: path.join(dashboardDir, 'src', 'utils', 'restartAstrBot.ts'),
307335
transform: (source) => {
308-
if (source.includes('const hasDesktopRestartCapability =')) {
336+
if (MODERN_DESKTOP_BRIDGE_PATTERNS.restartCapabilityGuard.test(source)) {
309337
return source;
310338
}
311339
return source.replace(
312-
/if\s*\(\s*desktopBridge\?\.isElectron\s*\)\s*\{/,
340+
LEGACY_DESKTOP_BRIDGE_PATTERNS.restartGuard,
313341
`const hasDesktopRestartCapability =
314342
!!desktopBridge &&
315343
typeof desktopBridge.restartBackend === 'function' &&

0 commit comments

Comments
 (0)