Skip to content

Commit a5139df

Browse files
Riccardo Cipolleschifacebook-github-bot
Riccardo Cipolleschi
authored andcommitted
Add helper function to add dependencies (#41354)
Summary: 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
1 parent f40c57a commit a5139df

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
@@ -15,6 +15,7 @@
1515
require_relative "./test_utils/TargetDefinitionMock.rb"
1616
require_relative "./test_utils/XcodeprojMock.rb"
1717
require_relative "./test_utils/XcodebuildMock.rb"
18+
require_relative "./test_utils/SpecMock.rb"
1819

1920
class UtilsTests < Test::Unit::TestCase
2021
def setup
@@ -967,6 +968,108 @@ def test_creatHeaderSearchPathForFrameworks_whenMultiplePlatformsAndExtraPath_cr
967968
"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric-macOS/React_Fabric.framework/Headers/react/renderer/components/view/platform/ios",
968969
])
969970
end
971+
972+
# ===================== #
973+
# TEST - Add Dependency #
974+
# ===================== #
975+
def test_addDependency_whenNoHeaderSearchPathAndNoVersion_addsThem
976+
spec = SpecMock.new
977+
978+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric")
979+
980+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric"}])
981+
assert_equal(spec.to_hash["pod_target_xcconfig"], {"HEADER_SEARCH_PATHS" => "\"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\""})
982+
end
983+
984+
def test_addDependency_whenNoHeaderSearchPathAndVersion_addsThem
985+
spec = SpecMock.new
986+
987+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
988+
989+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}])
990+
assert_equal(spec.to_hash["pod_target_xcconfig"], {"HEADER_SEARCH_PATHS" => "\"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\""})
991+
end
992+
993+
def test_addDependency_whenHeaderSearchPathAndVersion_addsThemMaintainingTheSearchPaths
994+
spec = SpecMock.new
995+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
996+
997+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
998+
999+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}])
1000+
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\""})
1001+
end
1002+
1003+
def test_addDependencies_whenHeaderSearchPathAndVersion_addsThemMaintainingTheSearchPaths
1004+
spec = SpecMock.new
1005+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1006+
1007+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1008+
ReactNativePodsUtils.add_dependency(spec, "React-RCTFabric", "PODS_CONFIGURATION_BUILD_DIR", "RCTFabric", :additional_paths => [])
1009+
1010+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}, {:dependency_name => "React-RCTFabric" }])
1011+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1012+
"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\""})
1013+
end
1014+
1015+
def test_addDependencies_whenHeaderSearchPathAndVersionWithAdditionalPaths_addsThemMaintainingTheSearchPaths
1016+
spec = SpecMock.new
1017+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1018+
1019+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1020+
ReactNativePodsUtils.add_dependency(spec, "React-RCTFabric", "PODS_CONFIGURATION_BUILD_DIR", "RCTFabric", :additional_paths => ["react/renderer/components/view/platform/ios"])
1021+
1022+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}, {:dependency_name => "React-RCTFabric" }])
1023+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1024+
"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\""})
1025+
end
1026+
1027+
def test_addDependencies_whenHeaderSearchPathAndVersionWithAdditionalPathsAndPlatforms_addsThemMaintainingTheSearchPaths
1028+
spec = SpecMock.new
1029+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1030+
$RN_PLATFORMS = ['iOS', 'macOS']
1031+
1032+
ReactNativePodsUtils.add_dependency(spec, "React-Fabric", "PODS_CONFIGURATION_BUILD_DIR", "React_Fabric", :additional_paths => [], :version => '1000.0.0')
1033+
ReactNativePodsUtils.add_dependency(spec, "React-RCTFabric", "PODS_CONFIGURATION_BUILD_DIR", "RCTFabric", :additional_paths => ["react/renderer/components/view/platform/ios"])
1034+
1035+
expected_search_paths = [
1036+
"$(PODS_ROOT)/RCT-Folly",
1037+
"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric-iOS/React_Fabric.framework/Headers",
1038+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-iOS/RCTFabric.framework/Headers",
1039+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-iOS/RCTFabric.framework/Headers/react/renderer/components/view/platform/ios",
1040+
"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric-macOS/React_Fabric.framework/Headers",
1041+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-macOS/RCTFabric.framework/Headers",
1042+
"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric-macOS/RCTFabric.framework/Headers/react/renderer/components/view/platform/ios"
1043+
]
1044+
.map { |sp| return "\"#{sp}\"" }
1045+
.join(" ")
1046+
1047+
assert_equal(spec.dependencies, [{:dependency_name => "React-Fabric", "version" => '1000.0.0'}, {:dependency_name => "React-RCTFabric" }])
1048+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1049+
"HEADER_SEARCH_PATHS" => expected_search_paths})
1050+
end
1051+
1052+
def test_addDependencies_whenSubspecsAndHeaderSearchPathAndVersionWithAdditionalPathsAndPlatforms_addsThemMaintainingTheSearchPaths
1053+
spec = SpecMock.new
1054+
spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"] = "\"$(PODS_ROOT)/RCT-Folly\""
1055+
$RN_PLATFORMS = ['iOS', 'macOS']
1056+
1057+
ReactNativePodsUtils.add_dependency(spec, "ReactCommon", "PODS_CONFIGURATION_BUILD_DIR", "ReactCommon", :additional_paths => ["react/nativemodule/core"], :subspec_dependency => 'turbomodule/core')
1058+
1059+
expected_search_paths = [
1060+
"$(PODS_ROOT)/RCT-Folly",
1061+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-iOS/ReactCommon.framework/Headers",
1062+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-iOS/ReactCommon.framework/Headers/react/nativemodule/core",
1063+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-macOS/ReactCommon.framework/Headers",
1064+
"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon-macOS/ReactCommon.framework/Headers/react/nativemodule/core",
1065+
]
1066+
.map { |sp| return "\"#{sp}\"" }
1067+
.join(" ")
1068+
1069+
assert_equal(spec.dependencies, [{:dependency_name => "ReactCommon/turbomodule/core"}])
1070+
assert_equal(spec.to_hash["pod_target_xcconfig"], {
1071+
"HEADER_SEARCH_PATHS" => expected_search_paths})
1072+
end
9701073
end
9711074

