Skip to content

Commit

Permalink
Update ruby codegen to cleanup build folder.
Browse files Browse the repository at this point in the history
Summary:
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

Differential Revision: D38657027

fbshipit-source-id: aa9c4c7c6430c0a29aaa6cead82867f36990ad2e
  • Loading branch information
Riccardo Cipolleschi authored and facebook-github-bot committed Aug 12, 2022
1 parent 6fcfe2e commit d590118
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
58 changes: 58 additions & 0 deletions scripts/cocoapods/__tests__/codegen_utils-test.rb
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,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
Expand Down
39 changes: 39 additions & 0 deletions 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
1 change: 1 addition & 0 deletions scripts/cocoapods/codegen.rb
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
20 changes: 20 additions & 0 deletions scripts/cocoapods/codegen_utils.rb
Expand Up @@ -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
2 changes: 2 additions & 0 deletions scripts/react_native_pods.rb
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 d590118

Please sign in to comment.