Skip to content

Instantly share code, notes, and snippets.

@Irvyne
Last active September 1, 2016 09:29
Show Gist options
  • Save Irvyne/b853eaf627cb4aeb68ad2cbcdfba7a85 to your computer and use it in GitHub Desktop.
Save Irvyne/b853eaf627cb4aeb68ad2cbcdfba7a85 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Version 0.0.4
##########
# Config #
##########
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
blue=`tput setaf 4`
purple=`tput setaf 5`
cyan=`tput setaf 6`
white=`tput setaf 7`
bold=`tput bold`
reset=`tput sgr0`
silent=false
#############
# Functions #
#############
function binExists ()
{
local bin=$1
local displayName=$2
if ! type "$bin" > /dev/null 2>&1; then
if [ "$silent" != true ] ; then
displayHeader $displayName false
echo "${red}${bold}$bin${reset}${red} is not detected on the system. Use '${bold}-s${reset}${red}' to enter silent mode!"
fi
return 1; # false
else
displayHeader $displayName true
return 0; # true
fi
}
function fileExists ()
{
local file=`which $1`
local displayName=$2
if [ -f "$file" ] ; then
displayHeader $displayName true
return 0; # true
else
displayHeader $displayName false
return 1; # false
fi
}
function isBinWritable ()
{
local file=`which $1`
if [ -w "$file" ] ; then
return 0; # true
else
echo "${red}${bold}`which $file`${reset}${red} is not writable!${reset}"
return 1; # false
fi
}
function isDirWritable ()
{
local dir=$1
if [ -w "$dir" ] ; then
return 0; # true
else
echo "${red}${bold}`which $dir`${reset}${red} is not writable!${reset}"
return 1; # false
fi
}
function displayHeader ()
{
local length=$((${#1}+4))
local exists=$2
if [ "$exists" = true ] ; then
printf "\n${green}"
else
printf "\n${red}"
fi
printf "%0.s#" $(seq 1 $length)
printf "\n%s\n" "# $1 #"
printf "%0.s#" $(seq 1 $length)
printf "${reset}\n\n"
}
########
# Init #
########
while getopts ":s" opt; do
case $opt in
s)
silent=true >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit
;;
esac
done
printf "Starting upgrading system"
if [ "$silent" = true ] ; then
printf " ${cyan}(${bold}silent mode${reset}${cyan})${reset}.\n"
else
printf ".\n"
fi
printf "Please wait for a while"
for i in {1..3} ; do
printf "."
sleep 0.3s
if [ $i = 3 ] ; then
printf "\n"
fi
done
############
# Homebrew #
############
if binExists brew Homebrew ; then
brew update; # Fetch the newest version of Homebrew and all formulae from GitHub.
brew upgrade; # Upgrade outdated & unpinned brews.
brew cleanup; # Remove any older versions from the cellar.
brew prune; # Remove dead symlinks from the Homebrew prefix.
brew cask cleanup; # Cleans up cached downloads and tracker symlinks.
# Display and upgrade casks if necessary
casks=( $(brew cask list) )
for cask in ${casks[@]} ; do
installed="$(brew cask info $cask | grep 'Not installed')"
if [[ $installed = *[!\ ]* ]]; then
echo "${purple}${cask}${reset} requires ${red}update${reset}."
(set -x; brew cask uninstall $cask --force;)
(set -x; brew cask install $cask --force;)
else
echo "${purple}${cask}${reset} is ${green}up-to-date${reset}."
fi
done
fi
#############
# Oh My ZSH #
#############
#if fileExists ~/.oh-my-zsh/tools/upgrade.sh Oh_My_ZSH ; then
# $ZSH sh ~/.oh-my-zsh/tools/upgrade.sh
#fi
############
# Composer #
############
if binExists composer Composer ; then
if isBinWritable composer ; then
composer self-update
fi
fi
###########
# Symfony #
###########
if binExists symfony Symfony_Installer ; then
if isBinWritable symfony ; then
symfony self-update
fi
fi
#######
# NPM #
#######
if binExists npm NPM ; then
if isDirWritable $(npm root -g) ; then
npm update -g
echo "${green}NPM updated!${reset}"
else
echo "${red}${bold}$(npm root -g)${reset}${red} is not writable! You probably installed NodeJS as root (unlucky you are).${reset}"
echo "${red}Try to run update as sudo ==> ${bold}sudo npm update -g${reset}${red} <==${reset}"
fi
fi
printf "\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment