##// END OF EJS Templates
Let redmine:plugins:assets mirror a single plugin assets with name=....
Jean-Philippe Lang -
r9416:b097a1753e67
parent child
Show More
@@ -1,440 +1,444
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine #:nodoc:
19 19
20 20 class PluginNotFound < StandardError; end
21 21 class PluginRequirementError < StandardError; end
22 22
23 23 # Base class for Redmine plugins.
24 24 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
25 25 #
26 26 # Redmine::Plugin.register :example do
27 27 # name 'Example plugin'
28 28 # author 'John Smith'
29 29 # description 'This is an example plugin for Redmine'
30 30 # version '0.0.1'
31 31 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
32 32 # end
33 33 #
34 34 # === Plugin attributes
35 35 #
36 36 # +settings+ is an optional attribute that let the plugin be configurable.
37 37 # It must be a hash with the following keys:
38 38 # * <tt>:default</tt>: default value for the plugin settings
39 39 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
40 40 # Example:
41 41 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
42 42 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
43 43 #
44 44 # When rendered, the plugin settings value is available as the local variable +settings+
45 45 class Plugin
46 46 cattr_accessor :directory
47 47 self.directory = File.join(Rails.root, 'plugins')
48 48
49 49 cattr_accessor :public_directory
50 50 self.public_directory = File.join(Rails.root, 'public', 'plugin_assets')
51 51
52 52 @registered_plugins = {}
53 53 class << self
54 54 attr_reader :registered_plugins
55 55 private :new
56 56
57 57 def def_field(*names)
58 58 class_eval do
59 59 names.each do |name|
60 60 define_method(name) do |*args|
61 61 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
62 62 end
63 63 end
64 64 end
65 65 end
66 66 end
67 67 def_field :name, :description, :url, :author, :author_url, :version, :settings
68 68 attr_reader :id
69 69
70 70 # Plugin constructor
71 71 def self.register(id, &block)
72 72 p = new(id)
73 73 p.instance_eval(&block)
74 74 # Set a default name if it was not provided during registration
75 75 p.name(id.to_s.humanize) if p.name.nil?
76 76
77 77 # Adds plugin locales if any
78 78 # YAML translation files should be found under <plugin>/config/locales/
79 79 ::I18n.load_path += Dir.glob(File.join(p.directory, 'config', 'locales', '*.yml'))
80 80
81 81 # Prepends the app/views directory of the plugin to the view path
82 82 view_path = File.join(p.directory, 'app', 'views')
83 83 if File.directory?(view_path)
84 84 ActionController::Base.prepend_view_path(view_path)
85 85 end
86 86
87 87 # Adds the app/{controllers,helpers,models} directories of the plugin to the autoload path
88 88 Dir.glob File.expand_path(File.join(p.directory, 'app', '{controllers,helpers,models}')) do |dir|
89 89 ActiveSupport::Dependencies.autoload_paths += [dir]
90 90 end
91 91
92 92 registered_plugins[id] = p
93 93 end
94 94
95 95 # Returns an array of all registered plugins
96 96 def self.all
97 97 registered_plugins.values.sort
98 98 end
99 99
100 100 # Finds a plugin by its id
101 101 # Returns a PluginNotFound exception if the plugin doesn't exist
102 102 def self.find(id)
103 103 registered_plugins[id.to_sym] || raise(PluginNotFound)
104 104 end
105 105
106 106 # Clears the registered plugins hash
107 107 # It doesn't unload installed plugins
108 108 def self.clear
109 109 @registered_plugins = {}
110 110 end
111 111
112 112 # Checks if a plugin is installed
113 113 #
114 114 # @param [String] id name of the plugin
115 115 def self.installed?(id)
116 116 registered_plugins[id.to_sym].present?
117 117 end
118 118
119 119 def self.load
120 120 Dir.glob(File.join(self.directory, '*')).sort.each do |directory|
121 121 if File.directory?(directory)
122 122 lib = File.join(directory, "lib")
123 123 if File.directory?(lib)
124 124 $:.unshift lib
125 125 ActiveSupport::Dependencies.autoload_paths += [lib]
126 126 end
127 127 initializer = File.join(directory, "init.rb")
128 128 if File.file?(initializer)
129 129 require initializer
130 130 end
131 131 end
132 132 end
133 133 end
134 134
135 135 def initialize(id)
136 136 @id = id.to_sym
137 137 end
138 138
139 139 def directory
140 140 File.join(self.class.directory, id.to_s)
141 141 end
142 142
143 143 def public_directory
144 144 File.join(self.class.public_directory, id.to_s)
145 145 end
146 146
147 147 def assets_directory
148 148 File.join(directory, 'assets')
149 149 end
150 150
151 151 def <=>(plugin)
152 152 self.id.to_s <=> plugin.id.to_s
153 153 end
154 154
155 155 # Sets a requirement on Redmine version
156 156 # Raises a PluginRequirementError exception if the requirement is not met
157 157 #
158 158 # Examples
159 159 # # Requires Redmine 0.7.3 or higher
160 160 # requires_redmine :version_or_higher => '0.7.3'
161 161 # requires_redmine '0.7.3'
162 162 #
163 163 # # Requires a specific Redmine version
164 164 # requires_redmine :version => '0.7.3' # 0.7.3 only
165 165 # requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
166 166 def requires_redmine(arg)
167 167 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
168 168 arg.assert_valid_keys(:version, :version_or_higher)
169 169
170 170 current = Redmine::VERSION.to_a
171 171 arg.each do |k, v|
172 172 v = [] << v unless v.is_a?(Array)
173 173 versions = v.collect {|s| s.split('.').collect(&:to_i)}
174 174 case k
175 175 when :version_or_higher
176 176 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
177 177 unless (current <=> versions.first) >= 0
178 178 raise PluginRequirementError.new("#{id} plugin requires Redmine #{v} or higher but current is #{current.join('.')}")
179 179 end
180 180 when :version
181 181 unless versions.include?(current.slice(0,3))
182 182 raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{v.join(', ')} but current is #{current.join('.')}")
183 183 end
184 184 end
185 185 end
186 186 true
187 187 end
188 188
189 189 # Sets a requirement on a Redmine plugin version
190 190 # Raises a PluginRequirementError exception if the requirement is not met
191 191 #
192 192 # Examples
193 193 # # Requires a plugin named :foo version 0.7.3 or higher
194 194 # requires_redmine_plugin :foo, :version_or_higher => '0.7.3'
195 195 # requires_redmine_plugin :foo, '0.7.3'
196 196 #
197 197 # # Requires a specific version of a Redmine plugin
198 198 # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only
199 199 # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
200 200 def requires_redmine_plugin(plugin_name, arg)
201 201 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
202 202 arg.assert_valid_keys(:version, :version_or_higher)
203 203
204 204 plugin = Plugin.find(plugin_name)
205 205 current = plugin.version.split('.').collect(&:to_i)
206 206
207 207 arg.each do |k, v|
208 208 v = [] << v unless v.is_a?(Array)
209 209 versions = v.collect {|s| s.split('.').collect(&:to_i)}
210 210 case k
211 211 when :version_or_higher
212 212 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
213 213 unless (current <=> versions.first) >= 0
214 214 raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}")
215 215 end
216 216 when :version
217 217 unless versions.include?(current.slice(0,3))
218 218 raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}")
219 219 end
220 220 end
221 221 end
222 222 true
223 223 end
224 224
225 225 # Adds an item to the given +menu+.
226 226 # The +id+ parameter (equals to the project id) is automatically added to the url.
227 227 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
228 228 #
229 229 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
230 230 #
231 231 def menu(menu, item, url, options={})
232 232 Redmine::MenuManager.map(menu).push(item, url, options)
233 233 end
234 234 alias :add_menu_item :menu
235 235
236 236 # Removes +item+ from the given +menu+.
237 237 def delete_menu_item(menu, item)
238 238 Redmine::MenuManager.map(menu).delete(item)
239 239 end
240 240
241 241 # Defines a permission called +name+ for the given +actions+.
242 242 #
243 243 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
244 244 # permission :destroy_contacts, { :contacts => :destroy }
245 245 # permission :view_contacts, { :contacts => [:index, :show] }
246 246 #
247 247 # The +options+ argument can be used to make the permission public (implicitly given to any user)
248 248 # or to restrict users the permission can be given to.
249 249 #
250 250 # Examples
251 251 # # A permission that is implicitly given to any user
252 252 # # This permission won't appear on the Roles & Permissions setup screen
253 253 # permission :say_hello, { :example => :say_hello }, :public => true
254 254 #
255 255 # # A permission that can be given to any user
256 256 # permission :say_hello, { :example => :say_hello }
257 257 #
258 258 # # A permission that can be given to registered users only
259 259 # permission :say_hello, { :example => :say_hello }, :require => :loggedin
260 260 #
261 261 # # A permission that can be given to project members only
262 262 # permission :say_hello, { :example => :say_hello }, :require => :member
263 263 def permission(name, actions, options = {})
264 264 if @project_module
265 265 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
266 266 else
267 267 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
268 268 end
269 269 end
270 270
271 271 # Defines a project module, that can be enabled/disabled for each project.
272 272 # Permissions defined inside +block+ will be bind to the module.
273 273 #
274 274 # project_module :things do
275 275 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
276 276 # permission :destroy_contacts, { :contacts => :destroy }
277 277 # end
278 278 def project_module(name, &block)
279 279 @project_module = name
280 280 self.instance_eval(&block)
281 281 @project_module = nil
282 282 end
283 283
284 284 # Registers an activity provider.
285 285 #
286 286 # Options:
287 287 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
288 288 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
289 289 #
290 290 # A model can provide several activity event types.
291 291 #
292 292 # Examples:
293 293 # register :news
294 294 # register :scrums, :class_name => 'Meeting'
295 295 # register :issues, :class_name => ['Issue', 'Journal']
296 296 #
297 297 # Retrieving events:
298 298 # Associated model(s) must implement the find_events class method.
299 299 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
300 300 #
301 301 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
302 302 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
303 303 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
304 304 #
305 305 # Note that :view_scrums permission is required to view these events in the activity view.
306 306 def activity_provider(*args)
307 307 Redmine::Activity.register(*args)
308 308 end
309 309
310 310 # Registers a wiki formatter.
311 311 #
312 312 # Parameters:
313 313 # * +name+ - human-readable name
314 314 # * +formatter+ - formatter class, which should have an instance method +to_html+
315 315 # * +helper+ - helper module, which will be included by wiki pages
316 316 def wiki_format_provider(name, formatter, helper)
317 317 Redmine::WikiFormatting.register(name, formatter, helper)
318 318 end
319 319
320 320 # Returns +true+ if the plugin can be configured.
321 321 def configurable?
322 322 settings && settings.is_a?(Hash) && !settings[:partial].blank?
323 323 end
324 324
325 325 def mirror_assets
326 326 source = assets_directory
327 327 destination = public_directory
328 328 return unless File.directory?(source)
329 329
330 330 source_files = Dir[source + "/**/*"]
331 331 source_dirs = source_files.select { |d| File.directory?(d) }
332 332 source_files -= source_dirs
333 333
334 334 unless source_files.empty?
335 335 base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, ''))
336 336 FileUtils.mkdir_p(base_target_dir)
337 337 end
338 338
339 339 source_dirs.each do |dir|
340 340 # strip down these paths so we have simple, relative paths we can
341 341 # add to the destination
342 342 target_dir = File.join(destination, dir.gsub(source, ''))
343 343 begin
344 344 FileUtils.mkdir_p(target_dir)
345 345 rescue Exception => e
346 346 raise "Could not create directory #{target_dir}: \n" + e
347 347 end
348 348 end
349 349
350 350 source_files.each do |file|
351 351 begin
352 352 target = File.join(destination, file.gsub(source, ''))
353 353 unless File.exist?(target) && FileUtils.identical?(file, target)
354 354 FileUtils.cp(file, target)
355 355 end
356 356 rescue Exception => e
357 357 raise "Could not copy #{file} to #{target}: \n" + e
358 358 end
359 359 end
360 360 end
361 361
362 # Mirrors all plugins' assets to public/plugin_assets
363 def self.mirror_assets
364 all.each do |plugin|
365 plugin.mirror_assets
362 # Mirrors assets from one or all plugins to public/plugin_assets
363 def self.mirror_assets(name=nil)
364 if name.present?
365 find(name).mirror_assets
366 else
367 all.each do |plugin|
368 plugin.mirror_assets
369 end
366 370 end
367 371 end
368 372
369 373 # The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>)
370 374 def migration_directory
371 375 File.join(Rails.root, 'plugins', id.to_s, 'db', 'migrate')
372 376 end
373 377
374 378 # Returns the version number of the latest migration for this plugin. Returns
375 379 # nil if this plugin has no migrations.
376 380 def latest_migration
377 381 migrations.last
378 382 end
379 383
380 384 # Returns the version numbers of all migrations for this plugin.
381 385 def migrations
382 386 migrations = Dir[migration_directory+"/*.rb"]
383 387 migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
384 388 end
385 389
386 390 # Migrate this plugin to the given version
387 391 def migrate(version = nil)
388 392 puts "Migrating #{id} (#{name})..."
389 393 Redmine::Plugin::Migrator.migrate_plugin(self, version)
390 394 end
391 395
392 396 # Migrates all plugins or a single plugin to a given version
393 397 # Exemples:
394 398 # Plugin.migrate
395 399 # Plugin.migrate('sample_plugin')
396 400 # Plugin.migrate('sample_plugin', 1)
397 401 #
398 402 def self.migrate(name=nil, version=nil)
399 403 if name.present?
400 404 find(name).migrate(version)
401 405 else
402 406 all.each do |plugin|
403 407 plugin.migrate
404 408 end
405 409 end
406 410 end
407 411
408 412 class Migrator < ActiveRecord::Migrator
409 413 # We need to be able to set the 'current' plugin being migrated.
410 414 cattr_accessor :current_plugin
411 415
412 416 class << self
413 417 # Runs the migrations from a plugin, up (or down) to the version given
414 418 def migrate_plugin(plugin, version)
415 419 self.current_plugin = plugin
416 420 return if current_version(plugin) == version
417 421 migrate(plugin.migration_directory, version)
418 422 end
419 423
420 424 def current_version(plugin=current_plugin)
421 425 # Delete migrations that don't match .. to_i will work because the number comes first
422 426 ::ActiveRecord::Base.connection.select_values(
423 427 "SELECT version FROM #{schema_migrations_table_name}"
424 428 ).delete_if{ |v| v.match(/-#{plugin.id}/) == nil }.map(&:to_i).max || 0
425 429 end
426 430 end
427 431
428 432 def migrated
429 433 sm_table = self.class.schema_migrations_table_name
430 434 ::ActiveRecord::Base.connection.select_values(
431 435 "SELECT version FROM #{sm_table}"
432 436 ).delete_if{ |v| v.match(/-#{current_plugin.id}/) == nil }.map(&:to_i).sort
433 437 end
434 438
435 439 def record_version_state_after_migrating(version)
436 440 super(version.to_s + "-" + current_plugin.id.to_s)
437 441 end
438 442 end
439 443 end
440 444 end
@@ -1,80 +1,86
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 namespace :redmine do
19 19 namespace :attachments do
20 20 desc 'Removes uploaded files left unattached after one day.'
21 21 task :prune => :environment do
22 22 Attachment.prune
23 23 end
24 24 end
25 25
26 26 namespace :tokens do
27 27 desc 'Removes expired tokens.'
28 28 task :prune => :environment do
29 29 Token.destroy_expired
30 30 end
31 31 end
32 32
33 33 namespace :watchers do
34 34 desc 'Removes watchers from what they can no longer view.'
35 35 task :prune => :environment do
36 36 Watcher.prune
37 37 end
38 38 end
39 39
40 40 desc 'Fetch changesets from the repositories'
41 41 task :fetch_changesets => :environment do
42 42 Repository.fetch_changesets
43 43 end
44 44
45 45 desc 'Migrates and copies plugins assets.'
46 46 task :plugins do
47 47 Rake::Task["redmine:plugins:migrate"].invoke
48 48 Rake::Task["redmine:plugins:assets"].invoke
49 49 end
50 50
51 51 namespace :plugins do
52 52 desc 'Migrates installed plugins.'
53 53 task :migrate => :environment do
54 54 name = ENV['name']
55 55 version = nil
56 56 version_string = ENV['version']
57 57 if version_string
58 58 if version_string =~ /^\d+$/
59 59 version = version_string.to_i
60 60 if name.nil?
61 61 abort "The VERSION argument requires a plugin NAME."
62 62 end
63 63 else
64 64 abort "Invalid version #{version_string} given."
65 65 end
66 66 end
67 67
68 68 begin
69 69 Redmine::Plugin.migrate(name, version)
70 70 rescue Redmine::PluginNotFound
71 71 abort "Plugin #{name} was not found."
72 72 end
73 73 end
74 74
75 75 desc 'Copies plugins assets into the public directory.'
76 76 task :assets => :environment do
77 Redmine::Plugin.mirror_assets
77 name = ENV['name']
78
79 begin
80 Redmine::Plugin.mirror_assets(name)
81 rescue Redmine::PluginNotFound
82 abort "Plugin #{name} was not found."
83 end
78 84 end
79 85 end
80 86 end
General Comments 0
You need to be logged in to leave comments. Login now