|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Based on https://github.com/Schniz/fnm/blob/0fc14222846161ec120c992206215556173b59ea/.ci/install.sh |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +INSTALL_DIR="$HOME/.phpup" |
| 8 | +RELEASE="latest" |
| 9 | +OS="$(uname -s)" |
| 10 | +REPOSITORY="https://github.com/masan4444/phpup" |
| 11 | + |
| 12 | +# Parse Flags |
| 13 | +parse_args() { |
| 14 | + while [[ $# -gt 0 ]]; do |
| 15 | + key="$1" |
| 16 | + |
| 17 | + case $key in |
| 18 | + -d | --install-dir) |
| 19 | + INSTALL_DIR="$2" |
| 20 | + shift # past argument |
| 21 | + shift # past value |
| 22 | + ;; |
| 23 | + -s | --skip-shell) |
| 24 | + SKIP_SHELL="true" |
| 25 | + shift # past argument |
| 26 | + ;; |
| 27 | + # --force-install | --force-no-brew) |
| 28 | + # echo "\`--force-install\`: I hope you know what you're doing." >&2 |
| 29 | + # FORCE_INSTALL="true" |
| 30 | + # shift |
| 31 | + # ;; |
| 32 | + -r | --release) |
| 33 | + RELEASE="$2" |
| 34 | + shift # past release argument |
| 35 | + shift # past release value |
| 36 | + ;; |
| 37 | + *) |
| 38 | + echo "Unrecognized argument $key" |
| 39 | + exit 1 |
| 40 | + ;; |
| 41 | + esac |
| 42 | + done |
| 43 | +} |
| 44 | + |
| 45 | +set_filename() { |
| 46 | + if [ "$OS" = "Linux" ]; then |
| 47 | + # Based on https://stackoverflow.com/a/45125525 |
| 48 | + case "$(uname -m)" in |
| 49 | + arm | armv7*) |
| 50 | + FILENAME="phpup-linux-armv7" |
| 51 | + ;; |
| 52 | + aarch* | armv8*) |
| 53 | + FILENAME="phpup-linux-aarch64" |
| 54 | + ;; |
| 55 | + *) |
| 56 | + FILENAME="phpup-linux" |
| 57 | + esac |
| 58 | + # elif [ "$OS" = "Darwin" ] && [ "$FORCE_INSTALL" = "true" ]; then |
| 59 | + elif [ "$OS" = "Darwin" ]; then |
| 60 | + FILENAME="phpup-macos" |
| 61 | + USE_HOMEBREW="false" |
| 62 | + # echo "Downloading the latest phpup binary from GitHub..." |
| 63 | + # echo " Pro tip: it's easier to use Homebrew for managing phpup in macOS." |
| 64 | + # echo " Remove the \`--force-no-brew\` so it will be easy to upgrade." |
| 65 | + # elif [ "$OS" = "Darwin" ]; then |
| 66 | + # USE_HOMEBREW="true" |
| 67 | + # echo "Downloading phpup using Homebrew..." |
| 68 | + else |
| 69 | + echo "OS $OS is not supported." |
| 70 | + echo "If you think that's a bug - please file an issue to $REPOSITORY/issues" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +download_phpup() { |
| 76 | + if [ "$USE_HOMEBREW" = "true" ]; then |
| 77 | + brew install phpup |
| 78 | + else |
| 79 | + if [ "$RELEASE" = "latest" ]; then |
| 80 | + URL="$REPOSITORY/releases/latest/download/$FILENAME.zip" |
| 81 | + else |
| 82 | + URL="$REPOSITORY/releases/download/$RELEASE/$FILENAME.zip" |
| 83 | + fi |
| 84 | + |
| 85 | + DOWNLOAD_DIR=$(mktemp -d) |
| 86 | + |
| 87 | + echo "Downloading $URL..." |
| 88 | + |
| 89 | + mkdir -p "$INSTALL_DIR/bin" &>/dev/null |
| 90 | + |
| 91 | + if ! curl --progress-bar --fail -L "$URL" -o "$DOWNLOAD_DIR/$FILENAME.zip"; then |
| 92 | + echo "Download failed. Check that the release/filename are correct." |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + |
| 96 | + unzip -q "$DOWNLOAD_DIR/$FILENAME.zip" -d "$DOWNLOAD_DIR" |
| 97 | + |
| 98 | + mv "$DOWNLOAD_DIR/$FILENAME/phpup" "$INSTALL_DIR/bin/phpup" |
| 99 | + cp -r "$DOWNLOAD_DIR/$FILENAME/completions" "$INSTALL_DIR" |
| 100 | + |
| 101 | + chmod u+x "$INSTALL_DIR/bin/phpup" |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +check_dependencies() { |
| 106 | + echo "Checking dependencies for the installation script..." |
| 107 | + |
| 108 | + echo -n "Checking availability of curl... " |
| 109 | + if hash curl 2>/dev/null; then |
| 110 | + echo "OK!" |
| 111 | + else |
| 112 | + echo "Missing!" |
| 113 | + SHOULD_EXIT="true" |
| 114 | + fi |
| 115 | + |
| 116 | + echo -n "Checking availability of unzip... " |
| 117 | + if hash unzip 2>/dev/null; then |
| 118 | + echo "OK!" |
| 119 | + else |
| 120 | + echo "Missing!" |
| 121 | + SHOULD_EXIT="true" |
| 122 | + fi |
| 123 | + |
| 124 | + if [ "$USE_HOMEBREW" = "true" ]; then |
| 125 | + echo -n "Checking availability of Homebrew (brew)... " |
| 126 | + if hash brew 2>/dev/null; then |
| 127 | + echo "OK!" |
| 128 | + else |
| 129 | + echo "Missing!" |
| 130 | + SHOULD_EXIT="true" |
| 131 | + fi |
| 132 | + fi |
| 133 | + |
| 134 | + if [ "$SHOULD_EXIT" = "true" ]; then |
| 135 | + echo "Not installing phpup due to missing dependencies." |
| 136 | + exit 1 |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +ensure_containing_dir_exists() { |
| 141 | + local CONTAINING_DIR |
| 142 | + CONTAINING_DIR="$(dirname "$1")" |
| 143 | + if [ ! -d "$CONTAINING_DIR" ]; then |
| 144 | + echo " >> Creating directory $CONTAINING_DIR" |
| 145 | + mkdir -p "$CONTAINING_DIR" |
| 146 | + fi |
| 147 | +} |
| 148 | + |
| 149 | +setup_shell() { |
| 150 | + CURRENT_SHELL="$(basename "$SHELL")" |
| 151 | + |
| 152 | + if [ "$CURRENT_SHELL" = "zsh" ]; then |
| 153 | + CONF_FILE=${ZDOTDIR:-$HOME}/.zshrc |
| 154 | + ensure_containing_dir_exists "$CONF_FILE" |
| 155 | + echo "Installing for Zsh. Appending the following to $CONF_FILE:" |
| 156 | + echo "" |
| 157 | + echo ' # PHP-UP' |
| 158 | + echo ' export PATH='"$INSTALL_DIR/bin"':$PATH' |
| 159 | + echo ' eval "$(phpup init --auto --recursive)"' |
| 160 | + echo ' fpath=('$INSTALL_DIR/completions/zsh' $fpath)' |
| 161 | + echo ' # To use completion, run `compinit` after adding $fpath' |
| 162 | + echo ' # compinit' |
| 163 | + |
| 164 | + echo '' >>$CONF_FILE |
| 165 | + echo '# PHP-UP' >>$CONF_FILE |
| 166 | + echo 'export PATH='$INSTALL_DIR/bin':$PATH' >>$CONF_FILE |
| 167 | + echo 'eval "$(phpup init --auto --recursive)"' >>$CONF_FILE |
| 168 | + echo 'fpath=('$INSTALL_DIR/completions/zsh' $fpath)' >>$CONF_FILE |
| 169 | + echo '# To use completion, run `compinit` after adding $fpath' >>$CONF_FILE |
| 170 | + echo '# compinit' >>$CONF_FILE |
| 171 | + |
| 172 | + # elif [ "$CURRENT_SHELL" = "fish" ]; then |
| 173 | + # CONF_FILE=$HOME/.config/fish/conf.d/phpup.fish |
| 174 | + # ensure_containing_dir_exists "$CONF_FILE" |
| 175 | + # echo "Installing for Fish. Appending the following to $CONF_FILE:" |
| 176 | + # echo "" |
| 177 | + # echo ' # PHP-UP' |
| 178 | + # echo ' set PATH '"$INSTALL_DIR"' $PATH' |
| 179 | + # echo ' phpup init --auto --recursive | source' |
| 180 | + |
| 181 | + # echo '# PHP-UP' >>$CONF_FILE |
| 182 | + # echo 'set PATH '"$INSTALL_DIR"' $PATH' >>$CONF_FILE |
| 183 | + # echo 'phpup init --auto --recursive | source' >>$CONF_FILE |
| 184 | + |
| 185 | + elif [ "$CURRENT_SHELL" = "bash" ]; then |
| 186 | + if [ "$OS" = "Darwin" ]; then |
| 187 | + CONF_FILE=$HOME/.profile |
| 188 | + else |
| 189 | + CONF_FILE=$HOME/.bashrc |
| 190 | + fi |
| 191 | + ensure_containing_dir_exists "$CONF_FILE" |
| 192 | + echo "Installing for Bash. Appending the following to $CONF_FILE:" |
| 193 | + echo "" |
| 194 | + echo ' # PHP-UP' |
| 195 | + echo ' export PATH='"$INSTALL_DIR/bin"':$PATH' |
| 196 | + echo ' eval "$(phpup init --auto --recursive)"' |
| 197 | + echo ' [ -s '"$INSTALL_DIR/completions/bash/_phpup"' ] && \. '"$INSTALL_DIR/completions/bash/_phpup"'' |
| 198 | + |
| 199 | + echo '' >>$CONF_FILE |
| 200 | + echo '# PHP-UP' >>$CONF_FILE |
| 201 | + echo 'export PATH='"$INSTALL_DIR/bin"':$PATH' >>$CONF_FILE |
| 202 | + echo 'eval "$(phpup init --auto --recursive)"' >>$CONF_FILE |
| 203 | + echo '[ -s '"$INSTALL_DIR/completions/bash/_phpup"' ] && \. '"$INSTALL_DIR/completions/bash/_phpup"'' >>$CONF_FILE |
| 204 | + |
| 205 | + else |
| 206 | + echo "Could not infer shell type. Please set up manually." |
| 207 | + exit 1 |
| 208 | + fi |
| 209 | + |
| 210 | + echo "" |
| 211 | + echo "In order to apply the changes, open a new terminal or run the following command:" |
| 212 | + echo "" |
| 213 | + echo " source $CONF_FILE" |
| 214 | +} |
| 215 | + |
| 216 | +parse_args "$@" |
| 217 | +set_filename |
| 218 | +check_dependencies |
| 219 | +download_phpup |
| 220 | +if [ "$SKIP_SHELL" != "true" ]; then |
| 221 | + setup_shell |
| 222 | +fi |
0 commit comments