2020-05-08 22:20:23 +03:00
|
|
|
#!/bin/bash
|
|
|
|
# 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.
|
|
|
|
|
2020-12-03 01:25:46 +03:00
|
|
|
# This script collects the JavaScript spec definitions for core
|
2020-12-22 19:33:56 +03:00
|
|
|
# native modules and components, then uses react-native-codegen
|
|
|
|
# to generate native code.
|
2020-05-08 22:20:23 +03:00
|
|
|
#
|
2021-01-05 00:32:03 +03:00
|
|
|
# Optionally, set these envvars to override defaults:
|
|
|
|
# - SRCS_DIR: Path to JavaScript sources
|
2021-04-17 00:40:54 +03:00
|
|
|
# - MODULES_LIBRARY_NAME: Defaults to FBReactNativeSpec
|
|
|
|
# - MODULES_OUTPUT_DIR: Defaults to React/$MODULES_LIBRARY_NAME/$MODULES_LIBRARY_NAME
|
|
|
|
# - COMPONENTS_LIBRARY_NAME: Defaults to rncore
|
|
|
|
# - COMPONENTS_OUTPUT_DIR: Defaults to ReactCommon/react/renderer/components/$COMPONENTS_LIBRARY_NAME
|
2021-01-05 00:32:03 +03:00
|
|
|
#
|
2020-05-08 22:20:23 +03:00
|
|
|
# Usage:
|
2020-12-22 19:33:56 +03:00
|
|
|
# ./scripts/generate-specs.sh
|
2021-04-17 00:40:54 +03:00
|
|
|
# SRCS_DIR=myapp/js MODULES_LIBRARY_NAME=MySpecs MODULES_OUTPUT_DIR=myapp/MySpecs ./scripts/generate-specs.sh
|
2020-05-08 22:20:23 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
# shellcheck disable=SC2038
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
|
2020-12-03 01:25:46 +03:00
|
|
|
TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
|
2020-05-08 22:20:23 +03:00
|
|
|
RN_DIR=$(cd "$THIS_DIR/.." && pwd)
|
2021-02-03 02:06:17 +03:00
|
|
|
NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
|
2020-12-22 19:33:56 +03:00
|
|
|
USE_FABRIC="${USE_FABRIC:-0}"
|
2020-07-01 05:05:16 +03:00
|
|
|
|
2021-04-02 23:55:18 +03:00
|
|
|
# Find path to Node
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
source "$RN_DIR/scripts/find-node.sh"
|
|
|
|
|
2020-12-03 01:25:46 +03:00
|
|
|
cleanup () {
|
|
|
|
set +e
|
|
|
|
rm -rf "$TEMP_DIR"
|
|
|
|
set -e
|
|
|
|
}
|
|
|
|
|
2020-07-01 05:05:16 +03:00
|
|
|
describe () {
|
|
|
|
printf "\\n\\n>>>>> %s\\n\\n\\n" "$1"
|
|
|
|
}
|
2020-05-08 22:20:23 +03:00
|
|
|
|
2020-12-22 19:33:56 +03:00
|
|
|
main() {
|
2021-01-05 00:32:03 +03:00
|
|
|
SRCS_DIR=${SRCS_DIR:-$(cd "$RN_DIR/Libraries" && pwd)}
|
2021-04-17 00:40:54 +03:00
|
|
|
MODULES_LIBRARY_NAME=${MODULES_LIBRARY_NAME:-FBReactNativeSpec}
|
2020-12-22 19:33:56 +03:00
|
|
|
|
2021-04-17 00:40:54 +03:00
|
|
|
COMPONENTS_LIBRARY_NAME=${COMPONENTS_LIBRARY_NAME:-rncore}
|
|
|
|
MODULES_OUTPUT_DIR=${MODULES_OUTPUT_DIR:-"$RN_DIR/React/$MODULES_LIBRARY_NAME/$MODULES_LIBRARY_NAME"}
|
|
|
|
# TODO: $COMPONENTS_PATH should be programmatically specified, and may change with use_frameworks! support.
|
|
|
|
COMPONENTS_PATH="ReactCommon/react/renderer/components"
|
|
|
|
COMPONENTS_OUTPUT_DIR=${COMPONENTS_OUTPUT_DIR:-"$RN_DIR/$COMPONENTS_PATH/$COMPONENTS_LIBRARY_NAME"}
|
2020-12-03 01:25:46 +03:00
|
|
|
|
2021-01-05 00:32:03 +03:00
|
|
|
TEMP_OUTPUT_DIR="$TEMP_DIR/out"
|
2020-12-22 19:33:56 +03:00
|
|
|
SCHEMA_FILE="$TEMP_DIR/schema.json"
|
2020-12-03 01:25:46 +03:00
|
|
|
|
2020-12-30 03:08:29 +03:00
|
|
|
CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
|
|
|
|
CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"
|
|
|
|
|
2021-04-02 23:55:18 +03:00
|
|
|
if [ -z "$NODE_BINARY" ]; then
|
2021-02-03 02:06:17 +03:00
|
|
|
echo "Error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable." 1>&2
|
2021-02-02 01:36:52 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-12-30 03:08:29 +03:00
|
|
|
if [ -d "$CODEGEN_REPO_PATH" ]; then
|
|
|
|
CODEGEN_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
|
|
|
|
elif [ -d "$CODEGEN_NPM_PATH" ]; then
|
|
|
|
CODEGEN_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
|
|
|
|
else
|
|
|
|
echo "Error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-12-03 06:11:05 +03:00
|
|
|
if [ ! -d "$CODEGEN_PATH/lib" ]; then
|
2020-12-03 01:25:46 +03:00
|
|
|
describe "Building react-native-codegen package"
|
2021-02-03 02:06:17 +03:00
|
|
|
bash "$CODEGEN_PATH/scripts/oss/build.sh"
|
|
|
|
fi
|
|
|
|
|
2020-07-01 05:05:16 +03:00
|
|
|
describe "Generating schema from flow types"
|
2021-02-03 02:06:17 +03:00
|
|
|
"$NODE_BINARY" "$CODEGEN_PATH/lib/cli/combine/combine-js-to-schema-cli.js" "$SCHEMA_FILE" "$SRCS_DIR"
|
2020-05-21 04:46:39 +03:00
|
|
|
|
2020-08-01 04:47:45 +03:00
|
|
|
describe "Generating native code from schema (iOS)"
|
2021-02-02 01:36:52 +03:00
|
|
|
pushd "$RN_DIR" >/dev/null || exit 1
|
2021-04-17 00:40:54 +03:00
|
|
|
"$NODE_BINARY" scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$TEMP_OUTPUT_DIR" "$MODULES_LIBRARY_NAME"
|
2021-02-02 01:36:52 +03:00
|
|
|
popd >/dev/null || exit 1
|
2020-05-21 04:46:39 +03:00
|
|
|
|
2021-02-02 01:36:52 +03:00
|
|
|
describe "Copying output to final directory"
|
2021-04-17 00:40:54 +03:00
|
|
|
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
|
|
|
|
find "$TEMP_OUTPUT_DIR" -type f -not -iname "$MODULES_LIBRARY_NAME*" -exec cp '{}' "$COMPONENTS_OUTPUT_DIR/" ';' || exit 1
|
2021-02-02 01:36:52 +03:00
|
|
|
|
|
|
|
echo >&2 'Done.'
|
2020-12-03 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
main "$@"
|