From 51a1171f4f5b979bac647bcc9f12079b96067620 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 9 Nov 2023 20:27:54 +0100 Subject: [PATCH 1/4] Fix `build_codegen!` not finding `@react-native/codegen` in pnpm setups --- packages/react-native/scripts/cocoapods/codegen.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/codegen.rb b/packages/react-native/scripts/cocoapods/codegen.rb index 7c6af06e1552..ef07f2d73e2b 100644 --- a/packages/react-native/scripts/cocoapods/codegen.rb +++ b/packages/react-native/scripts/cocoapods/codegen.rb @@ -11,16 +11,20 @@ # - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes. # @throws an error if it could not find the codegen folder. def build_codegen!(react_native_path, relative_installation_root, dir_manager: Dir) - codegen_repo_path = "#{basePath(react_native_path, relative_installation_root)}/../react-native-codegen"; - codegen_npm_path = "#{basePath(react_native_path, relative_installation_root)}/../@react-native/codegen"; + react_native_relpath = "#{basePath(react_native_path, relative_installation_root)}" + codegen_repo_path = "#{react_native_relpath}/../react-native-codegen"; + codegen_npm_path = "#{react_native_relpath}/../@react-native/codegen"; + codegen_pnpm_path = "#{react_native_relpath}/node_modules/@react-native/codegen"; codegen_cli_path = "" if dir_manager.exist?(codegen_repo_path) codegen_cli_path = codegen_repo_path elsif dir_manager.exist?(codegen_npm_path) codegen_cli_path = codegen_npm_path + elsif dir_manager.exist?(codegen_pnpm_path) + codegen_cli_path = codegen_pnpm_path else - raise "[codegen] Could not find react-native-codegen." + raise "[codegen] Could not find react-native-codegen" end if !dir_manager.exist?("#{codegen_cli_path}/lib") From 4cd1bd38e3c22efe530d0a2a732d1d8aec0d147a Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Fri, 10 Nov 2023 08:34:56 +0100 Subject: [PATCH 2/4] update test --- .../react-native/scripts/cocoapods/__tests__/codegen-test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb b/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb index 8d472aea862b..1b5c01bd52bf 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb @@ -89,6 +89,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissi assert_equal(DirMock.exist_invocation_params, [ @base_path + "/"+ @prefix + "/../react-native-codegen", @base_path + "/"+ @prefix + "/../@react-native/codegen", + @base_path + "/"+ @prefix + "/node_modules/@react-native/codegen", ]) assert_equal(Pod::UI.collected_messages, []) assert_equal($collected_commands, []) From 353bc4c5dc575f5200ef7a298bed48b15252a8f5 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Fri, 10 Nov 2023 14:55:23 +0100 Subject: [PATCH 3/4] use `node` --- .../cocoapods/__tests__/codegen-test.rb | 31 ++++++++-------- .../react-native/scripts/cocoapods/codegen.rb | 35 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb b/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb index 1b5c01bd52bf..217572b0e42b 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb @@ -86,15 +86,18 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissi assert_equal(FileMock.exist_invocation_params, [ @base_path + "/" + @prefix + "/React/Fabric/" + @third_party_provider_header ]) - assert_equal(DirMock.exist_invocation_params, [ - @base_path + "/"+ @prefix + "/../react-native-codegen", - @base_path + "/"+ @prefix + "/../@react-native/codegen", - @base_path + "/"+ @prefix + "/node_modules/@react-native/codegen", - ]) assert_equal(Pod::UI.collected_messages, []) assert_equal($collected_commands, []) assert_equal(FileMock.open_files.length, 0) - assert_equal(Pod::Executable.executed_commands.length, 0) + assert_equal(Pod::Executable.executed_commands, [ + { + "arguments"=> [ + "-p", + "require.resolve('@react-native/codegen/package.json', {paths: ['#{@base_path + "/"+ @prefix}']})" + ], + "command"=>"node" + } + ]) end def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCodegenrepoExists_dontBuildCodegen() @@ -131,7 +134,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCode assert_equal(FileMock.open_files_with_mode[@prefix + "/React/Fabric/tmpSchemaList.txt"], nil) assert_equal(FileMock.open_files[0].collected_write, ["[]"]) assert_equal(FileMock.open_files[0].fsync_invocation_count, 1) - assert_equal(Pod::Executable.executed_commands[0], { + assert_equal(Pod::Executable.executed_commands[1], { "command" => "node", "arguments" => [ @base_path + "/" + @prefix + "/scripts/generate-provider-cli.js", @@ -146,7 +149,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCode def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen() # Arrange - codegen_cli_path = @base_path + "/" + @prefix + "/../@react-native/codegen" + codegen_cli_path = @base_path + "/" + @prefix + "/../react-native-codegen" DirMock.mocked_existing_dirs([ codegen_cli_path, ]) @@ -161,7 +164,6 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen() @base_path + "/" + @prefix + "/React/Fabric/" + @tmp_schema_list_file ]) assert_equal(DirMock.exist_invocation_params, [ - @base_path + "/" + @prefix + "/../react-native-codegen", codegen_cli_path, codegen_cli_path + "/lib", ]) @@ -169,10 +171,10 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen() "[Codegen] building #{codegen_cli_path}.", "[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider" ]) - assert_equal($collected_commands, ["~/app/ios/../../../@react-native/codegen/scripts/oss/build.sh"]) + assert_equal($collected_commands, ["~/app/ios/../../../react-native-codegen/scripts/oss/build.sh"]) assert_equal(FileMock.open_files[0].collected_write, ["[]"]) assert_equal(FileMock.open_files[0].fsync_invocation_count, 1) - assert_equal(Pod::Executable.executed_commands[0], { + assert_equal(Pod::Executable.executed_commands[1], { "command" => "node", "arguments" => [ @base_path + "/" + @prefix + "/scripts/generate-provider-cli.js", @@ -186,7 +188,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen() def testCheckAndGenerateEmptyThirdPartyProvider_withAbsoluteReactNativePath_buildCodegen() # Arrange rn_path = 'packages/react-native' - codegen_cli_path = rn_path + "/../@react-native/codegen" + codegen_cli_path = rn_path + "/../react-native-codegen" DirMock.mocked_existing_dirs([ @base_path + "/" + codegen_cli_path, ]) @@ -202,7 +204,6 @@ def testCheckAndGenerateEmptyThirdPartyProvider_withAbsoluteReactNativePath_buil @base_path + "/" + rn_path + "/React/Fabric/" + @tmp_schema_list_file ]) assert_equal(DirMock.exist_invocation_params, [ - @base_path + "/" + rn_path + "/../react-native-codegen", @base_path + "/" + codegen_cli_path, @base_path + "/" + codegen_cli_path + "/lib", ]) @@ -210,10 +211,10 @@ def testCheckAndGenerateEmptyThirdPartyProvider_withAbsoluteReactNativePath_buil "[Codegen] building #{@base_path + "/" + codegen_cli_path}.", "[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider" ]) - assert_equal($collected_commands, [@base_path + "/" + rn_path + "/../@react-native/codegen/scripts/oss/build.sh"]) + assert_equal($collected_commands, [@base_path + "/" + rn_path + "/../react-native-codegen/scripts/oss/build.sh"]) assert_equal(FileMock.open_files[0].collected_write, ["[]"]) assert_equal(FileMock.open_files[0].fsync_invocation_count, 1) - assert_equal(Pod::Executable.executed_commands[0], { + assert_equal(Pod::Executable.executed_commands[1], { "command" => "node", "arguments" => [ @base_path + "/" + rn_path + "/scripts/generate-provider-cli.js", diff --git a/packages/react-native/scripts/cocoapods/codegen.rb b/packages/react-native/scripts/cocoapods/codegen.rb index ef07f2d73e2b..7d0bbc1aebf4 100644 --- a/packages/react-native/scripts/cocoapods/codegen.rb +++ b/packages/react-native/scripts/cocoapods/codegen.rb @@ -11,27 +11,26 @@ # - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes. # @throws an error if it could not find the codegen folder. def build_codegen!(react_native_path, relative_installation_root, dir_manager: Dir) - react_native_relpath = "#{basePath(react_native_path, relative_installation_root)}" - codegen_repo_path = "#{react_native_relpath}/../react-native-codegen"; - codegen_npm_path = "#{react_native_relpath}/../@react-native/codegen"; - codegen_pnpm_path = "#{react_native_relpath}/node_modules/@react-native/codegen"; - codegen_cli_path = "" + react_native_relpath = "#{relative_installation_root}/#{react_native_path}" + codegen_json_path = Pod::Executable.execute_command('node', [ + '-p', + "require.resolve('@react-native/codegen/package.json', {paths: ['#{react_native_relpath}']})" + ]) + if codegen_json_path.is_a? String + codegen_json_path = codegen_json_path.strip + codegen_path = Pathname.new(codegen_json_path).parent.relative_path_from(Pathname.pwd) + else + # This should only happen under testing + codegen_path = "#{react_native_relpath}/../react-native-codegen" + end - if dir_manager.exist?(codegen_repo_path) - codegen_cli_path = codegen_repo_path - elsif dir_manager.exist?(codegen_npm_path) - codegen_cli_path = codegen_npm_path - elsif dir_manager.exist?(codegen_pnpm_path) - codegen_cli_path = codegen_pnpm_path - else - raise "[codegen] Could not find react-native-codegen" - end + raise "[Codegen] Could not find react-native-codegen" unless dir_manager.exist?(codegen_path) - if !dir_manager.exist?("#{codegen_cli_path}/lib") - Pod::UI.puts "[Codegen] building #{codegen_cli_path}." - system("#{codegen_cli_path}/scripts/oss/build.sh") - end + if !dir_manager.exist?("#{codegen_path}/lib") + Pod::UI.puts "[Codegen] building #{codegen_path}." + system("#{codegen_path}/scripts/oss/build.sh") end +end # It generates an empty `ThirdPartyProvider`, required by Fabric to load the components # From c976db236ee0d308435243e2f4115a77eb57b631 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Mon, 13 Nov 2023 11:19:16 +0100 Subject: [PATCH 4/4] skip build unless in repo --- .../cocoapods/__tests__/codegen-test.rb | 40 +++++++++---------- .../react-native/scripts/cocoapods/codegen.rb | 22 ++-------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb b/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb index 217572b0e42b..6987399ad87f 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb @@ -68,7 +68,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenFileAlreadyExists_doNothing( assert_equal(Pod::Executable.executed_commands.length, 0) end - def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissing_raiseError() + def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissing_dontBuildCodegen() # Arrange FileMock.mocked_existing_files([ @@ -76,7 +76,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissi ]) # Act - assert_raise { + assert_nothing_raised { checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, dir_manager: DirMock, file_manager: FileMock) } @@ -84,20 +84,18 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissi assert_equal(Pathname.pwd_invocation_count, 1) assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1) assert_equal(FileMock.exist_invocation_params, [ - @base_path + "/" + @prefix + "/React/Fabric/" + @third_party_provider_header + @base_path + "/" + @prefix + "/React/Fabric/" + @third_party_provider_header, + @base_path + "/" + @prefix + "/React/Fabric/tmpSchemaList.txt", ]) - assert_equal(Pod::UI.collected_messages, []) - assert_equal($collected_commands, []) - assert_equal(FileMock.open_files.length, 0) - assert_equal(Pod::Executable.executed_commands, [ - { - "arguments"=> [ - "-p", - "require.resolve('@react-native/codegen/package.json', {paths: ['#{@base_path + "/"+ @prefix}']})" - ], - "command"=>"node" - } + assert_equal(DirMock.exist_invocation_params, [ + @base_path + "/"+ @prefix + "/../react-native-codegen", + ]) + assert_equal(Pod::UI.collected_messages, [ + "[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider", ]) + assert_equal($collected_commands, []) + assert_equal(FileMock.open_files.length, 1) + assert_equal(Pod::Executable.executed_commands.length, 1) end def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCodegenrepoExists_dontBuildCodegen() @@ -134,7 +132,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCode assert_equal(FileMock.open_files_with_mode[@prefix + "/React/Fabric/tmpSchemaList.txt"], nil) assert_equal(FileMock.open_files[0].collected_write, ["[]"]) assert_equal(FileMock.open_files[0].fsync_invocation_count, 1) - assert_equal(Pod::Executable.executed_commands[1], { + assert_equal(Pod::Executable.executed_commands[0], { "command" => "node", "arguments" => [ @base_path + "/" + @prefix + "/scripts/generate-provider-cli.js", @@ -168,13 +166,13 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen() codegen_cli_path + "/lib", ]) assert_equal(Pod::UI.collected_messages, [ - "[Codegen] building #{codegen_cli_path}.", + "[Codegen] building #{codegen_cli_path}", "[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider" ]) assert_equal($collected_commands, ["~/app/ios/../../../react-native-codegen/scripts/oss/build.sh"]) assert_equal(FileMock.open_files[0].collected_write, ["[]"]) assert_equal(FileMock.open_files[0].fsync_invocation_count, 1) - assert_equal(Pod::Executable.executed_commands[1], { + assert_equal(Pod::Executable.executed_commands[0], { "command" => "node", "arguments" => [ @base_path + "/" + @prefix + "/scripts/generate-provider-cli.js", @@ -208,13 +206,15 @@ def testCheckAndGenerateEmptyThirdPartyProvider_withAbsoluteReactNativePath_buil @base_path + "/" + codegen_cli_path + "/lib", ]) assert_equal(Pod::UI.collected_messages, [ - "[Codegen] building #{@base_path + "/" + codegen_cli_path}.", + "[Codegen] building #{@base_path + "/" + codegen_cli_path}", "[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider" ]) - assert_equal($collected_commands, [@base_path + "/" + rn_path + "/../react-native-codegen/scripts/oss/build.sh"]) + assert_equal($collected_commands, [ + @base_path + "/" + rn_path + "/../react-native-codegen/scripts/oss/build.sh", + ]) assert_equal(FileMock.open_files[0].collected_write, ["[]"]) assert_equal(FileMock.open_files[0].fsync_invocation_count, 1) - assert_equal(Pod::Executable.executed_commands[1], { + assert_equal(Pod::Executable.executed_commands[0], { "command" => "node", "arguments" => [ @base_path + "/" + rn_path + "/scripts/generate-provider-cli.js", diff --git a/packages/react-native/scripts/cocoapods/codegen.rb b/packages/react-native/scripts/cocoapods/codegen.rb index 7d0bbc1aebf4..7842f39f4226 100644 --- a/packages/react-native/scripts/cocoapods/codegen.rb +++ b/packages/react-native/scripts/cocoapods/codegen.rb @@ -11,25 +11,11 @@ # - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes. # @throws an error if it could not find the codegen folder. def build_codegen!(react_native_path, relative_installation_root, dir_manager: Dir) - react_native_relpath = "#{relative_installation_root}/#{react_native_path}" - codegen_json_path = Pod::Executable.execute_command('node', [ - '-p', - "require.resolve('@react-native/codegen/package.json', {paths: ['#{react_native_relpath}']})" - ]) - if codegen_json_path.is_a? String - codegen_json_path = codegen_json_path.strip - codegen_path = Pathname.new(codegen_json_path).parent.relative_path_from(Pathname.pwd) - else - # This should only happen under testing - codegen_path = "#{react_native_relpath}/../react-native-codegen" - end + codegen_repo_path = "#{basePath(react_native_path, relative_installation_root)}/../react-native-codegen" + return unless dir_manager.exist?(codegen_repo_path) && !dir_manager.exist?("#{codegen_repo_path}/lib") - raise "[Codegen] Could not find react-native-codegen" unless dir_manager.exist?(codegen_path) - - if !dir_manager.exist?("#{codegen_path}/lib") - Pod::UI.puts "[Codegen] building #{codegen_path}." - system("#{codegen_path}/scripts/oss/build.sh") - end + Pod::UI.puts "[Codegen] building #{codegen_repo_path}" + system("#{codegen_repo_path}/scripts/oss/build.sh") end # It generates an empty `ThirdPartyProvider`, required by Fabric to load the components