Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ruby codegen to cleanup build folder. #34398

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 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,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
@@ -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
21 changes: 21 additions & 0 deletions scripts/cocoapods/codegen_utils.rb
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
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