##// END OF EJS Templates
Adds Plugin#requires_redmine method so that plugin compatibility can be checked against current Redmine version (#2162)....
Jean-Philippe Lang -
r2040:9882217847a3
parent child
Show More
@@ -1,197 +1,232
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine #:nodoc:
18 module Redmine #:nodoc:
19
19
20 class PluginNotFound < StandardError; end
20 class PluginNotFound < StandardError; end
21 class PluginRequirementError < StandardError; end
21
22
22 # Base class for Redmine plugins.
23 # Base class for Redmine plugins.
23 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
24 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
24 #
25 #
25 # Redmine::Plugin.register :example do
26 # Redmine::Plugin.register :example do
26 # name 'Example plugin'
27 # name 'Example plugin'
27 # author 'John Smith'
28 # author 'John Smith'
28 # description 'This is an example plugin for Redmine'
29 # description 'This is an example plugin for Redmine'
29 # version '0.0.1'
30 # version '0.0.1'
30 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
31 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
31 # end
32 # end
32 #
33 #
33 # === Plugin attributes
34 # === Plugin attributes
34 #
35 #
35 # +settings+ is an optional attribute that let the plugin be configurable.
36 # +settings+ is an optional attribute that let the plugin be configurable.
36 # It must be a hash with the following keys:
37 # It must be a hash with the following keys:
37 # * <tt>:default</tt>: default value for the plugin settings
38 # * <tt>:default</tt>: default value for the plugin settings
38 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
39 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
39 # Example:
40 # Example:
40 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
41 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
41 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
42 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
42 #
43 #
43 # When rendered, the plugin settings value is available as the local variable +settings+
44 # When rendered, the plugin settings value is available as the local variable +settings+
44 class Plugin
45 class Plugin
45 @registered_plugins = {}
46 @registered_plugins = {}
46 class << self
47 class << self
47 attr_reader :registered_plugins
48 attr_reader :registered_plugins
48 private :new
49 private :new
49
50
50 def def_field(*names)
51 def def_field(*names)
51 class_eval do
52 class_eval do
52 names.each do |name|
53 names.each do |name|
53 define_method(name) do |*args|
54 define_method(name) do |*args|
54 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
55 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
55 end
56 end
56 end
57 end
57 end
58 end
58 end
59 end
59 end
60 end
60 def_field :name, :description, :url, :author, :author_url, :version, :settings
61 def_field :name, :description, :url, :author, :author_url, :version, :settings
61 attr_reader :id
62 attr_reader :id
62
63
63 # Plugin constructor
64 # Plugin constructor
64 def self.register(id, &block)
65 def self.register(id, &block)
65 p = new(id)
66 p = new(id)
66 p.instance_eval(&block)
67 p.instance_eval(&block)
67 # Set a default name if it was not provided during registration
68 # Set a default name if it was not provided during registration
68 p.name(id.to_s.humanize) if p.name.nil?
69 p.name(id.to_s.humanize) if p.name.nil?
69 registered_plugins[id] = p
70 registered_plugins[id] = p
70 end
71 end
71
72
72 # Returns an array off all registered plugins
73 # Returns an array off all registered plugins
73 def self.all
74 def self.all
74 registered_plugins.values.sort
75 registered_plugins.values.sort
75 end
76 end
76
77
77 # Finds a plugin by its id
78 # Finds a plugin by its id
78 # Returns a PluginNotFound exception if the plugin doesn't exist
79 # Returns a PluginNotFound exception if the plugin doesn't exist
79 def self.find(id)
80 def self.find(id)
80 registered_plugins[id.to_sym] || raise(PluginNotFound)
81 registered_plugins[id.to_sym] || raise(PluginNotFound)
81 end
82 end
82
83
83 # Clears the registered plugins hash
84 # Clears the registered plugins hash
84 # It doesn't unload installed plugins
85 # It doesn't unload installed plugins
85 def self.clear
86 def self.clear
86 @registered_plugins = {}
87 @registered_plugins = {}
87 end
88 end
88
89
89 def initialize(id)
90 def initialize(id)
90 @id = id.to_sym
91 @id = id.to_sym
91 end
92 end
92
93
93 def <=>(plugin)
94 def <=>(plugin)
94 self.id.to_s <=> plugin.id.to_s
95 self.id.to_s <=> plugin.id.to_s
95 end
96 end
97
98 # Sets a requirement on Redmine version
99 # Raises a PluginRequirementError exception if the requirement is not met
100 #
101 # Examples
102 # # Requires Redmine 0.7.3 or higher
103 # requires_redmine :version_or_higher => '0.7.3'
104 # requires_redmine '0.7.3'
105 #
106 # # Requires a specific Redmine version
107 # requires_redmine :version => '0.7.3' # 0.7.3 only
108 # requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
109 def requires_redmine(arg)
110 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
111 arg.assert_valid_keys(:version, :version_or_higher)
112
113 current = Redmine::VERSION.to_a
114 arg.each do |k, v|
115 v = [] << v unless v.is_a?(Array)
116 versions = v.collect {|s| s.split('.').collect(&:to_i)}
117 case k
118 when :version_or_higher
119 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
120 unless (current <=> versions.first) >= 0
121 raise PluginRequirementError.new("#{id} plugin requires Redmine #{v} or higher but current is #{current.join('.')}")
122 end
123 when :version
124 unless versions.include?(current.slice(0,3))
125 raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{v.join(', ')} but current is #{current.join('.')}")
126 end
127 end
128 end
129 true
130 end
96
131
97 # Adds an item to the given +menu+.
132 # Adds an item to the given +menu+.
98 # The +id+ parameter (equals to the project id) is automatically added to the url.
133 # The +id+ parameter (equals to the project id) is automatically added to the url.
99 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
134 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
100 #
135 #
101 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
136 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
102 #
137 #
103 def menu(menu, item, url, options={})
138 def menu(menu, item, url, options={})
104 Redmine::MenuManager.map(menu).push(item, url, options)
139 Redmine::MenuManager.map(menu).push(item, url, options)
105 end
140 end
106 alias :add_menu_item :menu
141 alias :add_menu_item :menu
107
142
108 # Removes +item+ from the given +menu+.
143 # Removes +item+ from the given +menu+.
109 def delete_menu_item(menu, item)
144 def delete_menu_item(menu, item)
110 Redmine::MenuManager.map(menu).delete(item)
145 Redmine::MenuManager.map(menu).delete(item)
111 end
146 end
112
147
113 # Defines a permission called +name+ for the given +actions+.
148 # Defines a permission called +name+ for the given +actions+.
114 #
149 #
115 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
150 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
116 # permission :destroy_contacts, { :contacts => :destroy }
151 # permission :destroy_contacts, { :contacts => :destroy }
117 # permission :view_contacts, { :contacts => [:index, :show] }
152 # permission :view_contacts, { :contacts => [:index, :show] }
118 #
153 #
119 # The +options+ argument can be used to make the permission public (implicitly given to any user)
154 # The +options+ argument can be used to make the permission public (implicitly given to any user)
120 # or to restrict users the permission can be given to.
155 # or to restrict users the permission can be given to.
121 #
156 #
122 # Examples
157 # Examples
123 # # A permission that is implicitly given to any user
158 # # A permission that is implicitly given to any user
124 # # This permission won't appear on the Roles & Permissions setup screen
159 # # This permission won't appear on the Roles & Permissions setup screen
125 # permission :say_hello, { :example => :say_hello }, :public => true
160 # permission :say_hello, { :example => :say_hello }, :public => true
126 #
161 #
127 # # A permission that can be given to any user
162 # # A permission that can be given to any user
128 # permission :say_hello, { :example => :say_hello }
163 # permission :say_hello, { :example => :say_hello }
129 #
164 #
130 # # A permission that can be given to registered users only
165 # # A permission that can be given to registered users only
131 # permission :say_hello, { :example => :say_hello }, :require => loggedin
166 # permission :say_hello, { :example => :say_hello }, :require => loggedin
132 #
167 #
133 # # A permission that can be given to project members only
168 # # A permission that can be given to project members only
134 # permission :say_hello, { :example => :say_hello }, :require => member
169 # permission :say_hello, { :example => :say_hello }, :require => member
135 def permission(name, actions, options = {})
170 def permission(name, actions, options = {})
136 if @project_module
171 if @project_module
137 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
172 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
138 else
173 else
139 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
174 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
140 end
175 end
141 end
176 end
142
177
143 # Defines a project module, that can be enabled/disabled for each project.
178 # Defines a project module, that can be enabled/disabled for each project.
144 # Permissions defined inside +block+ will be bind to the module.
179 # Permissions defined inside +block+ will be bind to the module.
145 #
180 #
146 # project_module :things do
181 # project_module :things do
147 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
182 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
148 # permission :destroy_contacts, { :contacts => :destroy }
183 # permission :destroy_contacts, { :contacts => :destroy }
149 # end
184 # end
150 def project_module(name, &block)
185 def project_module(name, &block)
151 @project_module = name
186 @project_module = name
152 self.instance_eval(&block)
187 self.instance_eval(&block)
153 @project_module = nil
188 @project_module = nil
154 end
189 end
155
190
156 # Registers an activity provider.
191 # Registers an activity provider.
157 #
192 #
158 # Options:
193 # Options:
159 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
194 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
160 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
195 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
161 #
196 #
162 # A model can provide several activity event types.
197 # A model can provide several activity event types.
163 #
198 #
164 # Examples:
199 # Examples:
165 # register :news
200 # register :news
166 # register :scrums, :class_name => 'Meeting'
201 # register :scrums, :class_name => 'Meeting'
167 # register :issues, :class_name => ['Issue', 'Journal']
202 # register :issues, :class_name => ['Issue', 'Journal']
168 #
203 #
169 # Retrieving events:
204 # Retrieving events:
170 # Associated model(s) must implement the find_events class method.
205 # Associated model(s) must implement the find_events class method.
171 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
206 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
172 #
207 #
173 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
208 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
174 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
209 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
175 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
210 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
176 #
211 #
177 # Note that :view_scrums permission is required to view these events in the activity view.
212 # Note that :view_scrums permission is required to view these events in the activity view.
178 def activity_provider(*args)
213 def activity_provider(*args)
179 Redmine::Activity.register(*args)
214 Redmine::Activity.register(*args)
180 end
215 end
181
216
182 # Registers a wiki formatter.
217 # Registers a wiki formatter.
183 #
218 #
184 # Parameters:
219 # Parameters:
185 # * +name+ - human-readable name
220 # * +name+ - human-readable name
186 # * +formatter+ - formatter class, which should have an instance method +to_html+
221 # * +formatter+ - formatter class, which should have an instance method +to_html+
187 # * +helper+ - helper module, which will be included by wiki pages
222 # * +helper+ - helper module, which will be included by wiki pages
188 def wiki_format_provider(name, formatter, helper)
223 def wiki_format_provider(name, formatter, helper)
189 Redmine::WikiFormatting.register(name, formatter, helper)
224 Redmine::WikiFormatting.register(name, formatter, helper)
190 end
225 end
191
226
192 # Returns +true+ if the plugin can be configured.
227 # Returns +true+ if the plugin can be configured.
193 def configurable?
228 def configurable?
194 settings && settings.is_a?(Hash) && !settings[:partial].blank?
229 settings && settings.is_a?(Hash) && !settings[:partial].blank?
195 end
230 end
196 end
231 end
197 end
232 end
@@ -1,41 +1,43
1 require 'rexml/document'
1 require 'rexml/document'
2
2
3 module Redmine
3 module Redmine
4 module VERSION #:nodoc:
4 module VERSION #:nodoc:
5 MAJOR = 0
5 MAJOR = 0
6 MINOR = 7
6 MINOR = 7
7 TINY = 3
7 TINY = 3
8
8
9 # Branch values:
9 # Branch values:
10 # * official release: nil
10 # * official release: nil
11 # * stable branch: stable
11 # * stable branch: stable
12 # * trunk: devel
12 # * trunk: devel
13 BRANCH = 'devel'
13 BRANCH = 'devel'
14
14
15 def self.revision
15 def self.revision
16 revision = nil
16 revision = nil
17 entries_path = "#{RAILS_ROOT}/.svn/entries"
17 entries_path = "#{RAILS_ROOT}/.svn/entries"
18 if File.readable?(entries_path)
18 if File.readable?(entries_path)
19 begin
19 begin
20 f = File.open(entries_path, 'r')
20 f = File.open(entries_path, 'r')
21 entries = f.read
21 entries = f.read
22 f.close
22 f.close
23 if entries.match(%r{^\d+})
23 if entries.match(%r{^\d+})
24 revision = $1.to_i if entries.match(%r{^\d+\s+dir\s+(\d+)\s})
24 revision = $1.to_i if entries.match(%r{^\d+\s+dir\s+(\d+)\s})
25 else
25 else
26 xml = REXML::Document.new(entries)
26 xml = REXML::Document.new(entries)
27 revision = xml.elements['wc-entries'].elements[1].attributes['revision'].to_i
27 revision = xml.elements['wc-entries'].elements[1].attributes['revision'].to_i
28 end
28 end
29 rescue
29 rescue
30 # Could not find the current revision
30 # Could not find the current revision
31 end
31 end
32 end
32 end
33 revision
33 revision
34 end
34 end
35
35
36 REVISION = self.revision
36 REVISION = self.revision
37 STRING = [MAJOR, MINOR, TINY, BRANCH, REVISION].compact.join('.')
37 ARRAY = [MAJOR, MINOR, TINY, BRANCH, REVISION].compact
38 STRING = ARRAY.join('.')
38
39
40 def self.to_a; ARRAY end
39 def self.to_s; STRING end
41 def self.to_s; STRING end
40 end
42 end
41 end
43 end
@@ -1,55 +1,78
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../../../test_helper'
18 require File.dirname(__FILE__) + '/../../../test_helper'
19
19
20 class Redmine::PluginTest < Test::Unit::TestCase
20 class Redmine::PluginTest < Test::Unit::TestCase
21
21
22 def setup
22 def setup
23 @klass = Redmine::Plugin
23 @klass = Redmine::Plugin
24 # In case some real plugins are installed
24 # In case some real plugins are installed
25 @klass.clear
25 @klass.clear
26 end
26 end
27
27
28 def teardown
28 def teardown
29 @klass.clear
29 @klass.clear
30 end
30 end
31
31
32 def test_register
32 def test_register
33 @klass.register :foo do
33 @klass.register :foo do
34 name 'Foo plugin'
34 name 'Foo plugin'
35 url 'http://example.net/plugins/foo'
35 url 'http://example.net/plugins/foo'
36 author 'John Smith'
36 author 'John Smith'
37 author_url 'http://example.net/jsmith'
37 author_url 'http://example.net/jsmith'
38 description 'This is a test plugin'
38 description 'This is a test plugin'
39 version '0.0.1'
39 version '0.0.1'
40 settings :default => {'sample_setting' => 'value', 'foo'=>'bar'}, :partial => 'foo/settings'
40 settings :default => {'sample_setting' => 'value', 'foo'=>'bar'}, :partial => 'foo/settings'
41 end
41 end
42
42
43 assert_equal 1, @klass.all.size
43 assert_equal 1, @klass.all.size
44
44
45 plugin = @klass.find('foo')
45 plugin = @klass.find('foo')
46 assert plugin.is_a?(Redmine::Plugin)
46 assert plugin.is_a?(Redmine::Plugin)
47 assert_equal :foo, plugin.id
47 assert_equal :foo, plugin.id
48 assert_equal 'Foo plugin', plugin.name
48 assert_equal 'Foo plugin', plugin.name
49 assert_equal 'http://example.net/plugins/foo', plugin.url
49 assert_equal 'http://example.net/plugins/foo', plugin.url
50 assert_equal 'John Smith', plugin.author
50 assert_equal 'John Smith', plugin.author
51 assert_equal 'http://example.net/jsmith', plugin.author_url
51 assert_equal 'http://example.net/jsmith', plugin.author_url
52 assert_equal 'This is a test plugin', plugin.description
52 assert_equal 'This is a test plugin', plugin.description
53 assert_equal '0.0.1', plugin.version
53 assert_equal '0.0.1', plugin.version
54 end
54 end
55
56 def test_requires_redmine
57 test = self
58 version = Redmine::VERSION.to_a.slice(0,3).join('.')
59
60 @klass.register :foo do
61 test.assert requires_redmine(:version_or_higher => '0.1.0')
62 test.assert requires_redmine(:version_or_higher => version)
63 test.assert requires_redmine(version)
64 test.assert_raise Redmine::PluginRequirementError do
65 requires_redmine(:version_or_higher => '99.0.0')
66 end
67
68 test.assert requires_redmine(:version => version)
69 test.assert requires_redmine(:version => [version, '99.0.0'])
70 test.assert_raise Redmine::PluginRequirementError do
71 requires_redmine(:version => '99.0.0')
72 end
73 test.assert_raise Redmine::PluginRequirementError do
74 requires_redmine(:version => ['98.0.0', '99.0.0'])
75 end
76 end
77 end
55 end
78 end
General Comments 0
You need to be logged in to leave comments. Login now