-
-
Save toonetown/65b4ab33286c584fd678eceb23365cb1 to your computer and use it in GitHub Desktop.
A script which updates brews, cast, software updates, etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script that will update all kinds of stuff (brews, casks, software updates, app store apps, etc) | |
set -o pipefail | |
# You can set any of these variables to "no" in your environment if you want to skip that particular portion | |
: ${SWUP_OS:="yes"} | |
: ${SWUP_BREW:="yes"} | |
: ${SWUP_MAS:="yes"} | |
: ${SWUP_BUNDLE:="yes"} | |
: ${SWUP_REMOVE_QUARANTINE:="yes"} | |
: ${SWUP_LOG:="${HOME}/Library/Logs/swup.log"} | |
# Searches for updates | |
HAS_OS="no" | |
HAS_BREW="no" | |
HAS_CASK="no" | |
HAS_MAS="no" | |
HAS_ANY="no" | |
function search_for_updates { | |
echo "========================" | tee -a "${SWUP_LOG}" | |
echo "Searching for updates..." | tee -a "${SWUP_LOG}" | |
if [ "${SWUP_OS}" == "yes" ]; then | |
echo " - Searching for OS updates..." | tee -a "${SWUP_LOG}" | |
softwareupdate --force -l >> "${SWUP_LOG}" 2>&1 || return $? | |
_O="$(softwareupdate --no-scan -l 2>/dev/null | grep -v 'Software Update Tool' | grep -v '^$' | grep -v 'No new')" | |
[ $(echo "${_O}" | grep '.' | wc -l) -gt 0 ] && { | |
echo "${_O}" | sed -e 's/^/ /g' | tee -a "${SWUP_LOG}" | |
HAS_OS="yes"; HAS_ANY="yes" | |
} | |
fi | |
which brew &>/dev/null && { | |
if [ "${SWUP_BREW}" == "yes" ]; then | |
echo " - Searching for homebrew updates..." | tee -a "${SWUP_LOG}" | |
brew update >> "${SWUP_LOG}" 2>&1 || return $? | |
_O="$(brew outdated --formula --verbose 2>/dev/null)" | |
[ $(echo "${_O}" | grep '.' | wc -l) -gt 0 ] && { | |
echo "${_O}" | sed -e 's/^/ /g' | tee -a "${SWUP_LOG}" | |
HAS_BREW="yes"; HAS_ANY="yes" | |
} | |
_O="$(brew outdated --cask --greedy-auto-updates --verbose 2>/dev/null)" | |
[ $(echo "${_O}" | grep '.' | wc -l) -gt 0 ] && { | |
echo "${_O}" | sed -e 's/^/ /g' | tee -a "${SWUP_LOG}" | |
HAS_CASK="yes"; HAS_ANY="yes" | |
} | |
fi | |
} | |
which mas &>/dev/null && mas account &>/dev/null && { | |
if [ "${SWUP_MAS}" == "yes" ]; then | |
echo " - Searching for App Store updates..." | tee -a "${SWUP_LOG}" | |
mas outdated >> "${SWUP_LOG}" 2>&1 || return $? | |
_O="$(mas outdated | cut -d' ' -f2- 2>/dev/null)" | |
[ $(echo "${_O}" | grep '.' | wc -l) -gt 0 ] && { | |
echo " - App Store updates" | tee -a "${SWUP_LOG}" | |
echo "${_O}" | sed -e 's/^/ /g' | tee -a "${SWUP_LOG}" | |
HAS_MAS="yes"; HAS_ANY="yes" | |
} | |
fi | |
} | |
[ "${HAS_ANY}" == "no" ] && { echo "" | tee -a "${SWUP_LOG}"; echo "No updates available" | tee -a "${SWUP_LOG}"; } | |
echo "" | tee -a "${SWUP_LOG}" | |
return 0 | |
} | |
# Actually performs the upgrade | |
function do_upgrade { | |
echo "========================" | tee -a "${SWUP_LOG}" | |
echo "Upgrading packages..." >> "${SWUP_LOG}" | |
read -p "Upgrading packages - Press enter to continue..." | |
if [ "${HAS_OS}" == "yes" ]; then | |
softwareupdate --no-scan -i -a 2>&1 | tee -a "${SWUP_LOG}" || return $? | |
fi | |
if [ "${HAS_BREW}" == "yes" ]; then | |
brew upgrade --formula 2>&1 | tee -a "${SWUP_LOG}" || return $? | |
fi | |
if [ "${HAS_CASK}" == "yes" ]; then | |
brew upgrade --cask $(brew outdated --cask --greedy-auto-updates --verbose | cut -d' ' -f1) 2>&1 \ | |
| tee -a "${SWUP_LOG}" || return $? | |
fi | |
if [ "${HAS_MAS}" == "yes" ]; then | |
mas upgrade 2>&1 | tee -a "${SWUP_LOG}" || return $? | |
fi | |
echo "" | tee -a "${SWUP_LOG}" | |
return 0 | |
} | |
# Cleans up leftover packages | |
function do_clean { | |
echo "========================" | tee -a "${SWUP_LOG}" | |
echo "Cleaning up..." | tee -a "${SWUP_LOG}" | |
which brew &>/dev/null && { | |
brew cleanup -s 2>&1 >> "${SWUP_LOG}" || return $? | |
rm -rvf "$(brew --cache)/downloads/"* 2>&1 >> "${SWUP_LOG}" || return $? | |
brew cleanup 2>&1 >> "${SWUP_LOG}" || return $? | |
} | |
[ "${SWUP_REMOVE_QUARANTINE}" == "yes" ] && { | |
xattr -d com.apple.quarantine /Applications/* &>/dev/null | |
} | |
echo "" | tee -a "${SWUP_LOG}" | |
return 0 | |
} | |
# Checks for bundle status | |
function check_bundle { | |
if [ "${SWUP_BUNDLE}" == "yes" -a -f "${HOME}/.Brewfile" ]; then | |
echo "========================" | tee -a "${SWUP_LOG}" | |
echo "Checking global bundle for unused packages..." | tee -a "${SWUP_LOG}" | |
brew bundle cleanup --global 2>&1 | sed -e 's/--force/--force --global/g' | tee -a "${SWUP_LOG}" | |
echo "" | tee -a "${SWUP_LOG}" | |
fi | |
return 0 | |
} | |
search_for_updates || { _r=$?; echo "Error searching for updates" | tee -a "${SWUP_LOG}"; exit ${_r}; } | |
[ "${HAS_ANY}" == "yes" ] && { | |
do_upgrade || { _r=$?; echo "Error upgrading packages" | tee -a "${SWUP_LOG}"; exit ${_r}; } | |
} | |
do_clean || { _r=$?; echo "Error cleaning up" | tee -a "${SWUP_LOG}"; exit ${_r}; } | |
check_bundle || { _r=$?; echo "Error checking bundle" | tee -a "${SWUP_LOG}"; exit ${_r}; } | |
echo "========================" | tee -a "${SWUP_LOG}" | |
echo "Done" | tee -a "${SWUP_LOG}" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment