From 6730b8c41a6dcdee45f50919f911355ccfee74dc Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:58:11 +0200 Subject: [PATCH 1/3] fix(apple): parse `.xcworkspace` for schemes --- .../commands/buildCommand/getConfiguration.ts | 4 +- .../cli-platform-apple/src/tools/getInfo.ts | 95 +++++++++++++++---- 2 files changed, 79 insertions(+), 20 deletions(-) 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..c7739efab 100644 --- a/packages/cli-platform-apple/src/tools/getInfo.ts +++ b/packages/cli-platform-apple/src/tools/getInfo.ts @@ -1,28 +1,87 @@ +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 '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 { + Workspace: {FileRef: refs}, + } = xmlParser.parse(workspace); + + 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); + 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; + } + + // For subsequent projects, merge schemes list + if (Array.isArray(schemes) && schemes.length > 0) { + result.schemes = result.schemes.concat(info.schemes); + } + + return result; + }, undefined); +} From eab47a16fd95bed1be31d5c68a4e24d1abfc1c0c Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:19:48 +0200 Subject: [PATCH 2/3] fixup! fix(apple): parse `.xcworkspace` for schemes --- .../cli-platform-apple/src/tools/getInfo.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/cli-platform-apple/src/tools/getInfo.ts b/packages/cli-platform-apple/src/tools/getInfo.ts index c7739efab..bdd40fedd 100644 --- a/packages/cli-platform-apple/src/tools/getInfo.ts +++ b/packages/cli-platform-apple/src/tools/getInfo.ts @@ -6,7 +6,12 @@ import * as path from 'path'; import type {IosInfo} from '../types'; function isErrorLike(err: unknown): err is {message: string} { - return 'message' in err && typeof err.message === 'string'; + return Boolean( + err && + typeof err === 'object' && + 'message' in err && + typeof err.message === 'string', + ); } function parseTargetList(json: string): IosInfo | undefined { @@ -48,11 +53,10 @@ export function getInfo( 'contents.xcworkspacedata', ); const workspace = fs.readFileSync(xcworkspacedata, {encoding: 'utf-8'}); - const { - Workspace: {FileRef: refs}, - } = xmlParser.parse(workspace); + const fileRef = xmlParser.parse(workspace).Workspace.FileRef; + const refs = Array.isArray(fileRef) ? fileRef : [fileRef]; - return refs.reduce((result, ref) => { + return refs.reduce((result, ref) => { const location = ref['@_location']; // Ignore the project generated by CocoaPods @@ -67,6 +71,10 @@ export function getInfo( 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 @@ -77,9 +85,13 @@ export function getInfo( 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(info.schemes); + result.schemes = result.schemes.concat(schemes); } return result; From 1811512c9a1937b048ac75a0a7920f7efdd703d8 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:30:32 +0200 Subject: [PATCH 3/3] fixup! fix(apple): parse `.xcworkspace` for schemes --- packages/cli-platform-apple/src/tools/getInfo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli-platform-apple/src/tools/getInfo.ts b/packages/cli-platform-apple/src/tools/getInfo.ts index bdd40fedd..a4de21ed8 100644 --- a/packages/cli-platform-apple/src/tools/getInfo.ts +++ b/packages/cli-platform-apple/src/tools/getInfo.ts @@ -75,7 +75,7 @@ export function getInfo( return result; } - const schemes = info?.schemes; + const schemes = info.schemes; // If this is the first project, use it as the "main" project if (!result) {