Skip to content

Commit a97f794

Browse files
authored
refactor(apple): resolve cleanup platform metadata once (#4393)
1 parent 2180602 commit a97f794

1 file changed

Lines changed: 11 additions & 30 deletions

File tree

‎scripts/clean-xcuitest-derived.mjs‎

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,31 @@ import {
1212

1313
const DERIVED_OVERRIDE = resolveRunnerDerivedOverride(process.env);
1414
const platforms = process.argv.slice(2);
15-
const requested = platforms.length > 0 ? platforms : ['ios', 'macos', 'tvos', 'visionos'];
16-
const supported = new Set(['ios', 'macos', 'tvos', 'visionos']);
17-
// An override pins one directory for every platform, as it does for the runner build.
18-
const DERIVED_PATHS = new Map(
19-
[...supported].map((platform) => [
20-
platform,
21-
DERIVED_OVERRIDE ?? resolveRunnerScriptDefaultDerivedPath(platform, os.homedir()),
22-
]),
23-
);
2415
const PLATFORM_LABELS = new Map([
2516
['ios', 'iOS'],
2617
['macos', 'macOS'],
2718
['tvos', 'tvOS'],
2819
['visionos', 'visionOS'],
2920
]);
21+
const requested = platforms.length > 0 ? platforms : [...PLATFORM_LABELS.keys()];
3022
const MAX_SUMMARY_ENTRY_NAMES = 3;
3123

3224
for (const platform of requested) {
33-
if (!supported.has(platform)) {
25+
const platformLabel = PLATFORM_LABELS.get(platform);
26+
if (!platformLabel) {
3427
console.error(`Unsupported XCTest cache platform: ${platform}`);
35-
console.error('Supported platforms: ios, macos, tvos, visionos');
28+
console.error(`Supported platforms: ${[...PLATFORM_LABELS.keys()].join(', ')}`);
3629
process.exitCode = 1;
3730
continue;
3831
}
39-
const targetPath = resolveDerivedPath(platform);
32+
const targetPath =
33+
DERIVED_OVERRIDE ?? resolveRunnerScriptDefaultDerivedPath(platform, os.homedir());
4034
try {
4135
assertSafeDerivedCleanup(targetPath, process.env);
4236
const result = cleanDerivedPath(platform, targetPath);
43-
console.log(formatCleanupResult(platform, targetPath, result));
37+
console.log(formatCleanupResult(platform, platformLabel, targetPath, result));
4438
} catch (error) {
45-
console.error(formatCleanupError(platform, targetPath, error));
39+
console.error(formatCleanupError(platformLabel, targetPath, error));
4640
process.exitCode = 1;
4741
}
4842
}
@@ -80,8 +74,7 @@ function cleanDerivedPath(platform, targetPath) {
8074
};
8175
}
8276

83-
function formatCleanupResult(platform, targetPath, result) {
84-
const platformLabel = resolvePlatformLabel(platform);
77+
function formatCleanupResult(platform, platformLabel, targetPath, result) {
8578
if (wholeDirectoryClean(platform))
8679
return formatWholeDerivedCleanupResult(platformLabel, targetPath, result);
8780
return formatIosCleanupResult(platformLabel, targetPath, result);
@@ -114,16 +107,10 @@ function formatKeptEntriesSuffix(preservedEntries) {
114107
return preservedEntries.length > 0 ? `; kept ${summarizeEntryNames(preservedEntries)}` : '';
115108
}
116109

117-
function formatCleanupError(platform, targetPath, error) {
110+
function formatCleanupError(platformLabel, targetPath, error) {
118111
const detail = error instanceof Error ? error.message : String(error);
119112
const hint = error instanceof AppError ? error.details?.hint : undefined;
120-
return `Failed to clean ${resolvePlatformLabel(platform)} XCTest derived data under ${targetPath}: ${detail}${typeof hint === 'string' ? `\n${hint}` : ''}`;
121-
}
122-
123-
function resolvePlatformLabel(platform) {
124-
const platformLabel = PLATFORM_LABELS.get(platform);
125-
if (platformLabel) return platformLabel;
126-
throw new Error(`Unsupported platform: ${platform}`);
113+
return `Failed to clean ${platformLabel} XCTest derived data under ${targetPath}: ${detail}${typeof hint === 'string' ? `\n${hint}` : ''}`;
127114
}
128115

129116
function summarizeEntryNames(entryNames) {
@@ -133,9 +120,3 @@ function summarizeEntryNames(entryNames) {
133120
}
134121
return `${names.slice(0, MAX_SUMMARY_ENTRY_NAMES).join(', ')} (+${names.length - MAX_SUMMARY_ENTRY_NAMES} more)`;
135122
}
136-
137-
function resolveDerivedPath(platform) {
138-
const targetPath = DERIVED_PATHS.get(platform);
139-
if (targetPath) return targetPath;
140-
throw new Error(`Unsupported platform: ${platform}`);
141-
}

0 commit comments

Comments
 (0)