| |   |
| 0 | | class PreCommit::Rspec < PreCommit |
| 1 | | def pre_commit |
| 2 | | check_for_gem_dependencies |
| 3 | | fix_cr_lf |
| 4 | | touch_revision_storing_files |
| 5 | | pre_commit_core |
| 6 | | pre_commit_textmate_bundle |
| 7 | | pre_commit_rails |
| 8 | | ok_to_commit |
| 9 | | end |
| 10 | | |
| 11 | | def check_for_gem_dependencies |
| 12 | | require "rubygems" |
| 13 | | gem 'rake' |
| 14 | | gem 'webby' |
| 15 | | gem 'coderay' |
| 16 | | gem 'RedCloth' |
| 17 | | gem 'syntax' |
| 18 | | gem 'diff-lcs' |
| 19 | | gem 'heckle' unless PLATFORM == "i386-mswin32" |
| 20 | | gem 'hpricot' |
| 21 | | end |
| 22 | | |
| 23 | | def fix_cr_lf |
| 24 | | files = FileList['**/*.rb']. |
| 25 | | exclude('example_rails_app/vendor/**'). |
| 26 | | exclude('rspec/translated_specs/**') |
| 27 | | $\="\n" |
| 28 | | files.each do |f| |
| 29 | | raw_content = File.read(f) |
| 30 | | fixed_content = "" |
| 31 | | raw_content.each_line do |line| |
| 32 | | fixed_content << line |
| 33 | | end |
| 34 | | unless raw_content == fixed_content |
| 35 | | File.open(f, "w") do |io| |
| 36 | | io.print fixed_content |
| 37 | | end |
| 38 | | end |
| 39 | | end |
| 40 | | end |
| 41 | | |
| 42 | | def touch_revision_storing_files |
| 43 | | files = [ |
| 44 | | 'rspec/lib/spec/version.rb', |
| 45 | | 'rspec_on_rails/lib/spec/rails/version.rb' |
| 46 | | ] |
| 47 | | build_time_utc = Time.now.utc.strftime('%Y%m%d%H%M%S') |
| 48 | | files.each do |path| |
| 49 | | abs_path = File.join(root_dir, path) |
| 50 | | content = File.open(abs_path).read |
| 51 | | touched_content = content.gsub(/BUILD_TIME_UTC = (\d*)\n/n, "BUILD_TIME_UTC = #{build_time_utc}\n") |
| 52 | | File.open(abs_path, 'w') do |io| |
| 53 | | io.write touched_content |
| 54 | | end |
| 55 | | end |
| 56 | | end |
| 57 | | |
| 58 | | def pre_commit_core |
| 59 | | Dir.chdir 'rspec' do |
| 60 | | rake = (PLATFORM == "i386-mswin32") ? "rake.bat" : "rake" |
| 61 | | system("#{rake} pre_commit --verbose --trace") |
| 62 | | raise "RSpec Core pre_commit failed" if error_code? |
| 63 | | end |
| 64 | | end |
| 65 | | |
| 66 | | def pre_commit_textmate_bundle |
| 67 | | Dir.chdir 'RSpec.tmbundle/Support' do |
| 68 | | rake = (PLATFORM == "i386-mswin32") ? "rake.bat" : "rake" |
| 69 | | system("#{rake} spec --verbose --trace") |
| 70 | | raise "RSpec Textmate Bundle specs failed" if error_code? |
| 71 | | end |
| 72 | | end |
| 73 | | |
| 74 | | def install_dependencies |
| 75 | | Dir.chdir 'example_rails_app' do |
| 76 | | rake_sh("-f Multirails.rake install_dependencies") |
| 77 | | end |
| 78 | | end |
| 79 | | |
| 80 | | def update_dependencies |
| 81 | | Dir.chdir 'example_rails_app' do |
| 82 | | rake_sh("-f Multirails.rake update_dependencies") |
| 83 | | end |
| 84 | | end |
| 85 | | |
| 86 | | def pre_commit_rails |
| 87 | | Dir.chdir 'example_rails_app' do |
| 88 | | rake = (PLATFORM == "i386-mswin32") ? "rake.cmd" : "rake" |
| 89 | | cmd = "#{rake} -f Multirails.rake pre_commit --trace" |
| 90 | | system(cmd) |
| 91 | | if error_code? |
| 92 | | message = <<-EOF |
| 93 | | ############################################################ |
| 94 | | RSpec on Rails Plugin pre_commit failed. For more info: |
| 95 | | |
| 96 | | cd example_rails_app |
| 97 | | #{cmd} |
| 98 | | |
| 99 | | ############################################################ |
| 100 | | EOF |
| 101 | | raise message.gsub(/^ /, '') |
| 102 | | end |
| 103 | | end |
| 104 | | end |
| 105 | | |
| 106 | | def ok_to_commit |
| 107 | | puts "OK TO COMMIT" |
| 108 | | end |
| 109 | | end |
| toggle raw diff |
--- a/pre_commit/lib/pre_commit/rspec.rb
+++ /dev/null
@@ -1,110 +0,0 @@
-class PreCommit::Rspec < PreCommit
- def pre_commit
- check_for_gem_dependencies
- fix_cr_lf
- touch_revision_storing_files
- pre_commit_core
- pre_commit_textmate_bundle
- pre_commit_rails
- ok_to_commit
- end
-
- def check_for_gem_dependencies
- require "rubygems"
- gem 'rake'
- gem 'webby'
- gem 'coderay'
- gem 'RedCloth'
- gem 'syntax'
- gem 'diff-lcs'
- gem 'heckle' unless PLATFORM == "i386-mswin32"
- gem 'hpricot'
- end
-
- def fix_cr_lf
- files = FileList['**/*.rb'].
- exclude('example_rails_app/vendor/**').
- exclude('rspec/translated_specs/**')
- $\="\n"
- files.each do |f|
- raw_content = File.read(f)
- fixed_content = ""
- raw_content.each_line do |line|
- fixed_content << line
- end
- unless raw_content == fixed_content
- File.open(f, "w") do |io|
- io.print fixed_content
- end
- end
- end
- end
-
- def touch_revision_storing_files
- files = [
- 'rspec/lib/spec/version.rb',
- 'rspec_on_rails/lib/spec/rails/version.rb'
- ]
- build_time_utc = Time.now.utc.strftime('%Y%m%d%H%M%S')
- files.each do |path|
- abs_path = File.join(root_dir, path)
- content = File.open(abs_path).read
- touched_content = content.gsub(/BUILD_TIME_UTC = (\d*)\n/n, "BUILD_TIME_UTC = #{build_time_utc}\n")
- File.open(abs_path, 'w') do |io|
- io.write touched_content
- end
- end
- end
-
- def pre_commit_core
- Dir.chdir 'rspec' do
- rake = (PLATFORM == "i386-mswin32") ? "rake.bat" : "rake"
- system("#{rake} pre_commit --verbose --trace")
- raise "RSpec Core pre_commit failed" if error_code?
- end
- end
-
- def pre_commit_textmate_bundle
- Dir.chdir 'RSpec.tmbundle/Support' do
- rake = (PLATFORM == "i386-mswin32") ? "rake.bat" : "rake"
- system("#{rake} spec --verbose --trace")
- raise "RSpec Textmate Bundle specs failed" if error_code?
- end
- end
-
- def install_dependencies
- Dir.chdir 'example_rails_app' do
- rake_sh("-f Multirails.rake install_dependencies")
- end
- end
-
- def update_dependencies
- Dir.chdir 'example_rails_app' do
- rake_sh("-f Multirails.rake update_dependencies")
- end
- end
-
- def pre_commit_rails
- Dir.chdir 'example_rails_app' do
- rake = (PLATFORM == "i386-mswin32") ? "rake.cmd" : "rake"
- cmd = "#{rake} -f Multirails.rake pre_commit --trace"
- system(cmd)
- if error_code?
- message = <<-EOF
- ############################################################
- RSpec on Rails Plugin pre_commit failed. For more info:
-
- cd example_rails_app
- #{cmd}
-
- ############################################################
- EOF
- raise message.gsub(/^ /, '')
- end
- end
- end
-
- def ok_to_commit
- puts "OK TO COMMIT"
- end
-end |
| |   |
| 0 | | class PreCommit::RspecOnRails < PreCommit |
| 1 | | def pre_commit |
| 2 | | install_plugins |
| 3 | | check_dependencies |
| 4 | | used_railses = [] |
| 5 | | VENDOR_DEPS.each do |dependency| |
| 6 | | rails_dir = File.expand_path(dependency[:checkout_path]) |
| 7 | | rails_version = rails_version_from_dir(rails_dir) |
| 8 | | begin |
| 9 | | rspec_pre_commit(rails_version, false) |
| 10 | | used_railses << rails_version |
| 11 | | rescue => e |
| 12 | | unless rails_version == 'edge' |
| 13 | | raise e |
| 14 | | end |
| 15 | | end |
| 16 | | end |
| 17 | | uninstall_plugins |
| 18 | | puts "All specs passed against the following released versions of Rails: #{used_railses.join(", ")}" |
| 19 | | unless used_railses.include?('edge') |
| 20 | | puts "There were errors running pre_commit against edge" |
| 21 | | end |
| 22 | | end |
| 23 | | |
| 24 | | def rails_version_from_dir(rails_dir) |
| 25 | | File.basename(rails_dir) |
| 26 | | end |
| 27 | | |
| 28 | | def rspec_pre_commit(rails_version=ENV['RSPEC_RAILS_VERSION'],uninstall=true) |
| 29 | | puts "#####################################################" |
| 30 | | puts "running pre_commit against rails #{rails_version}" |
| 31 | | puts "#####################################################" |
| 32 | | ENV['RSPEC_RAILS_VERSION'] = rails_version |
| 33 | | cleanup(uninstall) |
| 34 | | ensure_db_config |
| 35 | | clobber_sqlite_data |
| 36 | | install_plugins |
| 37 | | generate_rspec |
| 38 | | |
| 39 | | generate_login_controller |
| 40 | | create_purchase |
| 41 | | |
| 42 | | rake_sh "spec" |
| 43 | | rake_sh "spec:plugins:rspec_on_rails" |
| 44 | | |
| 45 | | # TODO - why is this necessary? Shouldn't the specs leave |
| 46 | | # a clean DB? |
| 47 | | rake_sh "db:test:prepare" |
| 48 | | sh "ruby vendor/plugins/rspec_on_rails/stories/all.rb" |
| 49 | | cleanup(uninstall) |
| 50 | | end |
| 51 | | |
| 52 | | def cleanup(uninstall=true) |
| 53 | | revert_routes |
| 54 | | rm_generated_login_controller_files |
| 55 | | destroy_purchase |
| 56 | | uninstall_plugins if uninstall |
| 57 | | end |
| 58 | | |
| 59 | | def revert_routes |
| 60 | | output = silent_sh("cp config/routes.rb.bak config/routes.rb") |
| 61 | | raise "Error reverting routes.rb" if shell_error?(output) |
| 62 | | end |
| 63 | | |
| 64 | | def create_purchase |
| 65 | | generate_purchase |
| 66 | | migrate_up |
| 67 | | end |
| 68 | | |
| 69 | | def install_plugins |
| 70 | | install_rspec_on_rails_plugin |
| 71 | | install_rspec_plugin |
| 72 | | end |
| 73 | | |
| 74 | | def install_rspec_on_rails_plugin |
| 75 | | rm_rf 'vendor/plugins/rspec_on_rails' |
| 76 | | copy '../rspec_on_rails', 'vendor/plugins/' |
| 77 | | end |
| 78 | | |
| 79 | | def install_rspec_plugin |
| 80 | | rm_rf 'vendor/plugins/rspec' |
| 81 | | copy '../rspec', 'vendor/plugins/' |
| 82 | | end |
| 83 | | |
| 84 | | def uninstall_plugins |
| 85 | | rm_rf 'vendor/plugins/rspec_on_rails' |
| 86 | | rm_rf 'vendor/plugins/rspec' |
| 87 | | rm_rf 'script/spec' |
| 88 | | rm_rf 'script/spec_server' |
| 89 | | rm_rf 'spec/spec_helper.rb' |
| 90 | | rm_rf 'spec/spec.opts' |
| 91 | | rm_rf 'spec/rcov.opts' |
| 92 | | end |
| 93 | | |
| 94 | | def copy(source, target) |
| 95 | | output = silent_sh("cp -R #{File.expand_path(source)} #{File.expand_path(target)}") |
| 96 | | raise "Error installing rspec" if shell_error?(output) |
| 97 | | end |
| 98 | | |
| 99 | | def generate_rspec |
| 100 | | result = silent_sh("ruby script/generate rspec --force") |
| 101 | | if error_code? || result =~ /^Missing/ |
| 102 | | raise "Failed to generate rspec environment:\n#{result}" |
| 103 | | end |
| 104 | | end |
| 105 | | |
| 106 | | def ensure_db_config |
| 107 | | config_path = 'config/database.yml' |
| 108 | | unless File.exists?(config_path) |
| 109 | | message = <<-EOF |
| 110 | | ##################################################### |
| 111 | | Could not find #{config_path} |
| 112 | | |
| 113 | | You can get rake to generate this file for you using either of: |
| 114 | | rake rspec:generate_mysql_config |
| 115 | | rake rspec:generate_sqlite3_config |
| 116 | | |
| 117 | | If you use mysql, you'll need to create dev and test |
| 118 | | databases and users for each. To do this, standing |
| 119 | | in rspec_on_rails, log into mysql as root and then... |
| 120 | | mysql> source db/mysql_setup.sql; |
| 121 | | |
| 122 | | There is also a teardown script that will remove |
| 123 | | the databases and users: |
| 124 | | mysql> source db/mysql_teardown.sql; |
| 125 | | ##################################################### |
| 126 | | EOF |
| 127 | | raise message.gsub(/^ /, '') |
| 128 | | end |
| 129 | | end |
| 130 | | |
| 131 | | def generate_mysql_config |
| 132 | | copy 'config/database.mysql.yml', 'config/database.yml' |
| 133 | | end |
| 134 | | |
| 135 | | def generate_sqlite3_config |
| 136 | | copy 'config/database.sqlite3.yml', 'config/database.yml' |
| 137 | | end |
| 138 | | |
| 139 | | def clobber_db_config |
| 140 | | rm 'config/database.yml' |
| 141 | | end |
| 142 | | |
| 143 | | def clobber_sqlite_data |
| 144 | | rm_rf 'db/*.db' |
| 145 | | end |
| 146 | | |
| 147 | | def generate_purchase |
| 148 | | generator = "ruby script/generate rspec_scaffold purchase order_id:integer created_at:datetime amount:decimal keyword:string description:text --force" |
| 149 | | notice = <<-EOF |
| 150 | | ##################################################### |
| 151 | | #{generator} |
| 152 | | ##################################################### |
| 153 | | EOF |
| 154 | | puts notice.gsub(/^ /, '') |
| 155 | | result = silent_sh(generator) |
| 156 | | if error_code? || result =~ /not/ |
| 157 | | raise "rspec_scaffold failed. #{result}" |
| 158 | | end |
| 159 | | end |
| 160 | | |
| 161 | | def purchase_migration_version |
| 162 | | "005" |
| 163 | | end |
| 164 | | |
| 165 | | def migrate_up |
| 166 | | rake_sh "db:migrate" |
| 167 | | end |
| 168 | | |
| 169 | | def destroy_purchase |
| 170 | | migrate_down |
| 171 | | rm_generated_purchase_files |
| 172 | | end |
| 173 | | |
| 174 | | def migrate_down |
| 175 | | notice = <<-EOF |
| 176 | | ##################################################### |
| 177 | | Migrating down and reverting config/routes.rb |
| 178 | | ##################################################### |
| 179 | | EOF |
| 180 | | puts notice.gsub(/^ /, '') |
| 181 | | rake_sh "db:migrate", 'VERSION' => (purchase_migration_version.to_i - 1) |
| 182 | | output = silent_sh("cp config/routes.rb.bak config/routes.rb") |
| 183 | | raise "revert failed: #{output}" if error_code? |
| 184 | | end |
| 185 | | |
| 186 | | def rm_generated_purchase_files |
| 187 | | puts "#####################################################" |
| 188 | | puts "Removing generated files:" |
| 189 | | generated_files = %W{ |
| 190 | | app/helpers/purchases_helper.rb |
| 191 | | app/models/purchase.rb |
| 192 | | app/controllers/purchases_controller.rb |
| 193 | | app/views/purchases |
| 194 | | db/migrate/#{purchase_migration_version}_create_purchases.rb |
| 195 | | spec/models/purchase_spec.rb |
| 196 | | spec/helpers/purchases_helper_spec.rb |
| 197 | | spec/controllers/purchases_controller_spec.rb |
| 198 | | spec/controllers/purchases_routing_spec.rb |
| 199 | | spec/fixtures/purchases.yml |
| 200 | | spec/views/purchases |
| 201 | | } |
| 202 | | generated_files.each do |file| |
| 203 | | rm_rf file |
| 204 | | end |
| 205 | | puts "#####################################################" |
| 206 | | end |
| 207 | | |
| 208 | | def generate_login_controller |
| 209 | | generator = "ruby script/generate rspec_controller login signup login logout --force" |
| 210 | | notice = <<-EOF |
| 211 | | ##################################################### |
| 212 | | #{generator} |
| 213 | | ##################################################### |
| 214 | | EOF |
| 215 | | puts notice.gsub(/^ /, '') |
| 216 | | result = silent_sh(generator) |
| 217 | | if error_code? || result =~ /not/ |
| 218 | | raise "rspec_scaffold failed. #{result}" |
| 219 | | end |
| 220 | | end |
| 221 | | |
| 222 | | def rm_generated_login_controller_files |
| 223 | | puts "#####################################################" |
| 224 | | puts "Removing generated files:" |
| 225 | | generated_files = %W{ |
| 226 | | app/helpers/login_helper.rb |
| 227 | | app/controllers/login_controller.rb |
| 228 | | app/views/login |
| 229 | | spec/helpers/login_helper_spec.rb |
| 230 | | spec/controllers/login_controller_spec.rb |
| 231 | | spec/views/login |
| 232 | | } |
| 233 | | generated_files.each do |file| |
| 234 | | rm_rf file |
| 235 | | end |
| 236 | | puts "#####################################################" |
| 237 | | end |
| 238 | | |
| 239 | | def install_dependencies |
| 240 | | VENDOR_DEPS.each do |dep| |
| 241 | | puts "\nChecking for #{dep[:name]} ..." |
| 242 | | dest = dep[:checkout_path] |
| 243 | | if File.exists?(dest) |
| 244 | | puts "#{dep[:name]} already installed" |
| 245 | | else |
| 246 | | cmd = "svn co #{dep[:url]} #{dest}" |
| 247 | | puts "Installing #{dep[:name]}" |
| 248 | | puts "This may take a while." |
| 249 | | puts cmd |
| 250 | | system(cmd) |
| 251 | | puts "Done!" |
| 252 | | end |
| 253 | | end |
| 254 | | puts |
| 255 | | end |
| 256 | | |
| 257 | | def check_dependencies |
| 258 | | VENDOR_DEPS.each do |dep| |
| 259 | | unless File.exist?(dep[:checkout_path]) |
| 260 | | raise "There is no checkout of #{dep[:checkout_path]}. Please run rake install_dependencies" |
| 261 | | end |
| 262 | | # Verify that the current working copy is right |
| 263 | | if `svn info #{dep[:checkout_path]}` =~ /^URL: (.*)/ |
| 264 | | actual_url = $1 |
| 265 | | if actual_url != dep[:url] |
| 266 | | raise "Your working copy in #{dep[:checkout_path]} points to \n#{actual_url}\nIt has moved to\n#{dep[:url]}\nPlease delete the working copy and run rake install_dependencies" |
| 267 | | end |
| 268 | | end |
| 269 | | end |
| 270 | | end |
| 271 | | |
| 272 | | def update_dependencies |
| 273 | | check_dependencies |
| 274 | | VENDOR_DEPS.each do |dep| |
| 275 | | next if dep[:tagged?] # |
| 276 | | puts "\nUpdating #{dep[:name]} ..." |
| 277 | | dest = dep[:checkout_path] |
| 278 | | system("svn cleanup #{dest}") |
| 279 | | cmd = "svn up #{dest}" |
| 280 | | puts cmd |
| 281 | | system(cmd) |
| 282 | | puts "Done!" |
| 283 | | end |
| 284 | | end |
| 285 | | |
| 286 | | VENDOR_DEPS = [ |
| 287 | | { |
| 288 | | :checkout_path => "vendor/rails/2.0.2", |
| 289 | | :name => "rails 2.0.2", |
| 290 | | :url => "http://dev.rubyonrails.org/svn/rails/tags/rel_2-0-2", |
| 291 | | :tagged? => true |
| 292 | | }, |
| 293 | | { |
| 294 | | :checkout_path => "vendor/rails/1.2.6", |
| 295 | | :name => "rails 1.2.6", |
| 296 | | :url => "http://dev.rubyonrails.org/svn/rails/tags/rel_1-2-6", |
| 297 | | :tagged? => true |
| 298 | | }, |
| 299 | | { |
| 300 | | :checkout_path => "vendor/rails/1.2.3", |
| 301 | | :name => "rails 1.2.3", |
| 302 | | :url => "http://dev.rubyonrails.org/svn/rails/tags/rel_1-2-3", |
| 303 | | :tagged? => true |
| 304 | | }, |
| 305 | | { |
| 306 | | :checkout_path => "vendor/rails/edge", |
| 307 | | :name => "edge rails", |
| 308 | | :url => "http://svn.rubyonrails.org/rails/trunk", |
| 309 | | :tagged? => false |
| 310 | | } |
| 311 | | ] |
| 312 | | end |
| toggle raw diff |
--- a/pre_commit/lib/pre_commit/rspec_on_rails.rb
+++ /dev/null
@@ -1,313 +0,0 @@
-class PreCommit::RspecOnRails < PreCommit
- def pre_commit
- install_plugins
- check_dependencies
- used_railses = []
- VENDOR_DEPS.each do |dependency|
- rails_dir = File.expand_path(dependency[:checkout_path])
- rails_version = rails_version_from_dir(rails_dir)
- begin
- rspec_pre_commit(rails_version, false)
- used_railses << rails_version
- rescue => e
- unless rails_version == 'edge'
- raise e
- end
- end
- end
- uninstall_plugins
- puts "All specs passed against the following released versions of Rails: #{used_railses.join(", ")}"
- unless used_railses.include?('edge')
- puts "There were errors running pre_commit against edge"
- end
- end
-
- def rails_version_from_dir(rails_dir)
- File.basename(rails_dir)
- end
-
- def rspec_pre_commit(rails_version=ENV['RSPEC_RAILS_VERSION'],uninstall=true)
- puts "#####################################################"
- puts "running pre_commit against rails #{rails_version}"
- puts "#####################################################"
- ENV['RSPEC_RAILS_VERSION'] = rails_version
- cleanup(uninstall)
- ensure_db_config
- clobber_sqlite_data
- install_plugins
- generate_rspec
-
- generate_login_controller
- create_purchase
-
- rake_sh "spec"
- rake_sh "spec:plugins:rspec_on_rails"
-
- # TODO - why is this necessary? Shouldn't the specs leave
- # a clean DB?
- rake_sh "db:test:prepare"
- sh "ruby vendor/plugins/rspec_on_rails/stories/all.rb"
- cleanup(uninstall)
- end
-
- def cleanup(uninstall=true)
- revert_routes
- rm_generated_login_controller_files
- destroy_purchase
- uninstall_plugins if uninstall
- end
-
- def revert_routes
- output = silent_sh("cp config/routes.rb.bak config/routes.rb")
- raise "Error reverting routes.rb" if shell_error?(output)
- end
-
- def create_purchase
- generate_purchase
- migrate_up
- end
-
- def install_plugins
- install_rspec_on_rails_plugin
- install_rspec_plugin
- end
-
- def install_rspec_on_rails_plugin
- rm_rf 'vendor/plugins/rspec_on_rails'
- copy '../rspec_on_rails', 'vendor/plugins/'
- end
-
- def install_rspec_plugin
- rm_rf 'vendor/plugins/rspec'
- copy '../rspec', 'vendor/plugins/'
- end
-
- def uninstall_plugins
- rm_rf 'vendor/plugins/rspec_on_rails'
- rm_rf 'vendor/plugins/rspec'
- rm_rf 'script/spec'
- rm_rf 'script/spec_server'
- rm_rf 'spec/spec_helper.rb'
- rm_rf 'spec/spec.opts'
- rm_rf 'spec/rcov.opts'
- end
-
- def copy(source, target)
- output = silent_sh("cp -R #{File.expand_path(source)} #{File.expand_path(target)}")
- raise "Error installing rspec" if shell_error?(output)
- end
-
- def generate_rspec
- result = silent_sh("ruby script/generate rspec --force")
- if error_code? || result =~ /^Missing/
- raise "Failed to generate rspec environment:\n#{result}"
- end
- end
-
- def ensure_db_config
- config_path = 'config/database.yml'
- unless File.exists?(config_path)
- message = <<-EOF
- #####################################################
- Could not find #{config_path}
-
- You can get rake to generate this file for you using either of:
- rake rspec:generate_mysql_config
- rake rspec:generate_sqlite3_config
-
- If you use mysql, you'll need to create dev and test
- databases and users for each. To do this, standing
- in rspec_on_rails, log into mysql as root and then...
- mysql> source db/mysql_setup.sql;
-
- There is also a teardown script that will remove
- the databases and users:
- mysql> source db/mysql_teardown.sql;
- #####################################################
- EOF
- raise message.gsub(/^ /, '')
- end
- end
-
- def generate_mysql_config
- copy 'config/database.mysql.yml', 'config/database.yml'
- end
-
- def generate_sqlite3_config
- copy 'config/database.sqlite3.yml', 'config/database.yml'
- end
-
- def clobber_db_config
- rm 'config/database.yml'
- end
-
- def clobber_sqlite_data
- rm_rf 'db/*.db'
- end
-
- def generate_purchase
- generator = "ruby script/generate rspec_scaffold purchase order_id:integer created_at:datetime amount:decimal keyword:string description:text --force"
- notice = <<-EOF
- #####################################################
- #{generator}
- #####################################################
- EOF
- puts notice.gsub(/^ /, '')
- result = silent_sh(generator)
- if error_code? || result =~ /not/
- raise "rspec_scaffold failed. #{result}"
- end
- end
-
- def purchase_migration_version
- "005"
- end
-
- def migrate_up
- rake_sh "db:migrate"
- end
-
- def destroy_purchase
- migrate_down
- rm_generated_purchase_files
- end
-
- def migrate_down
- notice = <<-EOF
- #####################################################
- Migrating down and reverting config/routes.rb
- #####################################################
- EOF
- puts notice.gsub(/^ /, '')
- rake_sh "db:migrate", 'VERSION' => (purchase_migration_version.to_i - 1)
- output = silent_sh("cp config/routes.rb.bak config/routes.rb")
- raise "revert failed: #{output}" if error_code?
- end
-
- def rm_generated_purchase_files
- puts "#####################################################"
- puts "Removing generated files:"
- generated_files = %W{
- app/helpers/purchases_helper.rb
- app/models/purchase.rb
- app/controllers/purchases_controller.rb
- app/views/purchases
- db/migrate/#{purchase_migration_version}_create_purchases.rb
- spec/models/purchase_spec.rb
- spec/helpers/purchases_helper_spec.rb
- spec/controllers/purchases_controller_spec.rb
- spec/controllers/purchases_routing_spec.rb
- spec/fixtures/purchases.yml
- spec/views/purchases
- }
- generated_files.each do |file|
- rm_rf file
- end
- puts "#####################################################"
- end
-
- def generate_login_controller
- generator = "ruby script/generate rspec_controller login signup login logout --force"
- notice = <<-EOF
- #####################################################
- #{generator}
- #####################################################
- EOF
- puts notice.gsub(/^ /, '')
- result = silent_sh(generator)
- if error_code? || result =~ /not/
- raise "rspec_scaffold failed. #{result}"
- end
- end
-
- def rm_generated_login_controller_files
- puts "#####################################################"
- puts "Removing generated files:"
- generated_files = %W{
- app/helpers/login_helper.rb
- app/controllers/login_controller.rb
- app/views/login
- spec/helpers/login_helper_spec.rb
- spec/controllers/login_controller_spec.rb
- spec/views/login
- }
- generated_files.each do |file|
- rm_rf file
- end
- puts "#####################################################"
- end
-
- def install_dependencies
- VENDOR_DEPS.each do |dep|
- puts "\nChecking for #{dep[:name]} ..."
- dest = dep[:checkout_path]
- if File.exists?(dest)
- puts "#{dep[:name]} already installed"
- else
- cmd = "svn co #{dep[:url]} #{dest}"
- puts "Installing #{dep[:name]}"
- puts "This may take a while."
- puts cmd
- system(cmd)
- puts "Done!"
- end
- end
- puts
- end
-
- def check_dependencies
- VENDOR_DEPS.each do |dep|
- unless File.exist?(dep[:checkout_path])
- raise "There is no checkout of #{dep[:checkout_path]}. Please run rake install_dependencies"
- end
- # Verify that the current working copy is right
- if `svn info #{dep[:checkout_path]}` =~ /^URL: (.*)/
- actual_url = $1
- if actual_url != dep[:url]
- raise "Your working copy in #{dep[:checkout_path]} points to \n#{actual_url}\nIt has moved to\n#{dep[:url]}\nPlease delete the working copy and run rake install_dependencies"
- end
- end
- end
- end
-
- def update_dependencies
- check_dependencies
- VENDOR_DEPS.each do |dep|
- next if dep[:tagged?] #
- puts "\nUpdating #{dep[:name]} ..."
- dest = dep[:checkout_path]
- system("svn cleanup #{dest}")
- cmd = "svn up #{dest}"
- puts cmd
- system(cmd)
- puts "Done!"
- end
- end
-
- VENDOR_DEPS = [
- {
- :checkout_path => "vendor/rails/2.0.2",
- :name => "rails 2.0.2",
- :url => "http://dev.rubyonrails.org/svn/rails/tags/rel_2-0-2",
- :tagged? => true
- },
- {
- :checkout_path => "vendor/rails/1.2.6",
- :name => "rails 1.2.6",
- :url => "http://dev.rubyonrails.org/svn/rails/tags/rel_1-2-6",
- :tagged? => true
- },
- {
- :checkout_path => "vendor/rails/1.2.3",
- :name => "rails 1.2.3",
- :url => "http://dev.rubyonrails.org/svn/rails/tags/rel_1-2-3",
- :tagged? => true
- },
- {
- :checkout_path => "vendor/rails/edge",
- :name => "edge rails",
- :url => "http://svn.rubyonrails.org/rails/trunk",
- :tagged? => false
- }
- ]
-end |