Last active
December 29, 2023 16:05
-
-
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.
This file contains 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
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage looks something like this: