Skip to content

Commit

Permalink
Introduce "outputDir" property of "codegenConfig" (#41782)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #41782

This diff adds `outputDir` property to `codegenConfig`.
Now codegen output dir is resolved like this:
1. It is set to `outputDir` argument of `generate-codegen-artifacts.js` if it is present.
2. *[New]* It is set to `outputDir` property of `codegenConfig` if it is present.
3. It is set to the project root.

Changelog: [General][Added] - Introduce "outputDir" property of "codegenConfig"

Reviewed By: cipolleschi

Differential Revision: D51494009

fbshipit-source-id: 0f6e3607b29a3c6d228a88a9460d55bb65c7e55a
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Dec 15, 2023
1 parent 5d1eac0 commit d45a01d
Showing 1 changed file with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ function findProjectRootLibraries(pkgJson, projectRoot) {
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app',
);

if (pkgJson.codegenConfig == null) {
console.log(
'[Codegen] The "codegenConfig" field is not defined in package.json. Assuming there is nothing to generate at the app level.',
);
return [];
}

if (typeof pkgJson.codegenConfig !== 'object') {
throw '[Codegen] "codegenConfig" field must be an Object.';
}

return extractLibrariesFromJSON(pkgJson, projectRoot);
}

Expand All @@ -175,11 +186,16 @@ function buildCodegenIfNeeded() {
});
}

function computeIOSOutputDir(outputPath, projectRoot) {
return path.join(
outputPath ? outputPath : projectRoot,
'build/generated/ios',
);
function computeOutputPath(projectRoot, baseOutputPath, pkgJson) {
if (baseOutputPath == null) {
const baseOutputPathOverride = pkgJson.codegenConfig.outputDir;
if (baseOutputPathOverride && typeof baseOutputPathOverride === 'string') {
baseOutputPath = baseOutputPathOverride;
} else {
baseOutputPath = projectRoot;
}
}
return path.join(baseOutputPath, 'build', 'generated', 'ios');
}

function generateSchemaInfo(library) {
Expand Down Expand Up @@ -264,8 +280,7 @@ function createComponentProvider(schemas) {
console.log(`Generated provider in: ${outputDir}`);
}

function findCodegenEnabledLibraries(projectRoot) {
const pkgJson = readPkgJsonInDirectory(projectRoot);
function findCodegenEnabledLibraries(pkgJson, projectRoot) {
return [
...findExternalLibraries(pkgJson),
...findProjectRootLibraries(pkgJson, projectRoot),
Expand Down Expand Up @@ -315,32 +330,38 @@ function cleanupEmptyFilesAndFolders(filepath) {
* - generate the code
*
* @parameter projectRoot: the directory with the app source code, where the package.json lives.
* @parameter outputPath: the base output path for the CodeGen.
* @parameter baseOutputPath: the base output path for the CodeGen.
* @throws If it can't find a config file for react-native.
* @throws If it can't find a CodeGen configuration in the file.
* @throws If it can't find a cli for the CodeGen.
*/
function execute(projectRoot, outputPath) {
buildCodegenIfNeeded();

function execute(projectRoot, baseOutputPath) {
try {
const libraries = findCodegenEnabledLibraries(projectRoot);
console.log(
`[Codegen] Analyzing ${path.join(projectRoot, 'package.json')}`,
);

const pkgJson = readPkgJsonInDirectory(projectRoot);

buildCodegenIfNeeded();

const libraries = findCodegenEnabledLibraries(pkgJson, projectRoot);

if (libraries.length === 0) {
console.log('[Codegen] No codegen-enabled libraries found.');
return;
}

const iosOutputDir = computeIOSOutputDir(outputPath, projectRoot);
const outputPath = computeOutputPath(projectRoot, baseOutputPath, pkgJson);

const schemaInfos = generateSchemaInfos(libraries);
generateNativeCode(iosOutputDir, schemaInfos);
generateNativeCode(outputPath, schemaInfos);

const schemas = schemaInfos
.filter(needsThirdPartyComponentProvider)
.map(schemaInfo => schemaInfo.schema);
createComponentProvider(schemas);
cleanupEmptyFilesAndFolders(iosOutputDir);
cleanupEmptyFilesAndFolders(outputPath);
} catch (err) {
console.error(err);
process.exitCode = 1;
Expand Down

0 comments on commit d45a01d

Please sign in to comment.