Skip to content

Instantly share code, notes, and snippets.

@greglittlefield-wf
Last active November 7, 2019 20:31
Show Gist options
  • Save greglittlefield-wf/74b23500cd5dd69af29a7d556888b891 to your computer and use it in GitHub Desktop.
Save greglittlefield-wf/74b23500cd5dd69af29a7d556888b891 to your computer and use it in GitHub Desktop.
HomeBrew Dart version installer
#!/usr/bin/env bash
# install-dart-version.sh
#
# A script that installs a specific version of Dart using HomeBrew.
#
# Given a version, it'll first try to switch to a local install.
#
# If that version is not available, it will search through the Dart
# formula's history to find the right version.
# pipefail is important for `indent` to not swallow the exit code
set -o pipefail
indent() { sed 's/^/ /'; }
dart_version=$1
if [ -z "$dart_version" ]; then
echo Installs the given Dart version using HomeBrew.
echo
echo "Usage: $0 <Dart version>"
exit 0
fi
echo "Installing Dart \"$dart_version\"..."
echo
echo Trying to use previously installed version...
brew switch dart "$dart_version" 2>&1 | indent
brew_switch_exit_code=$?
if [ $brew_switch_exit_code -eq 0 ]; then
echo
echo "Dart \"$dart_version\" installed!"
exit 0
fi
echo
echo "Dart \"$dart_version\" not already installed. Searching formula history..."
echo
cd $(dirname "$(brew formula dart)") || exit 1
# Finding revision based on this message search is brittle, but so far, all commit messages are formatted consistently.
REVISION=$(git log --pretty=format:%H --grep "Updated stable branch to revision $dart_version\$")
if [ -z "$REVISION" ]; then
echo 'No matching Dart version found. Try running `brew update` and rerunning this script.'
exit 1
fi
echo "Found formula revision with the matching Dart version!"
git log -1 "$REVISION" 2>&1 | indent
echo
echo Installing...
echo
# Update the working copy to the given revision
git checkout "$REVISION" -- . || exit 1
# Install the formula in the working copy
brew unlink dart
brew install dart
# Undo the working copy changes
git reset --hard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment