|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +homebrew= |
| 5 | +type -p brew >/dev/null && homebrew=1 |
| 6 | + |
| 7 | +if ! type -p git >/dev/null; then |
| 8 | + git() { |
| 9 | + echo "Error: git is required to proceed. Please install git and try again." >&2 |
| 10 | + exit 1 |
| 11 | + } |
| 12 | +fi |
| 13 | + |
| 14 | +rbenv="$(command -v rbenv ~/.rbenv/bin/rbenv | head -1)" |
| 15 | + |
| 16 | +if [ -n "$rbenv" ]; then |
| 17 | + echo "rbenv already seems installed in \`$rbenv'." |
| 18 | + cd "${rbenv%/*}" |
| 19 | + |
| 20 | + if [ -x ./brew ]; then |
| 21 | + echo "Trying to update with Homebrew..." |
| 22 | + brew update >/dev/null |
| 23 | + if brew list rbenv | grep -q rbenv/HEAD; then |
| 24 | + brew reinstall rbenv |
| 25 | + else |
| 26 | + brew upgrade rbenv |
| 27 | + fi |
| 28 | + elif git remote -v 2>/dev/null | grep -q rbenv; then |
| 29 | + echo "Trying to update with git..." |
| 30 | + git pull --tags origin master |
| 31 | + cd .. |
| 32 | + fi |
| 33 | +else |
| 34 | + if [ -n "$homebrew" ]; then |
| 35 | + echo "Installing rbenv with Homebrew..." |
| 36 | + brew update |
| 37 | + brew install rbenv |
| 38 | + rbenv="$(brew --prefix)/bin/rbenv" |
| 39 | + else |
| 40 | + echo "Installing rbenv with git..." |
| 41 | + mkdir -p ~/.rbenv |
| 42 | + cd ~/.rbenv |
| 43 | + git init |
| 44 | + git remote add -f -t master origin https://github.com/rbenv/rbenv.git |
| 45 | + git checkout -b master origin/master |
| 46 | + rbenv=~/.rbenv/bin/rbenv |
| 47 | + fi |
| 48 | +fi |
| 49 | + |
| 50 | +rbenv_root="$("$rbenv" root)" |
| 51 | +ruby_build="$(command -v "$rbenv_root"/plugins/*/bin/rbenv-install rbenv-install | head -1)" |
| 52 | + |
| 53 | +echo |
| 54 | +if [ -n "$ruby_build" ]; then |
| 55 | + echo "\`rbenv install' command already available in \`$ruby_build'." |
| 56 | + cd "${ruby_build%/*}" |
| 57 | + |
| 58 | + if [ -x ./brew ]; then |
| 59 | + echo "Trying to update with Homebrew..." |
| 60 | + brew update >/dev/null |
| 61 | + brew upgrade ruby-build |
| 62 | + elif git remote -v 2>/dev/null | grep -q ruby-build; then |
| 63 | + echo "Trying to update with git..." |
| 64 | + git pull origin master |
| 65 | + fi |
| 66 | +else |
| 67 | + if [ -n "$homebrew" ]; then |
| 68 | + echo "Installing ruby-build with Homebrew..." |
| 69 | + brew update |
| 70 | + brew install ruby-build |
| 71 | + else |
| 72 | + echo "Installing ruby-build with git..." |
| 73 | + mkdir -p "${rbenv_root}/plugins" |
| 74 | + git clone https://github.com/rbenv/ruby-build.git "${rbenv_root}/plugins/ruby-build" |
| 75 | + fi |
| 76 | +fi |
| 77 | + |
| 78 | +# Enable caching of rbenv-install downloads |
| 79 | +mkdir -p "${rbenv_root}/cache" |
| 80 | + |
| 81 | +# Detect the type of shell that invoked the installer: |
| 82 | +shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)" |
| 83 | +shell="${shell%% *}" |
| 84 | +shell="${shell##-}" |
| 85 | +shell="${shell:-$SHELL}" |
| 86 | +shell="${shell##*/}" |
| 87 | + |
| 88 | +case "$shell" in |
| 89 | +bash | zsh| ksh | ksh93 | mksh | fish ) |
| 90 | + # allow known shells to be configured |
| 91 | + ;; |
| 92 | +* ) |
| 93 | + # default to bash in case the parent process is not a known shell |
| 94 | + shell=bash |
| 95 | + ;; |
| 96 | +esac |
| 97 | + |
| 98 | +echo |
| 99 | +echo "Setting up your shell with \`rbenv init $shell' ..." |
| 100 | +"$rbenv" init "$shell" |
| 101 | + |
| 102 | +echo |
| 103 | +echo "All done! After reloading your terminal window, rbenv should be good to go." |
0 commit comments