Skip to content

Commit af788fa

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 Differential Revision: D38657027 fbshipit-source-id: f7cd5e248d1f15d8c41dafc372d03be0a683ab8d
1 parent 6fcfe2e commit af788fa

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

scripts/cocoapods/__tests__/codegen_utils-test.rb

Lines changed: 58 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,62 @@ 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+
end
389+
390+
def testCleanUpCodegenFolder_whenFolderDoesNotExists_doNothing
391+
# Arrange
392+
CodegenUtils.set_cleanup_done(false)
393+
codegen_dir = "build/generated/ios"
394+
395+
# Act
396+
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
397+
398+
# Assert
399+
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 0)
400+
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [])
401+
assert_equal(Dir.glob_invocation, [])
402+
end
403+
404+
def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone
405+
# Arrange
406+
CodegenUtils.set_cleanup_done(false)
407+
codegen_dir = "build/generated/ios"
408+
codegen_path = "#{@base_path}/#{codegen_dir}"
409+
globs = [
410+
"/MyModuleSpecs/MyModule.h",
411+
"#{codegen_path}/MyModuleSpecs/MyModule.mm",
412+
"#{codegen_path}/react/components/MyComponent/ShadowNode.h",
413+
"#{codegen_path}/react/components/MyComponent/ShadowNode.mm",
414+
]
415+
Dir.mocked_existing_dirs(codegen_path)
416+
Dir.mocked_existing_globs(globs, "#{codegen_path}/*")
417+
418+
# Act
419+
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
420+
421+
# Assert
422+
assert_equal(Dir.exist_invocation_params, [codegen_path])
423+
assert_equal(Dir.glob_invocation, ["#{codegen_path}/*"])
424+
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 1)
425+
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [globs])
426+
427+
end
428+
371429
private
372430

373431
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: 2 additions & 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,
@@ -98,5 +99,6 @@ def run_codegen!(
9899
:fabric_enabled => fabric_enabled
99100
)
100101
codegen_utils.generate_react_codegen_podspec!(react_codegen_spec, codegen_output_dir)
102+
Pod::UI.puts "[Codegen] >>> Generated the React-Codegen"
101103
end
102104
end

scripts/cocoapods/codegen_utils.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def generate_react_codegen_podspec!(spec, codegen_output_dir)
5656
f.write(spec.to_json)
5757
f.fsync
5858
end
59-
59+
puts "[Codegen] >>> written React-Codegen.podspec.json to disk"
6060
@@REACT_CODEGEN_PODSPEC_GENERATED = true
6161
end
6262

@@ -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+
297+
codegen_path = File.join(app_path, codegen_dir)
298+
return if !Dir.exist?(codegen_path)
299+
300+
FileUtils.rm_rf(Dir.glob("#{codegen_path}/*"))
301+
CodegenUtils.set_cleanup_done(true)
302+
Pod::UI.puts "[Codegen] >>> Codegen Dir cleaned up"
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)