Skip to content

Commit 3ecf1bc

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Refactor generate-artifacts-executor.js: keep schema in memory instead of writing/reading from disk (facebook#41554)
Summary: Pull Request resolved: facebook#41554 This diff removes inefficiency where we first write schema to disk in `combine-js-to-schema.js`, and then read it from disk in `generate-specs-cli-executor.js`. With this change we can just pass it as an argument. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D51161162 fbshipit-source-id: 35d14ca3e53e4bf999520c635c66909c20081096
1 parent e989d0b commit 3ecf1bc

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
'use strict';
1313

14-
const {combineSchemasInFileList} = require('./combine-js-to-schema');
14+
const {
15+
combineSchemasInFileListAndWriteToFile,
16+
} = require('./combine-js-to-schema');
1517
const {parseArgs} = require('./combine-utils');
1618

1719
const {platform, outfile, fileList} = parseArgs(process.argv);
1820

19-
combineSchemasInFileList(fileList, platform, outfile);
21+
combineSchemasInFileListAndWriteToFile(fileList, platform, outfile);

packages/react-native-codegen/src/cli/combine/combine-js-to-schema.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,29 @@ function expandDirectoriesIntoFiles(
7272
function combineSchemasInFileList(
7373
fileList: Array<string>,
7474
platform: ?string,
75-
outfile: string,
76-
): void {
75+
): SchemaType {
7776
const expandedFileList = expandDirectoriesIntoFiles(fileList, platform);
7877
const combined = combineSchemas(expandedFileList);
7978
if (Object.keys(combined.modules).length === 0) {
8079
console.error(
8180
'No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules.',
8281
);
8382
}
83+
return combined;
84+
}
85+
86+
function combineSchemasInFileListAndWriteToFile(
87+
fileList: Array<string>,
88+
platform: ?string,
89+
outfile: string,
90+
): void {
91+
const combined = combineSchemasInFileList(fileList, platform);
8492
const formattedSchema = JSON.stringify(combined, null, 2);
8593
fs.writeFileSync(outfile, formattedSchema);
8694
}
8795

8896
module.exports = {
8997
combineSchemas,
9098
combineSchemasInFileList,
99+
combineSchemasInFileListAndWriteToFile,
91100
};

packages/react-native/scripts/codegen/generate-artifacts-executor.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,31 +256,28 @@ function computeIOSOutputDir(outputPath, appRootDir) {
256256
return path.join(outputPath ? outputPath : appRootDir, 'build/generated/ios');
257257
}
258258

259-
function generateSchema(tmpDir, library) {
260-
const pathToSchema = path.join(tmpDir, 'schema.json');
259+
function generateSchema(library) {
261260
const pathToJavaScriptSources = path.join(
262261
library.libraryPath,
263262
library.config.jsSrcsDir,
264263
);
265264
console.log(`\n\n[Codegen] >>>>> Processing ${library.config.name}`);
266265
// Generate one schema for the entire library...
267-
utils
266+
return utils
268267
.getCombineJSToSchema()
269-
.combineSchemasInFileList([pathToJavaScriptSources], 'ios', pathToSchema);
270-
console.log(`[Codegen] Generated schema: ${pathToSchema}`);
271-
return pathToSchema;
268+
.combineSchemasInFileList([pathToJavaScriptSources], 'ios');
272269
}
273270

274-
function generateCode(iosOutputDir, library, tmpDir, pathToSchema) {
271+
function generateCode(iosOutputDir, library, tmpDir, schema) {
275272
// ...then generate native code artifacts.
276273
const libraryTypeArg = library.config.type ? `${library.config.type}` : '';
277274

278275
const tmpOutputDir = path.join(tmpDir, 'out');
279276
fs.mkdirSync(tmpOutputDir, {recursive: true});
280277

281-
generateSpecsCLIExecutor.execute(
278+
generateSpecsCLIExecutor.generateSpecFromInMemorySchema(
282279
'ios',
283-
pathToSchema,
280+
schema,
284281
tmpOutputDir,
285282
library.config.name,
286283
'com.facebook.fbreact.specs',
@@ -295,34 +292,31 @@ function generateCode(iosOutputDir, library, tmpDir, pathToSchema) {
295292
console.log(`[Codegen] Generated artifacts: ${iosOutputDir}`);
296293
}
297294

298-
function generateNativeCodegenFiles(libraries, iosOutputDir, schemaPaths) {
295+
function generateNativeCodegenFiles(libraries, iosOutputDir) {
296+
const schemas = {};
299297
libraries.forEach(library => {
300298
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), library.config.name));
301-
const pathToSchema = generateSchema(tmpDir, library);
302-
generateCode(iosOutputDir, library, tmpDir, pathToSchema);
299+
const schema = generateSchema(library);
300+
generateCode(iosOutputDir, library, tmpDir, schema);
303301

304302
// Filter the react native core library out.
305303
// In the future, core library and third party library should
306304
// use the same way to generate/register the fabric components.
307305
if (!isReactNativeCoreLibrary(library.config.name)) {
308-
schemaPaths[library.config.name] = pathToSchema;
306+
schemas[library.config.name] = schema;
309307
}
310308
});
309+
return schemas;
311310
}
312311

313-
function createComponentProvider(schemaPaths) {
312+
function createComponentProvider(schemas) {
314313
console.log('\n\n>>>>> Creating component provider');
315314
const outputDir = path.join(
316315
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
317316
'React',
318317
'Fabric',
319318
);
320319
mkdirp.sync(outputDir);
321-
const schemas = {};
322-
for (const libraryName of Object.keys(schemaPaths)) {
323-
const tmpSchemaText = fs.readFileSync(schemaPaths[libraryName], 'utf-8');
324-
schemas[libraryName] = JSON.parse(tmpSchemaText);
325-
}
326320
utils.getCodegen().generateFromSchemas(
327321
{
328322
schemas: schemas,
@@ -413,13 +407,11 @@ function execute(appRootDir, outputPath, baseCodegenConfigFileDir) {
413407
return;
414408
}
415409

416-
const schemaPaths = {};
417-
418410
const iosOutputDir = computeIOSOutputDir(outputPath, appRootDir);
419411

420-
generateNativeCodegenFiles(libraries, iosOutputDir, schemaPaths);
412+
const schemas = generateNativeCodegenFiles(libraries, iosOutputDir);
421413

422-
createComponentProvider(schemaPaths);
414+
createComponentProvider(schemas);
423415
cleanupEmptyFilesAndFolders(iosOutputDir);
424416
} catch (err) {
425417
console.error(err);

packages/react-native/scripts/codegen/generate-specs-cli-executor.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,15 @@ function validateLibraryType(libraryType) {
7070
}
7171
}
7272

73-
function generateSpec(
73+
function generateSpecFromInMemorySchema(
7474
platform,
75-
schemaPath,
75+
schema,
7676
outputDirectory,
7777
libraryName,
7878
packageName,
7979
libraryType,
8080
) {
8181
validateLibraryType(libraryType);
82-
83-
let schema = readAndParseSchema(schemaPath);
84-
8582
createOutputDirectoryIfNeeded(outputDirectory, libraryName);
8683
function composePath(intermediate) {
8784
return path.join(outputDirectory, intermediate, libraryName);
@@ -121,6 +118,25 @@ function generateSpec(
121118
}
122119
}
123120

121+
function generateSpec(
122+
platform,
123+
schemaPath,
124+
outputDirectory,
125+
libraryName,
126+
packageName,
127+
libraryType,
128+
) {
129+
generateSpecFromInMemorySchema(
130+
platform,
131+
readAndParseSchema(schemaPath),
132+
outputDirectory,
133+
libraryName,
134+
packageName,
135+
libraryType,
136+
);
137+
}
138+
124139
module.exports = {
125140
execute: generateSpec,
141+
generateSpecFromInMemorySchema: generateSpecFromInMemorySchema,
126142
};

0 commit comments

Comments
 (0)