From e71b094b24ea5f135308b1e66c86216d9d693403 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Mon, 19 Dec 2022 04:09:55 -0800 Subject: [PATCH] Fix cleanup not working on template app (#35679) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35679 The Codegen cleanup step was not always working due to an issue with the codegen folder path. It was working for RNTester, but not for apps created from the Template. ## Changelog: [iOS][Fixed] - Fix path issue to properly run the codegen cleanup step Reviewed By: jacdebug Differential Revision: D42137600 fbshipit-source-id: ba4cb03d4c6eb17fda70a4aff383908d2e468429 --- packages/rn-tester/Podfile | 3 +- .../cocoapods/__tests__/codegen_utils-test.rb | 28 +++++++++++-------- scripts/cocoapods/codegen_utils.rb | 10 +++---- scripts/react_native_pods.rb | 7 +++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/rn-tester/Podfile b/packages/rn-tester/Podfile index 3e6f2c798d1cab..36b03d00d2cbaf 100644 --- a/packages/rn-tester/Podfile +++ b/packages/rn-tester/Podfile @@ -37,7 +37,8 @@ def pods(target_name, options = {}, use_flipper: !IN_CI && !USE_FRAMEWORKS) flipper_configuration: use_flipper ? FlipperConfiguration.enabled : FlipperConfiguration.disabled, app_path: "#{Dir.pwd}", config_file_dir: "#{Dir.pwd}/node_modules", - production: !ENV['PRODUCTION'].nil? + production: !ENV['PRODUCTION'].nil?, + ios_folder: '.', ) pod 'ReactCommon/turbomodule/samples', :path => "#{@prefix_path}/ReactCommon" diff --git a/scripts/cocoapods/__tests__/codegen_utils-test.rb b/scripts/cocoapods/__tests__/codegen_utils-test.rb index be5c2cdf7f8173..f8580d20288bfe 100644 --- a/scripts/cocoapods/__tests__/codegen_utils-test.rb +++ b/scripts/cocoapods/__tests__/codegen_utils-test.rb @@ -380,9 +380,10 @@ def testCleanUpCodegenFolder_whenCleanupDone_doNothing # Arrange CodegenUtils.set_cleanup_done(true) codegen_dir = "build/generated/ios" + ios_folder = '.' # Act - CodegenUtils.clean_up_build_folder(@base_path, codegen_dir) + CodegenUtils.clean_up_build_folder(@base_path, ios_folder, codegen_dir) # Assert assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0) @@ -394,9 +395,10 @@ def testCleanUpCodegenFolder_whenFolderDoesNotExists_markAsCleanupDone # Arrange CodegenUtils.set_cleanup_done(false) codegen_dir = "build/generated/ios" + ios_folder = '.' # Act - CodegenUtils.clean_up_build_folder(@base_path, codegen_dir) + CodegenUtils.clean_up_build_folder(@base_path, ios_folder, codegen_dir) # Assert assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0) @@ -409,7 +411,8 @@ def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone # Arrange CodegenUtils.set_cleanup_done(false) codegen_dir = "build/generated/ios" - codegen_path = "#{@base_path}/#{codegen_dir}" + ios_folder = '.' + codegen_path = "#{@base_path}/./#{codegen_dir}" globs = [ "/MyModuleSpecs/MyModule.h", "#{codegen_path}/MyModuleSpecs/MyModule.mm", @@ -420,7 +423,7 @@ def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone Dir.mocked_existing_globs(globs, "#{codegen_path}/*") # Act - CodegenUtils.clean_up_build_folder(@base_path, codegen_dir) + CodegenUtils.clean_up_build_folder(@base_path, ios_folder, codegen_dir) # Assert assert_equal(Dir.exist_invocation_params, [codegen_path, codegen_path]) @@ -437,10 +440,11 @@ def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone def test_assertCodegenFolderIsEmpty_whenItDoesNotExists_doesNotAbort # Arrange codegen_dir = "build/generated/ios" - codegen_path = "#{@base_path}/#{codegen_dir}" + codegen_path = "#{@base_path}/./#{codegen_dir}" + ios_folder = '.' # Act - CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir) + CodegenUtils.assert_codegen_folder_is_empty(@base_path, ios_folder, codegen_dir) # Assert assert_equal(Pod::UI.collected_warns, []) @@ -449,12 +453,13 @@ def test_assertCodegenFolderIsEmpty_whenItDoesNotExists_doesNotAbort def test_assertCodegenFolderIsEmpty_whenItExistsAndIsEmpty_doesNotAbort # Arrange codegen_dir = "build/generated/ios" - codegen_path = "#{@base_path}/#{codegen_dir}" + codegen_path = "#{@base_path}/./#{codegen_dir}" + ios_folder = '.' Dir.mocked_existing_dirs(codegen_path) Dir.mocked_existing_globs([], "#{codegen_path}/*") # Act - CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir) + CodegenUtils.assert_codegen_folder_is_empty(@base_path, ios_folder, codegen_dir) # Assert assert_equal(Pod::UI.collected_warns, []) @@ -463,18 +468,19 @@ def test_assertCodegenFolderIsEmpty_whenItExistsAndIsEmpty_doesNotAbort def test_assertCodegenFolderIsEmpty_whenItIsNotEmpty_itAborts # Arrange codegen_dir = "build/generated/ios" - codegen_path = "#{@base_path}/#{codegen_dir}" + codegen_path = "#{@base_path}/./#{codegen_dir}" + ios_folder = '.' Dir.mocked_existing_dirs(codegen_path) Dir.mocked_existing_globs(["#{codegen_path}/MyModuleSpecs/MyModule.mm",], "#{codegen_path}/*") # Act assert_raises() { - CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir) + CodegenUtils.assert_codegen_folder_is_empty(@base_path, ios_folder, codegen_dir) } # Assert assert_equal(Pod::UI.collected_warns, [ - "Unable to remove the content of ~/app/ios/build/generated/ios folder. Please run rm -rf ~/app/ios/build/generated/ios and try again." + "Unable to remove the content of ~/app/ios/./build/generated/ios folder. Please run rm -rf ~/app/ios/./build/generated/ios and try again." ]) end diff --git a/scripts/cocoapods/codegen_utils.rb b/scripts/cocoapods/codegen_utils.rb index c4aad591a8e490..8b5aac5ef140b3 100644 --- a/scripts/cocoapods/codegen_utils.rb +++ b/scripts/cocoapods/codegen_utils.rb @@ -306,22 +306,22 @@ def self.cleanup_done return @@CLEANUP_DONE end - def self.clean_up_build_folder(app_path, codegen_dir) + def self.clean_up_build_folder(app_path, ios_folder, codegen_dir) return if CodegenUtils.cleanup_done() CodegenUtils.set_cleanup_done(true) - codegen_path = File.join(app_path, codegen_dir) + codegen_path = File.join(app_path, ios_folder, codegen_dir) return if !Dir.exist?(codegen_path) FileUtils.rm_rf(Dir.glob("#{codegen_path}/*")) - CodegenUtils.assert_codegen_folder_is_empty(app_path, codegen_dir) + CodegenUtils.assert_codegen_folder_is_empty(app_path, ios_folder, codegen_dir) end # Need to split this function from the previous one to be able to test it properly. - def self.assert_codegen_folder_is_empty(app_path, codegen_dir) + def self.assert_codegen_folder_is_empty(app_path, ios_folder, codegen_dir) # double check that the files have actually been deleted. # Emit an error message if not. - codegen_path = File.join(app_path, codegen_dir) + codegen_path = File.join(app_path, ios_folder, codegen_dir) if Dir.exist?(codegen_path) && Dir.glob("#{codegen_path}/*").length() != 0 Pod::UI.warn "Unable to remove the content of #{codegen_path} folder. Please run rm -rf #{codegen_path} and try again." abort diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 064fc32eb9db6a..82d48cef76cd48 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -51,6 +51,7 @@ def prepare_react_native_project! # - flipper_configuration: The configuration to use for flipper. # - app_path: path to the React Native app. Required by the New Architecture. # - config_file_dir: directory of the `package.json` file, required by the New Architecture. +# - ios_folder: the folder where the iOS code base lives. For a template app, it is `ios`, the default. For RNTester, it is `.`. def use_react_native! ( path: "../node_modules/react-native", fabric_enabled: false, @@ -59,13 +60,15 @@ def use_react_native! ( hermes_enabled: ENV['USE_HERMES'] && ENV['USE_HERMES'] == '0' ? false : true, flipper_configuration: FlipperConfiguration.disabled, app_path: '..', - config_file_dir: '') + config_file_dir: '', + ios_folder: 'ios' +) # 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) + CodegenUtils.clean_up_build_folder(app_path, ios_folder, $CODEGEN_OUTPUT_DIR) # We are relying on this flag also in third parties libraries to proper install dependencies. # Better to rely and enable this environment flag if the new architecture is turned on using flags.