react-native-xcode.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Copyright (c) Facebook, Inc. and its affiliates.
  3. #
  4. # This source code is licensed under the MIT license found in the
  5. # LICENSE file in the root directory of this source tree.
  6. # Bundle React Native app's code and image assets.
  7. # This script is supposed to be invoked as part of Xcode build process
  8. # and relies on environment variables (including PWD) set by Xcode
  9. # Print commands before executing them (useful for troubleshooting)
  10. set -x
  11. DEST=$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH
  12. # Enables iOS devices to get the IP address of the machine running Metro
  13. if [[ "$CONFIGURATION" = *Debug* && ! "$PLATFORM_NAME" == *simulator ]]; then
  14. IP=$(ipconfig getifaddr en0)
  15. if [[ -z "$IP" || -n "`ifconfig $value | grep 'baseT'`" ]]; then
  16. IP=$(ipconfig getifaddr en1)
  17. fi
  18. if [ -z "$IP" ]; then
  19. IP=$(ifconfig | grep 'inet ' | grep -v ' 127.' | grep -v ' 169.254.' |cut -d\ -f2 | awk 'NR==1{print $1}')
  20. fi
  21. echo "$IP" > "$DEST/ip.txt"
  22. fi
  23. if [[ "$SKIP_BUNDLING" ]]; then
  24. echo "SKIP_BUNDLING enabled; skipping."
  25. exit 0;
  26. fi
  27. case "$CONFIGURATION" in
  28. *Debug*)
  29. if [[ "$PLATFORM_NAME" == *simulator ]]; then
  30. if [[ "$FORCE_BUNDLING" ]]; then
  31. echo "FORCE_BUNDLING enabled; continuing to bundle."
  32. else
  33. echo "Skipping bundling in Debug for the Simulator (since the packager bundles for you). Use the FORCE_BUNDLING flag to change this behavior."
  34. exit 0;
  35. fi
  36. else
  37. echo "Bundling for physical device. Use the SKIP_BUNDLING flag to change this behavior."
  38. fi
  39. DEV=true
  40. ;;
  41. "")
  42. echo "$0 must be invoked by Xcode"
  43. exit 1
  44. ;;
  45. *)
  46. DEV=false
  47. ;;
  48. esac
  49. # Path to react-native folder inside node_modules
  50. REACT_NATIVE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  51. # The project should be located next to where react-native is installed
  52. # in node_modules.
  53. PROJECT_ROOT=${PROJECT_ROOT:-"$REACT_NATIVE_DIR/../.."}
  54. cd "$PROJECT_ROOT" || exit
  55. # Define NVM_DIR and source the nvm.sh setup script
  56. [ -z "$NVM_DIR" ] && export NVM_DIR="$HOME/.nvm"
  57. # Define entry file
  58. if [[ "$ENTRY_FILE" ]]; then
  59. # Use ENTRY_FILE defined by user
  60. :
  61. elif [[ -s "index.ios.js" ]]; then
  62. ENTRY_FILE=${1:-index.ios.js}
  63. else
  64. ENTRY_FILE=${1:-index.js}
  65. fi
  66. if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
  67. . "$HOME/.nvm/nvm.sh"
  68. elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
  69. . "$(brew --prefix nvm)/nvm.sh"
  70. fi
  71. # Set up the nodenv node version manager if present
  72. if [[ -x "$HOME/.nodenv/bin/nodenv" ]]; then
  73. eval "$("$HOME/.nodenv/bin/nodenv" init -)"
  74. elif [[ -x "$(command -v brew)" && -x "$(brew --prefix nodenv)/bin/nodenv" ]]; then
  75. eval "$("$(brew --prefix nodenv)/bin/nodenv" init -)"
  76. fi
  77. # Set up the ndenv of anyenv if preset
  78. if [[ ! -x node && -d ${HOME}/.anyenv/bin ]]; then
  79. export PATH=${HOME}/.anyenv/bin:${PATH}
  80. if [[ "$(anyenv envs | grep -c ndenv )" -eq 1 ]]; then
  81. eval "$(anyenv init -)"
  82. fi
  83. fi
  84. # check and assign NODE_BINARY env
  85. # shellcheck source=/dev/null
  86. source "$REACT_NATIVE_DIR/scripts/node-binary.sh"
  87. [ -z "$NODE_ARGS" ] && export NODE_ARGS=""
  88. [ -z "$CLI_PATH" ] && export CLI_PATH="$REACT_NATIVE_DIR/cli.js"
  89. [ -z "$BUNDLE_COMMAND" ] && BUNDLE_COMMAND="bundle"
  90. if [[ -z "$BUNDLE_CONFIG" ]]; then
  91. CONFIG_ARG=""
  92. else
  93. CONFIG_ARG="--config $BUNDLE_CONFIG"
  94. fi
  95. BUNDLE_FILE="$DEST/main.jsbundle"
  96. "$NODE_BINARY" $NODE_ARGS "$CLI_PATH" $BUNDLE_COMMAND \
  97. $CONFIG_ARG \
  98. --entry-file "$ENTRY_FILE" \
  99. --platform ios \
  100. --dev $DEV \
  101. --reset-cache \
  102. --bundle-output "$BUNDLE_FILE" \
  103. --assets-dest "$DEST" \
  104. $EXTRA_PACKAGER_ARGS
  105. if [[ $DEV != true && ! -f "$BUNDLE_FILE" ]]; then
  106. echo "error: File $BUNDLE_FILE does not exist. This must be a bug with" >&2
  107. echo "React Native, please report it here: https://github.com/facebook/react-native/issues"
  108. exit 2
  109. fi