Index: lib/redmine/plugin.rb =================================================================== --- lib/redmine/plugin.rb (revision 13338) +++ lib/redmine/plugin.rb (working copy) @@ -49,6 +49,8 @@ cattr_accessor :public_directory self.public_directory = File.join(Rails.root, 'public', 'plugin_assets') + cattr_accessor :plugin_requirements + @registered_plugins = {} @used_partials = {} @@ -138,6 +140,7 @@ end def self.load + clear_plugin_requirements Dir.glob(File.join(self.directory, '*')).sort.each do |directory| if File.directory?(directory) lib = File.join(directory, "lib") @@ -151,6 +154,7 @@ end end end + check_plugin_requirements end def initialize(id) @@ -239,26 +243,47 @@ # # Requires a specific version of a Redmine plugin # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0 + # + # For Redmine >= 2.6.0, the requirement check is postponed until all + # plugins are loaded, so that you can require a plugin which name is + # lexically greater than the current one (before that plugin were loaded in + # lexical order, hence no possibility for plugin "bar" to depend on plugin + # "foo" which was not loaded). + # + # Note that this is still not perfect, as you cannot directly "require + # foo_file" in plugin "bar" if "foo_file" is in the plugin "foo" in + # "lib/foo_file". But that should be a minor problem for plugin authors for + # now. def requires_redmine_plugin(plugin_name, arg) - arg = { :version_or_higher => arg } unless arg.is_a?(Hash) - arg.assert_valid_keys(:version, :version_or_higher) + self.class.plugin_requirements << [id, plugin_name, arg] + end - plugin = Plugin.find(plugin_name) - current = plugin.version.split('.').collect(&:to_i) + def self.clear_plugin_requirements + self.plugin_requirements = [] + end - arg.each do |k, v| - v = [] << v unless v.is_a?(Array) - versions = v.collect {|s| s.split('.').collect(&:to_i)} - case k - when :version_or_higher - raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1 - unless (current <=> versions.first) >= 0 - raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}") + def self.check_plugin_requirements + self.plugin_requirements.each do |plugin_id, required_plugin_name, arg| + arg = { :version_or_higher => arg } unless arg.is_a?(Hash) + arg.assert_valid_keys(:version, :version_or_higher) + + plugin = Plugin.find(required_plugin_name) + current = plugin.version.split('.').collect(&:to_i) + + arg.each do |k, v| + v = [] << v unless v.is_a?(Array) + versions = v.collect {|s| s.split('.').collect(&:to_i)} + case k + when :version_or_higher + raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1 + unless (current <=> versions.first) >= 0 + raise PluginRequirementError.new("#{plugin_id} plugin requires the #{required_plugin_name} plugin #{v} or higher but current is #{current.join('.')}") + end + when :version + unless versions.include?(current.slice(0,3)) + raise PluginRequirementError.new("#{plugin_id} plugin requires one the following versions of #{required_plugin_name}: #{v.join(', ')} but current is #{current.join('.')}") + end end - when :version - unless versions.include?(current.slice(0,3)) - raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}") - end end end true Index: test/unit/lib/redmine/plugin_test.rb =================================================================== --- test/unit/lib/redmine/plugin_test.rb (revision 13338) +++ test/unit/lib/redmine/plugin_test.rb (working copy) @@ -146,32 +146,51 @@ name 'Other' version other_version end - @klass.register :foo do - test.assert requires_redmine_plugin(:other, :version_or_higher => '0.1.0') - test.assert requires_redmine_plugin(:other, :version_or_higher => other_version) - test.assert requires_redmine_plugin(:other, other_version) - test.assert_raise Redmine::PluginRequirementError do - requires_redmine_plugin(:other, :version_or_higher => '99.0.0') + should_pass = [ + [:other, :version_or_higher => '0.1.0'], + [:other, :version_or_higher => other_version], + [:other, other_version], + [:other, :version => other_version], + [:other, :version => [other_version, '99.0.0']], + ] + should_fail_with_plugin_requirement = [ + [:other, :version_or_higher => '99.0.0'], + [:other, :version => '99.0.0'], + [:other, :version => ['98.0.0', '99.0.0']], + ] + should_fail_with_plugin_not_found = [ + [:missing, :version_or_higher => '0.1.0'], + [:missing, '0.1.0'], + [:missing, :version => '0.1.0'], + ] + should_pass.each do |(name, constraint)| + test.assert_block do + @klass.clear_plugin_requirements + @klass.register :foo do + requires_redmine_plugin(name, constraint) + end + @klass.check_plugin_requirements end - test.assert requires_redmine_plugin(:other, :version => other_version) - test.assert requires_redmine_plugin(:other, :version => [other_version, '99.0.0']) + end + should_fail_with_plugin_requirement.each do |(name, constraint)| test.assert_raise Redmine::PluginRequirementError do - requires_redmine_plugin(:other, :version => '99.0.0') + @klass.clear_plugin_requirements + @klass.register :foo do + requires_redmine_plugin(name, constraint) + end + @klass.check_plugin_requirements end - test.assert_raise Redmine::PluginRequirementError do - requires_redmine_plugin(:other, :version => ['98.0.0', '99.0.0']) - end - # Missing plugin + end + should_fail_with_plugin_not_found.each do |(name, constraint)| test.assert_raise Redmine::PluginNotFound do - requires_redmine_plugin(:missing, :version_or_higher => '0.1.0') + @klass.clear_plugin_requirements + @klass.register :foo do + requires_redmine_plugin(name, constraint) + end + @klass.check_plugin_requirements end - test.assert_raise Redmine::PluginNotFound do - requires_redmine_plugin(:missing, '0.1.0') - end - test.assert_raise Redmine::PluginNotFound do - requires_redmine_plugin(:missing, :version => '0.1.0') - end end + end def test_settings_warns_about_possible_partial_collision