Skip to content

Commit

Permalink
Refactor generate-artifacts-executor.js: decouple schema and native f…
Browse files Browse the repository at this point in the history
…iles generation (facebook#41555)

Summary:
Pull Request resolved: facebook#41555

This diff splits `generateNativeCodegenFiles` into two simpler steps: `generateSchemaInfos` and `generateCode`. `SchemaInfo` is a (library, schema) pair, it is convenient for further transformations.
Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D51204077

fbshipit-source-id: 8a1f585a79a2a0241b544a8a131b59250d803e2e
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Nov 22, 2023
1 parent 3ecf1bc commit 980fb0c
Showing 1 changed file with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,57 +256,62 @@ function computeIOSOutputDir(outputPath, appRootDir) {
return path.join(outputPath ? outputPath : appRootDir, 'build/generated/ios');
}

function generateSchema(library) {
function generateSchemaInfo(library) {
const pathToJavaScriptSources = path.join(
library.libraryPath,
library.config.jsSrcsDir,
);
console.log(`\n\n[Codegen] >>>>> Processing ${library.config.name}`);
// Generate one schema for the entire library...
return utils
.getCombineJSToSchema()
.combineSchemasInFileList([pathToJavaScriptSources], 'ios');
return {
library: library,
schema: utils
.getCombineJSToSchema()
.combineSchemasInFileList([pathToJavaScriptSources], 'ios'),
};
}

function generateCode(iosOutputDir, library, tmpDir, schema) {
// ...then generate native code artifacts.
const libraryTypeArg = library.config.type ? `${library.config.type}` : '';

function generateCode(iosOutputDir, schemaInfo) {
const tmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), schemaInfo.library.config.name),
);
const tmpOutputDir = path.join(tmpDir, 'out');
fs.mkdirSync(tmpOutputDir, {recursive: true});

generateSpecsCLIExecutor.generateSpecFromInMemorySchema(
'ios',
schema,
schemaInfo.schema,
tmpOutputDir,
library.config.name,
schemaInfo.library.config.name,
'com.facebook.fbreact.specs',
libraryTypeArg,
schemaInfo.library.config.type,
);

// Finally, copy artifacts to the final output directory.
const outputDir =
CORE_LIBRARIES_WITH_OUTPUT_FOLDER[library.config.name] ?? iosOutputDir;
CORE_LIBRARIES_WITH_OUTPUT_FOLDER[schemaInfo.library.config.name] ??
iosOutputDir;
fs.mkdirSync(outputDir, {recursive: true});
// TODO: Fix this. This will not work on Windows.
execSync(`cp -R ${tmpOutputDir}/* "${outputDir}"`);
console.log(`[Codegen] Generated artifacts: ${iosOutputDir}`);
console.log(`[Codegen] Generated artifacts: ${outputDir}`);
}

function generateNativeCodegenFiles(libraries, iosOutputDir) {
const schemas = {};
libraries.forEach(library => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), library.config.name));
const schema = generateSchema(library);
generateCode(iosOutputDir, library, tmpDir, schema);

// Filter the react native core library out.
// In the future, core library and third party library should
// use the same way to generate/register the fabric components.
if (!isReactNativeCoreLibrary(library.config.name)) {
schemas[library.config.name] = schema;
}
function generateSchemaInfos(libraries) {
return libraries.map(generateSchemaInfo);
}

function generateNativeCode(iosOutputDir, schemaInfos) {
return schemaInfos.map(schemaInfo => {
generateCode(iosOutputDir, schemaInfo);
});
return schemas;
}

function needsThirdPartyComponentProvider(schemaInfo) {
// Filter the react native core library out.
// In the future, core library and third party library should
// use the same way to generate/register the fabric components.
return !isReactNativeCoreLibrary(schemaInfo.library.config.name);
}

function createComponentProvider(schemas) {
Expand Down Expand Up @@ -409,8 +414,12 @@ function execute(appRootDir, outputPath, baseCodegenConfigFileDir) {

const iosOutputDir = computeIOSOutputDir(outputPath, appRootDir);

const schemas = generateNativeCodegenFiles(libraries, iosOutputDir);
const schemaInfos = generateSchemaInfos(libraries);
generateNativeCode(iosOutputDir, schemaInfos);

const schemas = schemaInfos
.filter(needsThirdPartyComponentProvider)
.map(schemaInfo => schemaInfo.schema);
createComponentProvider(schemas);
cleanupEmptyFilesAndFolders(iosOutputDir);
} catch (err) {
Expand Down

0 comments on commit 980fb0c

Please sign in to comment.