mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Remove flow-node requirement from native modules codegen script
Summary: Use transpiled `react-native-codegen` scripts. By avoiding use of flow-node, we no longer need to upgrade flow-remove-types in order to run the native modules codegen script. The interface for the script remains the same: ``` ./scripts/generate-native-modules-specs.sh [optionalOutputDir] ``` Changelog: [Internal] Reviewed By: TheSavior Differential Revision: D21629705 fbshipit-source-id: 7714a67e36c8151a3b0b49285b6a6c93a525d7bc
This commit is contained in:
committed by
Facebook GitHub Bot
parent
eb504e613e
commit
2e756e024f
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const RNCodegen = require('../packages/react-native-codegen/lib/generators/RNCodegen.js');
|
||||
const fs = require('fs');
|
||||
const mkdirp = require('mkdirp');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
function generateSpec(schemaPath, outputDirectory) {
|
||||
const libraryName = 'FBReactNativeSpec';
|
||||
const moduleSpecName = 'FBReactNativeSpec';
|
||||
const schemaText = fs.readFileSync(schemaPath, 'utf-8');
|
||||
|
||||
if (schemaText == null) {
|
||||
throw new Error(`Can't find schema at ${schemaPath}`);
|
||||
}
|
||||
|
||||
const tempOutputDirectory = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), 'react-native-codegen-'),
|
||||
);
|
||||
|
||||
let schema;
|
||||
try {
|
||||
schema = JSON.parse(schemaText);
|
||||
} catch (err) {
|
||||
throw new Error(`Can't parse schema to JSON. ${schemaPath}`);
|
||||
}
|
||||
|
||||
RNCodegen.generate(
|
||||
{libraryName, schema, outputDirectory: tempOutputDirectory, moduleSpecName},
|
||||
{
|
||||
generators: [
|
||||
'descriptors',
|
||||
'events',
|
||||
'props',
|
||||
'tests',
|
||||
'shadow-nodes',
|
||||
'modules',
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
if (!outputDirectory) {
|
||||
outputDirectory = path.resolve(
|
||||
__dirname,
|
||||
'..',
|
||||
'Libraries',
|
||||
libraryName,
|
||||
moduleSpecName,
|
||||
);
|
||||
}
|
||||
mkdirp.sync(outputDirectory);
|
||||
|
||||
const fileNames = [`${moduleSpecName}.h`, `${moduleSpecName}-generated.mm`];
|
||||
fileNames.forEach(fileName => {
|
||||
const newOutput = `${tempOutputDirectory}/${fileName}`;
|
||||
const prevOutput = `${outputDirectory}/${fileName}`;
|
||||
fs.copyFileSync(newOutput, prevOutput);
|
||||
});
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const schemaPath = args[0];
|
||||
const outputDir = args[1];
|
||||
generateSpec(schemaPath, outputDir);
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -27,26 +27,21 @@ describe () {
|
||||
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
|
||||
RN_DIR=$(cd "$THIS_DIR/.." && pwd)
|
||||
|
||||
TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
|
||||
|
||||
SRCS_DIR=$(cd "$RN_DIR/Libraries" && pwd)
|
||||
LIBRARY_NAME="FBReactNativeSpec"
|
||||
MODULE_SPEC_NAME="FBReactNativeSpec"
|
||||
|
||||
SCHEMA_FILE="$RN_DIR/schema-native-modules.json"
|
||||
OUTPUT_DIR="$SRCS_DIR/$LIBRARY_NAME/$MODULE_SPEC_NAME"
|
||||
|
||||
describe "Generating schema from flow types"
|
||||
grep --exclude NativeUIManager.js --include=Native\*.js -rnwl "$SRCS_DIR" -e 'export interface Spec extends TurboModule' -e "export default \(TurboModuleRegistry.get(Enforcing)?<Spec>\('.*\): Spec\);/" \
|
||||
| xargs yarn flow-node packages/react-native-codegen/src/cli/combine/combine-js-to-schema-cli.js "$SCHEMA_FILE"
|
||||
|
||||
describe "Generating native code from schema"
|
||||
yarn flow-node packages/react-native-codegen/buck_tests/generate-tests.js "$SCHEMA_FILE" "$LIBRARY_NAME" "$TEMP_DIR" "$MODULE_SPEC_NAME"
|
||||
|
||||
if [ -n "$1" ]; then
|
||||
OUTPUT_DIR="$1"
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
fi
|
||||
|
||||
describe "Copying $MODULE_SPEC_NAME output to $OUTPUT_DIR"
|
||||
cp "$TEMP_DIR"/$MODULE_SPEC_NAME* "$OUTPUT_DIR/."
|
||||
pushd "$RN_DIR/packages/react-native-codegen" >/dev/null
|
||||
yarn
|
||||
yarn build
|
||||
popd >/dev/null
|
||||
|
||||
describe "Generating schema from flow types"
|
||||
grep --exclude NativeUIManager.js --include=Native\*.js -rnwl "$SRCS_DIR" -e 'export interface Spec extends TurboModule' -e "export default \(TurboModuleRegistry.get(Enforcing)?<Spec>\('.*\): Spec\);/" \
|
||||
| xargs yarn node packages/react-native-codegen/lib/cli/combine/combine-js-to-schema-cli.js "$SCHEMA_FILE"
|
||||
|
||||
describe "Generating native code from schema"
|
||||
yarn node scripts/generate-native-modules-specs-cli.js "$SCHEMA_FILE" "$OUTPUT_DIR"
|
||||
|
||||
Reference in New Issue
Block a user