-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add buildIOS command * refactor: extract shared functions from build-ios * refactor: extract shared functions from run-ios * feat: default build ios to generic destination * feat: update build-ios examples * docs: update build-ios docs * refactor: use shared build flags * feat: rename configuratio to mode * feat: add deprecation note * fix: ts issue * docs: reword xcconfig argument for build ios * refactor: improvements after code review * fix: pass mode to iOS install command Co-authored-by: Paweł Kata <gmabber@gmail.com> Co-authored-by: Adam Trzcinski <trzcinski.adam@gmail.com>
- Loading branch information
1 parent
ee15ba3
commit e932dd6
Showing
15 changed files
with
814 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/cli-platform-ios/src/commands/buildIOS/buildProject.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import child_process, { | ||
ChildProcess, | ||
SpawnOptionsWithoutStdio, | ||
} from 'child_process'; | ||
import chalk from 'chalk'; | ||
import {IOSProjectInfo} from '@react-native-community/cli-types'; | ||
import {logger, CLIError} from '@react-native-community/cli-tools'; | ||
import {getLoader} from '@react-native-community/cli-tools'; | ||
|
||
export type BuildFlags = { | ||
mode: string; | ||
packager: boolean; | ||
verbose: boolean; | ||
xcconfig?: string; | ||
buildFolder?: string; | ||
port: number; | ||
terminal: string | undefined; | ||
}; | ||
|
||
export function buildProject( | ||
xcodeProject: IOSProjectInfo, | ||
udid: string | undefined, | ||
scheme: string, | ||
args: BuildFlags, | ||
): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const xcodebuildArgs = [ | ||
xcodeProject.isWorkspace ? '-workspace' : '-project', | ||
xcodeProject.name, | ||
...(args.xcconfig ? ['-xcconfig', args.xcconfig] : []), | ||
...(args.buildFolder ? ['-derivedDataPath', args.buildFolder] : []), | ||
'-configuration', | ||
args.mode, | ||
'-scheme', | ||
scheme, | ||
'-destination', | ||
udid | ||
? `id=${udid}` | ||
: args.mode === 'Debug' | ||
? 'generic/platform=iOS Simulator' | ||
: 'generic/platform=iOS', | ||
]; | ||
const loader = getLoader(); | ||
logger.info( | ||
`Building ${chalk.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 errorOutput = ''; | ||
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)}`, | ||
); | ||
} | ||
} | ||
}); | ||
|
||
buildProcess.stderr.on('data', (data: Buffer) => { | ||
errorOutput += data; | ||
}); | ||
buildProcess.on('close', (code: number) => { | ||
if (xcodebuildOutputFormatter) { | ||
xcodebuildOutputFormatter.stdin.end(); | ||
} else { | ||
loader.stop(); | ||
} | ||
if (code !== 0) { | ||
reject( | ||
new CLIError( | ||
` | ||
Failed to build iOS project. | ||
We ran "xcodebuild" command but it exited with error code ${code}. To debug build | ||
logs further, consider building your app with Xcode.app, by opening | ||
${xcodeProject.name}. | ||
`, | ||
xcodebuildOutputFormatter | ||
? undefined | ||
: buildOutput + '\n' + errorOutput, | ||
), | ||
); | ||
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({ | ||
packager, | ||
terminal, | ||
port, | ||
}: { | ||
packager: boolean; | ||
terminal: string | undefined; | ||
port: number; | ||
}): SpawnOptionsWithoutStdio { | ||
if (packager) { | ||
return { | ||
env: { | ||
...process.env, | ||
RCT_TERMINAL: terminal, | ||
RCT_METRO_PORT: port.toString(), | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
env: { | ||
...process.env, | ||
RCT_TERMINAL: terminal, | ||
RCT_NO_LAUNCH_PACKAGER: 'true', | ||
}, | ||
}; | ||
} |
Oops, something went wrong.