Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Created July 7, 2012 20:31
Show Gist options
  • Save olivierlacan/3068005 to your computer and use it in GitHub Desktop.
Save olivierlacan/3068005 to your computer and use it in GitHub Desktop.
Track changes to a skeleton Rails app since a given version
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------- #
# 1304046900 Track changes to a skeleton Rails app since a given `base_version`
# -------------------------------------------------------------------------------- #
## Changelog
# - Update 7/7/2012:
# * rewrote to use Bundler instead of RubyGems
# * attempting to switch to a master branch for which no commit existing was causing a pathspec error
# * simplified rails new commands by running them on the current path (after changing directory to tmp/skeleton_check of course).
# * renamed `old_version` variable to `base_version` for clarity
# * added `new_version` variable in case you don't want to upgrade to the latest stable Rails, empty by default
#
## Requirements
# * RubyGems installed (http://rubygems.org/pages/download)
# * Bundler installed (gem install bundler)
#
## Usage
# Place this script anywhere and run ./rails_default_diff.sh
#
## Configuration
# Insert the base version of Rails you want to upgrade from below.
base_version="3.1.4"
new_version="" # use the same format as base_version
#
# ================================================================================ #
# ================================================================================ #
## No Touching!
### Don't modify anything below this line unless you know what you're doing
# Exit script in case anything fails
set -e
print "$(tput setaf 2)# Creating skeleton Rails $base_version app $(tput sgr0)"
mkdir -p tmp/skeleton_check
cd tmp/skeleton_check
git init
bundle init # creates a Gemfile with rubygems as the source
echo "gem 'rails', '$base_version'" >> Gemfile # appends to the end of Gemfile
bundle
bundle exec rails new . -f
git add -A .
git commit -m "# Install $base_version"
git checkout -b $base_version # creates and checks out a new branch
print "$(tput setaf 2)# Removing all the things $(tput sgr0)"
# recursively removes all files and folders within the current path (tmp/skeleton_check)
rm -rf *
print "$(tput setaf 2)# Updating Rails to latest version $(tput sgr0)"
# creating a new blank Gemfile
bundle init
# if a new_version was set, use that
if [ "$new_version" ]; then
print "$(tput setaf 2)# Upgrading to Rails $new_version $(tput sgr0)"
# appending a requirement for the new_version at the bottom of the Gemfile
echo "gem 'rails', '$new_version'" >> Gemfile
# if a new_version wasn't set, use the latest version of Rails
else
print "$(tput setaf 2)# Using latest version of Rails (oooh, danger!) $(tput sgr0)"
echo "gem 'rails'" >> Gemfile
fi
bundle
print "$(tput setaf 2)# Creating new Rails app on top of old one $(tput sgr0)"
bundle exec rails new . -f
git add -A .
git checkout master
git commit -m "Install latest Rails"
if output=$(git diff --exit-code --name-status $base_version HEAD); then
print "No changes"
else
# Group name statuses
#
while read -r flag file; do
case $flag in
(M*) modified+=($file) ;;
(A*) added+=($file) ;;
(D*) deleted+=($file) ;;
esac
done <<< $output
# Display groups
#
if [[ -n $modified ]]; then
print -f "M %s\n" $modified
echo
fi
if [[ -n $added ]]; then
print -f "A %s\n" $added
echo
fi
if [[ -n $deleted ]]; then
print -f "D %s\n" $deleted
echo
fi
print -n "$(tput setaf 2)# Show full git-diff of modified files? $(tput sgr0) (Y/n)"
if read -q; then
# running a diff on all the modified files and ouputting the result to the system $EDITOR
git diff $base_version HEAD $modified | ruby -pe 'puts "\n#########" if /^diff/ && $. > 1' | $EDITOR
echo
fi
print "$(tput setaf 2)# Current version: $(rails -v) $(tput sgr0)"
fi
# going back to the original path where the script was being executed from
cd ../../
# cleaning up our mess
rm -rf tmp/skeleton_check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment