annotate vendor/plugins/engines/tasks/engines.rake @ 904:0a8317a50fa0 redmine-1.1

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