expo-targets 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/.eslintrc.js +2 -0
  2. package/README.md +244 -0
  3. package/android/build.gradle +22 -0
  4. package/android/src/main/java/expo/modules/targets/ExpoTargetsLogger.kt +98 -0
  5. package/android/src/main/java/expo/modules/targets/ExpoTargetsReceiver.kt +136 -0
  6. package/android/src/main/java/expo/modules/targets/ExpoTargetsRemoteViewsProvider.kt +72 -0
  7. package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetProvider.kt +47 -0
  8. package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetSync.kt +78 -0
  9. package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetUpdateReceiver.kt +30 -0
  10. package/android/src/main/java/expo/modules/targets/extension/ExpoTargetsExtensionModule.kt +27 -0
  11. package/android/src/main/java/expo/modules/targets/messages/ExpoTargetsMessagesModule.kt +40 -0
  12. package/android/src/main/java/expo/modules/targets/storage/ExpoTargetsStorageModule.kt +119 -0
  13. package/app.plugin.js +1 -0
  14. package/build/metro/src/index.d.ts +2 -0
  15. package/build/metro/src/index.d.ts.map +1 -0
  16. package/build/metro/src/index.js +6 -0
  17. package/build/metro/src/index.js.map +1 -0
  18. package/build/metro/src/withTargetsMetro.d.ts +6 -0
  19. package/build/metro/src/withTargetsMetro.d.ts.map +1 -0
  20. package/build/metro/src/withTargetsMetro.js +171 -0
  21. package/build/metro/src/withTargetsMetro.js.map +1 -0
  22. package/build/plugin/src/android/config-plugins/withAndroidColors.d.ts +7 -0
  23. package/build/plugin/src/android/config-plugins/withAndroidColors.d.ts.map +1 -0
  24. package/build/plugin/src/android/config-plugins/withAndroidColors.js +56 -0
  25. package/build/plugin/src/android/config-plugins/withAndroidColors.js.map +1 -0
  26. package/build/plugin/src/android/config-plugins/withAndroidManifest.d.ts +7 -0
  27. package/build/plugin/src/android/config-plugins/withAndroidManifest.d.ts.map +1 -0
  28. package/build/plugin/src/android/config-plugins/withAndroidManifest.js +70 -0
  29. package/build/plugin/src/android/config-plugins/withAndroidManifest.js.map +1 -0
  30. package/build/plugin/src/android/config-plugins/withAndroidModule.d.ts +7 -0
  31. package/build/plugin/src/android/config-plugins/withAndroidModule.d.ts.map +1 -0
  32. package/build/plugin/src/android/config-plugins/withAndroidModule.js +41 -0
  33. package/build/plugin/src/android/config-plugins/withAndroidModule.js.map +1 -0
  34. package/build/plugin/src/android/config-plugins/withAndroidResources.d.ts +7 -0
  35. package/build/plugin/src/android/config-plugins/withAndroidResources.d.ts.map +1 -0
  36. package/build/plugin/src/android/config-plugins/withAndroidResources.js +75 -0
  37. package/build/plugin/src/android/config-plugins/withAndroidResources.js.map +1 -0
  38. package/build/plugin/src/android/config-plugins/withAndroidTarget.d.ts +28 -0
  39. package/build/plugin/src/android/config-plugins/withAndroidTarget.d.ts.map +1 -0
  40. package/build/plugin/src/android/config-plugins/withAndroidTarget.js +28 -0
  41. package/build/plugin/src/android/config-plugins/withAndroidTarget.js.map +1 -0
  42. package/build/plugin/src/android/config-plugins/withGradleModule.d.ts +7 -0
  43. package/build/plugin/src/android/config-plugins/withGradleModule.d.ts.map +1 -0
  44. package/build/plugin/src/android/config-plugins/withGradleModule.js +86 -0
  45. package/build/plugin/src/android/config-plugins/withGradleModule.js.map +1 -0
  46. package/build/plugin/src/android/config-plugins/withGradleSettings.d.ts +7 -0
  47. package/build/plugin/src/android/config-plugins/withGradleSettings.d.ts.map +1 -0
  48. package/build/plugin/src/android/config-plugins/withGradleSettings.js +23 -0
  49. package/build/plugin/src/android/config-plugins/withGradleSettings.js.map +1 -0
  50. package/build/plugin/src/android/withAndroidAppBuildGradle.d.ts +7 -0
  51. package/build/plugin/src/android/withAndroidAppBuildGradle.d.ts.map +1 -0
  52. package/build/plugin/src/android/withAndroidAppBuildGradle.js +31 -0
  53. package/build/plugin/src/android/withAndroidAppBuildGradle.js.map +1 -0
  54. package/build/plugin/src/android/withAndroidKotlinGradleProperty.d.ts +3 -0
  55. package/build/plugin/src/android/withAndroidKotlinGradleProperty.d.ts.map +1 -0
  56. package/build/plugin/src/android/withAndroidKotlinGradleProperty.js +24 -0
  57. package/build/plugin/src/android/withAndroidKotlinGradleProperty.js.map +1 -0
  58. package/build/plugin/src/android/withAndroidProjectKotlinVersion.d.ts +3 -0
  59. package/build/plugin/src/android/withAndroidProjectKotlinVersion.d.ts.map +1 -0
  60. package/build/plugin/src/android/withAndroidProjectKotlinVersion.js +27 -0
  61. package/build/plugin/src/android/withAndroidProjectKotlinVersion.js.map +1 -0
  62. package/build/plugin/src/android/withAndroidTarget.d.ts +10 -0
  63. package/build/plugin/src/android/withAndroidTarget.d.ts.map +1 -0
  64. package/build/plugin/src/android/withAndroidTarget.js +22 -0
  65. package/build/plugin/src/android/withAndroidTarget.js.map +1 -0
  66. package/build/plugin/src/android/withAndroidWidget.d.ts +13 -0
  67. package/build/plugin/src/android/withAndroidWidget.d.ts.map +1 -0
  68. package/build/plugin/src/android/withAndroidWidget.js +564 -0
  69. package/build/plugin/src/android/withAndroidWidget.js.map +1 -0
  70. package/build/plugin/src/config.d.ts +141 -0
  71. package/build/plugin/src/config.d.ts.map +1 -0
  72. package/build/plugin/src/config.js +54 -0
  73. package/build/plugin/src/config.js.map +1 -0
  74. package/build/plugin/src/index.d.ts +9 -0
  75. package/build/plugin/src/index.d.ts.map +1 -0
  76. package/build/plugin/src/index.js +11 -0
  77. package/build/plugin/src/index.js.map +1 -0
  78. package/build/plugin/src/ios/config-plugins/withEntitlements.d.ts +11 -0
  79. package/build/plugin/src/ios/config-plugins/withEntitlements.d.ts.map +1 -0
  80. package/build/plugin/src/ios/config-plugins/withEntitlements.js +49 -0
  81. package/build/plugin/src/ios/config-plugins/withEntitlements.js.map +1 -0
  82. package/build/plugin/src/ios/config-plugins/withIOSTarget.d.ts +17 -0
  83. package/build/plugin/src/ios/config-plugins/withIOSTarget.d.ts.map +1 -0
  84. package/build/plugin/src/ios/config-plugins/withIOSTarget.js +152 -0
  85. package/build/plugin/src/ios/config-plugins/withIOSTarget.js.map +1 -0
  86. package/build/plugin/src/ios/config-plugins/withIosColorset.d.ts +8 -0
  87. package/build/plugin/src/ios/config-plugins/withIosColorset.d.ts.map +1 -0
  88. package/build/plugin/src/ios/config-plugins/withIosColorset.js +25 -0
  89. package/build/plugin/src/ios/config-plugins/withIosColorset.js.map +1 -0
  90. package/build/plugin/src/ios/config-plugins/withIosStickerPack.d.ts +8 -0
  91. package/build/plugin/src/ios/config-plugins/withIosStickerPack.d.ts.map +1 -0
  92. package/build/plugin/src/ios/config-plugins/withIosStickerPack.js +52 -0
  93. package/build/plugin/src/ios/config-plugins/withIosStickerPack.js.map +1 -0
  94. package/build/plugin/src/ios/config-plugins/withPodfile.d.ts +12 -0
  95. package/build/plugin/src/ios/config-plugins/withPodfile.d.ts.map +1 -0
  96. package/build/plugin/src/ios/config-plugins/withPodfile.js +162 -0
  97. package/build/plugin/src/ios/config-plugins/withPodfile.js.map +1 -0
  98. package/build/plugin/src/ios/config-plugins/withXcodeChanges.d.ts +15 -0
  99. package/build/plugin/src/ios/config-plugins/withXcodeChanges.d.ts.map +1 -0
  100. package/build/plugin/src/ios/config-plugins/withXcodeChanges.js +646 -0
  101. package/build/plugin/src/ios/config-plugins/withXcodeChanges.js.map +1 -0
  102. package/build/plugin/src/ios/target.d.ts +41 -0
  103. package/build/plugin/src/ios/target.d.ts.map +1 -0
  104. package/build/plugin/src/ios/target.js +574 -0
  105. package/build/plugin/src/ios/target.js.map +1 -0
  106. package/build/plugin/src/ios/utils/asset.d.ts +60 -0
  107. package/build/plugin/src/ios/utils/asset.d.ts.map +1 -0
  108. package/build/plugin/src/ios/utils/asset.js +375 -0
  109. package/build/plugin/src/ios/utils/asset.js.map +1 -0
  110. package/build/plugin/src/ios/utils/file.d.ts +33 -0
  111. package/build/plugin/src/ios/utils/file.d.ts.map +1 -0
  112. package/build/plugin/src/ios/utils/file.js +88 -0
  113. package/build/plugin/src/ios/utils/file.js.map +1 -0
  114. package/build/plugin/src/ios/utils/index.d.ts +12 -0
  115. package/build/plugin/src/ios/utils/index.d.ts.map +1 -0
  116. package/build/plugin/src/ios/utils/index.js +38 -0
  117. package/build/plugin/src/ios/utils/index.js.map +1 -0
  118. package/build/plugin/src/ios/utils/paths.d.ts +128 -0
  119. package/build/plugin/src/ios/utils/paths.d.ts.map +1 -0
  120. package/build/plugin/src/ios/utils/paths.js +141 -0
  121. package/build/plugin/src/ios/utils/paths.js.map +1 -0
  122. package/build/plugin/src/ios/utils/plist.d.ts +29 -0
  123. package/build/plugin/src/ios/utils/plist.d.ts.map +1 -0
  124. package/build/plugin/src/ios/utils/plist.js +114 -0
  125. package/build/plugin/src/ios/utils/plist.js.map +1 -0
  126. package/build/plugin/src/ios/utils/podfile.d.ts +82 -0
  127. package/build/plugin/src/ios/utils/podfile.d.ts.map +1 -0
  128. package/build/plugin/src/ios/utils/podfile.js +408 -0
  129. package/build/plugin/src/ios/utils/podfile.js.map +1 -0
  130. package/build/plugin/src/ios/utils/reactNativeSwift.d.ts +11 -0
  131. package/build/plugin/src/ios/utils/reactNativeSwift.d.ts.map +1 -0
  132. package/build/plugin/src/ios/utils/reactNativeSwift.js +263 -0
  133. package/build/plugin/src/ios/utils/reactNativeSwift.js.map +1 -0
  134. package/build/plugin/src/ios/utils/xcode.d.ts +185 -0
  135. package/build/plugin/src/ios/utils/xcode.d.ts.map +1 -0
  136. package/build/plugin/src/ios/utils/xcode.js +706 -0
  137. package/build/plugin/src/ios/utils/xcode.js.map +1 -0
  138. package/build/plugin/src/logger.d.ts +10 -0
  139. package/build/plugin/src/logger.d.ts.map +1 -0
  140. package/build/plugin/src/logger.js +62 -0
  141. package/build/plugin/src/logger.js.map +1 -0
  142. package/build/plugin/src/parseTargetConfig.d.ts +3 -0
  143. package/build/plugin/src/parseTargetConfig.d.ts.map +1 -0
  144. package/build/plugin/src/parseTargetConfig.js +113 -0
  145. package/build/plugin/src/parseTargetConfig.js.map +1 -0
  146. package/build/plugin/src/withTargetsDir.d.ts +6 -0
  147. package/build/plugin/src/withTargetsDir.d.ts.map +1 -0
  148. package/build/plugin/src/withTargetsDir.js +90 -0
  149. package/build/plugin/src/withTargetsDir.js.map +1 -0
  150. package/build/src/Target.d.ts +43 -0
  151. package/build/src/Target.d.ts.map +1 -0
  152. package/build/src/Target.js +128 -0
  153. package/build/src/Target.js.map +1 -0
  154. package/build/src/index.d.ts +9 -0
  155. package/build/src/index.d.ts.map +1 -0
  156. package/build/src/index.js +27 -0
  157. package/build/src/index.js.map +1 -0
  158. package/build/src/modules/extension/index.d.ts +18 -0
  159. package/build/src/modules/extension/index.d.ts.map +1 -0
  160. package/build/src/modules/extension/index.js +44 -0
  161. package/build/src/modules/extension/index.js.map +1 -0
  162. package/build/src/modules/messages/index.d.ts +40 -0
  163. package/build/src/modules/messages/index.d.ts.map +1 -0
  164. package/build/src/modules/messages/index.js +94 -0
  165. package/build/src/modules/messages/index.js.map +1 -0
  166. package/build/src/modules/storage/index.d.ts +16 -0
  167. package/build/src/modules/storage/index.d.ts.map +1 -0
  168. package/build/src/modules/storage/index.js +122 -0
  169. package/build/src/modules/storage/index.js.map +1 -0
  170. package/expo-module.config.json +17 -0
  171. package/ios/ExpoTargetsExtension.podspec +23 -0
  172. package/ios/ExpoTargetsExtensionModule.swift +181 -0
  173. package/ios/ExpoTargetsMessages.podspec +23 -0
  174. package/ios/ExpoTargetsMessagesModule.swift +267 -0
  175. package/ios/ExpoTargetsStorage.podspec +23 -0
  176. package/ios/ExpoTargetsStorageModule.swift +91 -0
  177. package/ios/Tests/.swiftlint.yml +52 -0
  178. package/ios/Tests/Helpers/MockExtensionContext.swift +58 -0
  179. package/ios/Tests/Helpers/MockUserDefaults.swift +70 -0
  180. package/ios/Tests/Helpers/MockWidgetCenter.swift +71 -0
  181. package/ios/Tests/Helpers/TestHelpers.swift +90 -0
  182. package/ios/Tests/Integration/TargetIntegrationTests.swift +478 -0
  183. package/ios/Tests/Main/AllTests.swift +80 -0
  184. package/ios/Tests/Package.swift +66 -0
  185. package/ios/Tests/Performance/StoragePerformanceTests.swift +461 -0
  186. package/ios/Tests/Unit/Extension/ExpoTargetsExtensionModuleTests.swift +501 -0
  187. package/ios/Tests/Unit/Storage/ExpoTargetsStorageModuleTests.swift +578 -0
  188. package/ios/Tests/Unit/Template/ShareExtensionTemplateTests.swift +421 -0
  189. package/ios/Tests/XCTestPlan.xctestplan +87 -0
  190. package/metro/src/index.ts +1 -0
  191. package/metro/src/withTargetsMetro.ts +247 -0
  192. package/metro/tsconfig.build.json +9 -0
  193. package/package.json +74 -0
  194. package/plugin/src/android/withAndroidAppBuildGradle.ts +34 -0
  195. package/plugin/src/android/withAndroidTarget.ts +26 -0
  196. package/plugin/src/android/withAndroidWidget.ts +778 -0
  197. package/plugin/src/config.ts +255 -0
  198. package/plugin/src/index.ts +19 -0
  199. package/plugin/src/ios/config-plugins/withEntitlements.ts +69 -0
  200. package/plugin/src/ios/config-plugins/withIOSTarget.ts +179 -0
  201. package/plugin/src/ios/config-plugins/withIosColorset.ts +29 -0
  202. package/plugin/src/ios/config-plugins/withIosStickerPack.ts +65 -0
  203. package/plugin/src/ios/config-plugins/withPodfile.ts +238 -0
  204. package/plugin/src/ios/config-plugins/withXcodeChanges.ts +891 -0
  205. package/plugin/src/ios/target.ts +668 -0
  206. package/plugin/src/ios/templates/MessagesViewController.swift +120 -0
  207. package/plugin/src/ios/templates/ReactNativeViewController.swift +240 -0
  208. package/plugin/src/ios/templates/action-extension-data.swift +106 -0
  209. package/plugin/src/ios/templates/clip-extension-data.swift +28 -0
  210. package/plugin/src/ios/templates/messages-extension-data.swift +28 -0
  211. package/plugin/src/ios/templates/share-extension-data.swift +113 -0
  212. package/plugin/src/ios/utils/asset.ts +490 -0
  213. package/plugin/src/ios/utils/file.ts +80 -0
  214. package/plugin/src/ios/utils/index.ts +12 -0
  215. package/plugin/src/ios/utils/paths.ts +253 -0
  216. package/plugin/src/ios/utils/plist.ts +101 -0
  217. package/plugin/src/ios/utils/podfile.ts +534 -0
  218. package/plugin/src/ios/utils/reactNativeSwift.ts +283 -0
  219. package/plugin/src/ios/utils/xcode.ts +989 -0
  220. package/plugin/src/logger.ts +63 -0
  221. package/plugin/src/parseTargetConfig.ts +115 -0
  222. package/plugin/src/types/normalize-colors.d.ts +3 -0
  223. package/plugin/src/withTargetsDir.ts +120 -0
  224. package/plugin/tsconfig.build.json +9 -0
  225. package/plugin/tsconfig.json +9 -0
  226. package/src/Target.ts +239 -0
  227. package/src/index.ts +52 -0
  228. package/src/modules/extension/index.ts +54 -0
  229. package/src/modules/messages/index.ts +124 -0
  230. package/src/modules/storage/index.ts +126 -0
  231. package/tsconfig.json +8 -0
