Skip to content

Commit

Permalink
Introduce "codegenConfig.isLibrary" property (facebook#41655)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#41655

This diff adds support for checked-in codegen artifacts for libraries.
It introduces a new property to `coegenConfig`, called `isLibrary`. If codegen sees `isLibrary: true` in a project's dependency, it assumes that the library has codegen artifacts in it, and will not generate any code.

Changelog: [General][Added] - Introduce "codegenConfig.isLibrary" property.

Differential Revision: https://internalfb.com/D51207265

fbshipit-source-id: 74f8b3651de3f4d921a5be5aa7a905c964b43680
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Dec 15, 2023
1 parent d45a01d commit df825ed
Showing 1 changed file with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ function computeOutputPath(projectRoot, baseOutputPath, pkgJson) {
baseOutputPath = projectRoot;
}
}
return path.join(baseOutputPath, 'build', 'generated', 'ios');
if (pkgJson.codegenConfig.isLibrary) {
// Don't create nested directories for libraries to make importing generated headers easier.
return baseOutputPath;
} else {
return path.join(baseOutputPath, 'build', 'generated', 'ios');
}
}

function generateSchemaInfo(library) {
Expand Down Expand Up @@ -260,6 +265,16 @@ function needsThirdPartyComponentProvider(schemaInfo) {
return !isReactNativeCoreLibrary(schemaInfo.library.config.name);
}

function mustGenerateNativeCode(includeLibraryPath, schemaInfo) {
// If library's 'codegenConfig' sets 'isLibrary' to 'true',
// then we assume that native code is shipped with the library,
// and we don't need to generate it.
return (
schemaInfo.library.libraryPath === includeLibraryPath ||
!schemaInfo.library.config.isLibrary
);
}

function createComponentProvider(schemas) {
console.log('\n\n>>>>> Creating component provider');
const outputDir = path.join(
Expand All @@ -281,10 +296,12 @@ function createComponentProvider(schemas) {
}

function findCodegenEnabledLibraries(pkgJson, projectRoot) {
return [
...findExternalLibraries(pkgJson),
...findProjectRootLibraries(pkgJson, projectRoot),
];
const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot);
if (pkgJson.codegenConfig.isLibrary) {
return projectLibraries;
} else {
return [...projectLibraries, ...findExternalLibraries(pkgJson)];
}
}

// It removes all the empty files and empty folders
Expand Down Expand Up @@ -355,12 +372,19 @@ function execute(projectRoot, baseOutputPath) {
const outputPath = computeOutputPath(projectRoot, baseOutputPath, pkgJson);

const schemaInfos = generateSchemaInfos(libraries);
generateNativeCode(outputPath, schemaInfos);
generateNativeCode(
outputPath,
schemaInfos.filter(schemaInfo =>
mustGenerateNativeCode(projectRoot, schemaInfo),
),
);

const schemas = schemaInfos
.filter(needsThirdPartyComponentProvider)
.map(schemaInfo => schemaInfo.schema);
createComponentProvider(schemas);
if (!pkgJson.codegenConfig.isLibrary) {
const schemas = schemaInfos
.filter(needsThirdPartyComponentProvider)
.map(schemaInfo => schemaInfo.schema);
createComponentProvider(schemas);
}
cleanupEmptyFilesAndFolders(outputPath);
} catch (err) {
console.error(err);
Expand Down

0 comments on commit df825ed

Please sign in to comment.