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        // Fixes HEADER_SEARCH_PATHS
51        paths: ['React-Core.podspec', 'ReactCommon.podspec'],
52        replace:
53          /(Headers\/Private\/|Headers\/Public\/|_BUILD_DIR\)\/)(React-Core|React-bridging|React-hermes|hermes-engine)/g,
54        with: `$1${versionName}$2`,
55      },
56
57      // React-bridging
58      {
59        paths: 'React-bridging.podspec',
60        replace: /\bheader_mappings_dir\s*=\s*"."/,
61        with: 'header_mappings_dir    = "react/bridging"',
62      },
63
64      // React-cxxreact
65      {
66        // Fixes excluding SampleCxxModule.* files
67        paths: 'React-cxxreact.podspec',
68        replace: /\.exclude_files(\s*=\s*["'])(SampleCxxModule\.\*)(["'])/g,
69        with: `.exclude_files$1${versionName}$2$3`,
70      },
71
72      // Yoga
73      {
74        // Unprefixes inner directory for source_files
75        paths: 'Yoga.podspec',
76        replace: /\{(Yoga),(YGEnums),(YGMacros),(YGNode),(YGStyle),(YGValue)\}/g,
77        with: `{${versionName}$1,${versionName}$2,${versionName}$3,${versionName}$4,${versionName}$5,${versionName}$6}`,
78      },
79
80      // FBReactNativeSpec
81      {
82        // Remove codegen from build phase script
83        paths: 'FBReactNativeSpec.podspec',
84        replace: /\n  use_react_native_codegen!\((.|\n)+?\n  }\)\n/gm,
85        with: '',
86      },
87
88      // hermes
89      {
90        paths: 'hermes-engine.podspec',
91        replace: /\b(hermes\.xcframework)/g,
92        with: `${versionName}$1`,
93      },
94      {
95        paths: 'hermes-engine.podspec',
96        replace:
97          '  source[:http] = "https://github.com/facebook/react-native/releases/download/v#{version}/hermes-runtime-darwin-v#{version}.tar.gz"',
98        with: `\
99  if File.exist?(File.join(__dir__, "destroot"))
100    source[:path] = '.'
101  else
102    source[:http] = 'https://github.com/expo/react-native/releases/download/sdk-${versionName
103      .replace('ABI', '')
104      .replace(/_/g, '.')}/${versionName}hermes.tar.gz'
105  end`,
106      },
107      {
108        // Revert the previous podspec source transform
109        paths: 'hermes-engine.podspec',
110        replace: /(\.source\s*)= \{ :path => "." \}\n/g,
111        with: '$1= source\n',
112      },
113    ],
114  };
115}
116