Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dirty shell to compare installed gems versions with the ones expected from a Gemfile
# Compares installed gems versions with the expected ones from a Gemfile.
# Ignores non-strict-equal dependencies (i.e. ~> 1.5 will output the same as = 1.5).
DEST_DIR="$HOME/Desktop/gems-comparison" # no trailing slash
GEMSET="mesh"
INSTALLED_GEMS_FILE="$DEST_DIR/installedGems.txt"
WANTED_GEMS_WITH_VERSIONS_FILE="$DEST_DIR/wantedGemsWithVersion.txt"
WANTED_GEMS_FILE="$DEST_DIR/wantedGems.txt"
mkdir -p "$DEST_DIR"
cat Gemfile | egrep '^\s*gem' | cut -d "'" -f "2,4" | tr -d "'" | sed 's:[~>=]:\ :g' | sed 's|https://| |g' > "$WANTED_GEMS_WITH_VERSIONS_FILE"
rvm @"$GEMSET" do gem list > "$INSTALLED_GEMS_FILE"
cat "$WANTED_GEMS_WITH_VERSIONS_FILE" | cut -d " " -f 1 > "$WANTED_GEMS_FILE"
for gem in $(cat "$WANTED_GEMS_FILE")
do
installedVersion=$(grep "^$gem " "$INSTALLED_GEMS_FILE" | cut -d '(' -f 2 | tr -d ')')
wantedVersion=$(grep "^$gem " "$WANTED_GEMS_WITH_VERSIONS_FILE" | rev | cut -d ' ' -f 1 | rev)
if [[ "$wantedVersion" = "" ]]
then echo "!! Unspecified version for $gem."
elif [[ "$installedVersion" != "$wantedVersion" ]]
then echo "** Mismatching version: $gem (wanted $wantedVersion, installed $installedVersion)"
else echo " Gem $gem ok (wanted $wantedVersion, installed $installedVersion)"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment