openfoodfoundation/openfoodnetwork

View on GitHub
script/install-bundler

Summary

Maintainability
Test Coverage
#!/bin/sh

# This shell script looks for the last used Bundler version in Gemfile.lock and
# installs exactly that version, removing all other versions.

# Command line arguments are passed on to `gem install`. So you can call this
# script with with arguments:
#
#   ./script/install-bundler --no-ri --no-rdoc

# This script is used by ofn-install and can by handy in your development
# environment.

# Fail if a single command fails.
set -e

# `grep`:      find the occurrences of "BUNDLED WITH" (-m is unnecessary and behaves different on macOS)
# `-A`:        print the next line after "BUNDLED WITH" as well
# `-F`:        find exactly that string
# `tail -n 1`: take the last line, the version line
# `tr -d`:     delete all spaces, the indent before the version
version="$(grep -A 1 -F "BUNDLED WITH" Gemfile.lock | tail -n 1  | tr -d '[:space:]')"

# if the length of $version is zero
if [ -z "$version" ]; then
  echo >&2 "No bundler version in Gemfile.lock."
  exit 1
fi

# Get the currently used bundler version.
# We ignore all errors with `2>/dev/null || true` in case there is no bundler
# or only an orphaned shim installed.
current="$(bundler --version 2>/dev/null || true)"

if [ "$current" = "Bundler version $version" ]; then
  echo >&2 "Already up-to-date: $current"
  exit 0
fi

# Passing on all arguments of this script with "$@".
gem install bundler -v "$version" "$@"
gem uninstall bundler -v "!= $version" 2>/dev/null || true