Skip to content

Commit

Permalink
Update FabricComponentsProvider to use template functions
Browse files Browse the repository at this point in the history
Summary:
## Rationale
- Consistency with our other generators.
- Past effort, with rationale: D32021441 (3848f48)

Changelog: [Internal]

Reviewed By: sota000

Differential Revision: D32546918

fbshipit-source-id: 5167ca5d7154c0e2fa0867521feaf6a6a382900e
  • Loading branch information
RSNara authored and facebook-github-bot committed Nov 19, 2021
1 parent 0731959 commit 6b1ccef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {SchemaType} from '../../CodegenSchema';
// File path -> contents
type FilesOutput = Map<string, string>;

const template = `
const FileTemplate = ({lookupFuncs}: {lookupFuncs: string}) => `
/*
* ${'C'}opyright (c) Facebook, Inc. and its affiliates.
*
Expand All @@ -36,7 +36,7 @@ extern "C" {
Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char *name);
::_LOOKUP_FUNCS_::
${lookupFuncs}
#ifdef __cplusplus
}
Expand All @@ -46,8 +46,15 @@ Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char
`;

const lookupFuncTemplate = `
Class<RCTComponentViewProtocol> ::_CLASSNAME_::Cls(void) __attribute__((used)); // ::_LIBRARY_NAME_::
const LookupFuncTemplate = ({
className,
libraryName,
}: {
className: string,
libraryName: string,
}) =>
`
Class<RCTComponentViewProtocol> ${className}Cls(void) __attribute__((used)); // ${libraryName}
`.trim();

module.exports = {
Expand Down Expand Up @@ -84,9 +91,10 @@ module.exports = {
return;
}

return lookupFuncTemplate
.replace(/::_LIBRARY_NAME_::/g, libraryName)
.replace(/::_CLASSNAME_::/g, componentName);
return LookupFuncTemplate({
className: componentName,
libraryName,
});
})
.join('\n');
})
Expand All @@ -95,10 +103,9 @@ module.exports = {
})
.join('\n');

const replacedTemplate = template.replace(
/::_LOOKUP_FUNCS_::/g,
const replacedTemplate = FileTemplate({
lookupFuncs,
);
});

return new Map([[fileName, replacedTemplate]]);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {SchemaType} from '../../CodegenSchema';
// File path -> contents
type FilesOutput = Map<string, string>;

const template = `
const FileTemplate = ({lookupMap}: {lookupMap: string}) => `
/**
* ${'C'}opyright (c) Facebook, Inc. and its affiliates.
*
Expand All @@ -34,7 +34,7 @@ const template = `
Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char *name) {
static std::unordered_map<std::string, Class (*)(void)> sFabricComponentsClassMap = {
::_LOOKUP_MAP_::
${lookupMap}
};
auto p = sFabricComponentsClassMap.find(name);
Expand All @@ -46,8 +46,14 @@ Class<RCTComponentViewProtocol> RCTThirdPartyFabricComponentsProvider(const char
}
`;

const lookupMapTemplate = `
{"::_CLASSNAME_::", ::_CLASSNAME_::Cls}, // ::_LIBRARY_NAME_::`;
const LookupMapTemplate = ({
className,
libraryName,
}: {
className: string,
libraryName: string,
}) => `
{"${className}", ${className}Cls}, // ${libraryName}`;

module.exports = {
generate(schemas: {[string]: SchemaType}): FilesOutput {
Expand Down Expand Up @@ -81,9 +87,10 @@ module.exports = {
if (components[componentName].interfaceOnly === true) {
return;
}
const replacedTemplate = lookupMapTemplate
.replace(/::_CLASSNAME_::/g, componentName)
.replace(/::_LIBRARY_NAME_::/g, libraryName);
const replacedTemplate = LookupMapTemplate({
className: componentName,
libraryName,
});

return replacedTemplate;
});
Expand All @@ -92,7 +99,7 @@ module.exports = {
})
.join('\n');

const replacedTemplate = template.replace(/::_LOOKUP_MAP_::/g, lookupMap);
const replacedTemplate = FileTemplate({lookupMap});

return new Map([[fileName, replacedTemplate]]);
},
Expand Down

0 comments on commit 6b1ccef

Please sign in to comment.