##// END OF EJS Templates
Adds a Plugin API to allow one plugin to depend on another....
Eric Davis -
r3061:39b44b1cb94f
parent child
Show More
@@ -132,6 +132,42 module Redmine #:nodoc:
132 132 true
133 133 end
134 134
135 # Sets a requirement on a Redmine plugin version
136 # Raises a PluginRequirementError exception if the requirement is not met
137 #
138 # Examples
139 # # Requires a plugin named :foo version 0.7.3 or higher
140 # requires_redmine_plugin :foo, :version_or_higher => '0.7.3'
141 # requires_redmine_plugin :foo, '0.7.3'
142 #
143 # # Requires a specific version of a Redmine plugin
144 # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only
145 # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
146 def requires_redmine_plugin(plugin_name, arg)
147 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
148 arg.assert_valid_keys(:version, :version_or_higher)
149
150 plugin = Plugin.find(plugin_name)
151 current = plugin.version.split('.').collect(&:to_i)
152
153 arg.each do |k, v|
154 v = [] << v unless v.is_a?(Array)
155 versions = v.collect {|s| s.split('.').collect(&:to_i)}
156 case k
157 when :version_or_higher
158 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
159 unless (current <=> versions.first) >= 0
160 raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}")
161 end
162 when :version
163 unless versions.include?(current.slice(0,3))
164 raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}")
165 end
166 end
167 end
168 true
169 end
170
135 171 # Adds an item to the given +menu+.
136 172 # The +id+ parameter (equals to the project id) is automatically added to the url.
137 173 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
@@ -75,4 +75,43 class Redmine::PluginTest < ActiveSupport::TestCase
75 75 end
76 76 end
77 77 end
78
79 def test_requires_redmine_plugin
80 test = self
81 other_version = '0.5.0'
82
83 @klass.register :other do
84 name 'Other'
85 version other_version
86 end
87
88 @klass.register :foo do
89 test.assert requires_redmine_plugin(:other, :version_or_higher => '0.1.0')
90 test.assert requires_redmine_plugin(:other, :version_or_higher => other_version)
91 test.assert requires_redmine_plugin(:other, other_version)
92 test.assert_raise Redmine::PluginRequirementError do
93 requires_redmine_plugin(:other, :version_or_higher => '99.0.0')
94 end
95
96 test.assert requires_redmine_plugin(:other, :version => other_version)
97 test.assert requires_redmine_plugin(:other, :version => [other_version, '99.0.0'])
98 test.assert_raise Redmine::PluginRequirementError do
99 requires_redmine_plugin(:other, :version => '99.0.0')
100 end
101 test.assert_raise Redmine::PluginRequirementError do
102 requires_redmine_plugin(:other, :version => ['98.0.0', '99.0.0'])
103 end
104 # Missing plugin
105 test.assert_raise Redmine::PluginNotFound do
106 requires_redmine_plugin(:missing, :version_or_higher => '0.1.0')
107 end
108 test.assert_raise Redmine::PluginNotFound do
109 requires_redmine_plugin(:missing, '0.1.0')
110 end
111 test.assert_raise Redmine::PluginNotFound do
112 requires_redmine_plugin(:missing, :version => '0.1.0')
113 end
114
115 end
116 end
78 117 end
General Comments 0
You need to be logged in to leave comments. Login now