Skip to content

Commit

Permalink
Codegen: separate Android/Cxx/iOS modules codegen outputs
Browse files Browse the repository at this point in the history
Summary:
For now, separate the definition of `modules` generator per platform to avoid file output collision. Additionally:
* For Android, produce files under java/ (plus nested subdirs based on packageName) and jni/ (for C++ files) - JavaPoet version already does it
* Allow configuring packageName for Android - JavaPoet version has this
* Avoid tmp directory dance in the CLI script, given the proper modules separation

Changelog: [Internal]

Reviewed By: hramos

Differential Revision: D24410864

fbshipit-source-id: 9bd6bc1d65bec037bfca32ec478f3af50d72e927
  • Loading branch information
fkgozali authored and facebook-github-bot committed Oct 20, 2020
1 parent d03c0f9 commit 13d9927
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 114 deletions.
7 changes: 5 additions & 2 deletions packages/react-native-codegen/buck_tests/generate-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const schemaPath = args[0];
const libraryName = args[1];
const outputDirectory = args[2];
const moduleSpecName = args[3];
const packageName = args[4];

const schemaText = fs.readFileSync(schemaPath, 'utf-8');

Expand All @@ -44,15 +45,17 @@ try {
}

RNCodegen.generate(
{libraryName, schema, outputDirectory, moduleSpecName},
{libraryName, schema, outputDirectory, moduleSpecName, packageName},
{
generators: [
'descriptors',
'events',
'props',
'tests',
'shadow-nodes',
'modules',
'modulesAndroid',
'modulesCxx',
'modulesIOS',
],
},
);
30 changes: 21 additions & 9 deletions packages/react-native-codegen/src/generators/RNCodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Options = $ReadOnly<{|
schema: SchemaType,
outputDirectory: string,
moduleSpecName: string,
packageName?: string, // Some platforms have a notion of package, which should be configurable.
|}>;

type Generators =
Expand All @@ -53,7 +54,9 @@ type Generators =
| 'props'
| 'tests'
| 'shadow-nodes'
| 'modules';
| 'modulesAndroid'
| 'modulesCxx'
| 'modulesIOS';

type Config = $ReadOnly<{|
generators: Array<Generators>,
Expand All @@ -70,17 +73,14 @@ const GENERATORS = {
generatePropsJavaInterface.generate,
generatePropsJavaDelegate.generate,
],
modules: [
generateModuleCpp.generate,
generateModuleH.generate,
generateModuleObjCpp.generate,
],
// TODO: Refactor this to consolidate various C++ output variation instead of forking Android.
// TODO: Refactor this to consolidate various C++ output variation instead of forking per platform.
modulesAndroid: [
GenerateModuleJniCpp.generate,
GenerateModuleJniH.generate,
generateModuleJavaSpec.generate,
],
modulesCxx: [generateModuleCpp.generate, generateModuleH.generate],
modulesIOS: [generateModuleObjCpp.generate],
tests: [generateTests.generate],
'shadow-nodes': [
generateShadowNodeCpp.generate,
Expand All @@ -93,6 +93,10 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
map.forEach((contents: string, fileName: string) => {
try {
const location = path.join(outputDir, fileName);
const dirName = path.dirname(location);
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName, {recursive: true});
}
fs.writeFileSync(location, contents);
} catch (error) {
success = false;
Expand Down Expand Up @@ -124,15 +128,23 @@ function checkFilesForChanges(

module.exports = {
generate(
{libraryName, schema, outputDirectory, moduleSpecName}: Options,
{
libraryName,
schema,
outputDirectory,
moduleSpecName,
packageName,
}: Options,
{generators, test}: Config,
): boolean {
schemaValidator.validate(schema);

const generatedFiles = [];
for (const name of generators) {
for (const generator of GENERATORS[name]) {
generatedFiles.push(...generator(libraryName, schema, moduleSpecName));
generatedFiles.push(
...generator(libraryName, schema, moduleSpecName, packageName),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'ComponentDescriptors.h';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'RCTComponentViewHelpers.h';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const moduleComponents: ComponentCollection = Object.keys(schema.modules)
.map(moduleName => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const moduleComponents: ComponentCollection = Object.keys(schema.modules)
.map(moduleName => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'Props.cpp';
const allImports: Set<string> = new Set([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'Props.h';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const files = new Map();
Object.keys(schema.modules).forEach(moduleName => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const files = new Map();
Object.keys(schema.modules).forEach(moduleName => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'ShadowNodes.cpp';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'ShadowNodes.h';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const fileName = 'Tests.cpp';
const allImports = new Set([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const nativeModules = getModules(schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const nativeModules = getModules(schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,12 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const files = new Map();
// TODO: Allow package configuration.
const packageName = 'com.facebook.fbreact.specs.beta';
const normalizedPackageName =
packageName != null ? packageName : 'com.facebook.fbreact.specs';
const outputDir = `java/${normalizedPackageName.replace(/\./g, '/')}`;
const nativeModules = getModules(schema);

Object.keys(nativeModules).forEach(hasteModuleName => {
Expand Down Expand Up @@ -365,9 +367,9 @@ module.exports = {
});

files.set(
`${className}.java`,
`${outputDir}/${className}.java`,
FileTemplate({
packageName,
packageName: normalizedPackageName,
className,
methods: methods.filter(Boolean).join('\n\n'),
imports: Array.from(imports)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const nativeModules = getModules(schema);

Expand Down Expand Up @@ -455,6 +456,6 @@ module.exports = {
moduleLookups,
include: `"${moduleSpecName}.h"`,
});
return new Map([[fileName, replacedTemplate]]);
return new Map([[`jni/${fileName}`, replacedTemplate]]);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const nativeModules = getModules(schema);
const modules = Object.keys(nativeModules)
Expand All @@ -117,9 +118,9 @@ module.exports = {
libraryName: libraryName,
});
return new Map([
[fileName, replacedTemplate],
[`jni/${fileName}`, replacedTemplate],
[
'Android.mk',
'jni/Android.mk',
AndroidMkTemplate({
libraryName: `react_codegen_${libraryName.toLowerCase()}`,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module.exports = {
libraryName: string,
schema: SchemaType,
moduleSpecName: string,
packageName?: string,
): FilesOutput {
const nativeModules = getModules(schema);

Expand Down
Loading

0 comments on commit 13d9927

Please sign in to comment.