Skip to content

Commit

Permalink
Created a rake task "bundle:use[3.0.6]" to easily switch rails versions
Browse files Browse the repository at this point in the history
  • Loading branch information
gregbell committed Jun 21, 2011
1 parent 6d63e36 commit 4898d08
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ source 'http://rubygems.org'

gemspec

# Set the RAILS env variable to test with a specific version of rails
case ENV["RAILS"]
require File.expand_path('../spec/support/detect_rails_version', __FILE__)

case detect_rails_version
when /3.0.(\d)*/
gem 'rails', "= 3.0.#{$1}"
gem "meta_search", '~> 1.0.0'
Expand Down
41 changes: 33 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@ def cmd(command)
system command
end

desc "Install all supported versions of rails"
task :install_all_rails do
(0..7).to_a.each do |v|
system "rm Gemfile.lock"
puts "Installing for RAILS=3.0.#{v}"
cmd "RAILS=3.0.#{v} bundle install"
require File.expand_path('../spec/support/detect_rails_version', __FILE__)

namespace :bundle do
desc "Change the version of rails."
task :use, :version do |t, args|
version = args[:version]

gem_lock_dir = ".gemfile-locks"
gem_lock_file = "#{gem_lock_dir}/Gemfile-#{version}.lock"

# Ensure our lock dir is created
cmd "mkdir #{gem_lock_dir}" unless File.exists?(gem_lock_dir)

unless File.exists?(gem_lock_file)
cmd "rm Gemfile.lock" if File.exists?("Gemfile.lock")
cmd "export RAILS=#{version} && bundle install"
cmd "mv Gemfile.lock #{gem_lock_file}"
end

cmd "rm Gemfile.lock" if File.exists?("Gemfile.lock")
cmd "ln -s #{gem_lock_file} Gemfile.lock"
end
end

Expand All @@ -28,9 +43,19 @@ end
namespace :local do
desc "Creates a local rails app to play with in development"
task :setup do
unless File.exists? "test-rails-app"
system "bundle exec rails new test-rails-app -m spec/support/rails_template_with_data.rb"
rails_version = detect_rails_version
test_app_dir = ".test-rails-apps"
test_app_path = "#{test_app_dir}/test-rails-app-#{rails_version}"

# Ensure .test-rails-apps is created
cmd "mkdir #{test_app_dir}" unless File.exists?(test_app_dir)

unless File.exists? test_app_path
cmd "bundle exec rails new #{test_app_path} -m spec/support/rails_template_with_data.rb"
end

cmd "rm test-rails-app"
cmd "ln -s #{test_app_path} test-rails-app"
end

desc "Start a local rails app to play"
Expand Down
11 changes: 11 additions & 0 deletions spec/support/detect_rails_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Detects the current version of Rails that is being used
#
# You can pass it in as an ENV variable or it will use
# the current Gemfile.lock to find it
def detect_rails_version
return ENV['RAILS'] if ENV['RAILS']
return nil unless File.exists?("Gemfile.lock")

File.read("Gemfile.lock").match(/^\W*rails \(([a-z\d.]*)\)/)
return $1
end

0 comments on commit 4898d08

Please sign in to comment.