##// 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
@@ -18,6 +18,7
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.
@@ -93,6 +94,40 module Redmine #:nodoc:
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.
@@ -34,8 +34,10 module Redmine
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
@@ -52,4 +52,27 class Redmine::PluginTest < Test::Unit::TestCase
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