From 0be0ae40e76f1c11c15742bfd581a78b3d0f1403 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Sat, 13 Aug 2022 03:19:06 -0700 Subject: [PATCH] Update ruby codegen to cleanup build folder. (#34398) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/34398 This Diff cleans up the codegen folder for iOS when we install the pods. This is useful to start from a clean situation. The codegen runs after this step and we make sure that the file system is clean ## Changelog [iOS][Changed] - Cleanup codegen build folder before installing the pods Reviewed By: cortinico Differential Revision: D38657027 fbshipit-source-id: c9f558a3b508030062873ee7030feebb9ac05d5f --- .../cocoapods/__tests__/codegen_utils-test.rb | 61 +++++++++++++++++++ .../__tests__/test_utils/FileUtilsMock.rb | 39 ++++++++++++ scripts/cocoapods/codegen.rb | 1 + scripts/cocoapods/codegen_utils.rb | 21 +++++++ scripts/react_native_pods.rb | 2 + 5 files changed, 124 insertions(+) create mode 100644 scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb diff --git a/scripts/cocoapods/__tests__/codegen_utils-test.rb b/scripts/cocoapods/__tests__/codegen_utils-test.rb index 52a64a77fa5a53..cdceb5d8510d8e 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,65 @@ 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, []) + assert_equal(CodegenUtils.cleanup_done(), true) + end + + def testCleanUpCodegenFolder_whenFolderDoesNotExists_markAsCleanupDone + # 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, []) + assert_equal(CodegenUtils.cleanup_done(), true) + 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]) + assert_equal(CodegenUtils.cleanup_done(), true) + + 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..406e9936d236f8 100644 --- a/scripts/cocoapods/codegen_utils.rb +++ b/scripts/cocoapods/codegen_utils.rb @@ -280,4 +280,25 @@ 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() + CodegenUtils.set_cleanup_done(true) + + 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