Skip to content

Instantly share code, notes, and snippets.

@0xmachos
Created March 7, 2018 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xmachos/3cb7d989facbe8dc568b8d4912846a81 to your computer and use it in GitHub Desktop.
Save 0xmachos/3cb7d989facbe8dc568b8d4912846a81 to your computer and use it in GitHub Desktop.
Install the Ruby version manager rbenv and ruby-build to allow you to install Ruby versions
#!/usr/bin/env bash
set -eo pipefail
# -e exit if any command returns non-zero status code
# -o pipefail force pipelines to fail on first non-zero status code
FATAL="\\033[1;31mFATAL\\033[0m"
WARNING="\\033[1;33mWARNING\\033[0m"
PASS="\\033[1;32mPASS\\033[0m"
INFO="\\033[1;36mINFO\\033[0m"
function install_rbenv {
# Install rbenv
# Install ruby-build
echo -e "[$INFO] Installing rbenv and ruby-build"
# if apt install rbenv ; then
# # The apt package is outdated
# # https://github.com/rbenv/rbenv/issues/1068
# echo -e "[${PASS}] Installed rbenv"
# else
# echo -e "[${FATAL}] Failed to install rbenv"
# exit 1
# fi
echo -e "[${INFO}] Creating ${HOME}/.rbenv"
if mkdir -p "${HOME}/.rbenv" ; then
echo -e "[${PASS}] Created ${HOME}/.rbenv"
else
echo -e "[${FATAL}] Failed to create ${HOME}/.rbenv"
fi
echo -e "[${INFO}] Cloning rbenv from Github"
if git clone --quiet https://github.com/rbenv/rbenv.git "${HOME}/.rbenv" ; then
echo -e "[${PASS}] Cloned rbenv into ${HOME}/.rbenv"
else
echo -e "[${FATAL}] Failed to clone rbenv into ${HOME}/.rbenv"
exit 1
fi
echo -e "[${INFO}] Attempting to compile dynamic bash extension"
# Enter subshell
(
# TODO: Capture errors and echo them if make/configure fails
local configure_fail=""
cd "$HOME/.rbenv" || exit
if ./src/configure ; then
echo -e "[${PASS}] ./src/configure successful"
configure_fail="false"
else
echo -e "[${FATAL}] Failed to compiled dynamic bash extension"
configure_fail="true"
fi
if [[ "${configure_fail}" != "true" ]]; then
if make --silent -C src ; then
echo -e "[${PASS}] Sucessfully compiled dynamic bash extension"
else
echo -e "[${FATAL}] Failed to compiled dynamic bash extension"
exit 1
fi
fi
)
# Exit subshell
# Include rbenv binary directory in PATH
if grep -q 'export PATH="$HOME/.rbenv/bin:$PATH"' "${HOME}/.bashrc" ; then
echo -e "[${PASS}] "export PATH="$HOME/.rbenv/bin:$PATH"" already in '$HOME/.bashrc'"
else
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> "$HOME/.bashrc"
echo -e "[${PASS}] Added "export PATH="$HOME/.rbenv/bin:$PATH"" to '$HOME/.bashrc'"
fi
# Load rbenv automatically
echo 'eval "$(rbenv init -)"' >> "$HOME/.bashrc"
# WTF is this shit?
# Exit code is 0 but else condition triggers
if ~/.rbenv/bin/rbenv init ; then
echo -e "[${PASS}] wooo"
echo "Exit code: $?"
else
echo -e "[${FATAL}] boo"
echo "Exit code: $?"
fi
# So that PATH changes take effect in the current shell
# shellcheck disable=SC1090
source "$HOME/.bashrc"
# Install ruby-build
echo -e "[$INFO] Installing ruby-build"
if mkdir -p "$(rbenv root)/plugins" ; then
echo -e "[${PASS}] Sucessfully created $(rbenv root)/plugins"
else
echo -e "[${FATAL}] Failed to create $(rbenv root)/plugins"
fi
echo -e "[${INFO}] Cloning ruby-build from Github"
if git clone --quiet https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build ; then
echo -e "[${PASS}] Cloned ruby-build into $(rbenv root)/plugins/ruby-build"
else
echo -e "[${FATAL}] Failed to clone ruby-build into $(rbenv root)/plugins/ruby-build"
exit 1
fi
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
# # shellcheck disable=SC2016
# if grep -q 'eval $(rbenv init -)' "${HOME}/.bash_profile" ; then
# echo -e "[${PASS}] 'eval \"$(rbenv init -)\"' already in ~/.bash_profile"
# else
# echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
# # shellcheck disable=SC1090
# source "${HOME}"/.bash_profile
# fi
}
function rbenv_install_ruby {
if rbenv install -l | grep -q "${ruby_version}" ; then
echo -e "[${PASS}] rbenv Ruby version already ${ruby_version}"
else
echo -e "[${INFO}] Installing Ruby ${ruby_version} via rbenv"
if rbenv install "${ruby_version}" ; then
echo -e "[${PASS}] Ruby ${ruby_version} installed via rbenv"
rvm use "${ruby_version}" -- default
else
echo -e "[${FATAL}] Failed to install Ruby ${ruby_version} via rbenv"
exit 1
fi
fi
}
function main {
ruby_version="2.3.0"
install_rbenv
rbenv_install_ruby
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment