|
| 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 | + |
| 27 | +function output { |
| 28 | + style_start="" |
| 29 | + style_end="" |
| 30 | + if [ "${2:-}" != "" ]; then |
| 31 | + case $2 in |
| 32 | + "success") |
| 33 | + style_start="\033[0;32m" |
| 34 | + style_end="\033[0m" |
| 35 | + ;; |
| 36 | + "error") |
| 37 | + style_start="\033[31;31m" |
| 38 | + style_end="\033[0m" |
| 39 | + ;; |
| 40 | + "info"|"warning") |
| 41 | + style_start="\033[33m" |
| 42 | + style_end="\033[39m" |
| 43 | + ;; |
| 44 | + "heading") |
| 45 | + style_start="\033[1;33m" |
| 46 | + style_end="\033[22;39m" |
| 47 | + ;; |
| 48 | + esac |
| 49 | + fi |
| 50 | + |
| 51 | + builtin echo -e "${style_start}${1}${style_end}" |
| 52 | +} |
| 53 | + |
| 54 | +output "${CLI_NAME} installer" "heading" |
| 55 | + |
| 56 | +binary_dest="${HOME}/${CLI_CONFIG_DIR}/bin" |
| 57 | +custom_dir="false" |
| 58 | + |
| 59 | +# Getops does not support long option names |
| 60 | +while [[ $# -gt 0 ]]; do |
| 61 | +case $1 in |
| 62 | + --install-dir=*) |
| 63 | + binary_dest="${1#*=}" |
| 64 | + custom_dir="true" |
| 65 | + shift # past argument=value |
| 66 | + ;; |
| 67 | + --install-dir) |
| 68 | + binary_dest="${2:-}" |
| 69 | + custom_dir="true" |
| 70 | + shift # past argument |
| 71 | + shift # past value |
| 72 | + ;; |
| 73 | + *) |
| 74 | + output "Unknown option $1" "error" |
| 75 | + output "Usage: ${0} [--install-dir=dir]" |
| 76 | + exit 1 |
| 77 | + ;; |
| 78 | +esac |
| 79 | +done |
| 80 | + |
| 81 | +# Run environment checks. |
| 82 | +output "\nEnvironment check" "heading" |
| 83 | + |
| 84 | +# Check that cURL or wget is installed. |
| 85 | +downloader="" |
| 86 | +if command -v curl >/dev/null 2>&1; then |
| 87 | + downloader="curl" |
| 88 | + output " [*] cURL is installed" "success" |
| 89 | +elif command -v wget >/dev/null 2>&1; then |
| 90 | + downloader="wget" |
| 91 | + output " [*] wget is installed" "success" |
| 92 | +else |
| 93 | + output " [ ] ERROR: cURL or wget is required for installation." "error" |
| 94 | + exit 1 |
| 95 | +fi |
| 96 | + |
| 97 | +# Check that tar is installed. |
| 98 | +if command -v tar >/dev/null 2>&1; then |
| 99 | + output " [*] Tar is installed" "success" |
| 100 | +else |
| 101 | + output " [ ] ERROR: Tar is required for installation." "error" |
| 102 | + exit 1 |
| 103 | +fi |
| 104 | + |
| 105 | +# Check that Git is installed. |
| 106 | +if command -v git >/dev/null 2>&1; then |
| 107 | + output " [*] Git is installed" "success" |
| 108 | +else |
| 109 | + output " [ ] Warning: Git will be needed." "warning" |
| 110 | +fi |
| 111 | + |
| 112 | +kernel=$(uname -s 2>/dev/null || /usr/bin/uname -s) |
| 113 | +case ${kernel} in |
| 114 | + "Linux"|"linux") |
| 115 | + kernel="linux" |
| 116 | + ;; |
| 117 | + "Darwin"|"darwin") |
| 118 | + kernel="darwin" |
| 119 | + ;; |
| 120 | + *) |
| 121 | + output "OS '${kernel}' not supported" "error" |
| 122 | + exit 1 |
| 123 | + ;; |
| 124 | +esac |
| 125 | + |
| 126 | +machine=$(uname -m 2>/dev/null || /usr/bin/uname -m) |
| 127 | +case ${machine} in |
| 128 | + arm|armv7*) |
| 129 | + machine="arm" |
| 130 | + ;; |
| 131 | + aarch64*|armv8*|arm64) |
| 132 | + machine="arm64" |
| 133 | + ;; |
| 134 | + i[36]86) |
| 135 | + machine="386" |
| 136 | + if [ "darwin" = "${kernel}" ]; then |
| 137 | + output " [ ] Your architecture (${machine}) is not supported anymore" "error" |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | + ;; |
| 141 | + x86_64) |
| 142 | + machine="amd64" |
| 143 | + ;; |
| 144 | + *) |
| 145 | + output " [ ] Your architecture (${machine}) is not currently supported" "error" |
| 146 | + exit 1 |
| 147 | + ;; |
| 148 | +esac |
| 149 | + |
| 150 | +output " [*] Your architecture (${machine}) is supported" "success" |
| 151 | + |
| 152 | +if [ "darwin" = "${kernel}" ]; then |
| 153 | + machine="all" |
| 154 | +fi |
| 155 | + |
| 156 | +platform="${kernel}_${machine}" |
| 157 | + |
| 158 | +# The necessary checks have passed. Start downloading the right version. |
| 159 | +output "\nDownload" "heading" |
| 160 | + |
| 161 | +latest_url=${CLI_DOWNLOAD_URL_PATTERN/~platform~/${platform}} |
| 162 | +output " Downloading ${latest_url}..."; |
| 163 | +case $downloader in |
| 164 | + "curl") |
| 165 | + curl --fail --location "${latest_url}" > "/tmp/${CLI_TMP_NAME}.tar.gz" |
| 166 | + ;; |
| 167 | + "wget") |
| 168 | + wget -q --show-progress "${latest_url}" -O "/tmp/${CLI_TMP_NAME}.tar.gz" |
| 169 | + ;; |
| 170 | +esac |
| 171 | + |
| 172 | +# shellcheck disable=SC2181 |
| 173 | +if [ $? != 0 ]; then |
| 174 | + output " The download failed." "error" |
| 175 | + exit 1 |
| 176 | +fi |
| 177 | + |
| 178 | +output " Uncompress binary..." |
| 179 | +tar -xz --directory "/tmp" -f "/tmp/${CLI_TMP_NAME}.tar.gz" |
| 180 | +rm "/tmp/${CLI_TMP_NAME}.tar.gz" |
| 181 | + |
| 182 | +if [ ! -d "${binary_dest}" ]; then |
| 183 | + if ! mkdir -p "${binary_dest}"; then |
| 184 | + binary_dest="." |
| 185 | + fi |
| 186 | +fi |
| 187 | + |
| 188 | +if [ "${custom_dir}" == "true" ]; then |
| 189 | + output " Installing the binary into ${binary_dest} ..." |
| 190 | +else |
| 191 | + output " Installing the binary into your home directory..." |
| 192 | +fi |
| 193 | + |
| 194 | +if mv "/tmp/${CLI_EXECUTABLE}" "${binary_dest}/${CLI_EXECUTABLE}"; then |
| 195 | + output " The binary was saved to: ${binary_dest}/${CLI_EXECUTABLE}" |
| 196 | +else |
| 197 | + output " Failed to move the binary to ${binary_dest}." "error" |
| 198 | + rm "/tmp/${CLI_EXECUTABLE}" |
| 199 | + exit 1 |
| 200 | +fi |
| 201 | + |
| 202 | +#output " Installing the shell auto-completion..." |
| 203 | +#"${binary_dest}/${CLI_EXECUTABLE}" self:shell-setup --silent |
| 204 | +#if [ $? != 0 ]; then |
| 205 | +# output " Failed to install the shell auto-completion." "warning" |
| 206 | +#fi |
| 207 | + |
| 208 | +output "\nThe ${CLI_NAME} was installed successfully!" "success" |
| 209 | + |
| 210 | +if [ "${custom_dir}" == "false" ]; then |
| 211 | + output "\nUse it as a local file:" "info" |
| 212 | + output " ${binary_dest}/${CLI_EXECUTABLE}" |
| 213 | + output "\nOr add the following line to your shell configuration file:" "info" |
| 214 | + output " export PATH=\"\$HOME/${CLI_CONFIG_DIR}/bin:\$PATH\"" |
| 215 | + output "\nOr install it globally on your system:" "info" |
| 216 | + output " mv ${binary_dest}/${CLI_EXECUTABLE} /usr/local/bin/${CLI_EXECUTABLE}" |
| 217 | + output "\nThen start a new shell and run '${CLI_EXECUTABLE}'" "info" |
| 218 | +fi |
0 commit comments