Skip to content

Commit

Permalink
Automatically detect when use frameworks is used (#35636)
Browse files Browse the repository at this point in the history
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: #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
  • Loading branch information
Riccardo Cipolleschi committed Dec 19, 2022
1 parent 5b32348 commit f7b35c0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions scripts/cocoapods/__tests__/test_utils/TargetDefinitionMock.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions scripts/cocoapods/__tests__/utils-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions scripts/cocoapods/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions scripts/react_native_pods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f7b35c0

Please sign in to comment.