1import { XcodeProject } from 'xcode';
2
3import { resolvePathOrProject } from './Xcodeproj';
4import { getXCBuildConfigurationFromPbxproj } from '../Target';
5
6/**
7 * Find the Info.plist path linked to a specific build configuration.
8 *
9 * @param projectRoot
10 * @param param1
11 * @returns
12 */
13export function getInfoPlistPathFromPbxproj(
14  projectRootOrProject: string | XcodeProject,
15  {
16    targetName,
17    buildConfiguration = 'Release',
18  }: { targetName?: string; buildConfiguration?: string | 'Release' | 'Debug' } = {}
19): string | null {
20  const project = resolvePathOrProject(projectRootOrProject);
21  if (!project) {
22    return null;
23  }
24
25  const xcBuildConfiguration = getXCBuildConfigurationFromPbxproj(project, {
26    targetName,
27    buildConfiguration,
28  });
29  if (!xcBuildConfiguration) {
30    return null;
31  }
32  // The `INFOPLIST_FILE` is relative to the project folder, ex: app/Info.plist.
33  return sanitizeInfoPlistBuildProperty(xcBuildConfiguration.buildSettings.INFOPLIST_FILE);
34}
35
36function sanitizeInfoPlistBuildProperty(infoPlist?: string): string | null {
37  return infoPlist?.replace(/"/g, '').replace('$(SRCROOT)', '') ?? null;
38}
39