From cfb6c3cd0a753335987bfde02cdafbe21d3a297a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Mon, 23 Aug 2021 17:19:00 -0700 Subject: [PATCH] Codegen: Add codegen.js wrapper around generate-specs.sh Summary: Adds a simple wrapper around the generate-specs.sh bash script. Supports optional flags. Usage: `node ./codegen.js --srcs ./js --modules_library_name FBReactNativeSpec` Remove unused `USE_FABRIC` envvar code from `generate-specs.sh`. Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D30439132 fbshipit-source-id: 07099c1d899606ac2e679fac6d32ea2fa4af40fc --- scripts/codegen.js | 54 +++++++++++++++++++++++++++++++++++++++ scripts/generate-specs.sh | 6 ++--- 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 scripts/codegen.js diff --git a/scripts/codegen.js b/scripts/codegen.js new file mode 100644 index 00000000000000..12ff0917dd6642 --- /dev/null +++ b/scripts/codegen.js @@ -0,0 +1,54 @@ +#!/usr/bin/env node +/** + * 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'; + +require('shelljs/global'); +const yargs = require('yargs'); +const execSync = require('child_process').execSync; + +let argv = yargs.option('srcs', { + alias: 'srcs_dir', + type: 'string', + description: 'Path to JavaScript sources', +}).option('modules_library_name', { + type: 'string', + description: 'Native modules interfaces library name', +}).option('modules_output_dir', { + type: 'string', + description: 'Native modules interfaces output dir', +}).option('components_library_name', { + type: 'string', + description: 'Native components interfaces library name', +}).option('components_output_dir', { + type: 'string', + description: 'Native components interfaces output dir', +}).argv; + +let env_vars = []; +const { srcs_dir, modules_library_name, modules_output_dir, components_library_name, components_output_dir } = argv; + +if (srcs_dir) { + env_vars.push(`SRCS_DIR=${srcs_dir}`); +} +if (modules_library_name) { + env_vars.push(`MODULES_LIBRARY_NAME=${modules_library_name}`); +} +if (modules_output_dir) { + env_vars.push(`MODULES_OUTPUT_DIR=${modules_output_dir}`); +} +if (components_library_name) { + env_vars.push(`COMPONENTS_LIBRARY_NAME=${components_library_name}`); +} +if (components_output_dir) { + env_vars.push(`COMPONENTS_OUTPUT_DIR=${components_output_dir}`); +} + +execSync(`${env_vars.join(' ')} ./generate-specs.sh`); diff --git a/scripts/generate-specs.sh b/scripts/generate-specs.sh index b66f4c9b933ffe..4456557dfbb6cb 100755 --- a/scripts/generate-specs.sh +++ b/scripts/generate-specs.sh @@ -27,7 +27,6 @@ set -e THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd) TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX) RN_DIR=$(cd "$THIS_DIR/.." && pwd) -USE_FABRIC="${USE_FABRIC:-0}" # Find path to Node # shellcheck source=/dev/null @@ -42,7 +41,7 @@ cleanup () { } describe () { - printf "\\n\\n>>>>> %s\\n\\n\\n" "$1" + printf "\\n\\n>>>>> %s\\n\\n\\n" "$1" >&2 } main() { @@ -80,7 +79,7 @@ main() { bash "$CODEGEN_PATH/scripts/oss/build.sh" fi - describe "Generating schema from flow types" + describe "Generating schema from Flow types" "$NODE_BINARY" "$CODEGEN_PATH/lib/cli/combine/combine-js-to-schema-cli.js" "$SCHEMA_FILE" "$SRCS_DIR" describe "Generating native code from schema (iOS)" @@ -88,7 +87,6 @@ main() { "$NODE_BINARY" scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$TEMP_OUTPUT_DIR" "$MODULES_LIBRARY_NAME" popd >/dev/null || exit 1 - describe "Copying output to final directory" mkdir -p "$COMPONENTS_OUTPUT_DIR" "$MODULES_OUTPUT_DIR" cp -R "$TEMP_OUTPUT_DIR/$MODULES_LIBRARY_NAME.h" "$TEMP_OUTPUT_DIR/$MODULES_LIBRARY_NAME-generated.mm" "$MODULES_OUTPUT_DIR" || exit 1 find "$TEMP_OUTPUT_DIR" -type f | xargs sed -i.bak "s/$MODULES_LIBRARY_NAME/$COMPONENTS_LIBRARY_NAME/g" || exit 1