-
-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathbuild-settings.ts
More file actions
109 lines (89 loc) · 3.18 KB
/
build-settings.ts
File metadata and controls
109 lines (89 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import path from 'node:path';
import { XcodePlatform } from '../../../types/common.ts';
import type { CommandExecutor } from '../../../utils/execution/index.ts';
import { expandHomePrefix } from '../../../utils/expand-home.ts';
function resolvePathFromCwd(pathValue?: string): string | undefined {
if (!pathValue) {
return undefined;
}
const expanded = expandHomePrefix(pathValue);
if (path.isAbsolute(expanded)) {
return expanded;
}
return path.resolve(process.cwd(), expanded);
}
export type DevicePlatform = 'iOS' | 'watchOS' | 'tvOS' | 'visionOS';
export function mapDevicePlatform(platform?: DevicePlatform): XcodePlatform {
switch (platform) {
case 'watchOS':
return XcodePlatform.watchOS;
case 'tvOS':
return XcodePlatform.tvOS;
case 'visionOS':
return XcodePlatform.visionOS;
case 'iOS':
case undefined:
default:
return XcodePlatform.iOS;
}
}
export function getBuildSettingsDestination(platform: XcodePlatform, deviceId?: string): string {
if (deviceId) {
return `platform=${platform},id=${deviceId}`;
}
return `generic/platform=${platform}`;
}
export function extractAppPathFromBuildSettingsOutput(buildSettingsOutput: string): string {
const builtProductsDirMatch = buildSettingsOutput.match(/^\s*BUILT_PRODUCTS_DIR\s*=\s*(.+)$/m);
const fullProductNameMatch = buildSettingsOutput.match(/^\s*FULL_PRODUCT_NAME\s*=\s*(.+)$/m);
if (!builtProductsDirMatch || !fullProductNameMatch) {
throw new Error('Could not extract app path from build settings.');
}
return `${builtProductsDirMatch[1].trim()}/${fullProductNameMatch[1].trim()}`;
}
export type ResolveAppPathFromBuildSettingsParams = {
projectPath?: string;
workspacePath?: string;
scheme: string;
configuration?: string;
platform: XcodePlatform;
deviceId?: string;
derivedDataPath?: string;
extraArgs?: string[];
};
export async function resolveAppPathFromBuildSettings(
params: ResolveAppPathFromBuildSettingsParams,
executor: CommandExecutor,
): Promise<string> {
const command = ['xcodebuild', '-showBuildSettings'];
const workspacePath = resolvePathFromCwd(params.workspacePath);
const projectPath = resolvePathFromCwd(params.projectPath);
const derivedDataPath = resolvePathFromCwd(params.derivedDataPath);
let projectDir: string | undefined;
if (projectPath) {
command.push('-project', projectPath);
projectDir = path.dirname(projectPath);
} else if (workspacePath) {
command.push('-workspace', workspacePath);
projectDir = path.dirname(workspacePath);
}
command.push('-scheme', params.scheme);
command.push('-configuration', params.configuration ?? 'Debug');
command.push('-destination', getBuildSettingsDestination(params.platform, params.deviceId));
if (derivedDataPath) {
command.push('-derivedDataPath', derivedDataPath);
}
if (params.extraArgs && params.extraArgs.length > 0) {
command.push(...params.extraArgs);
}
const result = await executor(
command,
'Get App Path',
false,
projectDir ? { cwd: projectDir } : undefined,
);
if (!result.success) {
throw new Error(result.error ?? 'Unknown error');
}
return extractAppPathFromBuildSettingsOutput(result.output);
}