Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Last active December 29, 2023 16:05
Show Gist options
  • Select an option

  • Save stevenharman/6975adc0ceeaffd30ac2082268b7d896 to your computer and use it in GitHub Desktop.

Select an option

Save stevenharman/6975adc0ceeaffd30ac2082268b7d896 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 -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'
while getopts ":h" opt; do
case "${opt}" in
h)
echo "Usage: gemfresh [-h]" >&2
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
ensure_tool_available() {
tool=$1
binary=${2:-$1} # default to tool
if ! command -v "$binary" > /dev/null; then
printf "${CLEAR_LINE}๐Ÿ’€${RED} You must install $tool on your system before we can do the MATHS.${NO_COLOR}\n"
printf "โ„น๏ธ Try 'gem install $tool'\n"
exit 1
fi
}
printf "[1/4]๐Ÿ”Ž Checking binaries"
ensure_tool_available bundler bundle
ensure_tool_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"
# We need to disable the pipefail option b/c `bundle outdated` exits with a 1 if any gems are outdated.
set +o pipefail
# 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")
set -o pipefail
printf "${CLEAR_LINE}[4/4]๐Ÿงฎ Calculating Libyears (https://libyear.com)"
libyears=$(libyear-bundler --libyears --grand-total | bc)
printf "${CLEAR_LINE}${GREEN}Dependency Freshness:${NO_COLOR}\n"
printf "Outdated: %0.1f%% (${outdated_count}/${dependency_count})\n" "${outdated_percent}"
printf "Libyears: ${libyears}\n"
@stevenharman
Copy link
Copy Markdown
Author

Usage looks something like this:

$ ./gemfresh
Dependency Freshness:
Outdated: 48.8% (22/45)
Libyears: 17.5

@davemo
Copy link
Copy Markdown

davemo commented May 27, 2021

๐Ÿ’š This is sweet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment