2017-11-27 19:40:21 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
########
|
|
|
|
# Install checks
|
|
|
|
########
|
|
|
|
|
2019-01-21 15:29:33 +00:00
|
|
|
GIT_ROOT=$(git rev-parse --show-toplevel)
|
|
|
|
|
2017-11-27 19:40:21 +00:00
|
|
|
function program_exists() {
|
|
|
|
local program=$1
|
|
|
|
command -v "$program" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:32:30 +00:00
|
|
|
function program_version_exists() {
|
|
|
|
local program=$1
|
|
|
|
if ! program_exists "$program"; then
|
|
|
|
$(exit 1)
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
local required_version=$2
|
|
|
|
if echo "$($program --version)" | grep -q -wo "$required_version\|$required_version[^\.]"; then
|
|
|
|
$(exit 0)
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
$(exit 1)
|
|
|
|
}
|
|
|
|
|
2019-01-18 14:09:23 +00:00
|
|
|
function toolversion() {
|
2019-01-21 15:29:33 +00:00
|
|
|
${GIT_ROOT}/scripts/toolversion "${1}"
|
2019-01-10 18:32:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:40:21 +00:00
|
|
|
########
|
|
|
|
# Homebrew
|
|
|
|
########
|
|
|
|
|
|
|
|
function brew_install() {
|
|
|
|
local package=$1
|
2019-01-10 18:32:30 +00:00
|
|
|
local required_version=$2
|
|
|
|
local url="https://raw.githubusercontent.com/Homebrew/homebrew-core/${3:-master}/Formula/$package.rb"
|
2017-11-27 19:40:21 +00:00
|
|
|
|
|
|
|
if ! is_macos; then
|
2017-12-01 17:17:25 +00:00
|
|
|
return 1
|
2017-11-27 19:40:21 +00:00
|
|
|
fi
|
|
|
|
|
2019-01-10 18:32:30 +00:00
|
|
|
local pkginfo=$(brew info "$package")
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
# Package already exists locally
|
|
|
|
local prefix_expr="HOMEBREW_PREFIX: "
|
|
|
|
local brew_base=$(brew config | grep "$prefix_expr" | sed "s|$prefix_expr||")
|
|
|
|
local pkg_base="$brew_base/Cellar/$package/"
|
|
|
|
local installed_versions=$(echo "$pkginfo" | grep "$pkg_base" | sed -E "s:$pkg_base([^ ]+).*$:\1:")
|
|
|
|
local active_version=$(echo "$pkginfo" | grep -E "$pkg_base.*\*$" | sed -E "s:$pkg_base([^ ]+).*\*$:\1:")
|
|
|
|
|
|
|
|
for installed_version in $installed_versions;
|
|
|
|
do
|
|
|
|
if [ "$installed_version" = "$required_version" ]; then
|
|
|
|
if [ "$active_version" = "$required_version" ]; then
|
|
|
|
already_installed "$package $required_version"
|
|
|
|
else
|
|
|
|
brew unlink "$package"
|
|
|
|
brew switch "$package" "$required_version"
|
|
|
|
fi
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
2017-11-27 19:40:21 +00:00
|
|
|
fi
|
2019-01-10 18:32:30 +00:00
|
|
|
|
|
|
|
brew install $url
|
2017-11-27 19:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function brew_cask_install() {
|
|
|
|
local package=$1
|
|
|
|
|
|
|
|
if ! is_macos; then
|
2017-12-01 17:17:25 +00:00
|
|
|
return 1
|
2017-11-27 19:40:21 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if brew cask list | grep -q "$package"; then
|
|
|
|
already_installed "$package"
|
|
|
|
else
|
|
|
|
brew cask install $@
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function brew_tap() {
|
|
|
|
local cask=$1
|
|
|
|
|
|
|
|
if ! is_macos; then
|
2017-12-01 17:17:25 +00:00
|
|
|
return 1
|
2017-11-27 19:40:21 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if ! brew tap | grep -q "$cask"; then
|
|
|
|
brew tap "$cask"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:40:24 +00:00
|
|
|
###############
|
|
|
|
# Linux
|
|
|
|
###############
|
|
|
|
|
|
|
|
function linux_update() {
|
2019-01-10 18:32:30 +00:00
|
|
|
! is_linux && return 1
|
2018-02-07 19:40:24 +00:00
|
|
|
|
2019-01-10 18:32:30 +00:00
|
|
|
if program_exists "apt"; then
|
|
|
|
apt_update
|
|
|
|
elif program_exists "pacman"; then
|
|
|
|
pacman_update
|
|
|
|
else
|
|
|
|
echo "Unsupported Linux distro."
|
|
|
|
exit 1;
|
|
|
|
fi
|
2018-02-07 19:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function linux_is_installed() {
|
2019-01-10 18:32:30 +00:00
|
|
|
! is_linux && return 1
|
2018-02-07 19:40:24 +00:00
|
|
|
|
2019-01-10 18:32:30 +00:00
|
|
|
if program_exists 'apt'; then
|
|
|
|
apt_is_installed "$@"
|
|
|
|
elif program_exists 'pacman'; then
|
|
|
|
pacman_is_installed "$@"
|
|
|
|
else
|
|
|
|
echo "Unsupported Linux distro."
|
|
|
|
exit 1;
|
|
|
|
fi
|
2018-02-07 19:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# FIXME This command assumes that package names in different package managers (apt, pacman) are same.
|
|
|
|
# At this moment, it works as expected because we only call it for installing maven and nodejs.
|
|
|
|
# If this list grows, please consider adding some sort of mapping mechanism.
|
|
|
|
function linux_install() {
|
2019-01-10 18:32:30 +00:00
|
|
|
! is_linux && return 1
|
2018-02-07 19:40:24 +00:00
|
|
|
|
2019-01-10 18:32:30 +00:00
|
|
|
if program_exists "apt"; then
|
|
|
|
apt_install "$@"
|
|
|
|
elif program_exists "pacman"; then
|
|
|
|
pacman_install "$@"
|
|
|
|
else
|
|
|
|
echo "Unsupported Linux distro."
|
|
|
|
exit 1;
|
|
|
|
fi
|
2018-02-07 19:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:17:25 +00:00
|
|
|
|
|
|
|
###############
|
|
|
|
# Aptitude
|
|
|
|
###############
|
|
|
|
|
|
|
|
function apt_update() {
|
|
|
|
sudo apt update
|
|
|
|
}
|
|
|
|
|
|
|
|
function apt_is_installed() {
|
|
|
|
local package=$1
|
|
|
|
|
|
|
|
dpkg -s "$package" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
function apt_install() {
|
|
|
|
local package=$1
|
|
|
|
|
|
|
|
if apt_is_installed "$package"; then
|
|
|
|
cecho "+ $package already installed... skipping."
|
|
|
|
else
|
2019-01-10 18:32:30 +00:00
|
|
|
sudo apt install -y "$package" || exit 1
|
2017-12-01 17:17:25 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:40:24 +00:00
|
|
|
###############
|
|
|
|
# Pacman
|
|
|
|
###############
|
|
|
|
|
|
|
|
function pacman_update() {
|
2019-01-10 18:32:30 +00:00
|
|
|
sudo pacman -Syu
|
2018-02-07 19:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function pacman_is_installed() {
|
2019-01-10 18:32:30 +00:00
|
|
|
local package=$1
|
|
|
|
pacman -Qs $package >/dev/null 2>&1
|
2018-02-07 19:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function pacman_install() {
|
2019-01-10 18:32:30 +00:00
|
|
|
local package=$1
|
2018-02-07 19:40:24 +00:00
|
|
|
|
2019-01-10 18:32:30 +00:00
|
|
|
if pacman_is_installed "$package"; then
|
|
|
|
cecho "+ $package already installed... skipping."
|
|
|
|
else
|
|
|
|
sudo pacman -S --noconfirm "$package" || exit 1
|
|
|
|
fi
|
2018-02-07 19:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:40:21 +00:00
|
|
|
###############
|
|
|
|
# RVM
|
|
|
|
###############
|
|
|
|
|
|
|
|
function load_rvm_if_available() {
|
|
|
|
[ -f ~/.rvm/scripts/rvm ] && source ~/.rvm/scripts/rvm
|
|
|
|
}
|
|
|
|
|
|
|
|
###############
|
|
|
|
# NVM
|
|
|
|
###############
|
|
|
|
|
|
|
|
function load_nvm_if_available() {
|
|
|
|
[ -f ~/.nvm/nvm.sh ] && source ~/.nvm/nvm.sh
|
|
|
|
}
|
|
|
|
|
|
|
|
function nvm_installed() {
|
|
|
|
program_exists "nvm"
|
|
|
|
}
|