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