Skip to content

Instantly share code, notes, and snippets.

@mathias
Forked from stevenharman/gemfresh
Last active June 29, 2021 18:38
Show Gist options
  • Save mathias/9fde5920acef1b0b8faf49b6b1f305ca to your computer and use it in GitHub Desktop.
Save mathias/9fde5920acef1b0b8faf49b6b1f305ca to your computer and use it in GitHub Desktop.
gemfresh: A handy script for determining the freshness of dependencies in a Ruby code base. Leverages bundler and libyear-bundler.
#!/usr/bin/env bash
# shellcheck disable=SC2059
set -eu
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'
while getopts ":hc" opt; do
case "${opt}" in
h)
echo "Usage: gemfresh [-h] [-c]" >&2
echo "Generate a currency report for this Ruby project." >&2
echo " -c Output in CSV format, suitable for pasting into spreadsheets." >&2
exit 0
;;
c)
CSV_REQUESTED=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
ensure_dependency_available() {
dependency=$1
binary=${2:-$1} # default to dependency
if ! command -v "$binary" > /dev/null; then
printf "${CLEAR_LINE}💀${RED} You must install $dependency on your system before we can do the MATHS.${NO_COLOR}\n"
printf "ℹ️ Try 'gem install $dependency'\n"
exit 1
fi
}
printf "[1/4]🔎 Checking binaries"
ensure_dependency_available bundler bundle
ensure_dependency_available libyear-bundler
if ! [[ -e Gemfile ]];then
printf "${CLEAR_LINE}💀${RED} Gemfile not found. Are you in a Ruby project?${NO_COLOR}\n"
exit 1
fi
printf "${CLEAR_LINE}[2/4]🖇 Counting dependencies"
# List all dependncies by counting the literal * character in each line
# shellcheck disable=SC2063
dependency_count=$(bundle list | grep -c '*')
printf "${CLEAR_LINE}[3/4]📅 Determining outdated dependencies"
# Get list of outdated, remove blank lines, count the lines, and pipe to xargs to trim whitespace
outdated_count=$(bundle outdated --parseable | sed "/^\s*$/d" | wc -l | xargs)
outdated_percent=$(bc <<< "scale=3; (${outdated_count} / ${dependency_count}) * 100")
printf "${CLEAR_LINE}[4/4]🧮 Calculating Libyears (https://libyear.com)"
libyears=$(libyear-bundler --libyears --grand-total | bc)
if [ -z ${CSV_REQUESTED+x} ]
then
printf "${CLEAR_LINE}${GREEN}Dependency Freshness:${NO_COLOR}\n"
printf "Outdated: %0.1f%% (${outdated_count}/${dependency_count})\n" "${outdated_percent}"
printf "Libyears: ${libyears}\n"
else
printf "${CLEAR_LINE}${dependency_count},${outdated_count},%0.1f%%,${libyears}\n" "${outdated_percent}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment