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

root/tools/cia/trunk/Rakefile

Revision 1114, 7.5 kB (checked in by davidhh, 4 years ago)

First import

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.template = "#{ENV['template']}.rb" if ENV['template']
75   rdoc.title    = "Rails Framework Documentation"
76   rdoc.options << '--line-numbers --inline-source'
77   rdoc.rdoc_files.include('README')
78   rdoc.rdoc_files.include('CHANGELOG')
79   rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG')
80   rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE')
81   rdoc.rdoc_files.include('vendor/rails/activerecord/README')
82   rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG')
83   rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb')
84   rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*')
85   rdoc.rdoc_files.include('vendor/rails/actionpack/README')
86   rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG')
87   rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb')
88   rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb')
89   rdoc.rdoc_files.include('vendor/rails/actionmailer/README')
90   rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG')
91   rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb')
92   rdoc.rdoc_files.include('vendor/rails/actionwebservice/README')
93   rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
94   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service.rb')
95   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/*.rb')
96   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/api/*.rb')
97   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/client/*.rb')
98   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/container/*.rb')
99   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/dispatcher/*.rb')
100   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/protocol/*.rb')
101   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/support/*.rb')
102   rdoc.rdoc_files.include('vendor/rails/activesupport/README')
103   rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
104   rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
105 }
106
107 desc "Report code statistics (KLOCs, etc) from the application"
108 task :stats => [ :environment ] do
109   require 'code_statistics'
110   CodeStatistics.new(
111     ["Helpers", "app/helpers"],
112     ["Controllers", "app/controllers"],
113     ["APIs", "app/apis"],
114     ["Components", "components"],
115     ["Functionals", "test/functional"],
116     ["Models", "app/models"],
117     ["Units", "test/unit"]
118   ).to_s
119 end
120
121 desc "Recreate the test databases from the development structure"
122 task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
123   abcs = ActiveRecord::Base.configurations
124   case abcs["test"]["adapter"]
125     when  "mysql"
126       ActiveRecord::Base.establish_connection(:test)
127       ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
128       IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
129         ActiveRecord::Base.connection.execute(table)
130       end
131     when "postgresql"
132       ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"]
133       ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"]
134       ENV['PGPASSWORD'] = abcs["test"]["password"]
135       `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
136     when "sqlite", "sqlite3"
137       `#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
138     else
139       raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
140   end
141 end
142
143 desc "Dump the database structure to a SQL file"
144 task :db_structure_dump => :environment do
145   abcs = ActiveRecord::Base.configurations
146   case abcs[RAILS_ENV]["adapter"]
147     when "mysql"
148       ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
149       File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
150     when "postgresql"
151       ENV['PGHOST']     = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
152       ENV['PGPORT']     = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
153       ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"]
154       `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
155     when "sqlite", "sqlite3"
156       `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
157     else
158       raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
159   end
160 end
161
162 desc "Empty the test database"
163 task :purge_test_database => :environment do
164   abcs = ActiveRecord::Base.configurations
165   case abcs["test"]["adapter"]
166     when "mysql"
167       ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
168       ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
169     when "postgresql"
170       ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"]
171       ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"]
172       ENV['PGPASSWORD'] = abcs["test"]["password"]
173       `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
174       `createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
175     when "sqlite","sqlite3"
176       File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
177     else
178       raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
179   end
180 end
Note: See TracBrowser for help on using the browser.