Skip to content

Commit 192da64

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Introduce "headerPrefix" codegen option (#41956)
Summary: Pull Request resolved: #41956 By default, generated Cxx sources for components all end up in same directory. However the include declarations in them look like this: ``` #include <react/renderer/components/${libraryName}/ShadowNodes.h> ``` 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. Reviewed By: zeyap Differential Revision: D51811596 fbshipit-source-id: 8d9f714959f2e7d77a11c59e0004f8284149931a
1 parent 7fb7454 commit 192da64

33 files changed

+162
-70
lines changed

packages/react-native-codegen/e2e/__tests__/components/GenerateComponentDescriptorH-test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ fixtures.forEach(fixture => {
2525
it(`GenerateComponentDescriptorH can generate for '${fixture}'`, () => {
2626
const libName = 'RNCodegenModuleFixtures';
2727
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
28-
const output = generator.generate(libName, schema);
28+
const output = generator.generate(
29+
libName,
30+
schema,
31+
'',
32+
false,
33+
`react/renderer/components/${libName}/`,
34+
);
2935
expect(Object.fromEntries(output)).toMatchSnapshot();
3036
});
3137
});

packages/react-native-codegen/e2e/__tests__/components/GenerateEventEmitterCpp-test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ fixtures.forEach(fixture => {
2525
it(`GenerateEventEmitterCpp can generate for '${fixture}'`, () => {
2626
const libName = 'RNCodegenModuleFixtures';
2727
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
28-
const output = generator.generate(libName, schema);
28+
const output = generator.generate(
29+
libName,
30+
schema,
31+
'',
32+
false,
33+
`react/renderer/components/${libName}/`,
34+
);
2935
expect(Object.fromEntries(output)).toMatchSnapshot();
3036
});
3137
});

packages/react-native-codegen/e2e/__tests__/components/GeneratePropsCpp-test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ fixtures.forEach(fixture => {
2525
it(`GeneratePropsCpp can generate for '${fixture}'`, () => {
2626
const libName = 'RNCodegenModuleFixtures';
2727
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
28-
const output = generator.generate(libName, schema);
28+
const output = generator.generate(
29+
libName,
30+
schema,
31+
'',
32+
false,
33+
`react/renderer/components/${libName}/`,
34+
);
2935
expect(Object.fromEntries(output)).toMatchSnapshot();
3036
});
3137
});

packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeCpp-test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ fixtures.forEach(fixture => {
2424
it(`GenerateShadowNodeCpp can generate for '${fixture}'`, () => {
2525
const libName = 'RNCodegenModuleFixtures';
2626
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
27-
const output = generator.generate(libName, schema, undefined, false);
27+
const output = generator.generate(
28+
libName,
29+
schema,
30+
'',
31+
false,
32+
`react/renderer/components/${libName}/`,
33+
);
2834
expect(Object.fromEntries(output)).toMatchSnapshot();
2935
});
3036
});

packages/react-native-codegen/e2e/__tests__/components/GenerateShadowNodeH-test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ fixtures.forEach(fixture => {
2424
it(`GenerateShadowNodeH can generate for '${fixture}'`, () => {
2525
const libName = 'RNCodegenModuleFixtures';
2626
const schema = parser.parseFile(`${FIXTURE_DIR}/${fixture}`);
27-
const output = generator.generate(libName, schema, undefined, false);
27+
const output = generator.generate(
28+
libName,
29+
schema,
30+
'',
31+
false,
32+
`react/renderer/components/${libName}/`,
33+
);
2834
expect(Object.fromEntries(output)).toMatchSnapshot();
2935
});
3036
});

packages/react-native-codegen/src/generators/RNCodegen.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type LibraryOptions = $ReadOnly<{
7777
outputDirectory: string,
7878
packageName?: string, // Some platforms have a notion of package, which should be configurable.
7979
assumeNonnull: boolean,
80+
useLocalIncludePaths?: boolean,
8081
}>;
8182

8283
type SchemasOptions = $ReadOnly<{
@@ -232,16 +233,24 @@ module.exports = {
232233
outputDirectory,
233234
packageName,
234235
assumeNonnull,
236+
useLocalIncludePaths,
235237
}: LibraryOptions,
236238
{generators, test}: LibraryConfig,
237239
): boolean {
238240
schemaValidator.validate(schema);
239241

242+
const defaultHeaderPrefix = 'react/renderer/components';
243+
const headerPrefix =
244+
useLocalIncludePaths === true
245+
? ''
246+
: `${defaultHeaderPrefix}/${libraryName}/`;
240247
function composePath(intermediate: string) {
241248
return path.join(outputDirectory, intermediate, libraryName);
242249
}
243250

244-
const componentIOSOutput = composePath('react/renderer/components/');
251+
const componentIOSOutput = composePath(
252+
useLocalIncludePaths === true ? '' : defaultHeaderPrefix,
253+
);
245254
const modulesIOSOutput = composePath('./');
246255

247256
const outputFoldersForGenerators = {
@@ -262,15 +271,19 @@ module.exports = {
262271

263272
for (const name of generators) {
264273
for (const generator of LIBRARY_GENERATORS[name]) {
265-
generator(libraryName, schema, packageName, assumeNonnull).forEach(
266-
(contents: string, fileName: string) => {
267-
generatedFiles.push({
268-
name: fileName,
269-
content: contents,
270-
outputDir: outputFoldersForGenerators[name],
271-
});
272-
},
273-
);
274+
generator(
275+
libraryName,
276+
schema,
277+
packageName,
278+
assumeNonnull,
279+
headerPrefix,
280+
).forEach((contents: string, fileName: string) => {
281+
generatedFiles.push({
282+
name: fileName,
283+
content: contents,
284+
outputDir: outputFoldersForGenerators[name],
285+
});
286+
});
274287
}
275288
}
276289
return checkOrWriteFiles(generatedFiles, test);

packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ type FilesOutput = Map<string, string>;
1717

1818
const FileTemplate = ({
1919
componentDescriptors,
20-
libraryName,
20+
headerPrefix,
2121
}: {
2222
componentDescriptors: string,
23-
libraryName: string,
23+
headerPrefix: string,
2424
}) => `
2525
/**
2626
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
@@ -33,7 +33,7 @@ const FileTemplate = ({
3333
3434
#pragma once
3535
36-
#include <react/renderer/components/${libraryName}/ShadowNodes.h>
36+
#include <${headerPrefix}ShadowNodes.h>
3737
#include <react/renderer/core/ConcreteComponentDescriptor.h>
3838
3939
namespace facebook::react {
@@ -54,6 +54,7 @@ module.exports = {
5454
schema: SchemaType,
5555
packageName?: string,
5656
assumeNonnull: boolean = false,
57+
headerPrefix?: string,
5758
): FilesOutput {
5859
const fileName = 'ComponentDescriptors.h';
5960

@@ -85,7 +86,7 @@ module.exports = {
8586

8687
const replacedTemplate = FileTemplate({
8788
componentDescriptors,
88-
libraryName,
89+
headerPrefix: headerPrefix ?? '',
8990
});
9091

9192
return new Map([[fileName, replacedTemplate]]);

packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ module.exports = {
380380
schema: SchemaType,
381381
packageName?: string,
382382
assumeNonnull: boolean = false,
383+
_headerPrefix?: string,
383384
): FilesOutput {
384385
const fileName = 'RCTComponentViewHelpers.h';
385386

packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ type ComponentCollection = $ReadOnly<{
3131

3232
const FileTemplate = ({
3333
events,
34-
libraryName,
3534
extraIncludes,
35+
headerPrefix,
3636
}: {
3737
events: string,
38-
libraryName: string,
3938
extraIncludes: Set<string>,
39+
headerPrefix: string,
4040
}) => `
4141
/**
4242
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
@@ -47,7 +47,7 @@ const FileTemplate = ({
4747
* ${'@'}generated by codegen project: GenerateEventEmitterCpp.js
4848
*/
4949
50-
#include <react/renderer/components/${libraryName}/EventEmitters.h>
50+
#include <${headerPrefix}EventEmitters.h>
5151
${[...extraIncludes].join('\n')}
5252
5353
namespace facebook::react {
@@ -411,6 +411,7 @@ module.exports = {
411411
schema: SchemaType,
412412
packageName?: string,
413413
assumeNonnull: boolean = false,
414+
headerPrefix?: string,
414415
): FilesOutput {
415416
const moduleComponents: ComponentCollection = Object.keys(schema.modules)
416417
.map(moduleName => {
@@ -442,9 +443,9 @@ module.exports = {
442443

443444
const fileName = 'EventEmitters.cpp';
444445
const replacedTemplate = FileTemplate({
445-
libraryName,
446446
events: componentEmitters,
447447
extraIncludes,
448+
headerPrefix: headerPrefix ?? '',
448449
});
449450

450451
return new Map([[fileName, replacedTemplate]]);

packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ module.exports = {
311311
schema: SchemaType,
312312
packageName?: string,
313313
assumeNonnull: boolean = false,
314+
_headerPrefix?: string,
314315
): FilesOutput {
315316
const moduleComponents: ComponentCollection = Object.keys(schema.modules)
316317
.map(moduleName => {

packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const {convertDefaultTypeToString, getImports} = require('./CppHelpers');
1818
type FilesOutput = Map<string, string>;
1919

2020
const FileTemplate = ({
21-
libraryName,
2221
imports,
2322
componentClasses,
23+
headerPrefix,
2424
}: {
25-
libraryName: string,
2625
imports: string,
2726
componentClasses: string,
27+
headerPrefix: string,
2828
}) => `
2929
/**
3030
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
@@ -35,7 +35,7 @@ const FileTemplate = ({
3535
* ${'@'}generated by codegen project: GeneratePropsCpp.js
3636
*/
3737
38-
#include <react/renderer/components/${libraryName}/Props.h>
38+
#include <${headerPrefix}Props.h>
3939
${imports}
4040
4141
namespace facebook::react {
@@ -104,6 +104,7 @@ module.exports = {
104104
schema: SchemaType,
105105
packageName?: string,
106106
assumeNonnull: boolean = false,
107+
headerPrefix?: string,
107108
): FilesOutput {
108109
const fileName = 'Props.cpp';
109110
const allImports: Set<string> = new Set([
@@ -151,8 +152,8 @@ module.exports = {
151152

152153
const replacedTemplate = FileTemplate({
153154
componentClasses: componentProps,
154-
libraryName,
155155
imports: Array.from(allImports).sort().join('\n').trim(),
156+
headerPrefix: headerPrefix ?? '',
156157
});
157158

158159
return new Map([[fileName, replacedTemplate]]);

packages/react-native-codegen/src/generators/components/GeneratePropsH.js

+1
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ module.exports = {
706706
schema: SchemaType,
707707
packageName?: string,
708708
assumeNonnull: boolean = false,
709+
_headerPrefix?: string,
709710
): FilesOutput {
710711
const fileName = 'Props.h';
711712

packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ module.exports = {
297297
schema: SchemaType,
298298
packageName?: string,
299299
assumeNonnull: boolean = false,
300+
_headerPrefix?: string,
300301
): FilesOutput {
301302
// TODO: This doesn't support custom package name yet.
302303
const normalizedPackageName = 'com.facebook.react.viewmanagers';

packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ module.exports = {
237237
schema: SchemaType,
238238
packageName?: string,
239239
assumeNonnull: boolean = false,
240+
_headerPrefix?: string,
240241
): FilesOutput {
241242
// TODO: This doesn't support custom package name yet.
242243
const normalizedPackageName = 'com.facebook.react.viewmanagers';

packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import type {SchemaType} from '../../CodegenSchema';
1616
type FilesOutput = Map<string, string>;
1717

1818
const FileTemplate = ({
19-
libraryName,
2019
componentNames,
20+
headerPrefix,
2121
}: {
22-
libraryName: string,
2322
componentNames: string,
23+
headerPrefix: string,
2424
}) => `
2525
/**
2626
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
@@ -31,7 +31,7 @@ const FileTemplate = ({
3131
* ${'@'}generated by codegen project: GenerateShadowNodeCpp.js
3232
*/
3333
34-
#include <react/renderer/components/${libraryName}/ShadowNodes.h>
34+
#include <${headerPrefix}ShadowNodes.h>
3535
3636
namespace facebook::react {
3737
@@ -51,6 +51,7 @@ module.exports = {
5151
schema: SchemaType,
5252
packageName?: string,
5353
assumeNonnull: boolean = false,
54+
headerPrefix?: string,
5455
): FilesOutput {
5556
const fileName = 'ShadowNodes.cpp';
5657

@@ -85,7 +86,7 @@ module.exports = {
8586

8687
const replacedTemplate = FileTemplate({
8788
componentNames,
88-
libraryName,
89+
headerPrefix: headerPrefix ?? '',
8990
});
9091

9192
return new Map([[fileName, replacedTemplate]]);

packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ import type {SchemaType} from '../../CodegenSchema';
1616
type FilesOutput = Map<string, string>;
1717

1818
const FileTemplate = ({
19-
imports,
20-
libraryName,
2119
componentClasses,
20+
headerPrefix,
2221
}: {
23-
imports: string,
24-
libraryName: string,
2522
componentClasses: string,
23+
headerPrefix: string,
2624
}) => `
2725
/**
2826
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
@@ -35,8 +33,9 @@ const FileTemplate = ({
3533
3634
#pragma once
3735
38-
${imports}#include <react/renderer/components/${libraryName}/Props.h>
39-
#include <react/renderer/components/${libraryName}/States.h>
36+
#include <${headerPrefix}EventEmitters.h>
37+
#include <${headerPrefix}Props.h>
38+
#include <${headerPrefix}States.h>
4039
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
4140
#include <jsi/jsi.h>
4241
@@ -72,6 +71,7 @@ module.exports = {
7271
schema: SchemaType,
7372
packageName?: string,
7473
assumeNonnull: boolean = false,
74+
headerPrefix?: string,
7575
): FilesOutput {
7676
const fileName = 'ShadowNodes.h';
7777

@@ -109,12 +109,9 @@ module.exports = {
109109
.filter(Boolean)
110110
.join('\n\n');
111111

112-
const eventEmitterImport = `#include <react/renderer/components/${libraryName}/EventEmitters.h>\n`;
113-
114112
const replacedTemplate = FileTemplate({
115113
componentClasses: moduleResults,
116-
libraryName,
117-
imports: eventEmitterImport,
114+
headerPrefix: headerPrefix ?? '',
118115
});
119116

120117
return new Map([[fileName, replacedTemplate]]);

0 commit comments

Comments
 (0)