setup-runtime 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env sh
  2. # set error-quit, verbose, non-variable-quit
  3. set -eu
  4. # see what system used
  5. __OS__=$(uname -s)
  6. # see what arch used
  7. __ARCH__=$(uname -m)
  8. # format arch name
  9. case $__ARCH__ in
  10. arm64 | aarch64) __ARCH__=aarch64 ;;
  11. x86_64|x64) __ARCH__=x86_64 ;;
  12. *) ;;
  13. esac
  14. # format uname
  15. case $__OS__ in
  16. Darwin) __OS_FIXED__=macos ;;
  17. Linux) __OS_FIXED__=linux ;;
  18. *) echo "Current OS is not supported" && exit 1 ;;
  19. esac
  20. # set project dir
  21. __DIR__=$(cd "$(dirname "$0")" && pwd)
  22. __PROJECT__=$(cd "${__DIR__}"/../ && pwd)
  23. # set download dir
  24. __PHP_RUNTIME_URL__="https://dl.static-php.dev/static-php-cli/bulk/php-8.2.13-cli-${__OS_FIXED__}-${__ARCH__}.tar.gz"
  25. __COMPOSER_URL__="https://getcomposer.org/download/latest-stable/composer.phar"
  26. # use china mirror
  27. # bash bin/setup-runtime --mirror china
  28. mirror=''
  29. while [ $# -gt 0 ]; do
  30. case "$1" in
  31. --mirror)
  32. mirror="$2"
  33. ;;
  34. --*)
  35. echo "Illegal option $1"
  36. ;;
  37. esac
  38. shift $(($# > 0 ? 1 : 0))
  39. done
  40. case "$mirror" in
  41. china)
  42. __PHP_RUNTIME_URL__="https://dl.static-php.dev/static-php-cli/bulk/php-8.2.13-cli-${__OS_FIXED__}-${__ARCH__}.tar.gz"
  43. __COMPOSER_URL__="https://mirrors.tencent.com/composer/composer.phar"
  44. ;;
  45. esac
  46. if ! command -v curl > /dev/null && command -v apk > /dev/null; then
  47. apk add --no-cache curl
  48. fi
  49. test -d "${__PROJECT__}"/downloads || mkdir "${__PROJECT__}"/downloads
  50. # download static php binary
  51. test -f "${__PROJECT__}"/downloads/runtime.tar.gz || { echo "Downloading $__PHP_RUNTIME_URL__ ..." && curl -#fSL -o "${__PROJECT__}"/downloads/runtime.tar.gz "$__PHP_RUNTIME_URL__" ; }
  52. test -f "${__DIR__}"/php || { tar -xf "${__PROJECT__}"/downloads/runtime.tar.gz -C "${__DIR__}"/ ; }
  53. chmod +x "${__DIR__}"/php
  54. # download composer
  55. test -f "${__DIR__}"/composer || curl -#fSL -o "${__DIR__}"/composer "$__COMPOSER_URL__"
  56. chmod +x "${__DIR__}"/composer
  57. # sanity check for php and composer
  58. "${__DIR__}"/php -v >/dev/null || { echo "Failed to run php" && exit 1; }
  59. "${__DIR__}"/php "${__DIR__}"/composer --version >/dev/null || { echo "Failed to run composer" && exit 1; }
  60. echo "Setup runtime OK!"
  61. echo "runtime bin path needs to add manually by command below:"
  62. echo ""
  63. echo " export PATH=\"${__DIR__}:\$PATH\""
  64. echo ""