Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/tools/conductor/Rakefile

Revision 1051, 6.2 kB (checked in by bitsweat, 3 years ago)

Skip rake tasks which operate on db if none is configured.

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