Files
react-native/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js
T
Kevin Gozali 77e0ba2fb0 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
2020-07-31 19:04:54 -07:00

102 lines
2.6 KiB
JavaScript

/**
* 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;
},
};