-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This Diff introduces some changes in the CodeGen to properly generate the types in the right folder. ## Issue The codegen on iOS defines the output folder once, before creating the generated code. When the code we have to generate is just a TurboModule (TM) or a Fabric Component (FC), this mechanism works properly. However, if a library has to generate both TM and FC, actually using the library type `all`, all the code is generated using the TurboModules' output folder. (**Note:** Android only works in this way) This generates invalid code because all the FC's `#import` directives assumes that the code is generated in the FC output path which, in this case, is not. ## Solution The adopted solution moves the responsibility to decide where the files has to be generated to the CodeGen step instead of in the preparatory phases. The two paths are precomputed in the `generate-artifacts.js` script (the entry point for the CodeGen) and they are passed to all the scripts that requires them. Once they reach the `RNCodegen.js` file, the generators creates the files and save them in the proper paths. ## Changelog [iOS][Changed] - CodeGen now supports the `"all"` library type. Reviewed By: cortinico, dmitryrykun Differential Revision: D35820848 fbshipit-source-id: ce7f5393936e2ae17f8b2c970f6a011d27f641f2
- Loading branch information
1 parent
3f09b48
commit 6718500
Showing
5 changed files
with
317 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {SchemaType} from '../../CodegenSchema.js'; | ||
|
||
const SCHEMA_WITH_TM_AND_FC: SchemaType = { | ||
modules: { | ||
ColoredView: { | ||
type: 'Component', | ||
components: { | ||
ColoredView: { | ||
extendsProps: [ | ||
{ | ||
type: 'ReactNativeBuiltInType', | ||
knownTypeName: 'ReactNativeCoreViewProps', | ||
}, | ||
], | ||
events: [], | ||
props: [ | ||
{ | ||
name: 'color', | ||
optional: false, | ||
typeAnnotation: { | ||
type: 'StringTypeAnnotation', | ||
default: null, | ||
}, | ||
}, | ||
], | ||
commands: [], | ||
}, | ||
}, | ||
}, | ||
NativeCalculator: { | ||
type: 'NativeModule', | ||
aliases: {}, | ||
spec: { | ||
properties: [ | ||
{ | ||
name: 'add', | ||
optional: false, | ||
typeAnnotation: { | ||
type: 'FunctionTypeAnnotation', | ||
returnTypeAnnotation: { | ||
type: 'PromiseTypeAnnotation', | ||
}, | ||
params: [ | ||
{ | ||
name: 'a', | ||
optional: false, | ||
typeAnnotation: { | ||
type: 'NumberTypeAnnotation', | ||
}, | ||
}, | ||
{ | ||
name: 'b', | ||
optional: false, | ||
typeAnnotation: { | ||
type: 'NumberTypeAnnotation', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
moduleNames: ['Calculator'], | ||
}, | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
all: SCHEMA_WITH_TM_AND_FC, | ||
}; |
69 changes: 69 additions & 0 deletions
69
packages/react-native-codegen/src/generators/__tests__/RNCodegen-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails oncall+react_native | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const rnCodegen = require('../RNCodegen.js'); | ||
const fixture = require('../__test_fixtures__/fixtures.js'); | ||
const path = require('path'); | ||
|
||
const invalidDirectory = 'invalid/'; | ||
const packageName = 'na'; | ||
const componentsOutputDir = 'react/renderer/components/library'; | ||
const modulesOutputDir = 'library'; | ||
|
||
describe('RNCodegen.generate', () => { | ||
it('when type `all`', () => { | ||
const expectedPaths = { | ||
'library.h': modulesOutputDir, | ||
'library-generated.mm': modulesOutputDir, | ||
'ShadowNodes.h': componentsOutputDir, | ||
'ShadowNodes.cpp': componentsOutputDir, | ||
'Props.h': componentsOutputDir, | ||
'Props.cpp': componentsOutputDir, | ||
'RCTComponentViewHelpers.h': componentsOutputDir, | ||
'EventEmitters.h': componentsOutputDir, | ||
'EventEmitters.cpp': componentsOutputDir, | ||
'ComponentDescriptors.h': componentsOutputDir, | ||
}; | ||
|
||
jest.mock('fs', () => ({ | ||
existsSync: location => { | ||
return true; | ||
}, | ||
writeFileSync: (location, content) => { | ||
let receivedDir = path.dirname(location); | ||
let receivedBasename = path.basename(location); | ||
|
||
let expectedPath = expectedPaths[receivedBasename]; | ||
expect(receivedDir).toEqual(expectedPath); | ||
}, | ||
})); | ||
|
||
const res = rnCodegen.generate( | ||
{ | ||
libraryName: 'library', | ||
schema: fixture.all, | ||
outputDirectory: invalidDirectory, | ||
packageName: packageName, | ||
assumeNonnull: true, | ||
componentsOutputDir: componentsOutputDir, | ||
modulesOutputDir: modulesOutputDir, | ||
}, | ||
{ | ||
generators: ['componentsIOS', 'modulesIOS'], | ||
test: false, | ||
}, | ||
); | ||
|
||
expect(res).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.