Skip to content

Instantly share code, notes, and snippets.

@quicksnap
Forked from jmeridth/post-merge.rb
Created September 1, 2012 22:23
Show Gist options
  • Save quicksnap/3589453 to your computer and use it in GitHub Desktop.
Save quicksnap/3589453 to your computer and use it in GitHub Desktop.
Somewhat hacky post-merge hook for rails bundle/migration updates
#!/usr/bin/env ruby
def is_mac?
RUBY_PLATFORM.downcase.include?("darwin")
end
def getc_input
# http://stackoverflow.com/a/174967/250407
# http://bit.ly/PMZdKv
begin
flag = is_mac? ? '-f' : '-F'
system("stty #{flag} /dev/tty raw -echo")
tty = open("/dev/tty", "r")
begin
str = tty.getc
end while !(["Y","N"].include?(str.chr.upcase) || str == "\r") # yuck..
ensure
system("stty #{flag} /dev/tty -raw echo")
tty.close
end
return str
end
# Fix RVM path problem from git subshell, puts "/usr/bin" at the end
ENV['PATH'] = ENV['PATH'].split(":").delete_if {|path| path == "/usr/bin" }.push("/usr/bin").join(":")
diff = `git diff --name-only HEAD@{1} HEAD`
if diff.index("db/migrate")
print "\n\033[34mA new migration has been found! Do you want to run it?\033[0m [Yn]: "
STDOUT.flush
str = getc_input
puts "" #newline
unless str.chr.upcase == 'N'
puts "\033[34mMigrating development and test DBs..\033[0m"
puts "\033[34m rake db:migrate...\033[0m\n"
puts `bundle exec rake db:migrate`
puts "\033[34m rake db:test:prepare...\033[0m\n"
puts `bundle exec rake db:test:prepare`
puts "\033[34mDone!\033[0m"
else
puts "\033[34mSkipping migrations\033[0m"
end
end
if diff.index("Gemfile")
print "\n\033[34mA new bundle update has been found! Do you want to install it?\033[0m [Yn]: "
STDOUT.flush
str = getc_input
puts "" #newline
unless str.chr.upcase == 'N'
puts "\033[34mUpdating bundles..\033[0m"
puts `bundle install`
puts "\033[34mDone!\033[0m"
else
puts "\033[34mSkipping bundle update\033[0m"
end
end
@unflores
Copy link

Heads up you can make this a little more readable by doing something like this:

    def green(text)
       "\033[34m#{text}\033[0m"
    end

    puts green('Done!')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment