Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
107 changes: 89 additions & 18 deletions packages/cli-platform-apple/src/tools/getInfo.ts
Original file line number Diff line number Diff line change
@@ -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<IosInfo | undefined>((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);
}