Mercurial > hg > soundsoftware-site
comparison vendor/plugins/engines/lib/tasks/engines.rake @ 514:7eba09d624db live
Merge
author | Chris Cannam |
---|---|
date | Thu, 14 Jul 2011 10:50:53 +0100 |
parents | cbce1fd3b1b7 |
children |
comparison
equal
deleted
inserted
replaced
512:b9aebdd7dd40 | 514:7eba09d624db |
---|---|
1 # This code lets us redefine existing Rake tasks, which is extremely | |
2 # handy for modifying existing Rails rake tasks. | |
3 # Credit for the original snippet of code goes to Jeremy Kemper | |
4 # http://pastie.caboo.se/9620 | |
5 unless Rake::TaskManager.methods.include?('redefine_task') | |
6 module Rake | |
7 module TaskManager | |
8 def redefine_task(task_class, args, &block) | |
9 task_name, arg_names, deps = resolve_args([args]) | |
10 task_name = task_class.scope_name(@scope, task_name) | |
11 deps = [deps] unless deps.respond_to?(:to_ary) | |
12 deps = deps.collect {|d| d.to_s } | |
13 task = @tasks[task_name.to_s] = task_class.new(task_name, self) | |
14 task.application = self | |
15 task.add_description(@last_description) | |
16 @last_description = nil | |
17 task.enhance(deps, &block) | |
18 task | |
19 end | |
20 | |
21 end | |
22 class Task | |
23 class << self | |
24 def redefine_task(args, &block) | |
25 Rake.application.redefine_task(self, [args], &block) | |
26 end | |
27 end | |
28 end | |
29 end | |
30 end | |
31 | |
32 namespace :db do | |
33 namespace :migrate do | |
34 desc 'Migrate database and plugins to current status.' | |
35 task :all => [ 'db:migrate', 'db:migrate:plugins' ] | |
36 | |
37 desc 'Migrate plugins to current status.' | |
38 task :plugins => :environment do | |
39 Engines.plugins.each do |plugin| | |
40 next unless plugin.respond_to?(:migration_directory) | |
41 next unless File.exists? plugin.migration_directory | |
42 puts "Migrating plugin #{plugin.name} ..." | |
43 plugin.migrate | |
44 end | |
45 end | |
46 | |
47 desc 'For engines coming from Rails version < 2.0 or for those previously updated to work with Sven Fuch\'s fork of engines, you need to upgrade the schema info table' | |
48 task :upgrade_plugin_migrations => :environment do | |
49 svens_fork_table_name = 'plugin_schema_migrations' | |
50 | |
51 # Check if app was previously using Sven's fork | |
52 if ActiveRecord::Base.connection.table_exists?(svens_fork_table_name) | |
53 old_sm_table = svens_fork_table_name | |
54 else | |
55 old_sm_table = ActiveRecord::Migrator.proper_table_name(Engines.schema_info_table) | |
56 end | |
57 | |
58 unless ActiveRecord::Base.connection.table_exists?(old_sm_table) | |
59 abort "Cannot find old migration table - assuming nothing needs to be done" | |
60 end | |
61 | |
62 # There are two forms of the engines schema info - pre-fix_plugin_migrations and post | |
63 # We need to figure this out before we continue. | |
64 | |
65 results = ActiveRecord::Base.connection.select_rows( | |
66 "SELECT version, plugin_name FROM #{old_sm_table}" | |
67 ).uniq | |
68 | |
69 def insert_new_version(plugin_name, version) | |
70 version_string = "#{version}-#{plugin_name}" | |
71 new_sm_table = ActiveRecord::Migrator.schema_migrations_table_name | |
72 | |
73 # Check if the row already exists for some reason - maybe run this task more than once. | |
74 return if ActiveRecord::Base.connection.select_rows("SELECT * FROM #{new_sm_table} WHERE version = #{version_string.dump.gsub("\"", "'")}").size > 0 | |
75 | |
76 puts "Inserting new version #{version} for plugin #{plugin_name}.." | |
77 ActiveRecord::Base.connection.insert("INSERT INTO #{new_sm_table} (version) VALUES (#{version_string.dump.gsub("\"", "'")})") | |
78 end | |
79 | |
80 # We need to figure out if they already used "fix_plugin_migrations" | |
81 versions = {} | |
82 results.each do |r| | |
83 versions[r[1]] ||= [] | |
84 versions[r[1]] << r[0].to_i | |
85 end | |
86 | |
87 if versions.values.find{ |v| v.size > 1 } == nil | |
88 puts "Fixing migration info" | |
89 # We only have one listed migration per plugin - this is pre-fix_plugin_migrations, | |
90 # so we build all versions required. In this case, all migrations should | |
91 versions.each do |plugin_name, version| | |
92 version = version[0] # There is only one version | |
93 | |
94 # We have to make an assumption that numeric migrations won't get this long.. | |
95 # I'm not sure if there is a better assumption, it should work in all | |
96 # current cases.. (touch wood..) | |
97 if version.to_s.size < "YYYYMMDDHHMMSS".size | |
98 # Insert version records for each migration | |
99 (1..version).each do |v| | |
100 insert_new_version(plugin_name, v) | |
101 end | |
102 else | |
103 # If the plugin is new-format "YYYYMMDDHHMMSS", we just copy it across... | |
104 # The case in which this occurs is very rare.. | |
105 insert_new_version(plugin_name, version) | |
106 end | |
107 end | |
108 else | |
109 puts "Moving migration info" | |
110 # We have multiple migrations listed per plugin - thus we can assume they have | |
111 # already applied fix_plugin_migrations - we just copy it across verbatim | |
112 versions.each do |plugin_name, version| | |
113 version.each { |v| insert_new_version(plugin_name, v) } | |
114 end | |
115 end | |
116 | |
117 puts "Migration info successfully migrated - removing old schema info table" | |
118 ActiveRecord::Base.connection.drop_table(old_sm_table) | |
119 end | |
120 | |
121 desc 'Migrate a specified plugin.' | |
122 task(:plugin => :environment) do | |
123 name = ENV['NAME'] | |
124 if plugin = Engines.plugins[name] | |
125 version = ENV['VERSION'] | |
126 puts "Migrating #{plugin.name} to " + (version ? "version #{version}" : 'latest version') + " ..." | |
127 plugin.migrate(version ? version.to_i : nil) | |
128 else | |
129 puts "Plugin #{name} does not exist." | |
130 end | |
131 end | |
132 end | |
133 end | |
134 | |
135 | |
136 namespace :db do | |
137 namespace :fixtures do | |
138 namespace :plugins do | |
139 | |
140 desc "Load plugin fixtures into the current environment's database." | |
141 task :load => :environment do | |
142 require 'active_record/fixtures' | |
143 ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym) | |
144 Dir.glob(File.join(RAILS_ROOT, 'vendor', 'plugins', ENV['PLUGIN'] || '**', | |
145 'test', 'fixtures', '*.yml')).each do |fixture_file| | |
146 Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*')) | |
147 end | |
148 end | |
149 | |
150 end | |
151 end | |
152 end | |
153 | |
154 # this is just a modification of the original task in railties/lib/tasks/documentation.rake, | |
155 # because the default task doesn't support subdirectories like <plugin>/app or | |
156 # <plugin>/component. These tasks now include every file under a plugin's load paths (see | |
157 # Plugin#load_paths). | |
158 namespace :doc do | |
159 | |
160 plugins = FileList['vendor/plugins/**'].collect { |plugin| File.basename(plugin) } | |
161 | |
162 namespace :plugins do | |
163 | |
164 # Define doc tasks for each plugin | |
165 plugins.each do |plugin| | |
166 desc "Create plugin documentation for '#{plugin}'" | |
167 Rake::Task.redefine_task(plugin => :environment) do | |
168 plugin_base = RAILS_ROOT + "/vendor/plugins/#{plugin}" | |
169 options = [] | |
170 files = Rake::FileList.new | |
171 options << "-o doc/plugins/#{plugin}" | |
172 options << "--title '#{plugin.titlecase} Plugin Documentation'" | |
173 options << '--line-numbers' << '--inline-source' | |
174 options << '-T html' | |
175 | |
176 # Include every file in the plugin's load_paths (see Plugin#load_paths) | |
177 if Engines.plugins[plugin] | |
178 files.include("#{plugin_base}/{#{Engines.plugins[plugin].load_paths.join(",")}}/**/*.rb") | |
179 end | |
180 if File.exists?("#{plugin_base}/README") | |
181 files.include("#{plugin_base}/README") | |
182 options << "--main '#{plugin_base}/README'" | |
183 end | |
184 files.include("#{plugin_base}/CHANGELOG") if File.exists?("#{plugin_base}/CHANGELOG") | |
185 | |
186 if files.empty? | |
187 puts "No source files found in #{plugin_base}. No documentation will be generated." | |
188 else | |
189 options << files.to_s | |
190 sh %(rdoc #{options * ' '}) | |
191 end | |
192 end | |
193 end | |
194 end | |
195 end | |
196 | |
197 | |
198 | |
199 namespace :test do | |
200 task :warn_about_multiple_plugin_testing_with_engines do | |
201 puts %{-~============== A Moste Polite Warninge ===========================~- | |
202 | |
203 You may experience issues testing multiple plugins at once when using | |
204 the code-mixing features that the engines plugin provides. If you do | |
205 experience any problems, please test plugins individually, i.e. | |
206 | |
207 $ rake test:plugins PLUGIN=my_plugin | |
208 | |
209 or use the per-type plugin test tasks: | |
210 | |
211 $ rake test:plugins:units | |
212 $ rake test:plugins:functionals | |
213 $ rake test:plugins:integration | |
214 $ rake test:plugins:all | |
215 | |
216 Report any issues on http://dev.rails-engines.org. Thanks! | |
217 | |
218 -~===============( ... as you were ... )============================~-} | |
219 end | |
220 | |
221 namespace :engines do | |
222 | |
223 def engine_plugins | |
224 Dir["vendor/plugins/*"].select { |f| File.directory?(File.join(f, "app")) }.map { |f| File.basename(f) }.join(",") | |
225 end | |
226 | |
227 desc "Run tests from within engines plugins (plugins with an 'app' directory)" | |
228 task :all => [:units, :functionals, :integration] | |
229 | |
230 desc "Run unit tests from within engines plugins (plugins with an 'app' directory)" | |
231 Rake::TestTask.new(:units => "test:plugins:setup_plugin_fixtures") do |t| | |
232 t.pattern = "vendor/plugins/{#{ENV['PLUGIN'] || engine_plugins}}/test/unit/**/*_test.rb" | |
233 t.verbose = true | |
234 end | |
235 | |
236 desc "Run functional tests from within engines plugins (plugins with an 'app' directory)" | |
237 Rake::TestTask.new(:functionals => "test:plugins:setup_plugin_fixtures") do |t| | |
238 t.pattern = "vendor/plugins/{#{ENV['PLUGIN'] || engine_plugins}}/test/functional/**/*_test.rb" | |
239 t.verbose = true | |
240 end | |
241 | |
242 desc "Run integration tests from within engines plugins (plugins with an 'app' directory)" | |
243 Rake::TestTask.new(:integration => "test:plugins:setup_plugin_fixtures") do |t| | |
244 t.pattern = "vendor/plugins/{#{ENV['PLUGIN'] || engine_plugins}}/test/integration/**/*_test.rb" | |
245 t.verbose = true | |
246 end | |
247 end | |
248 | |
249 namespace :plugins do | |
250 | |
251 desc "Run the plugin tests in vendor/plugins/**/test (or specify with PLUGIN=name)" | |
252 task :all => [:warn_about_multiple_plugin_testing_with_engines, | |
253 :units, :functionals, :integration] | |
254 | |
255 desc "Run all plugin unit tests" | |
256 Rake::TestTask.new(:units => :setup_plugin_fixtures) do |t| | |
257 t.pattern = "vendor/plugins/#{ENV['PLUGIN'] || "**"}/test/unit/**/*_test.rb" | |
258 t.verbose = true | |
259 end | |
260 | |
261 desc "Run all plugin functional tests" | |
262 Rake::TestTask.new(:functionals => :setup_plugin_fixtures) do |t| | |
263 t.pattern = "vendor/plugins/#{ENV['PLUGIN'] || "**"}/test/functional/**/*_test.rb" | |
264 t.verbose = true | |
265 end | |
266 | |
267 desc "Integration test engines" | |
268 Rake::TestTask.new(:integration => :setup_plugin_fixtures) do |t| | |
269 t.pattern = "vendor/plugins/#{ENV['PLUGIN'] || "**"}/test/integration/**/*_test.rb" | |
270 t.verbose = true | |
271 end | |
272 | |
273 desc "Mirrors plugin fixtures into a single location to help plugin tests" | |
274 task :setup_plugin_fixtures => :environment do | |
275 Engines::Testing.setup_plugin_fixtures | |
276 end | |
277 | |
278 # Patch the default plugin testing task to have setup_plugin_fixtures as a prerequisite | |
279 Rake::Task["test:plugins"].prerequisites << "test:plugins:setup_plugin_fixtures" | |
280 end | |
281 end |