From dbe059b899decf5e8bda066f0881d093381b6077 Mon Sep 17 00:00:00 2001 From: Dmytro Rykun Date: Wed, 20 Dec 2023 06:52:18 -0800 Subject: [PATCH] Introduce "headerPrefix" codegen option (#41956) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41956 By default, generated Cxx sources for components all end up in same directory. However the include declarations in them look like this: ``` #include ``` And not like this: ``` #include "ShadowNodes.h" ``` This works fine with Buck because it supports header prefixes. To get this working with CocoaPods we define additional `HEADER_SEARCH_PATHS` for our `React-Codegen` pod. This approach will not work if we want to generate code at the library level and check in the artifacts. That's because we don't have control over the Podspec there, and can't inject those additional `HEADER_SEARCH_PATHS`. This diff adds the `headerPrefix` argument to the codegen entry point. It is `react/renderer/components/${libraryName}` by default, but can become empty if we want to generate code at the library level, and don't want to deal with this nested header structure. *Note:* `RNCodegen` runs all the generators [in a loop](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/generators/RNCodegen.js#L263-L275), assuming that the all have same function signature So I had to add the `headerPrefix` argument to all the generators, even to the ones that don't really need it. Changelog: [General][Added] - Introduce "headerPrefix" codegen option. Differential Revision: D51811596 Reviewed By: zeyap fbshipit-source-id: 7690791db67cc0792f8e51a13668f79b6e158c0a --- .../GenerateComponentDescriptorH-test.js | 8 ++++- .../GenerateEventEmitterCpp-test.js | 8 ++++- .../components/GeneratePropsCpp-test.js | 8 ++++- .../components/GenerateShadowNodeCpp-test.js | 8 ++++- .../components/GenerateShadowNodeH-test.js | 8 ++++- .../src/generators/RNCodegen.js | 33 +++++++++++++------ .../GenerateComponentDescriptorH.js | 9 ++--- .../components/GenerateComponentHObjCpp.js | 1 + .../components/GenerateEventEmitterCpp.js | 9 ++--- .../components/GenerateEventEmitterH.js | 1 + .../generators/components/GeneratePropsCpp.js | 9 ++--- .../generators/components/GeneratePropsH.js | 1 + .../components/GeneratePropsJavaDelegate.js | 1 + .../components/GeneratePropsJavaInterface.js | 1 + .../components/GenerateShadowNodeCpp.js | 9 ++--- .../components/GenerateShadowNodeH.js | 17 ++++------ .../generators/components/GenerateStateCpp.js | 9 ++--- .../generators/components/GenerateStateH.js | 1 + .../generators/components/GenerateTests.js | 1 + .../GenerateComponentDescriptorH-test.js | 10 +++++- .../__tests__/GenerateEventEmitterCpp-test.js | 10 +++++- .../__tests__/GeneratePropsCpp-test.js | 10 +++++- .../__tests__/GenerateShadowNodeCpp-test.js | 8 ++++- .../__tests__/GenerateShadowNodeH-test.js | 10 +++++- .../__tests__/GenerateStateCpp-test.js | 10 +++++- .../generators/modules/GenerateModuleCpp.js | 1 + .../src/generators/modules/GenerateModuleH.js | 1 + .../modules/GenerateModuleJavaSpec.js | 1 + .../modules/GenerateModuleJniCpp.js | 1 + .../generators/modules/GenerateModuleJniH.js | 1 + .../modules/GenerateModuleObjCpp/index.js | 1 + .../facebook/react/utils/ProjectUtilsTest.kt | 2 +- .../generate-specs-cli-executor-test.js | 2 +- .../codegen/generate-artifacts-executor.js | 9 +++-- .../codegen/generate-specs-cli-executor.js | 18 ++-------- 35 files changed, 165 insertions(+), 72 deletions(-) diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js index d6e0e06267e5c3..8ddac9a7b8cde2 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js @@ -25,7 +25,13 @@ fixtures.forEach(fixture => { it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); - const output = generator.generate(libName, schema); + const output = generator.generate( + libName, + schema, + undefined, + false, + `react/renderer/components/${libName}/`, + ); expect(Object.fromEntries(output)).toMatchSnapshot(); }); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js index da247b4b5af1f2..62976215b011e9 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js @@ -25,7 +25,13 @@ fixtures.forEach(fixture => { it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); - const output = generator.generate(libName, schema); + const output = generator.generate( + libName, + schema, + undefined, + false, + `react/renderer/components/${libName}/`, + ); expect(Object.fromEntries(output)).toMatchSnapshot(); }); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js index 77ed2f1a24a3dc..37af11c17d2eed 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js @@ -25,7 +25,13 @@ fixtures.forEach(fixture => { it(`GeneratePropsCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); - const output = generator.generate(libName, schema); + const output = generator.generate( + libName, + schema, + '', + false, + `react/renderer/components/${libName}/`, + ); expect(Object.fromEntries(output)).toMatchSnapshot(); }); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js index 9e594a8543ee25..6a5f508d901381 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js @@ -24,7 +24,13 @@ fixtures.forEach(fixture => { it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); - const output = generator.generate(libName, schema, undefined, false); + const output = generator.generate( + libName, + schema, + '', + false, + `react/renderer/components/${libName}/`, + ); expect(Object.fromEntries(output)).toMatchSnapshot(); }); }); diff --git a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js index 10a80ddb69acbd..543732fad46533 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js +++ b/packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js @@ -24,7 +24,13 @@ fixtures.forEach(fixture => { it(`GenerateShadowNodeH can generate for '${fixture}'`, () => { const libName = 'RNCodegenModuleFixtures'; const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`); - const output = generator.generate(libName, schema, undefined, false); + const output = generator.generate( + libName, + schema, + '', + false, + `react/renderer/components/${libName}/`, + ); expect(Object.fromEntries(output)).toMatchSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/generators/RNCodegen.js b/packages/react-native-codegen/src/generators/RNCodegen.js index 5595ec0bb98bb7..ea9f7398d94b4e 100644 --- a/packages/react-native-codegen/src/generators/RNCodegen.js +++ b/packages/react-native-codegen/src/generators/RNCodegen.js @@ -77,6 +77,7 @@ type LibraryOptions = $ReadOnly<{ outputDirectory: string, packageName?: string, // Some platforms have a notion of package, which should be configurable. assumeNonnull: boolean, + useLocalIncludePaths?: boolean, }>; type SchemasOptions = $ReadOnly<{ @@ -232,16 +233,24 @@ module.exports = { outputDirectory, packageName, assumeNonnull, + useLocalIncludePaths, }: LibraryOptions, {generators, test}: LibraryConfig, ): boolean { schemaValidator.validate(schema); + const defaultHeaderPrefix = 'react/renderer/components'; + const headerPrefix = + useLocalIncludePaths === true + ? '' + : `${defaultHeaderPrefix}/${libraryName}/`; function composePath(intermediate: string) { return path.join(outputDirectory, intermediate, libraryName); } - const componentIOSOutput = composePath('react/renderer/components/'); + const componentIOSOutput = composePath( + useLocalIncludePaths === true ? '' : defaultHeaderPrefix, + ); const modulesIOSOutput = composePath('./'); const outputFoldersForGenerators = { @@ -262,15 +271,19 @@ module.exports = { for (const name of generators) { for (const generator of LIBRARY_GENERATORS[name]) { - generator(libraryName, schema, packageName, assumeNonnull).forEach( - (contents: string, fileName: string) => { - generatedFiles.push({ - name: fileName, - content: contents, - outputDir: outputFoldersForGenerators[name], - }); - }, - ); + generator( + libraryName, + schema, + packageName, + assumeNonnull, + headerPrefix, + ).forEach((contents: string, fileName: string) => { + generatedFiles.push({ + name: fileName, + content: contents, + outputDir: outputFoldersForGenerators[name], + }); + }); } } return checkOrWriteFiles(generatedFiles, test); diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js b/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js index 542aac11fee046..fae8d67f6e3437 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js @@ -17,10 +17,10 @@ type FilesOutput = Map; const FileTemplate = ({ componentDescriptors, - libraryName, + headerPrefix, }: { componentDescriptors: string, - libraryName: string, + headerPrefix: string, }) => ` /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). @@ -33,7 +33,7 @@ const FileTemplate = ({ #pragma once -#include +#include <${headerPrefix}ShadowNodes.h> #include namespace facebook::react { @@ -54,6 +54,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'ComponentDescriptors.h'; @@ -85,7 +86,7 @@ module.exports = { const replacedTemplate = FileTemplate({ componentDescriptors, - libraryName, + headerPrefix: headerPrefix ?? '', }); return new Map([[fileName, replacedTemplate]]); diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js index 3422c0c68bb39c..79724d1810eb67 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js @@ -380,6 +380,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'RCTComponentViewHelpers.h'; diff --git a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js index 49d877981a413e..5045f3d679ef4b 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js @@ -31,12 +31,12 @@ type ComponentCollection = $ReadOnly<{ const FileTemplate = ({ events, - libraryName, extraIncludes, + headerPrefix, }: { events: string, - libraryName: string, extraIncludes: Set, + headerPrefix: string, }) => ` /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). @@ -47,7 +47,7 @@ const FileTemplate = ({ * ${'@'}generated by codegen project: GenerateEventEmitterCpp.js */ -#include +#include <${headerPrefix}EventEmitters.h> ${[...extraIncludes].join('\n')} namespace facebook::react { @@ -411,6 +411,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const moduleComponents: ComponentCollection = Object.keys(schema.modules) .map(moduleName => { @@ -442,9 +443,9 @@ module.exports = { const fileName = 'EventEmitters.cpp'; const replacedTemplate = FileTemplate({ - libraryName, events: componentEmitters, extraIncludes, + headerPrefix: headerPrefix ?? '', }); return new Map([[fileName, replacedTemplate]]); diff --git a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js index ca3f35f92da6af..0e753e24d2c544 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js @@ -311,6 +311,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const moduleComponents: ComponentCollection = Object.keys(schema.modules) .map(moduleName => { diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js b/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js index 77e0b7deed5b7a..99f57cdeecba18 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js @@ -18,13 +18,13 @@ const {convertDefaultTypeToString, getImports} = require('./CppHelpers'); type FilesOutput = Map; const FileTemplate = ({ - libraryName, imports, componentClasses, + headerPrefix, }: { - libraryName: string, imports: string, componentClasses: string, + headerPrefix: string, }) => ` /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). @@ -35,7 +35,7 @@ const FileTemplate = ({ * ${'@'}generated by codegen project: GeneratePropsCpp.js */ -#include +#include <${headerPrefix}Props.h> ${imports} namespace facebook::react { @@ -104,6 +104,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'Props.cpp'; const allImports: Set = new Set([ @@ -151,8 +152,8 @@ module.exports = { const replacedTemplate = FileTemplate({ componentClasses: componentProps, - libraryName, imports: Array.from(allImports).sort().join('\n').trim(), + headerPrefix: headerPrefix ?? '', }); return new Map([[fileName, replacedTemplate]]); diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js index f2a71e3ee84e61..da7da65f3a992e 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js @@ -706,6 +706,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'Props.h'; diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js index 6e485fedbf938d..e45a4df573bcd7 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js @@ -297,6 +297,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { // TODO: This doesn't support custom package name yet. const normalizedPackageName = 'com.facebook.react.viewmanagers'; diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js index c8afd851e42ed3..28888b203e245b 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js @@ -237,6 +237,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { // TODO: This doesn't support custom package name yet. const normalizedPackageName = 'com.facebook.react.viewmanagers'; diff --git a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js index 8a595988309f4a..c17c1f5f77b4e0 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js @@ -16,11 +16,11 @@ import type {SchemaType} from '../../CodegenSchema'; type FilesOutput = Map; const FileTemplate = ({ - libraryName, componentNames, + headerPrefix, }: { - libraryName: string, componentNames: string, + headerPrefix: string, }) => ` /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). @@ -31,7 +31,7 @@ const FileTemplate = ({ * ${'@'}generated by codegen project: GenerateShadowNodeCpp.js */ -#include +#include <${headerPrefix}ShadowNodes.h> namespace facebook::react { @@ -51,6 +51,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'ShadowNodes.cpp'; @@ -85,7 +86,7 @@ module.exports = { const replacedTemplate = FileTemplate({ componentNames, - libraryName, + headerPrefix: headerPrefix ?? '', }); return new Map([[fileName, replacedTemplate]]); diff --git a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js index b2d3c047c341a9..9de89c70ae9033 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js @@ -16,13 +16,11 @@ import type {SchemaType} from '../../CodegenSchema'; type FilesOutput = Map; const FileTemplate = ({ - imports, - libraryName, componentClasses, + headerPrefix, }: { - imports: string, - libraryName: string, componentClasses: string, + headerPrefix: string, }) => ` /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). @@ -35,8 +33,9 @@ const FileTemplate = ({ #pragma once -${imports}#include -#include +#include <${headerPrefix}EventEmitters.h> +#include <${headerPrefix}Props.h> +#include <${headerPrefix}States.h> #include #include @@ -72,6 +71,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'ShadowNodes.h'; @@ -109,12 +109,9 @@ module.exports = { .filter(Boolean) .join('\n\n'); - const eventEmitterImport = `#include \n`; - const replacedTemplate = FileTemplate({ componentClasses: moduleResults, - libraryName, - imports: eventEmitterImport, + headerPrefix: headerPrefix ?? '', }); return new Map([[fileName, replacedTemplate]]); diff --git a/packages/react-native-codegen/src/generators/components/GenerateStateCpp.js b/packages/react-native-codegen/src/generators/components/GenerateStateCpp.js index 2c280eaf51b441..17de4923afb3de 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateStateCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateStateCpp.js @@ -16,11 +16,11 @@ import type {SchemaType} from '../../CodegenSchema'; type FilesOutput = Map; const FileTemplate = ({ - libraryName, stateClasses, + headerPrefix, }: { - libraryName: string, stateClasses: string, + headerPrefix: string, }) => ` /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). @@ -30,7 +30,7 @@ const FileTemplate = ({ * * ${'@'}generated by codegen project: GenerateStateCpp.js */ -#include +#include <${headerPrefix}States.h> namespace facebook::react { @@ -47,6 +47,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'States.cpp'; @@ -81,8 +82,8 @@ module.exports = { .join('\n'); const replacedTemplate = FileTemplate({ - libraryName, stateClasses, + headerPrefix: headerPrefix ?? '', }); return new Map([[fileName, replacedTemplate]]); diff --git a/packages/react-native-codegen/src/generators/components/GenerateStateH.js b/packages/react-native-codegen/src/generators/components/GenerateStateH.js index 9910c83aa05abc..3aee725fca3058 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateStateH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateStateH.js @@ -70,6 +70,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'States.h'; diff --git a/packages/react-native-codegen/src/generators/components/GenerateTests.js b/packages/react-native-codegen/src/generators/components/GenerateTests.js index f17f0aa631ef8b..8017f933ef4467 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateTests.js +++ b/packages/react-native-codegen/src/generators/components/GenerateTests.js @@ -171,6 +171,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const fileName = 'Tests.cpp'; const allImports = new Set([ diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js index 4074282cc57a38..e82eddd170c32a 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js @@ -21,7 +21,15 @@ describe('GenerateComponentDescriptorH', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate( + fixtureName, + fixture, + '', + false, + `react/renderer/components/${fixtureName}/`, + ), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js index 11c6328ab729da..65edf61ff7c5ef 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js @@ -21,7 +21,15 @@ describe('GenerateEventEmitterCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate( + fixtureName, + fixture, + '', + false, + `react/renderer/components/${fixtureName}/`, + ), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js index ff77b09ca49b77..0ee57230d1c1c0 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js @@ -21,7 +21,15 @@ describe('GeneratePropsCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate( + fixtureName, + fixture, + '', + false, + `react/renderer/components/${fixtureName}/`, + ), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js index 3acb8162fe65a3..71903e8b92e334 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js @@ -22,7 +22,13 @@ describe('GenerateShadowNodeCpp', () => { it(`can generate fixture ${fixtureName}`, () => { expect( - generator.generate(fixtureName, fixture, 'SampleSpec'), + generator.generate( + fixtureName, + fixture, + 'SampleSpec', + false, + `react/renderer/components/${fixtureName}/`, + ), ).toMatchSnapshot(); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js index cafe5cf886f75b..0ab80f0b559e19 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js @@ -21,7 +21,15 @@ describe('GenerateShadowNodeH', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate( + fixtureName, + fixture, + '', + false, + `react/renderer/components/${fixtureName}/`, + ), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateStateCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateStateCpp-test.js index 1ccba96ba02d9b..218bf057b4aa0b 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateStateCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateStateCpp-test.js @@ -21,7 +21,15 @@ describe('GenerateStateCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate( + fixtureName, + fixture, + '', + false, + `react/renderer/components/${fixtureName}/`, + ), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js index c4b849d566e3bd..e435945e5c7f0d 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js @@ -234,6 +234,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const nativeModules = getModules(schema); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js index f351cf02fa6036..b6272f2d065075 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js @@ -611,6 +611,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const nativeModules = getModules(schema); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js index bcfbc4f9e4e019..3eba2a53eb5c78 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js @@ -441,6 +441,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const files = new Map(); const nativeModules = getModules(schema); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js index e8381473437cf0..d4544873fa2ac2 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniCpp.js @@ -422,6 +422,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const nativeModules = getModules(schema); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniH.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniH.js index c69dd85ffc6c61..f5f116edfbf24a 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJniH.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJniH.js @@ -119,6 +119,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean = false, + headerPrefix?: string, ): FilesOutput { const nativeModules = getModules(schema); const modules = Object.keys(nativeModules) diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js index bb8bc6bc1fd64a..ce4c1048cd36c1 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js @@ -118,6 +118,7 @@ module.exports = { schema: SchemaType, packageName?: string, assumeNonnull: boolean, + headerPrefix?: string, ): FilesOutput { const nativeModules = getModules(schema); diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt index 2dbc3f432ecbf3..6c87c88edac641 100644 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt @@ -263,7 +263,7 @@ class ProjectUtilsTest { @Test fun needsCodegenFromPackageJson_withCodegenConfigInModel_returnsTrue() { val project = createProject() - val model = ModelPackageJson("1000.0.0", ModelCodegenConfig(null, null, null, null)) + val model = ModelPackageJson("1000.0.0", ModelCodegenConfig(null, null, null, null, false)) assertTrue(project.needsCodegenFromPackageJson(model)) } diff --git a/packages/react-native/scripts/codegen/__tests__/generate-specs-cli-executor-test.js b/packages/react-native/scripts/codegen/__tests__/generate-specs-cli-executor-test.js index 3bad56cbae6ef3..dc0d2a5eabab71 100644 --- a/packages/react-native/scripts/codegen/__tests__/generate-specs-cli-executor-test.js +++ b/packages/react-native/scripts/codegen/__tests__/generate-specs-cli-executor-test.js @@ -83,6 +83,6 @@ describe('generateSpec', () => { libraryType, ); - expect(mkdirpSyncInvoked).toBe(3); + expect(mkdirpSyncInvoked).toBe(1); }); }); diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index 9d46fd55b6d511..16cb90534fcab5 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -226,13 +226,14 @@ function generateSchemaInfo(library) { }; } -function generateCode(iosOutputDir, schemaInfo) { +function generateCode(iosOutputDir, schemaInfo, includesGeneratedCode) { const tmpDir = fs.mkdtempSync( path.join(os.tmpdir(), schemaInfo.library.config.name), ); const tmpOutputDir = path.join(tmpDir, 'out'); fs.mkdirSync(tmpOutputDir, {recursive: true}); + const useLocalIncludePaths = includesGeneratedCode; generateSpecsCLIExecutor.generateSpecFromInMemorySchema( 'ios', schemaInfo.schema, @@ -240,6 +241,7 @@ function generateCode(iosOutputDir, schemaInfo) { schemaInfo.library.config.name, 'com.facebook.fbreact.specs', schemaInfo.library.config.type, + useLocalIncludePaths, ); // Finally, copy artifacts to the final output directory. @@ -256,9 +258,9 @@ function generateSchemaInfos(libraries) { return libraries.map(generateSchemaInfo); } -function generateNativeCode(iosOutputDir, schemaInfos) { +function generateNativeCode(iosOutputDir, schemaInfos, includesGeneratedCode) { return schemaInfos.map(schemaInfo => { - generateCode(iosOutputDir, schemaInfo); + generateCode(iosOutputDir, schemaInfo, includesGeneratedCode); }); } @@ -381,6 +383,7 @@ function execute(projectRoot, baseOutputPath) { schemaInfos.filter(schemaInfo => mustGenerateNativeCode(projectRoot, schemaInfo), ), + pkgJsonIncludesGeneratedCode(pkgJson), ); if (!pkgJsonIncludesGeneratedCode(pkgJson)) { diff --git a/packages/react-native/scripts/codegen/generate-specs-cli-executor.js b/packages/react-native/scripts/codegen/generate-specs-cli-executor.js index fb88e4a1dd54d2..c0e9ef183d38c2 100644 --- a/packages/react-native/scripts/codegen/generate-specs-cli-executor.js +++ b/packages/react-native/scripts/codegen/generate-specs-cli-executor.js @@ -36,12 +36,6 @@ function createOutputDirectoryIfNeeded(outputDirectory, libraryName) { mkdirp.sync(outputDirectory); } -function createFolderIfDefined(folder) { - if (folder) { - mkdirp.sync(folder); - } -} - /** * This function read a JSON schema from a path and parses it. * It throws if the schema don't exists or it can't be parsed. @@ -77,25 +71,17 @@ function generateSpecFromInMemorySchema( libraryName, packageName, libraryType, + useLocalIncludePaths, ) { validateLibraryType(libraryType); createOutputDirectoryIfNeeded(outputDirectory, libraryName); - function composePath(intermediate) { - return path.join(outputDirectory, intermediate, libraryName); - } - - // These are hardcoded and should not be changed. - // The codegen creates some C++ code with #include directive - // which uses these paths. Those directive are not customizable yet. - createFolderIfDefined(composePath('react/renderer/components/')); - createFolderIfDefined(composePath('./')); - utils.getCodegen().generate( { libraryName, schema, outputDirectory, packageName, + useLocalIncludePaths, }, { generators: GENERATORS[libraryType][platform],