1import { TransformPipeline } from '.';
2
3export function podspecTransforms(versionName: string): TransformPipeline {
4  return {
5    transforms: [
6      // Common transforms
7      {
8        // Transforms some podspec fields by adding versionName at the beginning
9        replace: /\.(name|header_dir|module_name)(\s*=\s*["'])([^"']+)(["'])/g,
10        with: `.$1$2${versionName}$3$4`,
11      },
12      {
13        // Prefixes dependencies listed in the podspecs
14        replace:
15          /(\.dependency\s+["'])(Yoga|React\-|ReactCommon|RCT|FB|hermes-engine)(?!-Folly)([^"']*["'])/g,
16        with: `$1${versionName}$2$3`,
17      },
18      {
19        // Removes source conditional, but not the `source = {}` in hermes-engine.podspec
20        replace: /source\s*\=\s*\{ [.\S\s]+?end/g,
21        with: '',
22      },
23      {
24        // Points spec source at correct directory
25        replace: /(\.source\s*\=\s*)\S+\n/g,
26        with: '$1{ :path => "." }\n',
27      },
28
29      // React-Core & ReactCommon
30      {
31        // Fixes header_subspecs for RCTBlobHeaders
32        paths: 'React-Core.podspec',
33        replace: /\{(RCTBlobManager),(RCTFileReaderModule)\}/g,
34        with: `{${versionName}$1,${versionName}$2}`,
35      },
36      {
37        // Prefixes conflicting AccessibilityResources
38        paths: 'React-Core.podspec',
39        replace: /"AccessibilityResources"/g,
40        with: `"${versionName}AccessibilityResources"`,
41      },
42      {
43        // DEFINES_MODULE for swift integration
44        // Learn more: `packages/expo-modules-autolinking/scripts/ios/cocoapods/sandbox.rb`
45        paths: 'ReactCommon.podspec',
46        replace: /("USE_HEADERMAP" => "YES",)/g,
47        with: '$1 "DEFINES_MODULE" => "YES",',
48      },
49      {
50        // DEFINES_MODULE for swift integration
51        // Learn more: `packages/expo-modules-autolinking/scripts/ios/cocoapods/sandbox.rb`
52        paths: 'React-RCTAppDelegate.podspec',
53        replace: /("CLANG_CXX_LANGUAGE_STANDARD" => "c\+\+17")/g,
54        with: '$1, "DEFINES_MODULE" => "YES",',
55      },
56      {
57        // Fixes HEADER_SEARCH_PATHS
58        paths: ['React-Core.podspec', 'ReactCommon.podspec'],
59        replace:
60          /(Headers\/Private\/|Headers\/Public\/|_BUILD_DIR\)\/)(React-Core|React-bridging|React-hermes|hermes-engine)/g,
61        with: `$1${versionName}$2`,
62      },
63      // React-cxxreact
64      {
65        // Fixes excluding SampleCxxModule.* files
66        paths: 'React-cxxreact.podspec',
67        replace: /\.exclude_files(\s*=\s*["'])(SampleCxxModule\.\*)(["'])/g,
68        with: `.exclude_files$1${versionName}$2$3`,
69      },
70      {
71        // using jsc to expose jsi.h
72        paths: 'React-jsi.podspec',
73        replace: /^(\s+Pod::Spec.new do \|s\|.*)$/gm,
74        with: '\n# using jsc to expose jsi.h\njs_engine = :jsc$1',
75      },
76      {
77        paths: 'React-jsc.podspec',
78        replace: /\b(JSCRuntime\.)/g,
79        with: `${versionName}$1`,
80      },
81      // Yoga
82      {
83        // Unprefixes inner directory for source_files
84        paths: 'Yoga.podspec',
85        replace: /\{(Yoga),(YGEnums),(YGMacros),(YGNode),(YGStyle),(YGValue)\}/g,
86        with: `{${versionName}$1,${versionName}$2,${versionName}$3,${versionName}$4,${versionName}$5,${versionName}$6}`,
87      },
88
89      // FBReactNativeSpec
90      {
91        // Remove codegen from build phase script
92        paths: 'FBReactNativeSpec.podspec',
93        replace: /\n  use_react_native_codegen!\((.|\n)+?\n  }\)\n/gm,
94        with: '',
95      },
96
97      // hermes
98      {
99        paths: 'hermes-engine.podspec',
100        replace: /\b(hermes\.xcframework)/g,
101        with: `${versionName}$1`,
102      },
103      {
104        paths: 'hermes-engine.podspec',
105        replace:
106          '  source[:http] = "https://github.com/facebook/react-native/releases/download/v#{version}/hermes-runtime-darwin-v#{version}.tar.gz"',
107        with: `\
108  if File.exist?(File.join(__dir__, "destroot"))
109    source[:path] = '.'
110  else
111    source[:http] = 'https://github.com/expo/react-native/releases/download/sdk-${versionName
112      .replace('ABI', '')
113      .replace(/_/g, '.')}/${versionName}hermes.tar.gz'
114  end`,
115      },
116      {
117        // Revert the previous podspec source transform
118        paths: 'hermes-engine.podspec',
119        replace: /(\.source\s*)= \{ :path => "." \}\n/g,
120        with: '$1= source\n',
121      },
122    ],
123  };
124}
125