9721075
# ===== #

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,30 @@ def self.create_header_search_path_for_frameworks(base_folder, pod_name, framewo
244244
return search_paths
245245
end
246246

247+
# Add a new dependency to an existing spec, configuring also the headers search paths
248+
def self.add_dependency(spec, dependency_name, base_folder_for_frameworks, framework_name, additional_paths: [], version: nil, subspec_dependency: nil)
249+
# Update Search Path
250+
optional_current_search_path = spec.to_hash["pod_target_xcconfig"]["HEADER_SEARCH_PATHS"]
251+
current_search_paths = (optional_current_search_path != nil ? optional_current_search_path : "")
252+
.split(" ")
253+
create_header_search_path_for_frameworks(base_folder_for_frameworks, dependency_name, framework_name, additional_paths)
254+
.each { |path|
255+
wrapped_path = "\"#{path}\""
256+
current_search_paths << wrapped_path
257+
}
258+
current_pod_target_xcconfig = spec.to_hash["pod_target_xcconfig"]
259+
current_pod_target_xcconfig["HEADER_SEARCH_PATHS"] = current_search_paths.join(" ")
260+
spec.pod_target_xcconfig = current_pod_target_xcconfig
261+
262+
actual_dependency = subspec_dependency != nil ? "#{dependency_name}/#{subspec_dependency}" : dependency_name
263+
# Set Dependency
264+
if !version
265+
spec.dependency actual_dependency
266+
else
267+
spec.dependency actual_dependency, version
268+
end
269+
end
270+
247271
def self.update_search_paths(installer)
248272
return if ENV['USE_FRAMEWORKS'] == nil
249273

0 commit comments

Comments
 (0)