github.com/symfony-cli/symfony-cli@v0.0.0-20240514161054-ece2df437dfa/installer/bash-installer (about)

     1  #!/usr/bin/env bash
     2  # Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
     3  #
     4  # Symfony CLI installer: this file is part of Symfony CLI project.
     5  #
     6  # This program is free software: you can redistribute it and/or modify
     7  # it under the terms of the GNU Affero General Public License as
     8  # published by the Free Software Foundation, either version 3 of the
     9  # License, or (at your option) any later version.
    10  #
    11  # This program is distributed in the hope that it will be useful,
    12  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14  # GNU Affero General Public License for more details.
    15  #
    16  # You should have received a copy of the GNU Affero General Public License
    17  # along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  #
    19  set -euo pipefail
    20  
    21  CLI_CONFIG_DIR=".symfony5"
    22  CLI_EXECUTABLE="symfony"
    23  CLI_TMP_NAME="$CLI_EXECUTABLE-"$(date +"%s")
    24  CLI_NAME="Symfony CLI"
    25  CLI_DOWNLOAD_URL_PATTERN="https://github.com/symfony-cli/symfony-cli/releases/latest/download/symfony-cli_~platform~.tar.gz"
    26  CLI_TMPDIR="${TMPDIR:-/tmp}"
    27  
    28  function output {
    29      style_start=""
    30      style_end=""
    31      if [ "${2:-}" != "" ]; then
    32      case $2 in
    33          "success")
    34              style_start="\033[0;32m"
    35              style_end="\033[0m"
    36              ;;
    37          "error")
    38              style_start="\033[31;31m"
    39              style_end="\033[0m"
    40              ;;
    41          "info"|"warning")
    42              style_start="\033[33m"
    43              style_end="\033[39m"
    44              ;;
    45          "heading")
    46              style_start="\033[1;33m"
    47              style_end="\033[22;39m"
    48              ;;
    49      esac
    50      fi
    51  
    52      builtin echo -e "${style_start}${1}${style_end}"
    53  }
    54  
    55  output "${CLI_NAME} installer" "heading"
    56  
    57  binary_dest="${HOME}/${CLI_CONFIG_DIR}/bin"
    58  custom_dir="false"
    59  
    60  # Getops does not support long option names
    61  while [[ $# -gt 0 ]]; do
    62  case $1 in
    63      --install-dir=*)
    64          binary_dest="${1#*=}"
    65          custom_dir="true"
    66          shift # past argument=value
    67          ;;
    68      --install-dir)
    69          binary_dest="${2:-}"
    70          custom_dir="true"
    71          shift # past argument
    72          shift # past value
    73          ;;
    74      *)
    75          output "Unknown option $1" "error"
    76          output "Usage: ${0} [--install-dir=dir]"
    77          exit 1
    78          ;;
    79  esac
    80  done
    81  
    82  # Run environment checks.
    83  output "\nEnvironment check" "heading"
    84  
    85  # Check that cURL or wget is installed.
    86  downloader=""
    87  if command -v curl >/dev/null 2>&1; then
    88      downloader="curl"
    89      output "  [*] cURL is installed" "success"
    90  elif command -v wget >/dev/null 2>&1; then
    91      downloader="wget"
    92      output "  [*] wget is installed" "success"
    93  else
    94      output "  [ ] ERROR: cURL or wget is required for installation." "error"
    95      exit 1
    96  fi
    97  
    98  # Check that tar is installed.
    99  if command -v tar >/dev/null 2>&1; then
   100      output "  [*] Tar is installed" "success"
   101  else
   102      output "  [ ] ERROR: Tar is required for installation." "error"
   103      exit 1
   104  fi
   105  
   106  # Check that Git is installed.
   107  if command -v git >/dev/null 2>&1; then
   108      output "  [*] Git is installed" "success"
   109  else
   110      output "  [ ] Warning: Git will be needed." "warning"
   111  fi
   112  
   113  kernel=$(uname -s 2>/dev/null || /usr/bin/uname -s)
   114  case ${kernel} in
   115      "Linux"|"linux")
   116          kernel="linux"
   117          ;;
   118      "Darwin"|"darwin")
   119          kernel="darwin"
   120          ;;
   121      *)
   122          output "OS '${kernel}' not supported" "error"
   123          exit 1
   124          ;;
   125  esac
   126  
   127  machine=$(uname -m 2>/dev/null || /usr/bin/uname -m)
   128  case ${machine} in
   129      arm|armv6*)
   130          machine="armv6"
   131          ;;
   132      armv7*)
   133          # ARMv6 is upwards compatible with ARMv7
   134          machine="armv6"
   135          ;;
   136      aarch64*|armv8*|arm64)
   137          machine="arm64"
   138          ;;
   139      i[36]86)
   140          machine="386"
   141          if [ "darwin" = "${kernel}" ]; then
   142              output "  [ ] Your architecture (${machine}) is not supported anymore" "error"
   143              exit 1
   144          fi
   145          ;;
   146      x86_64)
   147          machine="amd64"
   148          ;;
   149      *)
   150          output "  [ ] Your architecture (${machine}) is not currently supported" "error"
   151          exit 1
   152          ;;
   153  esac
   154  
   155  output "  [*] Your architecture (${machine}) is supported" "success"
   156  
   157  if [ "darwin" = "${kernel}" ]; then
   158      machine="all"
   159  fi
   160  
   161  platform="${kernel}_${machine}"
   162  
   163  # The necessary checks have passed. Start downloading the right version.
   164  output "\nDownload" "heading"
   165  
   166  latest_url=${CLI_DOWNLOAD_URL_PATTERN/~platform~/${platform}}
   167  output "  Downloading ${latest_url}...";
   168  case $downloader in
   169      "curl")
   170          curl --fail --location "${latest_url}" > "${CLI_TMPDIR}/${CLI_TMP_NAME}.tar.gz"
   171          ;;
   172      "wget")
   173          wget -q --show-progress "${latest_url}" -O "${CLI_TMPDIR}/${CLI_TMP_NAME}.tar.gz"
   174          ;;
   175  esac
   176  
   177  # shellcheck disable=SC2181
   178  if [ $? != 0 ]; then
   179      output "  The download failed." "error"
   180      exit 1
   181  fi
   182  
   183  output "  Uncompress binary..."
   184  tar -xz --directory "${CLI_TMPDIR}" -f "${CLI_TMPDIR}/${CLI_TMP_NAME}.tar.gz"
   185  rm "${CLI_TMPDIR}/${CLI_TMP_NAME}.tar.gz"
   186  
   187  if [ ! -d "${binary_dest}" ]; then
   188      if ! mkdir -p "${binary_dest}"; then
   189          binary_dest="."
   190      fi
   191  fi
   192  
   193  if [ "${custom_dir}" == "true" ]; then
   194      output "  Installing the binary into ${binary_dest} ..."
   195  else
   196      output "  Installing the binary into your home directory..."
   197  fi
   198  
   199  if mv "${CLI_TMPDIR}/${CLI_EXECUTABLE}" "${binary_dest}/${CLI_EXECUTABLE}"; then
   200      output "  The binary was saved to: ${binary_dest}/${CLI_EXECUTABLE}"
   201  else
   202      output "  Failed to move the binary to ${binary_dest}." "error"
   203      rm "${CLI_TMPDIR}/${CLI_EXECUTABLE}"
   204      exit 1
   205  fi
   206  
   207  #output "  Installing the shell auto-completion..."
   208  #"${binary_dest}/${CLI_EXECUTABLE}" self:shell-setup --silent
   209  #if [ $? != 0 ]; then
   210  #    output "  Failed to install the shell auto-completion." "warning"
   211  #fi
   212  
   213  output "\nThe ${CLI_NAME} was installed successfully!" "success"
   214  
   215  if [ "${custom_dir}" == "false" ]; then
   216      output "\nUse it as a local file:" "info"
   217      output "  ${binary_dest}/${CLI_EXECUTABLE}"
   218      output "\nOr add the following line to your shell configuration file:" "info"
   219      output "  export PATH=\"\$HOME/${CLI_CONFIG_DIR}/bin:\$PATH\""
   220      output "\nOr install it globally on your system:" "info"
   221      output " mv ${binary_dest}/${CLI_EXECUTABLE} /usr/local/bin/${CLI_EXECUTABLE}"
   222      output "\nIf moving the file does not work, you might have to prefix the command with sudo."
   223      output "\nThen start a new shell and run '${CLI_EXECUTABLE}'" "info"
   224  fi