Skip to content

Instantly share code, notes, and snippets.

@gjorquera
Last active March 4, 2023 16:30
Show Gist options
  • Save gjorquera/a398ab36fd9fc49539b1ed2decf00ef0 to your computer and use it in GitHub Desktop.
Save gjorquera/a398ab36fd9fc49539b1ed2decf00ef0 to your computer and use it in GitHub Desktop.
Script Helpers

Script Helpers

Installation

Include the following lines at the top of your script (make sure you're using bash as the interpreter):

#!/usr/bin/env bash
cd "$(dirname $0)/.."
[ -f ".h" ] || curl -s -o ".h" -L https://https://gist.githubusercontent.com/gjorquera/a398ab36fd9fc49539b1ed2decf00ef0/raw/6bf500bfc5ea5adf3bb443557dc1a819655ff3e7/helpers.bash; . ".h"

Don't forget to add .cache.* and .h to your .gitignore file:

echo ".cache.*" >> .gitignore
echo ".h" >> .gitignore

Example

#!/usr/bin/env bash
cd "$(dirname $0)/.."
[ -f ".h" ] || curl -s -o ".h" -L https://gist.githubusercontent.com/gjorquera/a398ab36fd9fc49539b1ed2decf00ef0/raw/6bf500bfc5ea5adf3bb443557dc1a819655ff3e7/helpers.bash; . ".h"

RAILS_ENV=${RAILS_ENV:-development}

title "Checking system dependencies..."

ensure "which heroku" "brew install heroku-toolbelt"
ensure "which identify" "brew install imagemagick"
ensure "which phantomjs" "brew install phantomjs"

title "Setting up pow..."

ensure "which pow" "brew install pow"
ensure "test -d $HOME/.pow" "brew info pow"
ensure "echo 5000 > $HOME/.pow/kipict"

title "Setting up ruby..."

ensure "which ruby" "brew install rbenv"
ensure "which bundle" "gem install bundler"
ensure "cached Gemfile || bundle install"

title "Setting up postgres..."

ensure "which psql" "brew install postgresql"
ensure "echo '\\l' | psql postgres" "postgres -D /opt/boxen/homebrew/var/postgres"
ensure "echo '\\l' | psql postgres | grep kipict_$RAILS_ENV" \
  "heroku local:run rake db:create"
ensure "cached db/migrate || heroku local:run rake db:migrate"

title "Setting up redis..."

ensure "which redis-server" "brew install redis"
ensure "redis-cli ping | grep PONG" "redis-server /opt/boxen/homebrew/etc/redis.conf"

Helpers

title

Used to group several ensures under the same concept:

title "Checking system dependencies..."

ensure

Makes sure that a given command is successful and prints it's status along with a short message on error.

ensure "which identify"
ensure "test -d $HOME/.pow" "brew info pow"

cached

Caches the contents of a file or directory, returning whether the existing cache was updated or not.

It should be used inside a ensure to avoid running time consuming commands if the file or directory used to run that command hasn't changed.

ensure "cached Gemfile || bundle install"
ensure "cached db/migrate || heroku local:run rake db:migrate"
#!/usr/bin/env bash
# Script Helpers
#
# A helper file that can sourced into your project scripts to to DRY them up.
# If you don't know what project scripts are, please read
# [scripts-to-rule-them-all](https://github.com/github/scripts-to-rule-them-all).
# More information: https://github.com/gjorquera/script-helpers
title() { echo -e "\033[0;34m\n${1}\n\033[0m"; }
ensure() {
echo -n "$1... "
if (exec &>/dev/null; eval $1); then
echo -e "\033[0;32m✓ done\033[0m"
else
echo -e "\033[0;31m✗ error!$([ -z "$2" ] || echo "\nTry: $2")" && exit 1
fi
}
cached() {
[ -e "$1" ] || return 1
local cache="$(cat ".cache.${1//\//_}" 2>/dev/null)"
local new_cache=$(find "$1" -exec cat {} \; | shasum)
echo "$new_cache" > ".cache.${1//\//_}" && [[ "$new_cache" == "$cache" ]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment