|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const SOURCE_FILES = { |
| 4 | + intent: "T3CaptureIntent.swift", |
| 5 | + control: "T3CameraControl.swift", |
| 6 | + extension: "T3CameraCapture.swift", |
| 7 | +}; |
| 8 | +const EMBED_PHASE_NAME = "Embed ExtensionKit Extensions"; |
| 9 | + |
| 10 | +function stripComments(map) { |
| 11 | + return Object.fromEntries(Object.entries(map ?? {}).filter(([key]) => !key.endsWith("_comment"))); |
| 12 | +} |
| 13 | + |
| 14 | +function findByName(map, name) { |
| 15 | + const entry = Object.entries(stripComments(map)).find( |
| 16 | + ([, value]) => value?.name === name || value?.name === `"${name}"`, |
| 17 | + ); |
| 18 | + return entry ? { uuid: entry[0], value: entry[1] } : null; |
| 19 | +} |
| 20 | + |
| 21 | +function addObject(proj, section, object, comment) { |
| 22 | + const objects = proj.hash.project.objects; |
| 23 | + const uuid = proj.generateUuid(); |
| 24 | + objects[section] ??= {}; |
| 25 | + objects[section][uuid] = object; |
| 26 | + objects[section][`${uuid}_comment`] = comment; |
| 27 | + return uuid; |
| 28 | +} |
| 29 | + |
| 30 | +function sourcesPhaseOf(proj, target, targetName) { |
| 31 | + const phases = proj.hash.project.objects.PBXSourcesBuildPhase ?? {}; |
| 32 | + const ref = target.buildPhases?.find(({ value }) => phases[value]); |
| 33 | + if (!ref) { |
| 34 | + throw new Error(`addCameraControlTargets: ${targetName} has no Sources build phase.`); |
| 35 | + } |
| 36 | + return phases[ref.value]; |
| 37 | +} |
| 38 | + |
| 39 | +function addBuildFile(proj, phase, fileRef, name, phaseName, settings) { |
| 40 | + const uuid = addObject( |
| 41 | + proj, |
| 42 | + "PBXBuildFile", |
| 43 | + { isa: "PBXBuildFile", fileRef, fileRef_comment: name, ...(settings ? { settings } : {}) }, |
| 44 | + `${name} in ${phaseName}`, |
| 45 | + ); |
| 46 | + phase.files.push({ value: uuid, comment: `${name} in ${phaseName}` }); |
| 47 | +} |
| 48 | + |
| 49 | +function addPhase(proj, target, section, name, extra = {}) { |
| 50 | + const phase = { |
| 51 | + isa: section, |
| 52 | + buildActionMask: 2147483647, |
| 53 | + files: [], |
| 54 | + runOnlyForDeploymentPostprocessing: 0, |
| 55 | + ...extra, |
| 56 | + }; |
| 57 | + const uuid = addObject(proj, section, phase, name); |
| 58 | + target.buildPhases.push({ value: uuid, comment: name }); |
| 59 | + return phase; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Adds the locked camera capture extension and wires the shared intent into the app and widget targets. |
| 64 | + * @param {import('xcode').XcodeProject} proj |
| 65 | + * @param {{ appTargetName: string, widgetTargetName: string, extensionTargetName: string, |
| 66 | + * bundleIdentifier: string, displayName: string, cameraUsageDescription: string, |
| 67 | + * marketingVersion: string, buildNumber: string, appleTeamId?: string }} opts |
| 68 | + */ |
| 69 | +function addCameraControlTargets(proj, opts) { |
| 70 | + const objects = proj.hash.project.objects; |
| 71 | + if (findByName(objects.PBXNativeTarget, opts.extensionTargetName)) return false; |
| 72 | + |
| 73 | + const appTarget = findByName(objects.PBXNativeTarget, opts.appTargetName); |
| 74 | + const widgetTarget = findByName(objects.PBXNativeTarget, opts.widgetTargetName); |
| 75 | + if (!appTarget || !widgetTarget) { |
| 76 | + throw new Error( |
| 77 | + `addCameraControlTargets: missing ${appTarget ? opts.widgetTargetName : opts.appTargetName} target. ` + |
| 78 | + "Register withCameraControl before expo-widgets so its mods run after the widget target exists.", |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const fileRefs = {}; |
| 83 | + for (const [key, name] of Object.entries(SOURCE_FILES)) { |
| 84 | + fileRefs[key] = addObject( |
| 85 | + proj, |
| 86 | + "PBXFileReference", |
| 87 | + { |
| 88 | + isa: "PBXFileReference", |
| 89 | + lastKnownFileType: "sourcecode.swift", |
| 90 | + path: name, |
| 91 | + sourceTree: '"<group>"', |
| 92 | + }, |
| 93 | + name, |
| 94 | + ); |
| 95 | + } |
| 96 | + const infoPlistRef = addObject( |
| 97 | + proj, |
| 98 | + "PBXFileReference", |
| 99 | + { |
| 100 | + isa: "PBXFileReference", |
| 101 | + lastKnownFileType: "text.plist.xml", |
| 102 | + path: "Info.plist", |
| 103 | + sourceTree: '"<group>"', |
| 104 | + }, |
| 105 | + "Info.plist", |
| 106 | + ); |
| 107 | + const groupUuid = addObject( |
| 108 | + proj, |
| 109 | + "PBXGroup", |
| 110 | + { |
| 111 | + isa: "PBXGroup", |
| 112 | + children: [ |
| 113 | + ...Object.entries(SOURCE_FILES).map(([key, name]) => ({ |
| 114 | + value: fileRefs[key], |
| 115 | + comment: name, |
| 116 | + })), |
| 117 | + { value: infoPlistRef, comment: "Info.plist" }, |
| 118 | + ], |
| 119 | + path: opts.extensionTargetName, |
| 120 | + sourceTree: '"<group>"', |
| 121 | + }, |
| 122 | + opts.extensionTargetName, |
| 123 | + ); |
| 124 | + const mainGroup = objects.PBXGroup[proj.getFirstProject().firstProject.mainGroup]; |
| 125 | + mainGroup.children.push({ value: groupUuid, comment: opts.extensionTargetName }); |
| 126 | + |
| 127 | + const productName = `${opts.extensionTargetName}.appex`; |
| 128 | + const productRef = addObject( |
| 129 | + proj, |
| 130 | + "PBXFileReference", |
| 131 | + { |
| 132 | + isa: "PBXFileReference", |
| 133 | + explicitFileType: '"wrapper.extensionkit-extension"', |
| 134 | + includeInIndex: 0, |
| 135 | + path: productName, |
| 136 | + sourceTree: "BUILT_PRODUCTS_DIR", |
| 137 | + }, |
| 138 | + productName, |
| 139 | + ); |
| 140 | + const productsGroup = findByName(objects.PBXGroup, "Products"); |
| 141 | + productsGroup?.value.children.push({ value: productRef, comment: productName }); |
| 142 | + |
| 143 | + const buildSettings = { |
| 144 | + APPLICATION_EXTENSION_API_ONLY: "YES", |
| 145 | + CODE_SIGN_STYLE: "Automatic", |
| 146 | + CURRENT_PROJECT_VERSION: `"${opts.buildNumber}"`, |
| 147 | + ...(opts.appleTeamId ? { DEVELOPMENT_TEAM: opts.appleTeamId } : {}), |
| 148 | + GENERATE_INFOPLIST_FILE: "YES", |
| 149 | + INFOPLIST_FILE: `${opts.extensionTargetName}/Info.plist`, |
| 150 | + INFOPLIST_KEY_CFBundleDisplayName: `"${opts.displayName}"`, |
| 151 | + INFOPLIST_KEY_NSCameraUsageDescription: `"${opts.cameraUsageDescription}"`, |
| 152 | + IPHONEOS_DEPLOYMENT_TARGET: "18.0", |
| 153 | + LD_RUNPATH_SEARCH_PATHS: [ |
| 154 | + '"$(inherited)"', |
| 155 | + '"@executable_path/Frameworks"', |
| 156 | + '"@executable_path/../../Frameworks"', |
| 157 | + ], |
| 158 | + MARKETING_VERSION: `"${opts.marketingVersion}"`, |
| 159 | + PRODUCT_BUNDLE_IDENTIFIER: `"${opts.bundleIdentifier}"`, |
| 160 | + PRODUCT_NAME: '"$(TARGET_NAME)"', |
| 161 | + SDKROOT: "iphoneos", |
| 162 | + SKIP_INSTALL: "YES", |
| 163 | + SWIFT_VERSION: "5.0", |
| 164 | + TARGETED_DEVICE_FAMILY: '"1,2"', |
| 165 | + }; |
| 166 | + const { uuid: configurationListUuid } = proj.addXCConfigurationList( |
| 167 | + ["Debug", "Release"].map((name) => ({ |
| 168 | + name, |
| 169 | + isa: "XCBuildConfiguration", |
| 170 | + buildSettings: { ...buildSettings }, |
| 171 | + })), |
| 172 | + "Release", |
| 173 | + `Build configuration list for PBXNativeTarget "${opts.extensionTargetName}"`, |
| 174 | + ); |
| 175 | + |
| 176 | + const extensionTarget = { |
| 177 | + isa: "PBXNativeTarget", |
| 178 | + buildConfigurationList: configurationListUuid, |
| 179 | + buildPhases: [], |
| 180 | + buildRules: [], |
| 181 | + dependencies: [], |
| 182 | + name: opts.extensionTargetName, |
| 183 | + productName: opts.extensionTargetName, |
| 184 | + productReference: productRef, |
| 185 | + productType: '"com.apple.product-type.extensionkit-extension"', |
| 186 | + }; |
| 187 | + const extensionTargetUuid = addObject( |
| 188 | + proj, |
| 189 | + "PBXNativeTarget", |
| 190 | + extensionTarget, |
| 191 | + opts.extensionTargetName, |
| 192 | + ); |
| 193 | + const extensionSources = addPhase(proj, extensionTarget, "PBXSourcesBuildPhase", "Sources"); |
| 194 | + addPhase(proj, extensionTarget, "PBXFrameworksBuildPhase", "Frameworks"); |
| 195 | + addPhase(proj, extensionTarget, "PBXResourcesBuildPhase", "Resources"); |
| 196 | + addBuildFile(proj, extensionSources, fileRefs.extension, SOURCE_FILES.extension, "Sources"); |
| 197 | + addBuildFile(proj, extensionSources, fileRefs.intent, SOURCE_FILES.intent, "Sources"); |
| 198 | + |
| 199 | + proj.addToPbxProjectSection({ uuid: extensionTargetUuid, pbxNativeTarget: extensionTarget }); |
| 200 | + // addTargetDependency silently does nothing unless both sections exist. |
| 201 | + objects.PBXTargetDependency ??= {}; |
| 202 | + objects.PBXContainerItemProxy ??= {}; |
| 203 | + proj.addTargetDependency(appTarget.uuid, [extensionTargetUuid]); |
| 204 | + |
| 205 | + const embedPhase = addPhase(proj, appTarget.value, "PBXCopyFilesBuildPhase", EMBED_PHASE_NAME, { |
| 206 | + dstPath: '"$(EXTENSIONS_FOLDER_PATH)"', |
| 207 | + dstSubfolderSpec: 16, |
| 208 | + name: `"${EMBED_PHASE_NAME}"`, |
| 209 | + }); |
| 210 | + addBuildFile(proj, embedPhase, productRef, productName, EMBED_PHASE_NAME, { |
| 211 | + ATTRIBUTES: ["RemoveHeadersOnCopy"], |
| 212 | + }); |
| 213 | + |
| 214 | + const appSources = sourcesPhaseOf(proj, appTarget.value, opts.appTargetName); |
| 215 | + addBuildFile(proj, appSources, fileRefs.intent, SOURCE_FILES.intent, "Sources"); |
| 216 | + const widgetSources = sourcesPhaseOf(proj, widgetTarget.value, opts.widgetTargetName); |
| 217 | + addBuildFile(proj, widgetSources, fileRefs.intent, SOURCE_FILES.intent, "Sources"); |
| 218 | + addBuildFile(proj, widgetSources, fileRefs.control, SOURCE_FILES.control, "Sources"); |
| 219 | + return true; |
| 220 | +} |
| 221 | + |
| 222 | +module.exports = { addCameraControlTargets, SOURCE_FILES }; |
0 commit comments