diff --git a/packages/cli-platform-apple/src/commands/buildCommand/getConfiguration.ts b/packages/cli-platform-apple/src/commands/buildCommand/getConfiguration.ts index 47a24a5f0..2a2079e83 100644 --- a/packages/cli-platform-apple/src/commands/buildCommand/getConfiguration.ts +++ b/packages/cli-platform-apple/src/commands/buildCommand/getConfiguration.ts @@ -1,5 +1,5 @@ import chalk from 'chalk'; -import {IOSProjectInfo} from '@react-native-community/cli-types'; +import type {IOSProjectInfo} from '@react-native-community/cli-types'; import {logger} from '@react-native-community/cli-tools'; import {selectFromInteractiveMode} from '../../tools/selectFromInteractiveMode'; import {getInfo} from '../../tools/getInfo'; @@ -16,7 +16,7 @@ export async function getConfiguration( args: BuildFlags, platformName: ApplePlatform, ) { - const info = getInfo(); + const info = getInfo(xcodeProject, sourceDir); if (args.mode) { checkIfConfigurationExists(info?.configurations ?? [], args.mode); diff --git a/packages/cli-platform-apple/src/tools/getInfo.ts b/packages/cli-platform-apple/src/tools/getInfo.ts index 3bc96a865..a4de21ed8 100644 --- a/packages/cli-platform-apple/src/tools/getInfo.ts +++ b/packages/cli-platform-apple/src/tools/getInfo.ts @@ -1,28 +1,99 @@ +import type {IOSProjectInfo} from '@react-native-community/cli-types'; import execa from 'execa'; -import {IosInfo} from '../types'; +import {XMLParser} from 'fast-xml-parser'; +import * as fs from 'fs'; +import * as path from 'path'; +import type {IosInfo} from '../types'; -export function getInfo(): IosInfo | undefined { +function isErrorLike(err: unknown): err is {message: string} { + return Boolean( + err && + typeof err === 'object' && + 'message' in err && + typeof err.message === 'string', + ); +} + +function parseTargetList(json: string): IosInfo | undefined { try { - const value = JSON.parse( - execa.sync('xcodebuild', ['-list', '-json']).stdout, - ); - - if ('project' in value) { - return value.project; - } else if ('workspace' in value) { - return value.workspace; + const info = JSON.parse(json); + + if ('project' in info) { + return info.project; + } else if ('workspace' in info) { + return info.workspace; } return undefined; } catch (error) { - if ( - (error as Error)?.message && - (error as Error).message.includes('xcodebuild: error:') - ) { - const match = (error as Error).message.match(/xcodebuild: error: (.*)/); - const err = match ? match[0] : error; - throw new Error(err as any); + if (isErrorLike(error)) { + const match = error.message.match(/xcodebuild: error: (.*)/); + if (match) { + throw new Error(match[0]); + } } - throw new Error(error as any); + + throw error; } } + +export function getInfo( + projectInfo: IOSProjectInfo, + sourceDir: string, +): IosInfo | undefined { + if (!projectInfo.isWorkspace) { + const xcodebuild = execa.sync('xcodebuild', ['-list', '-json']); + return parseTargetList(xcodebuild.stdout); + } + + const xmlParser = new XMLParser({ignoreAttributes: false}); + const xcworkspacedata = path.join( + sourceDir, + projectInfo.name, + 'contents.xcworkspacedata', + ); + const workspace = fs.readFileSync(xcworkspacedata, {encoding: 'utf-8'}); + const fileRef = xmlParser.parse(workspace).Workspace.FileRef; + const refs = Array.isArray(fileRef) ? fileRef : [fileRef]; + + return refs.reduce((result, ref) => { + const location = ref['@_location']; + + // Ignore the project generated by CocoaPods + if (location.endsWith('/Pods.xcodeproj')) { + return result; + } + + const xcodebuild = execa.sync('xcodebuild', [ + '-list', + '-json', + '-project', + path.join(sourceDir, location.replace('group:', '')), + ]); + const info = parseTargetList(xcodebuild.stdout); + if (!info) { + return result; + } + + const schemes = info.schemes; + + // If this is the first project, use it as the "main" project + if (!result) { + if (!Array.isArray(schemes)) { + info.schemes = []; + } + return info; + } + + if (!Array.isArray(result.schemes)) { + throw new Error("This shouldn't happen since we set it earlier"); + } + + // For subsequent projects, merge schemes list + if (Array.isArray(schemes) && schemes.length > 0) { + result.schemes = result.schemes.concat(schemes); + } + + return result; + }, undefined); +}