From 60674e7496f604a7be82b12fc5b93b0f60734183 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Mon, 18 Nov 2024 02:39:02 -0800 Subject: [PATCH] Generate `RCTAppDependencyProvider` for apps (#47650) Summary: ## This Change: This change generates the `RCTAppDependencyProvider` for the apps, so that the amount of changes required by the users is minimal. ## Context React Native has a last temporal dependency on Codegen in the React-RCTAppDelegate pod. The RCTAppDelegate has the responsibility to provide various dependencies to react native, like third party components and various modules. ReactCodegen is generated when the user create the project, while React-RCTAppDelegate eists in React Native itself. This dependency means that we cannot prepare prebuilt for iOS for React Native because when we would have to create prebuilds, we would need the React Codegen, but we can't create a React codegen package that will fit all the apps, because React Codegen can contains App Specific modules and components and apps might have different dependencies. ## Changelog: [iOS][Added] - Introduce the RCTAppDependencyProvider to minimize the changes required y the users Differential Revision: D66074456 --- .../scripts/cocoapods/codegen_utils.rb | 1 + .../codegen/generate-artifacts-executor.js | 38 +++++++++++++ .../RCTAppDependencyProviderH.template | 25 +++++++++ .../RCTAppDependencyProviderMM.template | 55 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderH.template create mode 100644 packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderMM.template diff --git a/packages/react-native/scripts/cocoapods/codegen_utils.rb b/packages/react-native/scripts/cocoapods/codegen_utils.rb index 067184cd61dd43..76a1fc8586a2a9 100644 --- a/packages/react-native/scripts/cocoapods/codegen_utils.rb +++ b/packages/react-native/scripts/cocoapods/codegen_utils.rb @@ -143,6 +143,7 @@ def get_react_codegen_spec(package_json_file, folly_version: get_folly_config()[ 'React-debug': [], 'React-utils': [], 'React-featureflags': [], + 'React-RCTAppDelegate': [], } } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index 3e79c45ad685c2..26d8cabc0683ee 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -102,6 +102,22 @@ const THIRD_PARTY_COMPONENTS_MM_TEMPLATE_PATH = path.join( 'RCTThirdPartyComponentsProviderMM.template', ); +const APP_DEPENDENCY_PROVIDER_H_TEMPLATE_PATH = path.join( + REACT_NATIVE_PACKAGE_ROOT_FOLDER, + 'scripts', + 'codegen', + 'templates', + 'RCTAppDependencyProviderH.template', +); + +const APP_DEPENDENCY_PROVIDER_MM_TEMPLATE_PATH = path.join( + REACT_NATIVE_PACKAGE_ROOT_FOLDER, + 'scripts', + 'codegen', + 'templates', + 'RCTAppDependencyProviderMM.template', +); + const codegenLog = (text, info = false) => { // ANSI escape codes for colors and formatting const reset = '\x1b[0m'; @@ -628,6 +644,27 @@ function generateCustomURLHandlers(libraries, outputDir) { ); } +function generateAppDependencyProvider(outputDir) { + fs.mkdirSync(outputDir, {recursive: true}); + codegenLog('Generating RCTAppDependencyProvider'); + + const templateH = fs.readFileSync( + APP_DEPENDENCY_PROVIDER_H_TEMPLATE_PATH, + 'utf8', + ); + const finalPathH = path.join(outputDir, 'RCTAppDependencyProvider.h'); + fs.writeFileSync(finalPathH, templateH); + codegenLog(`Generated artifact: ${finalPathH}`); + + const templateMM = fs.readFileSync( + APP_DEPENDENCY_PROVIDER_MM_TEMPLATE_PATH, + 'utf8', + ); + const finalPathMM = path.join(outputDir, 'RCTAppDependencyProvider.mm'); + fs.writeFileSync(finalPathMM, templateMM); + codegenLog(`Generated artifact: ${finalPathMM}`); +} + function generateRCTThirdPartyComponents(libraries, outputDir) { fs.mkdirSync(outputDir, {recursive: true}); // Generate Header File @@ -886,6 +923,7 @@ function execute(projectRoot, targetPlatform, baseOutputPath) { generateRCTThirdPartyComponents(libraries, outputPath); generateCustomURLHandlers(libraries, outputPath); + generateAppDependencyProvider(outputPath); cleanupEmptyFilesAndFolders(outputPath); } diff --git a/packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderH.template b/packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderH.template new file mode 100644 index 00000000000000..5a1502d16fa115 --- /dev/null +++ b/packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderH.template @@ -0,0 +1,25 @@ +/* + * 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. + */ + + +#import + +#if __has_include() +#import +#elif __has_include() +#import +#else +#import "RCTDependencyProvider.h" +#endif + +NS_ASSUME_NONNULL_BEGIN + +@interface RCTAppDependencyProvider : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderMM.template b/packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderMM.template new file mode 100644 index 00000000000000..532d7b4b4ce527 --- /dev/null +++ b/packages/react-native/scripts/codegen/templates/RCTAppDependencyProviderMM.template @@ -0,0 +1,55 @@ +/* + * 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. + */ + +#import "RCTAppDependencyProvider.h" +#import "RCTModulesConformingToProtocolsProvider.h" +#import "RCTThirdPartyComponentsProvider.h" + +@implementation RCTAppDependencyProvider { + NSArray * _URLRequestHandlerClassNames; + NSArray * _imageDataDecoderClassNames; + NSArray * _imageURLLoaderClassNames; + NSDictionary> * _thirdPartyFabricComponents; +} + +- (nonnull NSArray *)URLRequestHandlerClassNames { + static dispatch_once_t requestUrlToken; + dispatch_once(&requestUrlToken, ^{ + self->_URLRequestHandlerClassNames = RCTModulesConformingToProtocolsProvider.URLRequestHandlerClassNames; + }); + + return _URLRequestHandlerClassNames; +} + +- (nonnull NSArray *)imageDataDecoderClassNames { + static dispatch_once_t dataDecoderToken; + dispatch_once(&dataDecoderToken, ^{ + _imageDataDecoderClassNames = RCTModulesConformingToProtocolsProvider.imageDataDecoderClassNames; + }); + + return _imageDataDecoderClassNames; +} + +- (nonnull NSArray *)imageURLLoaderClassNames { + static dispatch_once_t urlLoaderToken; + dispatch_once(&urlLoaderToken, ^{ + _imageURLLoaderClassNames = RCTModulesConformingToProtocolsProvider.imageURLLoaderClassNames; + }); + + return _imageURLLoaderClassNames; +} + +- (nonnull NSDictionary> *)thirdPartyFabricComponents { + static dispatch_once_t nativeComponentsToken; + dispatch_once(&nativeComponentsToken, ^{ + _thirdPartyFabricComponents = RCTThirdPartyComponentsProvider.thirdPartyFabricComponents; + }); + + return _thirdPartyFabricComponents; +} + +@end