Skip to content

Commit

Permalink
Generate RCTAppDependencyProvider for apps (#47650)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47650

## 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

Reviewed By: dmytrorykun

Differential Revision: D66074456

fbshipit-source-id: 073022e66da53eca6bf948aeda01f17ad85793ff
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Nov 18, 2024
1 parent b91626a commit 41c2502
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/react-native/scripts/cocoapods/codegen_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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': [],
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -886,6 +923,7 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {

generateRCTThirdPartyComponents(libraries, outputPath);
generateCustomURLHandlers(libraries, outputPath);
generateAppDependencyProvider(outputPath);

cleanupEmptyFilesAndFolders(outputPath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

#if __has_include(<React-RCTAppDelegate/RCTDependencyProvider.h>)
#import <React-RCTAppDelegate/RCTDependencyProvider.h>
#elif __has_include(<React_RCTAppDelegate/RCTDependencyProvider.h>)
#import <React_RCTAppDelegate/RCTDependencyProvider.h>
#else
#import "RCTDependencyProvider.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface RCTAppDependencyProvider : NSObject <RCTDependencyProvider>

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -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<NSString *> * _URLRequestHandlerClassNames;
NSArray<NSString *> * _imageDataDecoderClassNames;
NSArray<NSString *> * _imageURLLoaderClassNames;
NSDictionary<NSString *,Class<RCTComponentViewProtocol>> * _thirdPartyFabricComponents;
}

- (nonnull NSArray<NSString *> *)URLRequestHandlerClassNames {
static dispatch_once_t requestUrlToken;
dispatch_once(&requestUrlToken, ^{
self->_URLRequestHandlerClassNames = RCTModulesConformingToProtocolsProvider.URLRequestHandlerClassNames;
});

return _URLRequestHandlerClassNames;
}

- (nonnull NSArray<NSString *> *)imageDataDecoderClassNames {
static dispatch_once_t dataDecoderToken;
dispatch_once(&dataDecoderToken, ^{
_imageDataDecoderClassNames = RCTModulesConformingToProtocolsProvider.imageDataDecoderClassNames;
});

return _imageDataDecoderClassNames;
}

- (nonnull NSArray<NSString *> *)imageURLLoaderClassNames {
static dispatch_once_t urlLoaderToken;
dispatch_once(&urlLoaderToken, ^{
_imageURLLoaderClassNames = RCTModulesConformingToProtocolsProvider.imageURLLoaderClassNames;
});

return _imageURLLoaderClassNames;
}

- (nonnull NSDictionary<NSString *,Class<RCTComponentViewProtocol>> *)thirdPartyFabricComponents {
static dispatch_once_t nativeComponentsToken;
dispatch_once(&nativeComponentsToken, ^{
_thirdPartyFabricComponents = RCTThirdPartyComponentsProvider.thirdPartyFabricComponents;
});

return _thirdPartyFabricComponents;
}

@end

0 comments on commit 41c2502

Please sign in to comment.