Skip to content

Commit f7b35c0

Browse files
author
Riccardo Cipolleschi
committed
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: #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
1 parent 5b32348 commit f7b35c0

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
class TargetDefinitionMock
7+
attr_reader :build_type
8+
9+
def initialize(build_type)
10+
@build_type = build_type
11+
end
12+
end

scripts/cocoapods/__tests__/utils-test.rb

+53
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require_relative "./test_utils/FileMock.rb"
1414
require_relative "./test_utils/systemUtils.rb"
1515
require_relative "./test_utils/PathnameMock.rb"
16+
require_relative "./test_utils/TargetDefinitionMock.rb"
1617

1718
class UtilsTests < Test::Unit::TestCase
1819
def setup
@@ -30,6 +31,7 @@ def teardown
3031
Environment.reset()
3132
ENV['RCT_NEW_ARCH_ENABLED'] = '0'
3233
ENV['USE_HERMES'] = '1'
34+
ENV['USE_FRAMEWORKS'] = nil
3335
system_reset_commands
3436
end
3537

@@ -481,6 +483,57 @@ def test_createXcodeEnvIfMissing_whenItIsNotPresent_createsIt
481483
assert_equal(File.exist_invocation_params, ["/.xcode.env"])
482484
assert_equal($collected_commands, ["echo 'export NODE_BINARY=$(command -v node)' > /.xcode.env"])
483485
end
486+
487+
# ============================ #
488+
# Test - Detect Use Frameworks #
489+
# ============================ #
490+
def test_detectUseFrameworks_whenEnvAlreadySet_DoesNothing
491+
# Arrange
492+
ENV['USE_FRAMEWORKS'] = 'static'
493+
target_definition = TargetDefinitionMock.new('something')
494+
495+
# Act
496+
ReactNativePodsUtils.detect_use_frameworks(target_definition)
497+
498+
# Assert
499+
assert_equal(Pod::UI.collected_messages, [])
500+
end
501+
502+
def test_detectUseFrameworks_whenEnvNotSetAndNotUsed_setEnvVarToNil
503+
# Arrange
504+
target_definition = TargetDefinitionMock.new('static library')
505+
506+
# Act
507+
ReactNativePodsUtils.detect_use_frameworks(target_definition)
508+
509+
# Assert
510+
assert_equal(Pod::UI.collected_messages, ["Framework build type is static library"])
511+
assert_nil(ENV['USE_FRAMEWORKS'])
512+
end
513+
514+
def test_detectUseFrameworks_whenEnvNotSetAndStaticFrameworks_setEnvVarToStatic
515+
# Arrange
516+
target_definition = TargetDefinitionMock.new('static framework')
517+
518+
# Act
519+
ReactNativePodsUtils.detect_use_frameworks(target_definition)
520+
521+
# Assert
522+
assert_equal(Pod::UI.collected_messages, ["Framework build type is static framework"])
523+
assert_equal(ENV['USE_FRAMEWORKS'], 'static')
524+
end
525+
526+
def test_detectUseFrameworks_whenEnvNotSetAndDynamicFrameworks_setEnvVarToDynamic
527+
# Arrange
528+
target_definition = TargetDefinitionMock.new('dynamic framework')
529+
530+
# Act
531+
ReactNativePodsUtils.detect_use_frameworks(target_definition)
532+
533+
# Assert
534+
assert_equal(Pod::UI.collected_messages, ["Framework build type is dynamic framework"])
535+
assert_equal(ENV['USE_FRAMEWORKS'], 'dynamic')
536+
end
484537
end
485538

486539
def prepare_empty_user_project_mock

scripts/cocoapods/utils.rb

+22
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,26 @@ def self.create_xcode_env_if_missing
163163

164164
system("echo 'export NODE_BINARY=$(command -v node)' > #{file_path}")
165165
end
166+
167+
# It examines the target_definition property and sets the appropriate value for
168+
# ENV['USE_FRAMEWORKS'] variable.
169+
#
170+
# - parameter target_definition: The current target definition
171+
def self.detect_use_frameworks(target_definition)
172+
if ENV['USE_FRAMEWORKS'] != nil
173+
return
174+
end
175+
176+
framework_build_type = target_definition.build_type.to_s
177+
178+
Pod::UI.puts("Framework build type is #{framework_build_type}")
179+
180+
if framework_build_type === "static framework"
181+
ENV['USE_FRAMEWORKS'] = 'static'
182+
elsif framework_build_type === "dynamic framework"
183+
ENV['USE_FRAMEWORKS'] = 'dynamic'
184+
else
185+
ENV['USE_FRAMEWORKS'] = nil
186+
end
187+
end
166188
end

scripts/react_native_pods.rb

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def use_react_native! (
6161
app_path: '..',
6262
config_file_dir: '')
6363

64+
# Current target definition is provided by Cocoapods and it refers to the target
65+
# that has invoked the `use_react_native!` function.
66+
ReactNativePodsUtils.detect_use_frameworks(current_target_definition)
67+
6468
CodegenUtils.clean_up_build_folder(app_path, $CODEGEN_OUTPUT_DIR)
6569

6670
# We are relying on this flag also in third parties libraries to proper install dependencies.

0 commit comments

Comments
 (0)