#!/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"