Skip to content

Commit babb666

Browse files
committed
Add helper function to add dependencies (#41354)
Summary: Pull Request resolved: #41354 In order to make the infra scalable and avoid a maintenance nightmare for macOS and future platform, we are introducing this function that automate adding a dependency to a podspec and it generates the required search paths. ## Context Last week I helped macOS to work with static framework. When multiple platforms are specified, frameworks are build in two variants, the iOS and macOS one. This break all the HEADER_SEARCH_PATHS as now we have to properly specify the base folder from which the search path is generated. See also [this PR](microsoft#1967) where I manually make MacOS work with `use_framewroks!` ## Changelog: [Internal] - Add helper function to create header_search_path Reviewed By: shwanton Differential Revision: D51027343 fbshipit-source-id: 33ac4c07112eacb08067220397e38db0a19240fb
1 parent 1ff7152 commit babb666

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

packages/react-native/scripts/cocoapods/__tests__/utils-test.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative "./test_utils/TargetDefinitionMock.rb"
1717
require_relative "./test_utils/XcodeprojMock.rb"
1818
require_relative "./test_utils/XcodebuildMock.rb"
19+
require_relative "./test_utils/SpecMock.rb"
1920

2021
class UtilsTests < Test::Unit::TestCase
2122
def setup
@@ -1060,6 +1061,108 @@ def test_creatHeaderSearchPathForFrameworks_whenMultiplePlatformsAndExtraPath_cr
10601061
"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric-macOS/React_Fabric.framework/Headers/react/renderer/components/view/platform/ios",
10611062
])
10621063
end
1064+
1065+
# ===================== #
1066+
# TEST - Add Dependency #
1067+
# ===================== #
1068+
def test_addDependency_whenNoHeaderSearchPathAndNoVersion_addsThem
1069+
spec = SpecMock.new
1070+
1071+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric")
1072+
1073+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric"}])
1074+
assert_equal(spec.to_hash["pod_target_xcconfig"], {"HEADER_SEARCH_PATHS" => "\"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\""})
1075+
end
1076+
1077+
def test_addDependency_whenNoHeaderSearchPathAndVersion_addsThem
1078+
spec = SpecMock.new
1079+
1080+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1081+
1082+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}])
1083+
assert_equal(spec.to_hash["pod_target_xcconfig"], {"HEADER_SEARCH_PATHS" => "\"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\""})
1084+
end
1085+
1086+
def test_addDependency_whenHeaderSearchPathAndVersion_addsThemMaintainingTheSearchPaths
1087+
spec = SpecMock.new
1088+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1089+
1090+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1091+
1092+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}])
1093+
assert_equal(spec.to_hash["pod_target_xcconfig"], {"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/RCT-Folly\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\""})
1094+
end
1095+
1096+
def test_addDependencies_whenHeaderSearchPathAndVersion_addsThemMaintainingTheSearchPaths
1097+
spec = SpecMock.new
1098+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1099+
1100+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1101+
ReactNativePodsUtils.add_dependency(spec, "React-RCTFabric", "PODS_CONFIGURATION_BUILD_DIR", "RCTFabric", :additional_paths => [])
1102+
1103+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}, {:dependency_name => "React-RCTFabric" }])
1104+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1105+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/RCT-Folly\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric/RCTFabric.framework/Headers\""})
1106+
end
1107+
1108+
def test_addDependencies_whenHeaderSearchPathAndVersionWithAdditionalPaths_addsThemMaintainingTheSearchPaths
1109+
spec = SpecMock.new
1110+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1111+
1112+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1113+
ReactNativePodsUtils.add_dependency(spec, "React-RCTFabric", "PODS_CONFIGURATION_BUILD_DIR", "RCTFabric", :additional_paths => ["react/renderer/components/view/platform/ios"])
1114+
1115+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}, {:dependency_name => "React-RCTFabric" }])
1116+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1117+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/RCT-Folly\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric/RCTFabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric/RCTFabric.framework/Headers/react/renderer/components/view/platform/ios\""})
1118+
end
1119+
1120+
def test_addDependencies_whenHeaderSearchPathAndVersionWithAdditionalPathsAndPlatforms_addsThemMaintainingTheSearchPaths
1121+
spec = SpecMock.new
1122+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1123+
$RN_PLATFORMS = ['iOS', 'macOS']
1124+
1125+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1126+
ReactNativePodsUtils.add_dependency(spec, "React-RCTFabric", "PODS_CONFIGURATION_BUILD_DIR", "RCTFabric", :additional_paths => ["react/renderer/components/view/platform/ios"])
1127+
1128+
expected_search_paths = [
1129+
"$(PODS_ROOT)/RCT-Folly",
1130+
"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric-iOS/React_Fabric.framework/Headers",
1131+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-iOS/RCTFabric.framework/Headers",
1132+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-iOS/RCTFabric.framework/Headers/react/renderer/components/view/platform/ios",
1133+
"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric-macOS/React_Fabric.framework/Headers",
1134+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-macOS/RCTFabric.framework/Headers",
1135+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-macOS/RCTFabric.framework/Headers/react/renderer/components/view/platform/ios"
1136+
]
1137+
.map { |sp| return "\"#{sp}\"" }
1138+
.join(" ")
1139+
1140+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}, {:dependency_name => "React-RCTFabric" }])
1141+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1142+
"HEADER_SEARCH_PATHS" => expected_search_paths})
1143+
end
1144+
1145+
def test_addDependencies_whenSubspecsAndHeaderSearchPathAndVersionWithAdditionalPathsAndPlatforms_addsThemMaintainingTheSearchPaths
1146+
spec = SpecMock.new
1147+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1148+
$RN_PLATFORMS = ['iOS', 'macOS']
1149+
1150+
ReactNativePodsUtils.add_dependency(spec, "ReactCommon", "PODS_CONFIGURATION_BUILD_DIR", "ReactCommon", :additional_paths => ["react/nativemodule/core"], :subspec_dependency => 'turbomodule/core')
1151+
1152+
expected_search_paths = [
1153+
"$(PODS_ROOT)/RCT-Folly",
1154+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-iOS/ReactCommon.framework/Headers",
1155+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-iOS/ReactCommon.framework/Headers/react/nativemodule/core",
1156+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-macOS/ReactCommon.framework/Headers",
1157+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-macOS/ReactCommon.framework/Headers/react/nativemodule/core",
1158+
]
1159+
.map { |sp| return "\"#{sp}\"" }
1160+
.join(" ")
1161+
1162+
assert_equal(spec.dependencies, [{:dependency_name => "ReactCommon/turbomodule/core"}])
1163+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1164+
"HEADER_SEARCH_PATHS" => expected_search_paths})
1165+
end
10631166
end
10641167

