2004-11-24 01:04:44 +00:00
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
$VERBOSE = nil
2005-02-17 20:12:27 +00:00
TEST_CHANGES_SINCE = Time.now - 600
2004-11-24 01:04:44 +00:00
desc "Run all the tests on a fresh test database"
2005-02-07 14:08:38 +00:00
task :default => [ :test_units, :test_functional ]
2004-11-24 01:04:44 +00:00
2005-02-17 20:12:27 +00:00
desc 'Require application environment.'
task :environment do
unless defined? RAILS_ROOT
require File.dirname(__FILE__) + '/config/environment'
end
end
2005-06-16 06:12:15 +00:00
desc "Generate API documentation, show coding stats"
2004-11-24 01:04:44 +00:00
task :doc => [ :appdoc, :stats ]
2005-02-17 20:12:27 +00:00
# Look up tests for recently modified sources.
def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
FileList[source_pattern].map do |path|
if File.mtime(path) > touched_since
test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
test if File.exists?(test)
end
end.compact
end
desc 'Test recent changes.'
2005-07-05 09:15:21 +00:00
Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
2005-02-17 20:12:27 +00:00
since = TEST_CHANGES_SINCE
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
recent_tests('app/models/*.rb', 'test/unit', since) +
recent_tests('app/controllers/*.rb', 'test/functional', since)
t.libs << 'test'
t.verbose = true
t.test_files = touched.uniq
end
2005-07-05 09:15:21 +00:00
task :test_recent => [ :clone_structure_to_test ]
2005-02-17 20:12:27 +00:00
2004-11-24 01:04:44 +00:00
desc "Run the unit tests in test/unit"
2005-07-05 09:15:21 +00:00
Rake::TestTask.new("test_units") { |t|
2004-11-24 01:04:44 +00:00
t.libs << "test"
2005-02-07 13:14:05 +00:00
t.pattern = 'test/unit/**/*_test.rb'
2004-11-24 01:04:44 +00:00
t.verbose = true
}
2005-07-05 09:15:21 +00:00
task :test_units => [ :clone_structure_to_test ]
2004-11-24 01:04:44 +00:00
desc "Run the functional tests in test/functional"
2005-07-05 09:15:21 +00:00
Rake::TestTask.new("test_functional") { |t|
2004-11-24 01:04:44 +00:00
t.libs << "test"
2005-02-07 13:14:05 +00:00
t.pattern = 'test/functional/**/*_test.rb'
2004-11-24 01:04:44 +00:00
t.verbose = true
}
2005-07-05 09:15:21 +00:00
task :test_functional => [ :clone_structure_to_test ]
2004-11-24 01:04:44 +00:00
desc "Generate documentation for the application"
2005-07-05 09:15:21 +00:00
Rake::RDocTask.new("appdoc") { |rdoc|
2004-11-24 01:04:44 +00:00
rdoc.rdoc_dir = 'doc/app'
rdoc.title = "Rails Application Documentation"
rdoc.options << '--line-numbers --inline-source'
rdoc.rdoc_files.include('doc/README_FOR_APP')
rdoc.rdoc_files.include('app/**/*.rb')
}
desc "Generate documentation for the Rails framework"
2005-07-05 09:15:21 +00:00
Rake::RDocTask.new("apidoc") { |rdoc|
2004-11-24 01:04:44 +00:00
rdoc.rdoc_dir = 'doc/api'
2005-03-27 14:10:42 +00:00
rdoc.template = "#{ENV['template']}.rb" if ENV['template']
2004-11-24 01:04:44 +00:00
rdoc.title = "Rails Framework Documentation"
rdoc.options << '--line-numbers --inline-source'
rdoc.rdoc_files.include('README')
2004-12-17 00:42:53 +00:00
rdoc.rdoc_files.include('CHANGELOG')
2005-03-21 00:16:14 +00:00
rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG')
rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE')
rdoc.rdoc_files.include('vendor/rails/activerecord/README')
rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG')
rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb')
rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*')
rdoc.rdoc_files.include('vendor/rails/actionpack/README')
rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG')
rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionmailer/README')
rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG')
rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/README')
2005-03-27 13:33:54 +00:00
rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
2005-03-22 13:36:04 +00:00
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/api/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/client/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/container/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/dispatcher/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/protocol/*.rb')
rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/support/*.rb')
2005-03-21 00:16:14 +00:00
rdoc.rdoc_files.include('vendor/rails/activesupport/README')
2005-03-27 13:33:54 +00:00
rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
2005-03-21 00:16:14 +00:00
rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
2004-11-24 01:04:44 +00:00
}
desc "Report code statistics (KLOCs, etc) from the application"
2005-03-06 14:14:47 +00:00
task :stats => [ :environment ] do
2005-02-17 20:12:27 +00:00
require 'code_statistics'
2004-11-24 01:04:44 +00:00
CodeStatistics.new(
["Helpers", "app/helpers"],
2004-12-22 13:08:26 +00:00
["Controllers", "app/controllers"],
2005-03-06 12:32:02 +00:00
["APIs", "app/apis"],
["Components", "components"],
2004-12-22 13:08:26 +00:00
["Functionals", "test/functional"],
2004-11-24 01:04:44 +00:00
["Models", "app/models"],
2004-12-22 13:08:26 +00:00
["Units", "test/unit"]
2004-11-24 01:04:44 +00:00
).to_s
end
2005-07-05 09:15:21 +00:00
desc "Recreate the test databases from the development structure"
task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
abcs = ActiveRecord::Base.configurations
case abcs["test"]["adapter"]
when "mysql"
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
2005-01-24 13:12:40 +00:00
ActiveRecord::Base.connection.execute(table)
end
2005-07-05 09:15:21 +00:00
when "postgresql"
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
`psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
when "sqlite", "sqlite3"
`#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
2005-07-09 17:05:01 +00:00
when "sqlserver"
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
2005-07-24 14:05:09 +00:00
when "oci",
ActiveRecord::Base.establish_connection(:test)
IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
2005-07-05 09:15:21 +00:00
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
2004-11-24 01:04:44 +00:00
end
end
2005-07-05 09:15:21 +00:00
desc "Dump the database structure to a SQL file"
task :db_structure_dump => :environment do
abcs = ActiveRecord::Base.configurations
case abcs[RAILS_ENV]["adapter"]
2005-07-24 14:05:09 +00:00
when "mysql", "oci"
2005-07-05 09:15:21 +00:00
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
when "postgresql"
ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
`pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
when "sqlite", "sqlite3"
`#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
2005-07-09 17:05:01 +00:00
when "sqlserver"
`scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
`scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
2005-07-05 09:15:21 +00:00
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
2004-11-24 01:04:44 +00:00
end
end
2005-07-05 09:15:21 +00:00
desc "Empty the test database"
task :purge_test_database => :environment do
abcs = ActiveRecord::Base.configurations
case abcs["test"]["adapter"]
when "mysql"
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
when "postgresql"
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
`dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
`createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
when "sqlite","sqlite3"
File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
2005-07-09 17:05:01 +00:00
when "sqlserver"
dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
2005-07-24 14:05:09 +00:00
when "oci"
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
else
2005-07-05 09:15:21 +00:00
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
2004-11-24 01:04:44 +00:00
end
2004-12-22 13:08:26 +00:00
end
2005-04-13 05:32:21 +00:00
desc "Clears all *.log files in log/"
task :clear_logs => :environment do
FileList["log/*.log"].each do |log_file|
f = File.open(log_file, "w")
f.close
end
2005-06-25 11:19:47 +00:00
end
2005-07-04 09:22:12 +00:00
2005-07-09 15:46:29 +00:00
desc "Migrate the database according to the migrate scripts in db/migrate (only supported on PG/MySQL). A specific version can be targetted with VERSION=x"
2005-07-04 09:22:12 +00:00
task :migrate => :environment do
2005-07-09 16:59:35 +00:00
ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/db/migrate/', ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
2005-07-22 06:43:03 +00:00
end
desc "Load fixtures into the current environment's database"
task :load_fixtures => :environment do
require 'active_record/fixtures'
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |fixture_file|
Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))
end
end
2005-08-03 15:02:31 +00:00
desc "Push the latest revision into production using the release manager"
task :deploy do
system "script/switchtower -vvvv -r config/deploy -a deploy"
end
desc "Rollback to the release before the current release in production"
task :rollback do
system "script/switchtower -vvvv -r config/deploy -a rollback"
end
desc "Enumerate all available deployment tasks"
task :show_deploy_tasks do
system "script/switchtower -r config/deploy -a show_tasks"
end
desc "Execute a specific action using the release manager"
task :remote_exec do
unless ENV['ACTION']
raise "Please specify an action (or comma separated list of actions) via the ACTION environment variable"
end
actions = ENV['ACTION'].split(",").map { |a| "-a #{a}" }.join(" ")
system "script/switchtower -vvvv -r config/deploy #{actions}"
end