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: [
59          'React-Core.podspec',
60          'ReactCommon.podspec',
61          'React-NativeModulesApple.podspec',
62          'React-RCTAppDelegate.podspec',
63        ],
64        replace:
65          /(Headers\/Private\/|Headers\/Public\/|_BUILD_DIR\)\/)(React-Core|React-bridging|React-hermes|hermes-engine|ReactCommon|React-RCTFabric)/g,
66        with: `$1${versionName}$2`,
67      },
68      // React-cxxreact
69      {
70        // Fixes excluding SampleCxxModule.* files
71        paths: 'React-cxxreact.podspec',
72        replace: /\.exclude_files(\s*=\s*["'])(SampleCxxModule\.\*)(["'])/g,
73        with: `.exclude_files$1${versionName}$2$3`,
74      },
75      {
76        // using jsc to expose jsi.h
77        paths: 'React-jsi.podspec',
78        replace: /^(\s+Pod::Spec.new do \|s\|.*)$/gm,
79        with: '\n# using jsc to expose jsi.h\njs_engine = :jsc$1',
80      },
81      {
82        paths: 'React-jsc.podspec',
83        replace: /\b(JSCRuntime\.)/g,
84        with: `${versionName}$1`,
85      },
86      // Yoga
87      {
88        // Unprefixes inner directory for source_files
89        paths: 'Yoga.podspec',
90        replace: /\{(Yoga),(YGEnums),(YGMacros),(YGNode),(YGStyle),(YGValue)\}/g,
91        with: `{${versionName}$1,${versionName}$2,${versionName}$3,${versionName}$4,${versionName}$5,${versionName}$6}`,
92      },
93
94      // Remove codegen from build phase script
95      {
96        paths: ['FBReactNativeSpec.podspec', 'React-rncore.podspec'],
97        replace: /\n  use_react_native_codegen!\((.|\n)+?\n  }\)\n/gm,
98        with: '',
99      },
100
101      // hermes
102      {
103        paths: 'hermes-engine.podspec',
104        replace: /\b(hermes\.xcframework)/g,
105        with: `${versionName}$1`,
106      },
107      {
108        paths: 'hermes-engine.podspec',
109        replace: /  source\[:http\]\s*=\s*"http[^"]+"/,
110        with: `\
111  if File.exist?(File.join(__dir__, "destroot"))
112    source[:path] = '.'
113  else
114    source[:http] = 'https://github.com/expo/react-native/releases/download/sdk-${versionName
115      .replace('ABI', '')
116      .replace(/_/g, '.')}/${versionName}hermes.tar.gz'
117  end`,
118      },
119      {
120        // Revert the previous podspec source transform
121        paths: 'hermes-engine.podspec',
122        replace: /(\.source\s*)= \{ :path => "." \}\n/g,
123        with: '$1= source\n',
124      },
125    ],
126  };
127}
128