Skip to content

Instantly share code, notes, and snippets.

@soifou

soifou/cask.sh

Created Jan 26, 2017
Embed
What would you like to do?
Upgrade and clean macOS casks, the lazy way
#!/usr/bin/env bash
# forked from: https://gist.github.com/joshmanders/515d869a1fc059a1692c20ecdbaae2d3
# inspired by: https://gist.github.com/atais/9c72e469b1cbec35c7c430ce03de2a6b
# Installation
# $ ln -s /path/to/this/cask.sh /usr/local/bin/cask
#
# Usage
# $ cask upgrade (check newest release and download them)
# $ cask clean (keep only the latest release and remove the oldest)
readonly local caskroom="/usr/local/Caskroom"
red=`tput setaf 1`
green=`tput setaf 2`
orange=`tput setaf 3`
blue=`tput setaf 4`
reset=`tput sgr0`
upgrade() {
# update brew before doing anything
(set -x; brew update;)
casks=($(brew cask list))
for cask in ${casks[@]}; do
current="$(brew cask info ${cask} | sed -n '1p' | sed -n 's/^.*: \(.*\)$/\1/p')"
installed=($(ls ${caskroom}/${cask}))
if [[ "${current}" == "latest" ]]; then
echo "${orange}Skip${reset} ${red}${cask}${reset} ${orange}tagged with${reset} ${blue}latest${reset} ${orange}version...${reset}"
elif (! [[ " ${installed[@]} " == *" ${current} "* ]]); then
echo "${red}${cask}${reset} requires ${red}update${reset}."
echo "Upgrading ${red}${cask}${reset} to v${blue}${current}${reset}."
(set -x; brew cask install ${cask} --force;)
else
echo "${red}${cask}${reset}@${blue}${current}${reset} is ${green}up-to-date${reset}."
fi
done
}
# won't work with version tag "latest"
# won't work with casks not following strict semantic versions (http://semver.org/)
clean() {
casks=($(brew cask list))
seems_cleaned=true
for cask in ${casks[@]}
do
installed=($(ls ${caskroom}/${cask}))
if [ ${#installed[@]} -gt 1 ]; then
seems_cleaned=false
echo "${red}${cask}${reset} has installed multiple ${red}versions${reset}."
latest=$(ls -v1r ${caskroom}/${cask} | head -1)
echo " -> ${green}keeping latest version:${reset} ${blue}$latest${reset}"
outdated=$(ls -v1r ${caskroom}/${cask} | awk 'NR>1')
for version in $outdated; do
echo " -> ${orange}removing outdated version:${reset} ${blue}${version}${reset}"
(set -x;rm -rf ${caskroom}/${cask}/${version};)
done
fi
done
if [[ seems_cleaned ]]; then
echo "${green}All casks seems clean!${reset}"
fi
}
main() {
local args=("$@")
case "$1" in
upgrade) upgrade "${args[@]}" ;;
clean) clean "${args[@]}" ;;
*) brew cask "${args[@]}" ;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.