|
4 | 4 | # This source code is licensed under the MIT license found in the |
5 | 5 | # LICENSE file in the root directory of this source tree. |
6 | 6 |
|
7 | | -# This script collects the JavaScript spec definitions for native |
8 | | -# modules, then uses react-native-codegen to generate native code. |
9 | | -# The script will copy the generated code to the final location by |
10 | | -# default. Optionally, call the script with a path to the desired |
11 | | -# output location. |
| 7 | +# This script collects the JavaScript spec definitions for core |
| 8 | +# native modules, then uses react-native-codegen to generate |
| 9 | +# native code. |
| 10 | +# The script will use the local react-native-codegen package by |
| 11 | +# default. Optionally, set the CODEGEN_PATH to point to the |
| 12 | +# desired codegen library (e.g. when using react-native-codegen |
| 13 | +# from npm). |
12 | 14 | # |
13 | 15 | # Usage: |
14 | | -# ./scripts/generate-native-modules-specs.sh [output-dir] |
| 16 | +# ./scripts/generate-native-modules-specs.sh |
15 | 17 | # |
16 | 18 | # Example: |
17 | | -# ./scripts/generate-native-modules-specs.sh ./codegen-out |
| 19 | +# CODEGEN_PATH=.. ./scripts/generate-native-modules-specs.sh |
18 | 20 |
|
19 | 21 | # shellcheck disable=SC2038 |
20 | 22 |
|
21 | 23 | set -e |
22 | 24 |
|
23 | 25 | THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd) |
| 26 | +TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX) |
24 | 27 | RN_DIR=$(cd "$THIS_DIR/.." && pwd) |
25 | | -CODEGEN_DIR=$(cd "$RN_DIR/packages/react-native-codegen" && pwd) |
26 | | -OUTPUT_DIR="${1:-$RN_DIR/Libraries/FBReactNativeSpec/FBReactNativeSpec}" |
27 | | -SCHEMA_FILE="$RN_DIR/schema-native-modules.json" |
| 28 | +CODEGEN_PATH="${CODEGEN_PATH:-$(cd "$RN_DIR/packages" && pwd)}" |
| 29 | +CODEGEN_DIR="$CODEGEN_PATH/react-native-codegen" |
| 30 | + |
28 | 31 | YARN_BINARY="${YARN_BINARY:-$(command -v yarn)}" |
29 | 32 |
|
| 33 | +cleanup () { |
| 34 | + set +e |
| 35 | + rm -rf "$TEMP_DIR" |
| 36 | + set -e |
| 37 | +} |
| 38 | + |
30 | 39 | describe () { |
31 | 40 | printf "\\n\\n>>>>> %s\\n\\n\\n" "$1" |
32 | 41 | } |
33 | 42 |
|
34 | 43 | step_build_codegen () { |
35 | | - describe "Building react-native-codegen package" |
36 | | - pushd "$CODEGEN_DIR" >/dev/null || exit |
37 | | - "$YARN_BINARY" |
38 | | - "$YARN_BINARY" build |
39 | | - popd >/dev/null || exit |
| 44 | + if [ ! -d "$CODEGEN_DIR/lib" ]; then |
| 45 | + describe "Building react-native-codegen package" |
| 46 | + pushd "$CODEGEN_DIR" >/dev/null || exit |
| 47 | + "$YARN_BINARY" |
| 48 | + "$YARN_BINARY" build |
| 49 | + popd >/dev/null || exit |
| 50 | + fi |
40 | 51 | } |
41 | 52 |
|
42 | | -step_gen_schema () { |
| 53 | +run_codegen () { |
| 54 | + SRCS_DIR=$1 |
| 55 | + LIBRARY_NAME=$2 |
| 56 | + OUTPUT_DIR=$3 |
| 57 | + |
| 58 | + SCHEMA_FILE="$TEMP_DIR/schema-$LIBRARY_NAME.json" |
| 59 | + |
| 60 | + if [ ! -d "$CODEGEN_DIR/lib" ]; then |
| 61 | + describe "Building react-native-codegen package" |
| 62 | + pushd "$CODEGEN_DIR" >/dev/null || exit |
| 63 | + "$YARN_BINARY" |
| 64 | + "$YARN_BINARY" build |
| 65 | + popd >/dev/null || exit |
| 66 | + fi |
| 67 | + |
43 | 68 | describe "Generating schema from flow types" |
44 | | - SRCS_DIR=$(cd "$RN_DIR/Libraries" && pwd) |
45 | 69 | "$YARN_BINARY" node "$CODEGEN_DIR/lib/cli/combine/combine-js-to-schema-cli.js" "$SCHEMA_FILE" "$SRCS_DIR" |
46 | | -} |
47 | 70 |
|
48 | | -step_gen_specs () { |
49 | 71 | describe "Generating native code from schema (iOS)" |
50 | 72 | pushd "$RN_DIR" >/dev/null || exit |
51 | 73 | "$YARN_BINARY" --silent node scripts/generate-native-modules-specs-cli.js ios "$SCHEMA_FILE" "$OUTPUT_DIR" |
52 | 74 | popd >/dev/null || exit |
53 | 75 | } |
54 | 76 |
|
55 | | -step_build_codegen |
56 | | -step_gen_schema |
57 | | -step_gen_specs |
| 77 | +# Handle Core Modules |
| 78 | +run_codegen_core_modules () { |
| 79 | + LIBRARY_NAME="FBReactNativeSpec" |
| 80 | + SRCS_DIR=$(cd "$RN_DIR/Libraries" && pwd) |
| 81 | + OUTPUT_DIR="$SRCS_DIR/$LIBRARY_NAME/$LIBRARY_NAME" |
| 82 | + |
| 83 | + run_codegen "$SRCS_DIR" "$LIBRARY_NAME" "$OUTPUT_DIR" |
| 84 | +} |
| 85 | + |
| 86 | +main() { |
| 87 | + run_codegen_core_modules |
| 88 | +} |
| 89 | + |
| 90 | +trap cleanup EXIT |
| 91 | +main "$@" |
0 commit comments