Skip to content

Commit

Permalink
Update ruby codegen to cleanup build folder. (#34398)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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: 8a914457d7963521d6d8dc7819eba864736f50a0
  • Loading branch information
Riccardo Cipolleschi authored and kelset committed Nov 14, 2022
1 parent 35fa47b commit 3f1c55b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
61 changes: 61 additions & 0 deletions scripts/cocoapods/__tests__/codegen_utils-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +30,7 @@ def setup
end

def teardown
FileUtils::FileUtilsStorage.reset()
Finder.reset()
Pathname.reset()
Pod::UI.reset()
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions scripts/cocoapods/codegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions scripts/cocoapods/codegen_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions scripts/react_native_pods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f1c55b

Please sign in to comment.