Skip to content

Instantly share code, notes, and snippets.

@joakimk
Created March 31, 2012 10:01
Show Gist options
  • Save joakimk/2261445 to your computer and use it in GitHub Desktop.
Save joakimk/2261445 to your computer and use it in GitHub Desktop.
Bootstrap script (script/bootstrap) from a small rails project I'm working on. Designed to be quick to run when nothing changed. Intended to be used in CI builds and on dev machines.
$ time script/bootstrap
Bundler missing, installing.
Fetching: bundler-1.1.3.gem (100%)
Successfully installed bundler-1.1.3
1 gem installed
Gemfile changed, bundling.
Schema changed, updating databases.
real 0m9.163s
user 0m3.762s
sys 0m2.413s
$ time script/bootstrap
real 0m0.054s
user 0m0.033s
sys 0m0.011s
#!/usr/bin/env ruby
def check(key)
cache_file = "tmp/.#{key}"
if File.exists?(cache_file)
old_version = File.read(cache_file)
else
old_version = nil
end
version = yield(old_version)
File.open(cache_file, 'w') { |f| f.puts(version || 'cached') }
end
check(:bundler) do |old_version|
break if old_version
unless system("source .rvmrc; which bundle > /dev/null")
puts "Bundler missing, installing."
system("gem install bundler --no-ri --no-rdoc") || exit(1)
end
end
check(:gemfile) do |old_version|
# check both Gemfile and Gemfile.lock as there might be local changes that hasn't been bundled yet
version = File.read("Gemfile") + File.read("Gemfile.lock")
if version != old_version
puts "Gemfile changed, bundling."
system("bundle 1> /dev/null") || exit(1)
end
version
end
check(:schema) do |old_version|
version = File.readlines("db/schema.rb").find { |line| line.include?("define(:version") }
if version != old_version
puts "Schema changed, updating databases."
system("rake db:migrate db:test:prepare") || exit(1)
end
version
end
@joakimk
Copy link
Author

joakimk commented Mar 31, 2012

Tip: Add system('script/bootstrap') to script/rails before "APP_PATH".

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