10651168
# ===== #

packages/react-native/scripts/cocoapods/utils.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ def self.create_header_search_path_for_frameworks(base_folder, pod_name, framewo
270270
return search_paths
271271
end
272272

273+
# Add a new dependency to an existing spec, configuring also the headers search paths
274+
def self.add_dependency(spec, dependency_name, base_folder_for_frameworks, framework_name, additional_paths: [], version: nil, subspec_dependency: nil)
275+
# Update Search Path
276+
optional_current_search_path = spec.to_hash["pod_target_xcconfig"]["HEADER_SEARCH_PATHS"]
277+
current_search_paths = (optional_current_search_path != nil ? optional_current_search_path : "")
278+
.split(" ")
279+
create_header_search_path_for_frameworks(base_folder_for_frameworks, dependency_name, framework_name, additional_paths)
280+
.each { |path|
281+
wrapped_path = "\"#{path}\""
282+
current_search_paths << wrapped_path
283+
}
284+
current_pod_target_xcconfig = spec.to_hash["pod_target_xcconfig"]
285+
current_pod_target_xcconfig["HEADER_SEARCH_PATHS"] = current_search_paths.join(" ")
286+
spec.pod_target_xcconfig = current_pod_target_xcconfig
287+
288+
actual_dependency = subspec_dependency != nil ? "#{dependency_name}/#{subspec_dependency}" : dependency_name
289+
# Set Dependency
290+
if !version
291+
spec.dependency actual_dependency
292+
else
293+
spec.dependency actual_dependency, version
294+
end
295+
end
296+
273297
def self.update_search_paths(installer)
274298
return if ENV['USE_FRAMEWORKS'] == nil
275299

0 commit comments

Comments
 (0)