aboutsummaryrefslogtreecommitdiffstats
path: root/railties/fresh_rakefile
blob: 23f120ff499bc2691c1eda4150fb8d15eabdbc87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

$VERBOSE = nil
TEST_CHANGES_SINCE = Time.now - 600

desc "Run all the tests on a fresh test database"
task :default => [ :test_units, :test_functional ]


desc 'Require application environment.'
task :environment do
  unless defined? RAILS_ROOT
    require File.dirname(__FILE__) + '/config/environment'
  end
end

desc "Generate API documentation, show coding stats"
task :doc => [ :appdoc, :stats ]


# 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

def with_database(environment = RAILS_ENV)
  config = ActiveRecord::Base.configurations[environment]
  case config['adapter']
    when /mysql/
      ActiveRecord::Base.establish_connection(config)
      ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
      db = :mysql
    when /postgresql/
      ENV['PGHOST']     = config['host'].to_s     if config['host']
      ENV['PGPORT']     = config['port'].to_s     if config['port']
      ENV['PGPASSWORD'] = config['password'].to_s if config['password']
      db = :postgresql
    when /sqlite/
      db = :sqlite
    else 
      raise "Unknown database adapter '#{config['adapter']}'"
  end
  schema = "db/schema-#{db}.sql"
  yield(config, schema)[db].call
end

task :test_environment do
  ENV['RAILS_ENV'] = 'test'
end

desc 'Test recent changes.'
Rake::TestTask.new(:recent) do |t|
  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
task :test_recent => [:test_environment, :create_db]

desc "Run the unit tests in test/unit"
Rake::TestTask.new(:test_units) { |t|
  t.libs << "test"
  t.pattern = 'test/unit/**/*_test.rb'
  t.verbose = true
}
task :test_units => [:test_environment, :create_db]

desc "Run the functional tests in test/functional"
Rake::TestTask.new(:test_functional) { |t|
  t.libs << "test"
  t.pattern = 'test/functional/**/*_test.rb'
  t.verbose = true
}
task :test_functional => [:test_environment, :create_db]

desc "Generate documentation for the application"
Rake::RDocTask.new(:appdoc) { |rdoc|
  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"
Rake::RDocTask.new(:apidoc) { |rdoc|
  rdoc.rdoc_dir = 'doc/api'
  rdoc.template = "#{ENV['template']}.rb" if ENV['template']
  rdoc.title    = "Rails Framework Documentation"
  rdoc.options << '--line-numbers --inline-source'
  rdoc.rdoc_files.include('README')
  rdoc.rdoc_files.include('CHANGELOG')
  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')
  rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
  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')
  rdoc.rdoc_files.include('vendor/rails/activesupport/README')
  rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
  rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
}

desc "Report code statistics (KLOCs, etc) from the application"
task :stats => [ :environment ] do
  require 'code_statistics'
  CodeStatistics.new(
    ["Helpers", "app/helpers"], 
    ["Controllers", "app/controllers"], 
    ["APIs", "app/apis"],
    ["Components", "components"],
    ["Functionals", "test/functional"],
    ["Models", "app/models"],
    ["Units", "test/unit"]
  ).to_s
end

desc "Create the database for the current environment from the schema SQL"
task :create_db => :destroy_db do
  with_database do |config, schema|
    {:mysql => lambda do
      IO.read(schema).split("\n\n").each do |table|
        ActiveRecord::Base.connection.execute(table)
      end
    end,
    
    :postgresql => lambda do
      `createdb -T template0 -U "#{config['username']}" #{config['database']}`
      `psql -U "#{config['username']}" -f #{schema} #{config['database']}`
    end,
    
    :sqlite => lambda do
      `#{config['adapter']} #{config['dbfile']} < #{schema}`
    end}
  end
end

desc "Destroy the database for the current environment"
task :destroy_db => :environment do
  with_database do |config, schema|
    {:mysql => lambda do
      ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{config['database']}")
    end,
    
    :postgresql => lambda do
      `dropdb -U "#{config['username']}" #{config['database']}`      
    end,
    
    :sqlite => lambda do
      rm_f config['dbfile']
    end}
  end
end

desc "Extract the current environment's database structure into schema SQL"
task :extract_db_structure => :environment do
  with_database do |config, schema|
    {:mysql => lambda do
      File.open(schema, 'w+') do |sql|
        sql << ActiveRecord::Base.connection.structure_dump
      end
    end,
    
    :postgresql => lambda do
      `pg_dump -U "#{config['username']}" -s -x -O -f #{schema} #{config['database']}`
    end,
    
    :sqlite => lambda do
      `#{config['adapter']} #{config['dbfile']} .schema > #{schema}`
    end}
  end
end

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
end

desc "Migrate the database according to the migrate scripts in db/migrate"
task :migrate => :environment do
  ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/db/migrate/')
end