diff --git a/scripts/generate-native-modules-specs-cli.js b/scripts/generate-native-modules-specs-cli.js new file mode 100644 index 00000000000..2b72610e21b --- /dev/null +++ b/scripts/generate-native-modules-specs-cli.js @@ -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(); diff --git a/scripts/generate-native-modules-specs.sh b/scripts/generate-native-modules-specs.sh index 505a7769a33..6f77c36c503 100755 --- a/scripts/generate-native-modules-specs.sh +++ b/scripts/generate-native-modules-specs.sh @@ -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\);/" \ - | 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\);/" \ + | 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"