-
Notifications
You must be signed in to change notification settings - Fork 935
Expand file tree
/
Copy pathbuildProject.ts
More file actions
224 lines (204 loc) · 5.83 KB
/
buildProject.ts
File metadata and controls
224 lines (204 loc) · 5.83 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import child_process, {
ChildProcess,
SpawnOptionsWithoutStdio,
} from 'child_process';
import pico from 'picocolors';
import {IOSProjectInfo} from '@react-native-community/cli-types';
import {
logger,
CLIError,
printRunDoctorTip,
getLoader,
} from '@react-native-community/cli-tools';
import type {BuildFlags} from './buildOptions';
import {simulatorDestinationMap} from './simulatorDestinationMap';
import {supportedPlatforms} from '@react-native-community/cli-config-apple';
import {ApplePlatform} from '../../types';
function prettifyXcodebuildMessages(output: string): Set<string> {
const errorRegex = /error\b[^\S\r\n]*[:\-\s]*([^\r\n]*)/gim;
const errors = new Set<string>();
let match;
while ((match = errorRegex.exec(output)) !== null) {
if (match[1]) {
// match[1] contains the captured group that excludes any leading colons or spaces
errors.add(match[1].trim());
}
}
return errors;
}
export function buildProject(
xcodeProject: IOSProjectInfo,
platform: ApplePlatform,
udid: string | undefined,
mode: string,
scheme: string,
args: BuildFlags,
): Promise<string> {
return new Promise((resolve, reject) => {
const simulatorDest = simulatorDestinationMap?.[platform];
if (!simulatorDest) {
reject(
new CLIError(
`Unknown platform: ${platform}. Please, use one of: ${Object.values(
supportedPlatforms,
).join(', ')}.`,
),
);
return;
}
const isDevice = args.device;
let destination = '';
if (udid) {
destination = `id=${udid}`;
} else if (isDevice) {
destination = 'generic/platform=iOS';
} else if (mode === 'Debug') {
destination = `generic/platform=${simulatorDest}`;
} else {
destination = `generic/platform=${platform}`;
}
if (args.destination) {
destination += `,${args.destination}`;
}
const xcodebuildArgs = [
xcodeProject.isWorkspace ? '-workspace' : '-project',
xcodeProject.name,
...(args.xcconfig ? ['-xcconfig', args.xcconfig] : []),
...(args.buildFolder ? ['-derivedDataPath', args.buildFolder] : []),
'-configuration',
mode,
'-scheme',
scheme,
'-destination',
destination,
];
if (args.extraParams) {
xcodebuildArgs.push(...args.extraParams);
}
const loader = getLoader();
logger.info(
`Building ${pico.dim(
`(using "xcodebuild ${xcodebuildArgs.join(' ')}")`,
)}`,
);
let xcodebuildOutputFormatter: ChildProcess | any;
if (!args.verbose) {
if (xcbeautifyAvailable()) {
xcodebuildOutputFormatter = child_process.spawn('xcbeautify', [], {
stdio: ['pipe', process.stdout, process.stderr],
});
} else if (xcprettyAvailable()) {
xcodebuildOutputFormatter = child_process.spawn('xcpretty', [], {
stdio: ['pipe', process.stdout, process.stderr],
});
}
}
const buildProcess = child_process.spawn(
'xcodebuild',
xcodebuildArgs,
getProcessOptions(args),
);
let buildOutput = '';
let buildResolved = false;
buildProcess.stdout.on('data', (data: Buffer) => {
const stringData = data.toString();
buildOutput += stringData;
if (xcodebuildOutputFormatter) {
xcodebuildOutputFormatter.stdin.write(data);
} else {
if (logger.isVerbose()) {
logger.debug(stringData);
} else {
loader.start(
`Building the app${'.'.repeat(buildOutput.length % 10)}`,
);
}
}
// Workaround: xcodebuild on Xcode 26.2+ may hang after build succeeds.
// Detect BUILD SUCCEEDED and resolve immediately instead of waiting for close.
if (!buildResolved && stringData.includes('BUILD SUCCEEDED')) {
buildResolved = true;
if (xcodebuildOutputFormatter) {
xcodebuildOutputFormatter.stdin.end();
} else {
loader.stop();
}
logger.success('Successfully built the app');
buildProcess.kill();
resolve(buildOutput);
}
});
buildProcess.on('close', (code: number) => {
if (buildResolved) return;
if (xcodebuildOutputFormatter) {
xcodebuildOutputFormatter.stdin.end();
} else {
loader.stop();
}
if (code !== 0) {
printRunDoctorTip();
if (!xcodebuildOutputFormatter) {
Array.from(prettifyXcodebuildMessages(buildOutput)).forEach((error) =>
logger.error(error),
);
}
reject(
new CLIError(`
Failed to build ${platform} project.
"xcodebuild" exited with error code '${code}'. To debug build
logs further, consider building your app with Xcode.app, by opening
'${xcodeProject.name}'.`),
);
return;
}
logger.success('Successfully built the app');
resolve(buildOutput);
});
});
}
function xcbeautifyAvailable() {
try {
child_process.execSync('xcbeautify --version', {
stdio: [0, 'pipe', 'ignore'],
});
} catch (error) {
return false;
}
return true;
}
function xcprettyAvailable() {
try {
child_process.execSync('xcpretty --version', {
stdio: [0, 'pipe', 'ignore'],
});
} catch (error) {
return false;
}
return true;
}
function getProcessOptions<T extends BuildFlags>(
args: T,
): SpawnOptionsWithoutStdio {
if (
'packager' in args &&
typeof args.packager === 'boolean' &&
args.packager
) {
const terminal =
'terminal' in args && typeof args.terminal === 'string'
? args.terminal
: '';
const port =
'port' in args && typeof args.port === 'number' ? String(args.port) : '';
return {
env: {
...process.env,
RCT_TERMINAL: terminal,
RCT_METRO_PORT: port,
},
};
}
return {
env: process.env,
};
}