diff --git a/scripts/cocoapods/__tests__/codegen_utils-test.rb b/scripts/cocoapods/__tests__/codegen_utils-test.rb index 52a64a77fa5a53..9ee5a86d06153a 100644 --- a/scripts/cocoapods/__tests__/codegen_utils-test.rb +++ b/scripts/cocoapods/__tests__/codegen_utils-test.rb @@ -13,6 +13,7 @@ require_relative "./test_utils/FinderMock.rb" require_relative "./test_utils/CodegenUtilsMock.rb" require_relative "./test_utils/CodegenScriptPhaseExtractorMock.rb" +require_relative "./test_utils/FileUtilsMock.rb" class CodegenUtilsTests < Test::Unit::TestCase :base_path @@ -29,6 +30,7 @@ def setup end def teardown + FileUtils::FileUtilsStorage.reset() Finder.reset() Pathname.reset() Pod::UI.reset() @@ -368,6 +370,62 @@ def testUseReactCodegenDiscovery_whenParametersAreGood_executeCodegen ]) end + # ============================= # + # Test - CleanUpCodegenFolder # + # ============================= # + + def testCleanUpCodegenFolder_whenCleanupDone_doNothing + # Arrange + CodegenUtils.set_cleanup_done(true) + codegen_dir = "build/generated/ios" + + # Act + CodegenUtils.clean_up_build_folder(@base_path, codegen_dir) + + # Assert + assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0) + assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, []) + end + + def testCleanUpCodegenFolder_whenFolderDoesNotExists_doNothing + # Arrange + CodegenUtils.set_cleanup_done(false) + codegen_dir = "build/generated/ios" + + # Act + CodegenUtils.clean_up_build_folder(@base_path, codegen_dir) + + # Assert + assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0) + assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, []) + assert_equal(Dir.glob_invocation, []) + end + + def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone + # Arrange + CodegenUtils.set_cleanup_done(false) + codegen_dir = "build/generated/ios" + codegen_path = "#{@base_path}/#{codegen_dir}" + globs = [ + "/MyModuleSpecs/MyModule.h", + "#{codegen_path}/MyModuleSpecs/MyModule.mm", + "#{codegen_path}/react/components/MyComponent/ShadowNode.h", + "#{codegen_path}/react/components/MyComponent/ShadowNode.mm", + ] + Dir.mocked_existing_dirs(codegen_path) + Dir.mocked_existing_globs(globs, "#{codegen_path}/*") + + # Act + CodegenUtils.clean_up_build_folder(@base_path, codegen_dir) + + # Assert + assert_equal(Dir.exist_invocation_params, [codegen_path]) + assert_equal(Dir.glob_invocation, ["#{codegen_path}/*"]) + assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 1) + assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [globs]) + + end + private def get_podspec_no_fabric_no_script diff --git a/scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb b/scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb new file mode 100644 index 00000000000000..4675fcd60cf8bb --- /dev/null +++ b/scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb @@ -0,0 +1,39 @@ +# 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. + +module FileUtils + + + class FileUtilsStorage + @@RMRF_INVOCATION_COUNT = 0 + @@RMRF_PATHS = [] + + def self.rmrf_invocation_count + return @@RMRF_INVOCATION_COUNT + end + + def self.increase_rmrfi_invocation_count + @@RMRF_INVOCATION_COUNT += 1 + end + + def self.rmrf_paths + return @@RMRF_PATHS + end + + def self.push_rmrf_path(path) + @@RMRF_PATHS.push(path) + end + + def self.reset + @@RMRF_INVOCATION_COUNT = 0 + @@RMRF_PATHS = [] + end + end + + def self.rm_rf(path) + FileUtilsStorage.push_rmrf_path(path) + FileUtilsStorage.increase_rmrfi_invocation_count + end +end diff --git a/scripts/cocoapods/codegen.rb b/scripts/cocoapods/codegen.rb index 9e144039db7c4b..fdf6a84bbbb985 100644 --- a/scripts/cocoapods/codegen.rb +++ b/scripts/cocoapods/codegen.rb @@ -79,6 +79,7 @@ def run_codegen!( folly_version: '2021.07.22.00', codegen_utils: CodegenUtils.new() ) + if new_arch_enabled codegen_utils.use_react_native_codegen_discovery!( disable_codegen, diff --git a/scripts/cocoapods/codegen_utils.rb b/scripts/cocoapods/codegen_utils.rb index 6fcbb150feba5f..7c314c85e5c1c8 100644 --- a/scripts/cocoapods/codegen_utils.rb +++ b/scripts/cocoapods/codegen_utils.rb @@ -280,4 +280,24 @@ def use_react_native_codegen_discovery!( CodegenUtils.set_react_codegen_discovery_done(true) end + + @@CLEANUP_DONE = false + + def self.set_cleanup_done(newValue) + @@CLEANUP_DONE = newValue + end + + def self.cleanup_done + return @@CLEANUP_DONE + end + + def self.clean_up_build_folder(app_path, codegen_dir) + return if CodegenUtils.cleanup_done() + + codegen_path = File.join(app_path, codegen_dir) + return if !Dir.exist?(codegen_path) + + FileUtils.rm_rf(Dir.glob("#{codegen_path}/*")) + CodegenUtils.set_cleanup_done(true) + end end diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 34dc213326c5b6..bbcc5b7dab8d82 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -43,6 +43,8 @@ def use_react_native! ( app_path: '..', config_file_dir: '') + CodegenUtils.clean_up_build_folder(app_path, $CODEGEN_OUTPUT_DIR) + prefix = path # The version of folly that must be used