@@ -0,0 +1,534 @@
1
+ /**
2
+ * Podfile manipulation utilities.
3
+ * Provides helpers for injecting target blocks into Podfile.
4
+ */
5
+
6
+ /**
7
+ * Generate a Podfile target block for a React Native extension.
8
+ * Extension targets only inherit search paths, with no explicit pod dependencies.
9
+ * This avoids linking incompatible modules like Expo that contain UIApplication APIs.
10
+ *
11
+ * Note: ExpoTargetsMessages is accessed via the main app's autolinking through shared
12
+ * registry defined in generated code, so no direct pod dependency is needed.
13
+ */
14
+ export function generateReactNativeTargetBlock({
15
+ targetName,
16
+ deploymentTarget,
17
+ extensionType,
18
+ }: {
19
+ targetName: string;
20
+ deploymentTarget: string;
21
+ extensionType: string;
22
+ }): string {
23
+ return `
24
+ target '${targetName}' do
25
+ platform :ios, '${deploymentTarget}'
26
+ inherit! :search_paths
27
+ end
28
+ `;
29
+ }
30
+
31
+ /**
32
+ * Check if main app target uses frameworks.
33
+ * Returns true if use_frameworks! is found in the main target block.
34
+ */
35
+ export function mainTargetUsesFrameworks(
36
+ podfileContent: string,
37
+ mainTargetName: string
38
+ ): boolean {
39
+ const targetRegex = new RegExp(
40
+ `target\\s+'${mainTargetName}'\\s+do([\\s\\S]*?)(?=\\n\\s*target\\s+|post_install|$)`,
41
+ 'm'
42
+ );
43
+
44
+ const match = podfileContent.match(targetRegex);
45
+ if (!match) {
46
+ return false;
47
+ }
48
+
49
+ const targetBlock = match[1];
50
+ return targetBlock.includes('use_frameworks!');
51
+ }
52
+
53
+ /**
54
+ * Generate a Podfile target block for a standalone (non-RN) extension.
55
+ * Standalone targets use native code only (Swift/Obj-C), no React Native or Expo modules.
56
+ * Must match main app's use_frameworks! setting for CocoaPods integration.
57
+ */
58
+ export function generateStandaloneTargetBlock({
59
+ targetName,
60
+ deploymentTarget,
61
+ useFrameworks,
62
+ }: {
63
+ targetName: string;
64
+ deploymentTarget: string;
65
+ useFrameworks?: boolean;
66
+ }): string {
67
+ const frameworksLine = useFrameworks
68
+ ? ` use_frameworks! :linkage => :static\n`
69
+ : '';
70
+ return `
71
+ target '${targetName}' do
72
+ ${frameworksLine} platform :ios, '${deploymentTarget}'
73
+ end
74
+ `;
75
+ }
76
+
77
+ /**
78
+ * Insert a target block into a Podfile.
79
+ * React Native targets are nested inside main app to inherit dependencies.
80
+ * Standalone targets are inserted as siblings to avoid Expo module autolinking.
81
+ */
82
+ export function insertTargetBlock(
83
+ podfileContent: string,
84
+ targetBlock: string,
85
+ standalone: boolean = false,
86
+ logger?: { log: (message: string) => void }
87
+ ): string {
88
+ if (standalone) {
89
+ // Standalone targets: insert as sibling AFTER main target's closing 'end'
90
+ // This prevents CocoaPods from auto-generating Expo module providers
91
+
92
+ // Find the last 'end' in the Podfile - this should be the main target's closing 'end'
93
+ // since the main target is the outermost block in a standard Expo Podfile
94
+ const lastEndMatch = podfileContent.match(/\bend\b(?!.*\bend\b)/s);
95
+
96
+ if (!lastEndMatch) {
97
+ throw new Error('Could not find any end keyword in Podfile');
98
+ }
99
+
100
+ const mainTargetEndIndex = lastEndMatch.index! + lastEndMatch[0].length;
101
+
102
+ if (logger) {
103
+ logger.log(
104
+ `Inserting standalone target after main target's closing 'end' at position ${mainTargetEndIndex}`
105
+ );
106
+ } else {
107
+ console.log(
108
+ `[expo-targets] Inserting standalone target after main target's closing 'end' at position ${mainTargetEndIndex}`
109
+ );
110
+ }
111
+
112
+ // Insert after main target's closing 'end'
113
+ return (
114
+ podfileContent.slice(0, mainTargetEndIndex) +
115
+ '\n\n' +
116
+ targetBlock.trim() +
117
+ podfileContent.slice(mainTargetEndIndex)
118
+ );
119
+ }
120
+
121
+ if (logger) {
122
+ logger.log('Inserting React Native target nested inside main target');
123
+ } else {
124
+ console.log(
125
+ '[expo-targets] Inserting React Native target nested inside main target'
126
+ );
127
+ }
128
+
129
+ // React Native targets: nest inside main target before post_install
130
+ const postInstallIndex = podfileContent.indexOf('post_install do');
131
+
132
+ if (postInstallIndex !== -1) {
133
+ return (
134
+ podfileContent.slice(0, postInstallIndex) +
135
+ targetBlock +
136
+ '\n ' +
137
+ podfileContent.slice(postInstallIndex)
138
+ );
139
+ }
140
+
141
+ // Fallback: insert before the last 'end' of main target
142
+ const lastEndIndex = podfileContent.lastIndexOf('end');
143
+ return (
144
+ podfileContent.slice(0, lastEndIndex) +
145
+ targetBlock +
146
+ '\n' +
147
+ podfileContent.slice(lastEndIndex)
148
+ );
149
+ }
150
+
151
+ /**
152
+ * Check if a Podfile already contains a target block.
153
+ */
154
+ export function hasTargetBlock(
155
+ podfileContent: string,
156
+ targetName: string
157
+ ): boolean {
158
+ return podfileContent.includes(`target '${targetName}'`);
159
+ }
160
+
161
+ /**
162
+ * Remove an existing target block from a Podfile.
163
+ * Finds the target declaration and removes it along with its entire do/end block.
164
+ */
165
+ export function removeTargetBlock(
166
+ podfileContent: string,
167
+ targetName: string
168
+ ): string {
169
+ // Find the target block
170
+ const targetRegex = new RegExp(`target\\s+['"]${targetName}['"]\\s+do`, 'g');
171
+ const targetMatch = targetRegex.exec(podfileContent);
172
+
173
+ if (!targetMatch) {
174
+ return podfileContent; // Target doesn't exist, nothing to remove
175
+ }
176
+
177
+ // Find the corresponding closing 'end' by counting nested block pairs
178
+ const startIndex = targetMatch.index;
179
+ const afterDoIndex = targetMatch.index + targetMatch[0].length;
180
+ // Start at depth 1 because we're already inside the target block (after 'do')
181
+ let depth = 1;
182
+
183
+ // Match all Ruby block keywords that require 'end'
184
+ const blockRegex = /\b(do|if|unless|case|begin|class|module|def|end)\b/g;
185
+ blockRegex.lastIndex = afterDoIndex;
186
+
187
+ let match;
188
+ let targetEndIndex = -1;
189
+
190
+ while ((match = blockRegex.exec(podfileContent)) !== null) {
191
+ if (match[1] === 'end') {
192
+ depth--;
193
+ if (depth === 0) {
194
+ // Found the matching 'end' for target
195
+ targetEndIndex = match.index + match[0].length;
196
+ break;
197
+ }
198
+ } else {
199
+ // All other keywords open a block
200
+ depth++;
201
+ }
202
+ }
203
+
204
+ if (targetEndIndex === -1) {
205
+ throw new Error(`Could not find closing end for target '${targetName}'`);
206
+ }
207
+
208
+ // Remove the target block including surrounding whitespace
209
+ // Look back to remove leading whitespace/newlines
210
+ let removeStart = startIndex;
211
+ while (removeStart > 0 && /[\s\n]/.test(podfileContent[removeStart - 1])) {
212
+ removeStart--;
213
+ }
214
+
215
+ // Look forward to remove trailing newlines (but keep one newline if present)
216
+ let removeEnd = targetEndIndex;
217
+ while (
218
+ removeEnd < podfileContent.length &&
219
+ podfileContent[removeEnd] === '\n'
220
+ ) {
221
+ removeEnd++;
222
+ break; // Keep only one trailing newline
223
+ }
224
+
225
+ return podfileContent.slice(0, removeStart) + podfileContent.slice(removeEnd);
226
+ }
227
+
228
+ /**
229
+ * Ensure main app target has use_frameworks! to match extension requirements.
230
+ * CocoaPods requires host app and extensions to have matching use_frameworks! settings.
231
+ */
232
+ export function ensureMainTargetUsesFrameworks(
233
+ podfileContent: string,
234
+ mainTargetName: string
235
+ ): string {
236
+ // Check if main target already has use_frameworks!
237
+ const targetRegex = new RegExp(
238
+ `target\\s+'${mainTargetName}'\\s+do([\\s\\S]*?)(?=\\n\\s*target\\s+|post_install|$)`,
239
+ 'm'
240
+ );
241
+
242
+ const match = podfileContent.match(targetRegex);
243
+ if (!match) {
244
+ return podfileContent;
245
+ }
246
+
247
+ const targetBlock = match[1];
248
+
249
+ // Check if use_frameworks! already exists in this target
250
+ if (targetBlock.includes('use_frameworks!')) {
251
+ return podfileContent;
252
+ }
253
+
254
+ // Find insertion point: after "target 'Name' do" line
255
+ const targetLineRegex = new RegExp(
256
+ `(target\\s+'${mainTargetName}'\\s+do\\s*\\n)`,
257
+ 'm'
258
+ );
259
+
260
+ return podfileContent.replace(
261
+ targetLineRegex,
262
+ `$1 use_frameworks! :linkage => :static\n\n`
263
+ );
264
+ }
265
+
266
+ /**
267
+ * Update the Podfile platform line to match the highest deployment target.
268
+ * This ensures consistency between the main app platform and extension targets.
269
+ *
270
+ * Fixes deployment target mismatch where Podfile defaults to 15.1 but extension
271
+ * targets require a higher version (e.g., 17.0), causing linker errors.
272
+ */
273
+ export function updatePodfilePlatform(
274
+ podfileContent: string,
275
+ deploymentTarget: string
276
+ ): string {
277
+ // Match platform line with various formats:
278
+ // - platform :ios, '15.1'
279
+ // - platform :ios, podfile_properties['ios.deploymentTarget'] || '15.1'
280
+ // - platform :ios, "15.1"
281
+ const platformLineRegex =
282
+ /(platform\s+:ios\s*,\s*)(?:podfile_properties\[['"]ios\.deploymentTarget['"]\]\s*\|\|\s*)?(['"])([^'"]+)(['"])/;
283
+ const match = podfileContent.match(platformLineRegex);
284
+
285
+ if (!match) {
286
+ console.warn(
287
+ '[expo-targets] Could not find platform :ios line in Podfile to update'
288
+ );
289
+ return podfileContent;
290
+ }
291
+
292
+ const currentVersion = match[3];
293
+ const quoteChar = match[2]; // Preserve original quote style (' or ")
294
+ const currentVersionNum = parseFloat(currentVersion);
295
+ const newVersionNum = parseFloat(deploymentTarget);
296
+
297
+ // Only update if the new version is higher
298
+ if (newVersionNum > currentVersionNum) {
299
+ console.log(
300
+ `[expo-targets] Updating Podfile platform from iOS ${currentVersion} to ${deploymentTarget}`
301
+ );
302
+ // Replace the entire platform line with a simple version
303
+ // Use the same quote style as the original
304
+ return podfileContent.replace(
305
+ platformLineRegex,
306
+ `$1${quoteChar}${deploymentTarget}${quoteChar}`
307
+ );
308
+ }
309
+
310
+ return podfileContent;
311
+ }
312
+
313
+ /**
314
+ * Inject deployment target fixes into the main app's existing post_install hook.
315
+ * This ensures extension targets maintain their correct deployment targets even after
316
+ * react_native_post_install runs, which might override them.
317
+ *
318
+ * Fixes both the Pods Xcode project targets AND the xcconfig files that extensions reference.
319
+ */
320
+ export function ensureExtensionDeploymentTargets(
321
+ podfileContent: string,
322
+ extensions: { targetName: string; deploymentTarget: string }[]
323
+ ): string {
324
+ if (extensions.length === 0) {
325
+ return podfileContent;
326
+ }
327
+
328
+ // Remove any existing deployment target fix code
329
+ if (
330
+ podfileContent.includes('# [expo-targets] Fix extension deployment targets')
331
+ ) {
332
+ const fixRegex =
333
+ /\s*# \[expo-targets\] Fix extension deployment targets[\s\S]*?(?=\n\s{4}\w|\n\s{2}end)/;
334
+ podfileContent = podfileContent.replace(fixRegex, '');
335
+ }
336
+
337
+ // Generate the deployment target fix code to inject
338
+ const fixCode = `
339
+ # [expo-targets] Fix extension deployment targets
340
+ installer.pods_project.targets.each do |target|
341
+ ${extensions
342
+ .map(
343
+ (ext) => ` if target.name.include?('${ext.targetName}')
344
+ target.build_configurations.each do |config|
345
+ config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '${ext.deploymentTarget}'
346
+ # Also update the xcconfig file
347
+ xcconfig_path = config.base_configuration_reference.real_path
348
+ if xcconfig_path && File.exist?(xcconfig_path)
349
+ xcconfig_content = File.read(xcconfig_path)
350
+ # Remove existing IPHONEOS_DEPLOYMENT_TARGET if present
351
+ xcconfig_content.gsub!(/^IPHONEOS_DEPLOYMENT_TARGET = .*$/, '')
352
+ # Add deployment target
353
+ xcconfig_content += "\\nIPHONEOS_DEPLOYMENT_TARGET = ${ext.deploymentTarget}\\n"
354
+ File.write(xcconfig_path, xcconfig_content)
355
+ end
356
+ end
357
+ end`
358
+ )
359
+ .join('\n')}
360
+ end
361
+ `;
362
+
363
+ // Find the main app's post_install hook and inject our code before the closing 'end'
364
+ // Look for: post_install do |installer| ... react_native_post_install(...) ... end
365
+ const postInstallRegex =
366
+ /(post_install do \|installer\|[\s\S]*?react_native_post_install\([\s\S]*?\)[\s\S]*?)(\n\s{2}end)/;
367
+
368
+ const match = podfileContent.match(postInstallRegex);
369
+ if (match) {
370
+ // Inject our code before the closing 'end' of post_install
371
+ return podfileContent.replace(postInstallRegex, `$1${fixCode}$2`);
372
+ }
373
+
374
+ // Fallback: couldn't find the post_install hook, skip modification
375
+ console.warn(
376
+ '[expo-targets] Could not find post_install hook to inject deployment target fixes'
377
+ );
378
+ return podfileContent;
379
+ }
380
+
381
+ /**
382
+ * Ensure React Native extension targets have proper framework search paths.
383
+ * Uses START/END markers for reliable, idempotent injection.
384
+ */
385
+ export function ensureReactNativeExtensionFrameworkPaths(
386
+ podfileContent: string,
387
+ extensions: { targetName: string; deploymentTarget: string }[],
388
+ mainTargetName: string
389
+ ): string {
390
+ if (extensions.length === 0) {
391
+ return podfileContent;
392
+ }
393
+
394
+ const START_MARKER = ' # [expo-targets-start]';
395
+ const END_MARKER = ' # [expo-targets-end]';
396
+
397
+ // Remove any existing block between markers (idempotent)
398
+ const startIndex = podfileContent.indexOf(START_MARKER);
399
+ if (startIndex !== -1) {
400
+ const endIndex = podfileContent.indexOf(END_MARKER, startIndex);
401
+ if (endIndex !== -1) {
402
+ const beforeBlock = podfileContent.substring(0, startIndex);
403
+ const afterBlock = podfileContent.substring(endIndex + END_MARKER.length);
404
+ podfileContent = beforeBlock.trimEnd() + '\n' + afterBlock.trimStart();
405
+ }
406
+ }
407
+
408
+ // Generate fresh code with markers
409
+ const mainPodsTarget = `Pods-${mainTargetName}`;
410
+ const fixCode = `${START_MARKER}
411
+ # Fix React Native extension framework search paths
412
+ installer.pods_project.targets.each do |target|
413
+ ${extensions
414
+ .map(
415
+ (ext) => ` if target.name.include?('${ext.targetName}')
416
+ target.build_configurations.each do |config|
417
+ config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '${ext.deploymentTarget}'
418
+ # Copy framework search paths from main app target
419
+ main_target = installer.pods_project.targets.find { |t| t.name == '${mainPodsTarget}' }
420
+ if main_target
421
+ main_config = main_target.build_configurations.find { |c| c.name == config.name }
422
+ if main_config && main_config.build_settings['FRAMEWORK_SEARCH_PATHS']
423
+ existing_paths = config.build_settings['FRAMEWORK_SEARCH_PATHS'] || ['$(inherited)']
424
+ inherited_paths = main_config.build_settings['FRAMEWORK_SEARCH_PATHS']
425
+ # Merge inherited paths, avoiding duplicates
426
+ merged_paths = existing_paths + inherited_paths.reject { |p| existing_paths.include?(p) }
427
+ config.build_settings['FRAMEWORK_SEARCH_PATHS'] = merged_paths
428
+ end
429
+ end
430
+ # Update the xcconfig file with framework search paths and deployment target
431
+ xcconfig_path = config.base_configuration_reference.real_path
432
+ if xcconfig_path && File.exist?(xcconfig_path)
433
+ xcconfig_content = File.read(xcconfig_path)
434
+ # Remove existing IPHONEOS_DEPLOYMENT_TARGET if present
435
+ xcconfig_content.gsub!(/^IPHONEOS_DEPLOYMENT_TARGET = .*$/, '')
436
+ # Add deployment target
437
+ xcconfig_content += "\\nIPHONEOS_DEPLOYMENT_TARGET = ${ext.deploymentTarget}\\n"
438
+ # Copy framework search paths and other Swift-related settings from main app's xcconfig
439
+ if main_target
440
+ main_config = main_target.build_configurations.find { |c| c.name == config.name }
441
+ if main_config
442
+ main_xcconfig_path = main_config.base_configuration_reference.real_path
443
+ if main_xcconfig_path && File.exist?(main_xcconfig_path)
444
+ main_xcconfig_content = File.read(main_xcconfig_path)
445
+ # Copy FRAMEWORK_SEARCH_PATHS (needed for Swift imports)
446
+ framework_paths_match = main_xcconfig_content.match(/^FRAMEWORK_SEARCH_PATHS = (.+)$/m)
447
+ if framework_paths_match
448
+ xcconfig_content.gsub!(/^FRAMEWORK_SEARCH_PATHS = .*$/, '')
449
+ xcconfig_content += "\\nFRAMEWORK_SEARCH_PATHS = #{framework_paths_match[1]}\\n"
450
+ end
451
+ # Copy HEADER_SEARCH_PATHS (includes ExpoModulesCore headers)
452
+ header_paths_match = main_xcconfig_content.match(/^HEADER_SEARCH_PATHS = (.+)$/m)
453
+ if header_paths_match
454
+ xcconfig_content.gsub!(/^HEADER_SEARCH_PATHS = .*$/, '')
455
+ xcconfig_content += "\\nHEADER_SEARCH_PATHS = #{header_paths_match[1]}\\n"
456
+ end
457
+ # Copy OTHER_SWIFT_FLAGS (includes module map files for ExpoModulesCore)
458
+ swift_flags_match = main_xcconfig_content.match(/^OTHER_SWIFT_FLAGS = (.+)$/m)
459
+ if swift_flags_match
460
+ xcconfig_content.gsub!(/^OTHER_SWIFT_FLAGS = .*$/, '')
461
+ xcconfig_content += "\\nOTHER_SWIFT_FLAGS = #{swift_flags_match[1]}\\n"
462
+ end
463
+ # Copy SWIFT_INCLUDE_PATHS (Swift module search paths)
464
+ swift_include_match = main_xcconfig_content.match(/^SWIFT_INCLUDE_PATHS = (.+)$/m)
465
+ if swift_include_match
466
+ xcconfig_content.gsub!(/^SWIFT_INCLUDE_PATHS = .*$/, '')
467
+ xcconfig_content += "\\nSWIFT_INCLUDE_PATHS = #{swift_include_match[1]}\\n"
468
+ end
469
+ end
470
+ end
471
+ end
472
+ File.write(xcconfig_path, xcconfig_content)
473
+ end
474
+ end
475
+ end`
476
+ )
477
+ .join('\n')}
478
+ end
479
+ ${END_MARKER}`;
480
+
481
+ // Find where to inject (after react_native_post_install closing paren)
482
+ // Use line-by-line approach to find the closing paren properly
483
+ const lines = podfileContent.split('\n');
484
+ let reactNativeStartLine = -1;
485
+ let reactNativeEndLine = -1;
486
+ let parenDepth = 0;
487
+
488
+ for (let i = 0; i < lines.length; i++) {
489
+ const line = lines[i];
490
+ if (line.includes('react_native_post_install(')) {
491
+ reactNativeStartLine = i;
492
+ parenDepth =
493
+ (line.match(/\(/g) || []).length - (line.match(/\)/g) || []).length;
494
+ continue;
495
+ }
496
+ if (reactNativeStartLine !== -1) {
497
+ const openParens = (line.match(/\(/g) || []).length;
498
+ const closeParens = (line.match(/\)/g) || []).length;
499
+ parenDepth += openParens - closeParens;
500
+ if (parenDepth === 0 && closeParens > 0) {
501
+ reactNativeEndLine = i;
502
+ break;
503
+ }
504
+ }
505
+ }
506
+
507
+ if (reactNativeStartLine === -1 || reactNativeEndLine === -1) {
508
+ console.warn('[expo-targets] Could not find react_native_post_install');
509
+ return podfileContent;
510
+ }
511
+
512
+ // Find the position after the closing paren
513
+ const beforeLines = lines.slice(0, reactNativeEndLine + 1).join('\n');
514
+ const insertPosition = beforeLines.length;
515
+ const beforeInsert = podfileContent.substring(0, insertPosition);
516
+ const afterInsert = podfileContent.substring(insertPosition);
517
+
518
+ // Insert our code with proper newlines
519
+ // Ensure newline before our code
520
+ const needsNewlineBefore = !beforeInsert.endsWith('\n');
521
+
522
+ // Check what comes after - preserve any existing closing 'end' for post_install
523
+ const afterTrimmed = afterInsert.trimStart();
524
+ const hasPostInstallEnd = afterTrimmed.startsWith('end');
525
+
526
+ // Insert: newline + our code + newline (preserve existing post_install end if present)
527
+ return (
528
+ beforeInsert.trimEnd() +
529
+ (needsNewlineBefore ? '\n' : '') +
530
+ fixCode +
531
+ '\n' +
532
+ afterInsert.trimStart()
533
+ );
534
+ }