From f249bee171609d39ce230a0ceada1180a9a9b6dd Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Fri, 16 Dec 2022 04:36:29 -0800 Subject: [PATCH] Automatically detect when use frameworks is used (#35636) Summary: This PR introduce an automatic way to detect whether the user sets its podfile to use frameworks. In this way, users don't have to install pods with a specific environment flag but they can rely on the standard Cocoapods usage ## Changelog [IOS][ADDED] - Automatically detect whether use_frameworks! is used Pull Request resolved: https://github.com/facebook/react-native/pull/35636 Test Plan: - CircleCI is Green - Added unit tests - Tested locally with an app Reviewed By: dmytrorykun Differential Revision: D42029355 Pulled By: cipolleschi fbshipit-source-id: 76c92133deabbda59603b043a4d542737f10f044 --- .../test_utils/TargetDefinitionMock.rb | 12 +++++ scripts/cocoapods/__tests__/utils-test.rb | 53 +++++++++++++++++++ scripts/cocoapods/utils.rb | 22 ++++++++ scripts/react_native_pods.rb | 4 ++ 4 files changed, 91 insertions(+) create mode 100644 scripts/cocoapods/__tests__/test_utils/TargetDefinitionMock.rb diff --git a/scripts/cocoapods/__tests__/test_utils/TargetDefinitionMock.rb b/scripts/cocoapods/__tests__/test_utils/TargetDefinitionMock.rb new file mode 100644 index 00000000000000..0a4ddfab6d82d4 --- /dev/null +++ b/scripts/cocoapods/__tests__/test_utils/TargetDefinitionMock.rb @@ -0,0 +1,12 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +class TargetDefinitionMock + attr_reader :build_type + + def initialize(build_type) + @build_type = build_type + end +end diff --git a/scripts/cocoapods/__tests__/utils-test.rb b/scripts/cocoapods/__tests__/utils-test.rb index 429055cf4ca28b..fd1fdc3c5a1507 100644 --- a/scripts/cocoapods/__tests__/utils-test.rb +++ b/scripts/cocoapods/__tests__/utils-test.rb @@ -13,6 +13,7 @@ require_relative "./test_utils/FileMock.rb" require_relative "./test_utils/systemUtils.rb" require_relative "./test_utils/PathnameMock.rb" +require_relative "./test_utils/TargetDefinitionMock.rb" class UtilsTests < Test::Unit::TestCase def setup @@ -30,6 +31,7 @@ def teardown Environment.reset() ENV['RCT_NEW_ARCH_ENABLED'] = '0' ENV['USE_HERMES'] = '1' + ENV['USE_FRAMEWORKS'] = nil system_reset_commands end @@ -481,6 +483,57 @@ def test_createXcodeEnvIfMissing_whenItIsNotPresent_createsIt assert_equal(File.exist_invocation_params, ["/.xcode.env"]) assert_equal($collected_commands, ["echo 'export NODE_BINARY=$(command -v node)' > /.xcode.env"]) end + + # ============================ # + # Test - Detect Use Frameworks # + # ============================ # + def test_detectUseFrameworks_whenEnvAlreadySet_DoesNothing + # Arrange + ENV['USE_FRAMEWORKS'] = 'static' + target_definition = TargetDefinitionMock.new('something') + + # Act + ReactNativePodsUtils.detect_use_frameworks(target_definition) + + # Assert + assert_equal(Pod::UI.collected_messages, []) + end + + def test_detectUseFrameworks_whenEnvNotSetAndNotUsed_setEnvVarToNil + # Arrange + target_definition = TargetDefinitionMock.new('static library') + + # Act + ReactNativePodsUtils.detect_use_frameworks(target_definition) + + # Assert + assert_equal(Pod::UI.collected_messages, ["Framework build type is static library"]) + assert_nil(ENV['USE_FRAMEWORKS']) + end + + def test_detectUseFrameworks_whenEnvNotSetAndStaticFrameworks_setEnvVarToStatic + # Arrange + target_definition = TargetDefinitionMock.new('static framework') + + # Act + ReactNativePodsUtils.detect_use_frameworks(target_definition) + + # Assert + assert_equal(Pod::UI.collected_messages, ["Framework build type is static framework"]) + assert_equal(ENV['USE_FRAMEWORKS'], 'static') + end + + def test_detectUseFrameworks_whenEnvNotSetAndDynamicFrameworks_setEnvVarToDynamic + # Arrange + target_definition = TargetDefinitionMock.new('dynamic framework') + + # Act + ReactNativePodsUtils.detect_use_frameworks(target_definition) + + # Assert + assert_equal(Pod::UI.collected_messages, ["Framework build type is dynamic framework"]) + assert_equal(ENV['USE_FRAMEWORKS'], 'dynamic') + end end def prepare_empty_user_project_mock diff --git a/scripts/cocoapods/utils.rb b/scripts/cocoapods/utils.rb index dcf8642206498d..e5cc2e3c3df7b1 100644 --- a/scripts/cocoapods/utils.rb +++ b/scripts/cocoapods/utils.rb @@ -163,4 +163,26 @@ def self.create_xcode_env_if_missing system("echo 'export NODE_BINARY=$(command -v node)' > #{file_path}") end + + # It examines the target_definition property and sets the appropriate value for + # ENV['USE_FRAMEWORKS'] variable. + # + # - parameter target_definition: The current target definition + def self.detect_use_frameworks(target_definition) + if ENV['USE_FRAMEWORKS'] != nil + return + end + + framework_build_type = target_definition.build_type.to_s + + Pod::UI.puts("Framework build type is #{framework_build_type}") + + if framework_build_type === "static framework" + ENV['USE_FRAMEWORKS'] = 'static' + elsif framework_build_type === "dynamic framework" + ENV['USE_FRAMEWORKS'] = 'dynamic' + else + ENV['USE_FRAMEWORKS'] = nil + end + end end diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 1ad9ac372fbd66..064fc32eb9db6a 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -61,6 +61,10 @@ def use_react_native! ( app_path: '..', config_file_dir: '') + # Current target definition is provided by Cocoapods and it refers to the target + # that has invoked the `use_react_native!` function. + ReactNativePodsUtils.detect_use_frameworks(current_target_definition) + CodegenUtils.clean_up_build_folder(app_path, $CODEGEN_OUTPUT_DIR) # We are relying on this flag also in third parties libraries to proper install dependencies.