diff --git a/packages/react-native-brownfield/package.json b/packages/react-native-brownfield/package.json index 5c0ab637..77fe3c08 100644 --- a/packages/react-native-brownfield/package.json +++ b/packages/react-native-brownfield/package.json @@ -61,6 +61,7 @@ "files": [ "src", "lib", + "scripts", "android", "ios", "cpp", diff --git a/packages/react-native-brownfield/scripts/react_native_brownfield_post_integrate.rb b/packages/react-native-brownfield/scripts/react_native_brownfield_post_integrate.rb new file mode 100644 index 00000000..e02942ac --- /dev/null +++ b/packages/react-native-brownfield/scripts/react_native_brownfield_post_integrate.rb @@ -0,0 +1,22 @@ +def react_native_brownfield_post_integrate(installer) + projects = installer.aggregate_targets.map(&:user_project).compact.uniq + projects.each do |project| + modified = false + + project.native_targets.each do |target| + phases = target.build_phases + expo_idx = phases.index { |p| p.respond_to?(:name) && p.name == '[Expo] Configure project' } + patch_idx = phases.index { |p| p.respond_to?(:name) && p.name == 'Patch ExpoModulesProvider' } + + next if expo_idx.nil? || patch_idx.nil? + next if patch_idx > expo_idx + + patch = phases.delete_at(patch_idx) + expo_idx = phases.index { |p| p.respond_to?(:name) && p.name == '[Expo] Configure project' } + phases.insert(expo_idx + 1, patch) + modified = true + end + + project.save if modified + end +end diff --git a/packages/react-native-brownfield/src/expo-config-plugin/ios/podfileHelpers.ts b/packages/react-native-brownfield/src/expo-config-plugin/ios/podfileHelpers.ts index 5d24c729..e57ea9fa 100644 --- a/packages/react-native-brownfield/src/expo-config-plugin/ios/podfileHelpers.ts +++ b/packages/react-native-brownfield/src/expo-config-plugin/ios/podfileHelpers.ts @@ -6,44 +6,47 @@ const BROWNFIELD_POD_HOOK_MARKER_START = '# >>> react-native-brownfield expo phase ordering >>>'; const BROWNFIELD_POD_HOOK_MARKER_END = '# <<< react-native-brownfield expo phase ordering <<<'; +const BROWNFIELD_POST_INTEGRATE_REQUIRE = `require File.join(File.dirname(\`node --print "require.resolve('@callstack/react-native-brownfield/package.json')"\`), "scripts/react_native_brownfield_post_integrate")`; +const REACT_NATIVE_PODS_REQUIRE_REGEX = + /^require File\.join\(File\.dirname\(`node --print "require\.resolve\('react-native\/package\.json'\)"`\), "scripts\/react_native_pods"\)\s*$/m; -function ensureExpoPhaseOrderingHook(podfile: string): string { - if (podfile.includes(BROWNFIELD_POD_HOOK_MARKER_START)) { +function ensureBrownfieldPostIntegrateRequire(podfile: string): string { + if (podfile.includes('scripts/react_native_brownfield_post_integrate')) { return podfile; } + const reactNativePodsRequireMatch = podfile.match( + REACT_NATIVE_PODS_REQUIRE_REGEX + ); + if (reactNativePodsRequireMatch) { + const requireLine = reactNativePodsRequireMatch[0]; + return podfile.replace( + requireLine, + `${requireLine}\n${BROWNFIELD_POST_INTEGRATE_REQUIRE}\n` + ); + } + + return `${BROWNFIELD_POST_INTEGRATE_REQUIRE}\n\n${podfile}`; +} + +function ensureExpoPhaseOrderingHook(podfile: string): string { + let modifiedPodfile = ensureBrownfieldPostIntegrateRequire(podfile); + + if (modifiedPodfile.includes(BROWNFIELD_POD_HOOK_MARKER_START)) { + return modifiedPodfile; + } + const hook = ` ${BROWNFIELD_POD_HOOK_MARKER_START} -def reorder_brownfield_expo_patch_phase!(installer) - projects = installer.aggregate_targets.map(&:user_project).compact.uniq - projects.each do |project| - modified = false - - project.native_targets.each do |target| - phases = target.build_phases - expo_idx = phases.index { |p| p.respond_to?(:name) && p.name == '[Expo] Configure project' } - patch_idx = phases.index { |p| p.respond_to?(:name) && p.name == 'Patch ExpoModulesProvider' } - - next if expo_idx.nil? || patch_idx.nil? - next if patch_idx > expo_idx - - patch = phases.delete_at(patch_idx) - expo_idx = phases.index { |p| p.respond_to?(:name) && p.name == '[Expo] Configure project' } - phases.insert(expo_idx + 1, patch) - modified = true - end - - project.save if modified - end -end - post_integrate do |installer| - reorder_brownfield_expo_patch_phase!(installer) + react_native_brownfield_post_integrate(installer) end ${BROWNFIELD_POD_HOOK_MARKER_END} `; - return `${podfile.trimEnd()}\n\n${hook}\n`; + modifiedPodfile = `${modifiedPodfile.trimEnd()}\n\n${hook}\n`; + + return modifiedPodfile; } /**