Skip to content

Instantly share code, notes, and snippets.

@pheisiph
Created May 16, 2013 08:54
Show Gist options
  • Save pheisiph/5590367 to your computer and use it in GitHub Desktop.
Save pheisiph/5590367 to your computer and use it in GitHub Desktop.
Two scripts to be put into the script folder of a rails app. ./script/setup prepares the app/system/dependencies for booting the app for the first time. ./script/bootstrap should be run whenever changes from collaborators are pulled in. It installs dependencies and updates gems if necessary. It is fast, because actions are only executed if there…
# ./script/bootstrap
#!/usr/bin/env ruby
require 'fileutils'
# Capture ctrl-c and exit with a message.
Signal.trap("SIGINT") do
puts ; puts "Buh bye".red
exit 1
end
# MONKEY PATCH TIME!
class String
# colorization
def colorize(color_code) ; "\e[#{color_code}m#{self}\e[0m" ; end
def red ; colorize(31) ; end
def green ; colorize(32) ; end
def yellow ; colorize(33) ; end
def pink ; colorize(35) ; end
end
# Returns true if command is in $PATH, false otherwise.
def command?(command)
system("which #{command} > /dev/null 2>&1")
end
def checksums(file)
checksum = `cksum #{file}`.to_i
installed = File.read(".checksums/#{file}").to_i rescue nil
[checksum, installed]
end
def write_checksum(file, checksum)
File.open(".checksums/#{file}", 'wb') { |fd| fd.puts(checksum) }
end
def update_dependencies!
if !command?('brewdle')
puts "Brewdle is not installed. Installing now.".yellow
system('gem install brewdler')
end
file = 'Brewfile'
checksum, installed = checksums(file)
if checksum == installed
puts "All dependencies are installed.".green
else
puts "Some or all dependencies are missing. Installing them now.".yellow
puts `brewdle install`
write_checksum(file, checksum)
# puts "Done".green
end
end
# Checks if the Gemfile has changed after pull. If yes, run bundle update.
def update_gems!
file = 'Gemfile'
checksum, installed = checksums(file)
if checksum == installed && system('bundle check 1>/dev/null 2>&1')
puts "Gem environment up-to-date.".green
else
puts "Your Gemfile is not up to date. Running 'bundle install'.".yellow
system('bundle install')
write_checksum(file, checksum)
# puts "Done".green
end
end
# Project Root
root = File.expand_path('../..', __FILE__)
Dir.chdir(root)
# Create the local .checksums directory
FileUtils.mkdir_p('.checksums')
update_dependencies!
update_gems!
puts "Everything should to be up to date now."
puts "Start your external dependencies by running "
puts "`foreman start -f Procfile.dev`".yellow
# ./script/setup
#!/usr/bin/env ruby
require 'fileutils'
ROOT = File.expand_path('../..', __FILE__)
PROJECT = File.basename(ROOT)
Dir.chdir(ROOT)
# Capture ctrl-c and exit with a message.
Signal.trap("SIGINT") do
puts ; puts "Buh bye".red
exit 1
end
# MONKEY PATCH TIME!
class String
# colorization
def colorize(color_code) ; "\e[#{color_code}m#{self}\e[0m" ; end
def red ; colorize(31) ; end
def green ; colorize(32) ; end
def yellow ; colorize(33) ; end
def pink ; colorize(35) ; end
end
def setup_pow!
port = File.read(File.join([ROOT, '.foreman'])).split(':').last.strip
path_to_pow = [File.readlink("/Users/#{`whoami`.strip}/.pow")
puts "linking #{PROJECT} to POW! using port #{port}"
File.open(File.join(path_to_pow, PROJECT]), 'w+') {|f| f.write(port) }
puts "#{PROJECT} is now available as http://#{PROJECT.downcase}.dev".green
rescue
puts "Cannot link POW!".red
abort
end
puts "This script assumes that you have set up your system as a developer machine."
puts "If you have not, press CTRL-C now and run the setup script,"
puts "otherwise hit enter to continue. Also make sure your Ruby env is set up."
# wait for input
input = gets.chomp
# Set up pow! for this machine.
setup_pow!
exec './script/bootstrap'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment