Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RN][iOS] Automatically detect when use frameworks is used #35636

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add all the references!

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)
cipolleschi marked this conversation as resolved.
Show resolved Hide resolved

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