Skip to content

Commit 0e316ec

Browse files
Riccardo Cipolleschifacebook-github-bot
authored andcommitted
Update ruby codegen to cleanup build folder. (#34398)
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
1 parent 06b55a3 commit 0e316ec

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

scripts/cocoapods/__tests__/codegen_utils-test.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require_relative "./test_utils/FinderMock.rb"
1414
require_relative "./test_utils/CodegenUtilsMock.rb"
1515
require_relative "./test_utils/CodegenScriptPhaseExtractorMock.rb"
16+
require_relative "./test_utils/FileUtilsMock.rb"
1617

1718
class CodegenUtilsTests < Test::Unit::TestCase
1819
:base_path
@@ -29,6 +30,7 @@ def setup
2930
end
3031

3132
def teardown
33+
FileUtils::FileUtilsStorage.reset()
3234
Finder.reset()
3335
Pathname.reset()
3436
Pod::UI.reset()
@@ -368,6 +370,65 @@ def testUseReactCodegenDiscovery_whenParametersAreGood_executeCodegen
368370
])
369371
end
370372

373+
# ============================= #
374+
# Test - CleanUpCodegenFolder #
375+
# ============================= #
376+
377+
def testCleanUpCodegenFolder_whenCleanupDone_doNothing
378+
# Arrange
379+
CodegenUtils.set_cleanup_done(true)
380+
codegen_dir = "build/generated/ios"
381+
382+
# Act
383+
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
384+
385+
# Assert
386+
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0)
387+
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [])
388+
assert_equal(CodegenUtils.cleanup_done(), true)
389+
end
390+
391+
def testCleanUpCodegenFolder_whenFolderDoesNotExists_markAsCleanupDone
392+
# Arrange
393+
CodegenUtils.set_cleanup_done(false)
394+
codegen_dir = "build/generated/ios"
395+
396+
# Act
397+
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
398+
399+
# Assert
400+
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0)
401+
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [])
402+
assert_equal(Dir.glob_invocation, [])
403+
assert_equal(CodegenUtils.cleanup_done(), true)
404+
end
405+
406+
def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone
407+
# Arrange
408+
CodegenUtils.set_cleanup_done(false)
409+
codegen_dir = "build/generated/ios"
410+
codegen_path = "#{@base_path}/#{codegen_dir}"
411+
globs = [
412+
"/MyModuleSpecs/MyModule.h",
413+
"#{codegen_path}/MyModuleSpecs/MyModule.mm",
414+
"#{codegen_path}/react/components/MyComponent/ShadowNode.h",
415+
"#{codegen_path}/react/components/MyComponent/ShadowNode.mm",
416+
]
417+
Dir.mocked_existing_dirs(codegen_path)
418+
Dir.mocked_existing_globs(globs, "#{codegen_path}/*")
419+
420+
# Act
421+
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
422+
423+
# Assert
424+
assert_equal(Dir.exist_invocation_params, [codegen_path])
425+
assert_equal(Dir.glob_invocation, ["#{codegen_path}/*"])
426+
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 1)
427+
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [globs])
428+
assert_equal(CodegenUtils.cleanup_done(), true)
429+
430+
end
431+
371432
private
372433

373434
def get_podspec_no_fabric_no_script
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
module FileUtils
7+
8+
9+
class FileUtilsStorage
10+
@@RMRF_INVOCATION_COUNT = 0
11+
@@RMRF_PATHS = []
12+
13+
def self.rmrf_invocation_count
14+
return @@RMRF_INVOCATION_COUNT
15+
end
16+
17+
def self.increase_rmrfi_invocation_count
18+
@@RMRF_INVOCATION_COUNT += 1
19+
end
20+
21+
def self.rmrf_paths
22+
return @@RMRF_PATHS
23+
end
24+
25+
def self.push_rmrf_path(path)
26+
@@RMRF_PATHS.push(path)
27+
end
28+
29+
def self.reset
30+
@@RMRF_INVOCATION_COUNT = 0
31+
@@RMRF_PATHS = []
32+
end
33+
end
34+
35+
def self.rm_rf(path)
36+
FileUtilsStorage.push_rmrf_path(path)
37+
FileUtilsStorage.increase_rmrfi_invocation_count
38+
end
39+
end

scripts/cocoapods/codegen.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def run_codegen!(
7979
folly_version: '2021.07.22.00',
8080
codegen_utils: CodegenUtils.new()
8181
)
82+
8283
if new_arch_enabled
8384
codegen_utils.use_react_native_codegen_discovery!(
8485
disable_codegen,

scripts/cocoapods/codegen_utils.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,25 @@ def use_react_native_codegen_discovery!(
280280

281281
CodegenUtils.set_react_codegen_discovery_done(true)
282282
end
283+
284+
@@CLEANUP_DONE = false
285+
286+
def self.set_cleanup_done(newValue)
287+
@@CLEANUP_DONE = newValue
288+
end
289+
290+
def self.cleanup_done
291+
return @@CLEANUP_DONE
292+
end
293+
294+
def self.clean_up_build_folder(app_path, codegen_dir)
295+
return if CodegenUtils.cleanup_done()
296+
CodegenUtils.set_cleanup_done(true)
297+
298+
codegen_path = File.join(app_path, codegen_dir)
299+
return if !Dir.exist?(codegen_path)
300+
301+
FileUtils.rm_rf(Dir.glob("#{codegen_path}/*"))
302+
CodegenUtils.set_cleanup_done(true)
303+
end
283304
end

scripts/react_native_pods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def use_react_native! (
4343
app_path: '..',
4444
config_file_dir: '')
4545

46+
CodegenUtils.clean_up_build_folder(app_path, $CODEGEN_OUTPUT_DIR)
47+
4648
prefix = path
4749

4850
# The version of folly that must be used

0 commit comments

Comments
 (0)