Rakefile 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. require 'rake'
  2. require 'rake/testtask'
  3. require 'rake/rdoctask'
  4. $VERBOSE = nil
  5. TEST_CHANGES_SINCE = Time.now - 600
  6. desc "Run all the tests on a fresh test database"
  7. task :default => [ :test_units, :test_functional ]
  8. desc 'Require application environment.'
  9. task :environment do
  10. unless defined? RAILS_ROOT
  11. require File.dirname(__FILE__) + '/config/environment'
  12. end
  13. end
  14. desc "Generate API documentatio, show coding stats"
  15. task :doc => [ :appdoc, :stats ]
  16. # Look up tests for recently modified sources.
  17. def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
  18. FileList[source_pattern].map do |path|
  19. if File.mtime(path) > touched_since
  20. test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
  21. test if File.exists?(test)
  22. end
  23. end.compact
  24. end
  25. desc 'Test recent changes.'
  26. Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
  27. since = TEST_CHANGES_SINCE
  28. touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
  29. recent_tests('app/models/*.rb', 'test/unit', since) +
  30. recent_tests('app/controllers/*.rb', 'test/functional', since)
  31. t.libs << 'test'
  32. t.verbose = true
  33. t.test_files = touched.uniq
  34. end
  35. task :test_recent => [ :clone_structure_to_test ]
  36. desc "Run the unit tests in test/unit"
  37. Rake::TestTask.new("test_units") { |t|
  38. t.libs << "test"
  39. t.pattern = 'test/unit/**/*_test.rb'
  40. t.verbose = true
  41. }
  42. task :test_units => [ :clone_structure_to_test ]
  43. desc "Run the functional tests in test/functional"
  44. Rake::TestTask.new("test_functional") { |t|
  45. t.libs << "test"
  46. t.pattern = 'test/functional/**/*_test.rb'
  47. t.verbose = true
  48. }
  49. task :test_functional => [ :clone_structure_to_test ]
  50. desc "Generate documentation for the application"
  51. Rake::RDocTask.new("appdoc") { |rdoc|
  52. rdoc.rdoc_dir = 'doc/app'
  53. rdoc.title = "Rails Application Documentation"
  54. rdoc.options << '--line-numbers --inline-source'
  55. rdoc.rdoc_files.include('doc/README_FOR_APP')
  56. rdoc.rdoc_files.include('app/**/*.rb')
  57. }
  58. desc "Generate documentation for the Rails framework"
  59. Rake::RDocTask.new("apidoc") { |rdoc|
  60. rdoc.rdoc_dir = 'doc/api'
  61. rdoc.template = "#{ENV['template']}.rb" if ENV['template']
  62. rdoc.title = "Rails Framework Documentation"
  63. rdoc.options << '--line-numbers --inline-source'
  64. rdoc.rdoc_files.include('README')
  65. rdoc.rdoc_files.include('CHANGELOG')
  66. rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG')
  67. rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE')
  68. rdoc.rdoc_files.include('vendor/rails/activerecord/README')
  69. rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG')
  70. rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb')
  71. rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*')
  72. rdoc.rdoc_files.include('vendor/rails/actionpack/README')
  73. rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG')
  74. rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb')
  75. rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb')
  76. rdoc.rdoc_files.include('vendor/rails/actionmailer/README')
  77. rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG')
  78. rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb')
  79. rdoc.rdoc_files.include('vendor/rails/actionwebservice/README')
  80. rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
  81. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service.rb')
  82. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/*.rb')
  83. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/api/*.rb')
  84. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/client/*.rb')
  85. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/container/*.rb')
  86. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/dispatcher/*.rb')
  87. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/protocol/*.rb')
  88. rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/support/*.rb')
  89. rdoc.rdoc_files.include('vendor/rails/activesupport/README')
  90. rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
  91. rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
  92. }
  93. desc "Report code statistics (KLOCs, etc) from the application"
  94. task :stats => [ :environment ] do
  95. require 'code_statistics'
  96. CodeStatistics.new(
  97. ["Helpers", "app/helpers"],
  98. ["Controllers", "app/controllers"],
  99. ["APIs", "app/apis"],
  100. ["Components", "components"],
  101. ["Functionals", "test/functional"],
  102. ["Models", "app/models"],
  103. ["Units", "test/unit"]
  104. ).to_s
  105. end
  106. desc "Recreate the test databases from the development structure"
  107. task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
  108. abcs = ActiveRecord::Base.configurations
  109. case abcs["test"]["adapter"]
  110. when "mysql"
  111. ActiveRecord::Base.establish_connection(:test)
  112. ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
  113. IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
  114. ActiveRecord::Base.connection.execute(table)
  115. end
  116. when "postgresql"
  117. ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
  118. ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
  119. ENV['PGPASSWORD'] = abcs["test"]["password"]
  120. `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
  121. when "sqlite", "sqlite3"
  122. `#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
  123. else
  124. raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
  125. end
  126. end
  127. desc "Dump the database structure to a SQL file"
  128. task :db_structure_dump => :environment do
  129. abcs = ActiveRecord::Base.configurations
  130. case abcs[RAILS_ENV]["adapter"]
  131. when "mysql"
  132. ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
  133. File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
  134. when "postgresql"
  135. ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
  136. ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
  137. ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"]
  138. `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
  139. when "sqlite", "sqlite3"
  140. `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
  141. else
  142. raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
  143. end
  144. end
  145. desc "Empty the test database"
  146. task :purge_test_database => :environment do
  147. abcs = ActiveRecord::Base.configurations
  148. case abcs["test"]["adapter"]
  149. when "mysql"
  150. ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
  151. ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
  152. when "postgresql"
  153. ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
  154. ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
  155. ENV['PGPASSWORD'] = abcs["test"]["password"]
  156. `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
  157. `createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
  158. when "sqlite","sqlite3"
  159. File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
  160. else
  161. raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
  162. end
  163. end