Skip to content

Commit

Permalink
Add codegen step to generate Custom RCTURLRequest Handlers (facebook#…
Browse files Browse the repository at this point in the history
…42923)

Summary:

This change add the codegen step to generate the list of classes that can be used as custom URLHandlers

## Changelog
[iOS][Added] - Add the codegen step to generate custom URLHandlers

Differential Revision: D53441411
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Feb 8, 2024
1 parent 0ecd3a9 commit f6f10e8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ package-lock.json
/packages/react-native-codegen/**/*.xcodeproj
/packages/rn-tester/**/*.xcodeproj


# Ruby Gems (Bundler)
/packages/react-native/vendor
/packages/react-native/template/vendor
Expand All @@ -116,6 +117,7 @@ vendor/
/packages/react-native/template/ios/Podfile.lock
/packages/rn-tester/Gemfile.lock
/packages/**/RCTLegacyInteropComponents.mm
packages/**/RCTURLRequestHandlerProvider.mm

# Ignore RNTester specific Pods, but keep the __offline_mirrors__ here.
/packages/rn-tester/Pods/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @implementation RCTURLRequestHandlerProvider : NSObject
// The content of this array is codegenerated reading the
// codegenConfig.ios.customURLRequestHandler array
// e.g.:
@"MyCustomURLRequestHandler",

];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ const CORE_LIBRARIES_WITH_OUTPUT_FOLDER = {
},
};
const REACT_NATIVE = 'react-native';
const RCTREQUEST_HANDLER_PROVIDER_PATH = path.join(
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
'Libraries',
'AppDelegate',
'RCTURLRequestHandlerProvider.mm',
);
const RCTREQUEST_HANDLER_TEMPLATE_PATH = path.join(
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
'scripts',
'codegen',
'templates',
'RCTURLRequestHandlerProviderMM.template',
);

// HELPERS

Expand Down Expand Up @@ -497,6 +510,20 @@ function findCodegenEnabledLibraries(pkgJson, projectRoot) {
}
}

function generateCustomURLHandlers(libraries) {
const customURLHandlerClasses = libraries
.flatMap(library => library?.config?.ios?.protocols?.RCTURLRequestHandler)
.filter(Boolean);

const template = fs.readFileSync(RCTREQUEST_HANDLER_TEMPLATE_PATH, 'utf8');
const urlHandlersList = customURLHandlerClasses
.map(className => `@"${className}"`)
.join(',\n\t\t');
const finalFile = template.replace(/{classList}/, urlHandlersList);

fs.writeFileSync(RCTREQUEST_HANDLER_PROVIDER_PATH, finalFile);
}

// It removes all the empty files and empty folders
// it finds, starting from `filepath`, recursively.
//
Expand Down Expand Up @@ -609,6 +636,8 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {

createComponentProvider(schemas, supportedApplePlatforms);
}

generateCustomURLHandlers(libraries);
cleanupEmptyFilesAndFolders(outputPath);
}
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 "RCTURLRequestHandlerProvider.h"

@implementation RCTURLRequestHandlerProvider: NSObject

+ (NSArray<NSString *> *)customURLRequestHandlerClassNames
{
return @[
{classList}
];
}

@end

0 comments on commit f6f10e8

Please sign in to comment.