Codegen: Add JNI C++ generator for TurboModule subclasses

Summary:
This adds the C++ classes for Android, just like checked in here: https://github.com/facebook/react-native/tree/master/ReactAndroid/src/main/java/com/facebook/fbreact/specs/jni

The whitespace/formatting is slightly different but the functionality should be preserved.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23489368

fbshipit-source-id: 60c112a48b6de2d2eb19f7d1bcc894048334610f
This commit is contained in:
Kevin Gozali authored and Facebook GitHub Bot committed 2020-09-03 16:11:08 -07:00
1 parent fc2b1cac82
commit 02fed06340
8 files changed
+1137 -2

No files matched your search

@@ -0,0 +1,78 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {SchemaType} from '../../CodegenSchema';
type FilesOutput = Map<string, string>;
const moduleTemplate = `/**
* JNI C++ class for module '::_MODULE_NAME_::'
*/
class JSI_EXPORT Native::_MODULE_NAME_::SpecJSI : public JavaTurboModule {
public:
Native::_MODULE_NAME_::SpecJSI(const JavaTurboModule::InitParams &params);
};
`;
const template = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* ${'@'}generated by codegen project: GenerateModuleJniH.js
*/
#import <ReactCommon/JavaTurboModule.h>
#import <ReactCommon/TurboModule.h>
#import <fb/fbjni.h>
#import <jsi/jsi.h>
namespace facebook {
namespace react {
::_MODULES_::
} // namespace react
} // namespace facebook
`;
module.exports = {
generate(
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
): FilesOutput {
const nativeModules = Object.keys(schema.modules)
.sort()
.map(moduleName => {
const modules = schema.modules[moduleName].nativeModules;
if (modules == null) {
return null;
}
return modules;
})
.filter(Boolean)
.reduce((acc, components) => Object.assign(acc, components), {});
const modules = Object.keys(nativeModules)
.map(name => moduleTemplate.replace(/::_MODULE_NAME_::/g, name))
.join('\n');
const fileName = `${moduleSpecName}.h`;
const replacedTemplate = template.replace(/::_MODULES_::/g, modules);
return new Map([[fileName, replacedTemplate]]);
},
};