use_rails 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env ruby
  2. #
  3. # Switches the development environment to use the given
  4. # version of rails. Caches the Gemfile.locks so that
  5. # switching it very fast.
  6. #
  7. require File.expand_path("../../spec/support/detect_rails_version", __FILE__)
  8. def cmd(command)
  9. puts command
  10. exit 1 unless system command
  11. end
  12. version = ARGV[0]
  13. unless version
  14. puts "USAGE: ./script/#{__FILE__} VERSION [OPTIONS]"
  15. puts
  16. puts "Options:"
  17. puts " --clobber Add this flag to remove the existing Gemfile.lock before running"
  18. exit(1)
  19. end
  20. def file_or_symlink?(path)
  21. File.exist?(path) || File.symlink?(path)
  22. end
  23. gem_lock_dir = ".gemfile-locks"
  24. gem_lock_file = "#{gem_lock_dir}/Gemfile-#{version}.lock"
  25. # Ensure our lock dir is created
  26. cmd "mkdir #{gem_lock_dir}" unless File.exists?(gem_lock_dir)
  27. # --clobber passed in
  28. if File.exists?(gem_lock_file) && ARGV.include?('--clobber')
  29. cmd "rm #{gem_lock_file}"
  30. end
  31. write_rails_version(version)
  32. # Ensure that bundler installs
  33. ENV['RUBYOPT'] = ''
  34. if File.exists?(gem_lock_file)
  35. cmd("rm Gemfile.lock") if file_or_symlink?("Gemfile.lock")
  36. cmd("ln -s #{gem_lock_file} Gemfile.lock")
  37. cmd("bundle")
  38. else
  39. cmd "rm Gemfile.lock" if file_or_symlink?("Gemfile.lock")
  40. cmd "bundle install"
  41. cmd "mv Gemfile.lock #{gem_lock_file}"
  42. cmd("ln -s #{gem_lock_file} Gemfile.lock")
  43. end