mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
codegen: add Android NativeModule generator base structure
Summary: * Allow generate-native-modules-specs-cli.js to generate Android specific files * Add stub impl for Java spec generation (the output are still incomplete, see the next diffs for missing impl + tests) * Adjust the CLI to only produce modules stuffs, there's no need to produce components code Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D22860936 fbshipit-source-id: 38aae390189f143f6c6216995437ac1ff15a1788
This commit is contained in:
committed by
Facebook GitHub Bot
parent
4a5b074c79
commit
77e0ba2fb0
+101
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* 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 {
|
||||
FunctionTypeAnnotationParamTypeAnnotation,
|
||||
FunctionTypeAnnotationReturn,
|
||||
NativeModuleShape,
|
||||
TypeAliasTypeAnnotation,
|
||||
ObjectTypeAliasTypeShape,
|
||||
SchemaType,
|
||||
} from '../../CodegenSchema';
|
||||
|
||||
type FilesOutput = Map<string, string>;
|
||||
|
||||
const moduleTemplate = `/**
|
||||
* 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: GenerateModuleJavaSpec.js
|
||||
*
|
||||
* @nolint
|
||||
*/
|
||||
|
||||
package ::_PACKAGENAME_::;
|
||||
|
||||
::_IMPORTS_::
|
||||
|
||||
public abstract class ::_CLASSNAME_:: extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
|
||||
public ::_CLASSNAME_::(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
}
|
||||
|
||||
::_METHODS_::
|
||||
}
|
||||
`;
|
||||
|
||||
function getImportList(nativeModule: NativeModuleShape): Set<string> {
|
||||
const imports: Set<string> = new Set();
|
||||
|
||||
// Always required.
|
||||
imports.add('com.facebook.react.bridge.ReactApplicationContext');
|
||||
imports.add('com.facebook.react.bridge.ReactContextBaseJavaModule');
|
||||
imports.add('com.facebook.react.bridge.ReactMethod');
|
||||
imports.add('com.facebook.react.bridge.ReactModuleWithSpec');
|
||||
imports.add('com.facebook.react.turbomodule.core.interfaces.TurboModule');
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
// TODO: complete this implementation
|
||||
module.exports = {
|
||||
generate(
|
||||
libraryName: string,
|
||||
schema: SchemaType,
|
||||
moduleSpecName: string,
|
||||
): FilesOutput {
|
||||
const files = new Map();
|
||||
// TODO: Allow package configuration.
|
||||
const packageName = 'com.facebook.fbreact.specs';
|
||||
const nativeModules = Object.keys(schema.modules)
|
||||
.map(moduleName => {
|
||||
const modules = schema.modules[moduleName].nativeModules;
|
||||
if (modules == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return modules;
|
||||
})
|
||||
.filter(Boolean)
|
||||
.reduce((acc, components) => Object.assign(acc, components), {});
|
||||
|
||||
Object.keys(nativeModules).forEach(name => {
|
||||
const {aliases, properties} = nativeModules[name];
|
||||
const className = `Native${name}Spec`;
|
||||
|
||||
const methods = properties.map(method => {
|
||||
const traversedArgs = method.typeAnnotation.params.map(param => {});
|
||||
});
|
||||
|
||||
files.set(
|
||||
`${className}.java`,
|
||||
moduleTemplate
|
||||
.replace(/::_PACKAGENAME_::/g, packageName)
|
||||
.replace(/::_CLASSNAME_::/g, className),
|
||||
);
|
||||
});
|
||||
|
||||
return files;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user