forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate ThirdpartyFabricComponentsProvider to extend RCTFabricCompon…
…entsPlugins Summary: Currently, we don't have a way to extend RCTFabricComponentsPlugins in OSS. This diff adds a codegen to generate ThirdpartyFabricComponentsProviderwhich will be used to extend RCTFabricComponentsPlugins. It works almost exactly the same as RCTFabricComponentsPlugins, and in the future we might want to consider merging them together. Changelog: [internal] Reviewed By: hramos Differential Revision: D32128760 fbshipit-source-id: b4f3a082f94c3053251cc6a0323c488f649deaa9
- Loading branch information
1 parent
b27a83b
commit d065b06
Showing
7 changed files
with
454 additions
and
9 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
105 changes: 105 additions & 0 deletions
105
...t-native-codegen/src/generators/components/GenerateThirdPartyFabricComponentsProviderH.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,105 @@ | ||
/** | ||
* 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 {SchemaType} from '../../CodegenSchema'; | ||
|
||
// File path -> contents | ||
type FilesOutput = Map<string, string>; | ||
|
||
const template = ` | ||
/* | ||
* ${'C'}opyright (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 GenerateRCTThirdPartyFabricComponentsProviderH | ||
*/ | ||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wreturn-type-c-linkage" | ||
#import <React/RCTComponentViewProtocol.h> | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char *name); | ||
::_LOOKUP_FUNCS_:: | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#pragma GCC diagnostic pop | ||
`; | ||
|
||
const lookupFuncTemplate = ` | ||
Class<RCTComponentViewProtocol> ::_CLASSNAME_::Cls(void) __attribute__((used)); // ::_LIBRARY_NAME_:: | ||
`.trim(); | ||
|
||
module.exports = { | ||
generate(schemas: {[string]: SchemaType}): FilesOutput { | ||
const fileName = 'RCTThirdPartyFabricComponentsProvider.h'; | ||
|
||
const lookupFuncs = Object.keys(schemas) | ||
.map(libraryName => { | ||
const schema = schemas[libraryName]; | ||
return Object.keys(schema.modules) | ||
.map(moduleName => { | ||
const module = schema.modules[moduleName]; | ||
if (module.type !== 'Component') { | ||
return; | ||
} | ||
|
||
const {components} = module; | ||
// No components in this module | ||
if (components == null) { | ||
return null; | ||
} | ||
|
||
return Object.keys(components) | ||
.filter(componentName => { | ||
const component = components[componentName]; | ||
return !( | ||
component.excludedPlatforms && | ||
component.excludedPlatforms.includes('iOS') | ||
); | ||
}) | ||
.map(componentName => { | ||
const component = components[componentName]; | ||
if (component.interfaceOnly === true) { | ||
return; | ||
} | ||
|
||
return lookupFuncTemplate | ||
.replace(/::_LIBRARY_NAME_::/g, libraryName) | ||
.replace(/::_CLASSNAME_::/g, componentName); | ||
}) | ||
.join('\n'); | ||
}) | ||
.filter(Boolean) | ||
.join('\n'); | ||
}) | ||
.join('\n'); | ||
|
||
const replacedTemplate = template.replace( | ||
/::_LOOKUP_FUNCS_::/g, | ||
lookupFuncs, | ||
); | ||
|
||
return new Map([[fileName, replacedTemplate]]); | ||
}, | ||
}; |
99 changes: 99 additions & 0 deletions
99
...ive-codegen/src/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.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,99 @@ | ||
/** | ||
* 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 {SchemaType} from '../../CodegenSchema'; | ||
|
||
// File path -> contents | ||
type FilesOutput = Map<string, string>; | ||
|
||
const template = ` | ||
/** | ||
* ${'C'}opyright (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 GenerateRCTThirdPartyFabricComponentsProviderCpp | ||
*/ | ||
// OSS-compatibility layer | ||
#import "RCTThirdPartyFabricComponentsProvider.h" | ||
#import <string> | ||
#import <unordered_map> | ||
Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char *name) { | ||
static std::unordered_map<std::string, Class (*)(void)> sFabricComponentsClassMap = { | ||
::_LOOKUP_MAP_:: | ||
}; | ||
auto p = sFabricComponentsClassMap.find(name); | ||
if (p != sFabricComponentsClassMap.end()) { | ||
auto classFunc = p->second; | ||
return classFunc(); | ||
} | ||
return nil; | ||
} | ||
`; | ||
|
||
const lookupMapTemplate = ` | ||
{"::_CLASSNAME_::", ::_CLASSNAME_::Cls}, // ::_LIBRARY_NAME_::`; | ||
|
||
module.exports = { | ||
generate(schemas: {[string]: SchemaType}): FilesOutput { | ||
const fileName = 'RCTThirdPartyFabricComponentsProvider.mm'; | ||
|
||
const lookupMap = Object.keys(schemas) | ||
.map(libraryName => { | ||
const schema = schemas[libraryName]; | ||
return Object.keys(schema.modules) | ||
.map(moduleName => { | ||
const module = schema.modules[moduleName]; | ||
if (module.type !== 'Component') { | ||
return; | ||
} | ||
|
||
const {components} = module; | ||
// No components in this module | ||
if (components == null) { | ||
return null; | ||
} | ||
|
||
return Object.keys(components) | ||
.filter(componentName => { | ||
const component = components[componentName]; | ||
return !( | ||
component.excludedPlatforms && | ||
component.excludedPlatforms.includes('iOS') | ||
); | ||
}) | ||
.map(componentName => { | ||
if (components[componentName].interfaceOnly === true) { | ||
return; | ||
} | ||
const replacedTemplate = lookupMapTemplate | ||
.replace(/::_CLASSNAME_::/g, componentName) | ||
.replace(/::_LIBRARY_NAME_::/g, libraryName); | ||
|
||
return replacedTemplate; | ||
}); | ||
}) | ||
.filter(Boolean); | ||
}) | ||
.join('\n'); | ||
|
||
const replacedTemplate = template.replace(/::_LOOKUP_MAP_::/g, lookupMap); | ||
|
||
return new Map([[fileName, replacedTemplate]]); | ||
}, | ||
}; |
21 changes: 21 additions & 0 deletions
21
...n/src/generators/components/__tests__/GenerateThirdPartyFabricComponentsProviderH-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,21 @@ | ||
/** | ||
* 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. | ||
* | ||
* @emails oncall+react_native | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fixtures = require('../__test_fixtures__/fixtures.js'); | ||
const generator = require('../GenerateThirdPartyFabricComponentsProviderH.js'); | ||
|
||
describe('GenerateThirdPartyFabricComponentsProviderH', () => { | ||
it(`can generate fixtures`, () => { | ||
expect(generator.generate(fixtures)).toMatchSnapshot(); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
.../generators/components/__tests__/GenerateThirdPartyFabricComponentsProviderObjCpp-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,21 @@ | ||
/** | ||
* 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. | ||
* | ||
* @emails oncall+react_native | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fixtures = require('../__test_fixtures__/fixtures.js'); | ||
const generator = require('../GenerateThirdPartyFabricComponentsProviderObjCpp.js'); | ||
|
||
describe('GenerateThirdPartyFabricComponentsProviderObjCpp', () => { | ||
it(`can generate fixtures`, () => { | ||
expect(generator.generate(fixtures)).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.