Feature #27705 » 0001-Enable-to-use-gem-as-a-Redmine-plugin.patch
| .gitignore | ||
|---|---|---|
|
/.bundle
|
||
|
/Gemfile.lock
|
||
|
/Gemfile.local
|
||
|
/Gemfile.extension
|
||
|
/node_modules
|
||
|
yarn-error.log
|
||
| Gemfile | ||
|---|---|---|
|
end
|
||
|
# Load plugins' Gemfiles
|
||
|
Dir.glob File.expand_path("../plugins/*/{Gemfile,PluginGemfile}", __FILE__) do |file|
|
||
|
eval_gemfile file
|
||
|
Dir.glob(File.expand_path("./plugins/*/", __dir__)) do |entry|
|
||
|
if File.directory?(entry)
|
||
|
plugin_dir = File.expand_path(entry, __dir__)
|
||
|
files =
|
||
|
if Dir.exist?(plugin_dir + "*.gemspec")
|
||
|
Dir.glob(File.join(plugin_dir, "PluginGemfile"))
|
||
|
else
|
||
|
Dir.glob(File.join(plugin_dir,"{Gemfile,PluginGemfile}"))
|
||
|
end
|
||
|
files.each do |file|
|
||
|
eval_gemfile file
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
extension_gemfile = File.join(File.dirname(__FILE__), "Gemfile.extension")
|
||
|
if File.exist?(extension_gemfile)
|
||
|
group :redmine_extension do
|
||
|
eval_gemfile extension_gemfile
|
||
|
end
|
||
|
end
|
||
| Gemfile.extension.example | ||
|---|---|---|
|
# vim: set ft=ruby
|
||
|
# Copy this file to Gemfile.extension and add any rubygem plugins
|
||
|
# Example:
|
||
|
# gem 'redmine_sample_plugin', '~> 1.1.0'
|
||
| config/routes.rb | ||
|---|---|---|
|
get 'robots', :to => 'welcome#robots'
|
||
|
Dir.glob File.expand_path("#{Redmine::Plugin.directory}/*") do |plugin_dir|
|
||
|
file = File.join(plugin_dir, "config/routes.rb")
|
||
|
if File.exist?(file)
|
||
|
Redmine::Plugin.all.each do |plugin|
|
||
|
if file = plugin.routes
|
||
|
begin
|
||
|
instance_eval File.read(file)
|
||
|
rescue SyntaxError, StandardError => e
|
||
|
puts "An error occurred while loading the routes definition of #{File.basename(plugin_dir)} plugin (#{file}): #{e.message}."
|
||
|
puts "An error occurred while loading the routes definition of #{File.basename(plugin.directory)} plugin (#{file}): #{e.message}."
|
||
|
exit 1
|
||
|
end
|
||
|
end
|
||
| lib/redmine/plugin.rb | ||
|---|---|---|
|
# Exception raised when a plugin requirement is not met.
|
||
|
class PluginRequirementError < StandardError; end
|
||
|
# Exception raised when plugin id and gemspec metadata is not met.
|
||
|
class InvalidPluginId < StandardError; end
|
||
|
# Base class for Redmine plugins.
|
||
|
# Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
|
||
|
#
|
||
| ... | ... | |
|
p = new(id)
|
||
|
p.instance_eval(&block)
|
||
|
# Set a default name if it was not provided during registration
|
||
|
p.name(id.to_s.humanize) if p.name.nil?
|
||
|
# Set a default directory if it was not provided during registration
|
||
|
p.directory(File.join(self.directory, id.to_s)) if p.directory.nil?
|
||
|
# Set a default attributes if it was not provided during registration
|
||
|
p.set_default_attrs File.join(self.directory, id.to_s)
|
||
|
unless File.directory?(p.directory)
|
||
|
raise PluginNotFound, "Plugin not found. The directory for plugin #{p.id} should be #{p.directory}."
|
||
|
end
|
||
|
p.path = PluginLoader.directories.find {|d| d.to_s == p.directory}
|
||
|
loc = caller_locations(1, 1).first.absolute_path
|
||
|
if p.has_initializer? && (File.dirname(loc) != p.directory)
|
||
|
raise InvalidPluginId, "The location of init.rb is different from #{p.directory}. It called from #{loc}"
|
||
|
end
|
||
|
# Adds plugin locales if any
|
||
|
# YAML translation files should be found under <plugin>/config/locales/
|
||
| ... | ... | |
|
@used_partials[partial] = p.id
|
||
|
end
|
||
|
# Load dependencies
|
||
|
if p.gem?
|
||
|
p.path.gemspec.dependencies.each do |d|
|
||
|
PluginLoader.dependencies << d.name
|
||
|
end
|
||
|
end
|
||
|
registered_plugins[id] = p
|
||
|
end
|
||
| ... | ... | |
|
self.id.to_s <=> plugin.id.to_s
|
||
|
end
|
||
|
def set_default_attrs(default_dir)
|
||
|
dir = directory || default_dir
|
||
|
self.path = PluginLoader.find_path(plugin_id: id, plugin_dir: dir)
|
||
|
if gem?
|
||
|
spec = path.gemspec
|
||
|
name spec.summary if spec.summary
|
||
|
author spec.authors.join(",") if spec.authors
|
||
|
description spec.description if spec.description
|
||
|
version spec.version.to_s if spec.version
|
||
|
url spec.homepage if spec.homepage
|
||
|
author_url spec.metadata["author_url"] if spec.metadata["author_url"]
|
||
|
end
|
||
|
# Set a default name if it was not provided during registration
|
||
|
name(id.to_s.humanize) if name.nil?
|
||
|
# Set a default directory if it was not provided during registration
|
||
|
if directory.nil?
|
||
|
if path.present?
|
||
|
directory(path.to_s)
|
||
|
else
|
||
|
directory(default_dir)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
def gem?
|
||
|
path && path.gemspec.present?
|
||
|
end
|
||
|
def has_initializer?
|
||
|
path.present? && path.has_initializer?
|
||
|
end
|
||
|
def routes
|
||
|
file = File.join(directory, "config/routes.rb")
|
||
|
if File.exist?(file)
|
||
|
file
|
||
|
else
|
||
|
nil
|
||
|
end
|
||
|
end
|
||
|
# Sets a requirement on Redmine version
|
||
|
# Raises a PluginRequirementError exception if the requirement is not met
|
||
|
#
|
||
| lib/redmine/plugin_loader.rb | ||
|---|---|---|
|
module Redmine
|
||
|
class PluginPath
|
||
|
attr_reader :assets_dir, :initializer
|
||
|
attr_reader :assets_dir, :initializer, :gemspec
|
||
|
def initialize(dir)
|
||
|
def initialize(dir, gemspec = nil)
|
||
|
@dir = dir
|
||
|
@gemspec = gemspec
|
||
|
@assets_dir = File.join dir, 'assets'
|
||
|
@initializer = File.join dir, 'init.rb'
|
||
|
add_autoload_paths
|
||
|
end
|
||
|
def add_autoload_paths
|
||
|
# Add the plugin directories to rails autoload paths
|
||
|
engine_cfg = Rails::Engine::Configuration.new(self.to_s)
|
||
|
engine_cfg.paths.add 'lib', eager_load: true
|
||
|
engine_cfg.eager_load_paths.each do |dir|
|
||
|
Rails.autoloaders.main.push_dir dir
|
||
|
Rails.application.config.watchable_dirs[dir] = [:rb]
|
||
|
end
|
||
|
end
|
||
|
def run_initializer
|
||
| ... | ... | |
|
end
|
||
|
class PluginLoader
|
||
|
class PluginIdDuplicated < StandardError; end
|
||
|
# Absolute path to the directory where plugins are located
|
||
|
cattr_accessor :directory
|
||
|
self.directory = Rails.root.join('plugins')
|
||
| ... | ... | |
|
cattr_accessor :public_directory
|
||
|
self.public_directory = Rails.public_path.join('plugin_assets')
|
||
|
cattr_accessor :dependencies
|
||
|
self.dependencies = []
|
||
|
def self.create_assets_reloader
|
||
|
plugin_assets_dirs = {}
|
||
|
directories.each do |dir|
|
||
| ... | ... | |
|
def self.load
|
||
|
setup
|
||
|
add_autoload_paths
|
||
|
Rails.application.config.to_prepare do
|
||
|
PluginLoader.directories.each(&:run_initializer)
|
||
|
PluginLoader.require_dependencies
|
||
|
Redmine::Hook.call_hook :after_plugins_loaded
|
||
|
end
|
||
| ... | ... | |
|
def self.setup
|
||
|
@plugin_directories = []
|
||
|
Dir.glob(File.join(directory, '*')).sort.each do |directory|
|
||
|
next unless File.directory?(directory)
|
||
|
Dir.glob(File.join(directory, '*')).sort.each do |dir|
|
||
|
next unless File.directory?(dir)
|
||
|
@plugin_directories << PluginPath.new(directory)
|
||
|
@plugin_directories << PluginPath.new(dir)
|
||
|
end
|
||
|
end
|
||
|
def self.add_autoload_paths
|
||
|
directories.each do |directory|
|
||
|
# Add the plugin directories to rails autoload paths
|
||
|
engine_cfg = Rails::Engine::Configuration.new(directory.to_s)
|
||
|
engine_cfg.paths.add 'lib', eager_load: true
|
||
|
engine_cfg.eager_load_paths.each do |dir|
|
||
|
Rails.autoloaders.main.push_dir dir
|
||
|
Rails.application.config.watchable_dirs[dir] = [:rb]
|
||
|
# If there are plugins under plugins/, do not register a gem with the same name.
|
||
|
plugin_specs.each do |spec|
|
||
|
dir = File.join(directory, spec.name)
|
||
|
if File.directory?(dir)
|
||
|
warn "WARN: \"#{spec.name}\" plugin installed as gems also exist in the \"#{dir}\" directory; use the ones in \"#{dir}\"."
|
||
|
next
|
||
|
end
|
||
|
@plugin_directories << PluginPath.new(spec.full_gem_path, spec)
|
||
|
end
|
||
|
end
|
||
|
def self.plugin_specs
|
||
|
specs = Bundler.definition
|
||
|
.specs_for([:redmine_extension])
|
||
|
.to_a
|
||
|
.select{|s| s.name != 'bundler' && !s.metadata['redmine_plugin_id'].nil?}
|
||
|
duplicates = specs.group_by{|s| s.metadata['redmine_plugin_id']}.reject{|k, v| v.one?}.keys
|
||
|
raise PluginIdDuplicated.new("#{duplicates.join(',')} Duplicate plugin id") unless duplicates.empty?
|
||
|
specs
|
||
|
end
|
||
|
def self.require_dependencies
|
||
|
# Load dependencies. If the dependency is a redmine plugin, do not load it
|
||
|
# (it should already be initialized)
|
||
|
deps = dependencies - plugin_specs.map(&:name)
|
||
|
deps.uniq.each do |d|
|
||
|
require d
|
||
|
end
|
||
|
end
|
||
|
def self.find_path(plugin_id:, plugin_dir:)
|
||
|
path = directories.find {|d| d.gemspec.present? && d.gemspec.metadata['redmine_plugin_id'] == plugin_id.to_s }
|
||
|
if path.nil?
|
||
|
path = directories.find {|d| d.to_s == plugin_dir}
|
||
|
end
|
||
|
path
|
||
|
end
|
||
|
def self.directories
|
||
| test/fixtures/gem_plugins/.gitignore | ||
|---|---|---|
|
Gemfile.lock
|
||
| test/fixtures/gem_plugins/Gemfile | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
original_gemfile = File.join(File.dirname(__FILE__), "../../../Gemfile")
|
||
|
eval_gemfile original_gemfile
|
||
|
gem 'quux', path: './quux', require: false
|
||
|
group :redmine_extension do
|
||
|
gem 'baz', path: './baz'
|
||
|
end
|
||
| test/fixtures/gem_plugins/baz/Gemfile | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
source "https://rubygems.org"
|
||
|
# Specify your gem's dependencies in baz_plugin.gemspec
|
||
|
gemspec
|
||
| test/fixtures/gem_plugins/baz/baz.gemspec | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
Gem::Specification.new do |spec|
|
||
|
spec.name = "baz"
|
||
|
spec.version = "0.0.1"
|
||
|
spec.authors = ['johndoe', 'janedoe']
|
||
|
spec.email = ['johndoe@example.org']
|
||
|
spec.summary = "Baz Plugin"
|
||
|
spec.description = "This is a gemified plugin for Redmine"
|
||
|
spec.homepage = "https://example.org/plugins/baz"
|
||
|
spec.required_ruby_version = ">= 2.7.0"
|
||
|
spec.metadata['allowed_push_host'] = "https://example.org"
|
||
|
spec.metadata['redmine_plugin_id'] = "baz_plugin"
|
||
|
spec.metadata['rubygems_mfa_required'] = "true"
|
||
|
spec.files = Dir["{app,lib,config,assets,db}/**/*", "init.rb", "Gemfile", "README.rdoc"]
|
||
|
spec.add_dependency 'quux'
|
||
|
end
|
||
| test/fixtures/gem_plugins/baz/init.rb | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
Redmine::Plugin.register :baz_plugin do
|
||
|
name "This name should be overwritten with gemspec 'summary'"
|
||
|
author_url "https://example.org/this_url_should_not_be_overwritten_with_gemspec"
|
||
|
end
|
||
| test/fixtures/gem_plugins/baz/lib/baz_plugin.rb | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
module BazPlugin
|
||
|
class Error < StandardError; end
|
||
|
end
|
||
| test/fixtures/gem_plugins/quux/Gemfile | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
source "https://rubygems.org"
|
||
|
# Specify your gem's dependencies in quux.gemspec
|
||
|
gemspec
|
||
| test/fixtures/gem_plugins/quux/init.rb | ||
|---|---|---|
|
Redmine::Plugin.register :quux_plugin do
|
||
|
# For gemmed plugins, the attributes of the plugin are described in the gemspec.
|
||
|
# The correspondence between plugin attributes and gemspec is as follows
|
||
|
name "This name should be overwritten with gemspec 'summary'"
|
||
|
author_url "https://example.org/this_url_should_not_be_overwritten_with_gemspec"
|
||
|
end
|
||
| test/fixtures/gem_plugins/quux/lib/quux_plugin.rb | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
module QuuxPlugin
|
||
|
class Error < StandardError; end
|
||
|
end
|
||
| test/fixtures/gem_plugins/quux/quux.gemspec | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
Gem::Specification.new do |spec|
|
||
|
# Do not use constants or variables from the gem's own code in this block, as is normally
|
||
|
# done with gems. (e.g. Foo::VERSION)
|
||
|
# Specify the version of redmine or dependencies between plugins in the init.rb file.
|
||
|
spec.name = "quux"
|
||
|
spec.version = "0.0.1"
|
||
|
spec.authors = ["johndoe"]
|
||
|
spec.email = ["johndoe@example.org"]
|
||
|
spec.summary = "Quux plugin"
|
||
|
spec.description = "This is a plugin for Redmine"
|
||
|
spec.homepage = "https://example.org"
|
||
|
spec.required_ruby_version = ">= 2.7.0"
|
||
|
spec.metadata["author_url"] = spec.homepage
|
||
|
spec.files = Dir["{lib}/**/*", "init.rb", "Gemfile"]
|
||
|
# DO NOT DELETE this attribute
|
||
|
spec.metadata["redmine_plugin_id"] = "quux_plugin"
|
||
|
end
|
||
| test/fixtures/plugins/qux_plugin/init.rb | ||
|---|---|---|
|
# frozen_string_literal: true
|
||
|
Redmine::Plugin.register :baz_plugin do
|
||
|
name "This name should be overwritten with gemspec 'summary'"
|
||
|
end
|
||
| test/unit/lib/redmine/plugin_test.rb | ||
|---|---|---|
|
@klass.clear
|
||
|
end
|
||
|
def gemfile_for_test?
|
||
|
File.expand_path(ENV['BUNDLE_GEMFILE']) == Rails.root.join('test/fixtures/gem_plugins/Gemfile').expand_path.to_s
|
||
|
end
|
||
|
def test_register
|
||
|
@klass.register :foo_plugin do
|
||
|
name 'Foo plugin'
|
||
| ... | ... | |
|
assert_equal File.join(@klass.directory, 'foo_plugin', 'assets'), plugin.assets_directory
|
||
|
end
|
||
|
def test_gemified_plugin
|
||
|
skip unless gemfile_for_test?
|
||
|
path = Redmine::PluginLoader.find_path(plugin_id: "baz_plugin", plugin_dir: nil)
|
||
|
path.run_initializer
|
||
|
plugin = @klass.find('baz_plugin')
|
||
|
assert_equal :baz_plugin, plugin.id
|
||
|
assert_equal 'Baz Plugin', plugin.name
|
||
|
assert_equal 'https://example.org/plugins/baz', plugin.url
|
||
|
assert_equal 'johndoe,janedoe', plugin.author
|
||
|
assert_equal 'https://example.org/this_url_should_not_be_overwritten_with_gemspec', plugin.author_url
|
||
|
assert_equal 'This is a gemified plugin for Redmine', plugin.description
|
||
|
assert_equal '0.0.1', plugin.version
|
||
|
end
|
||
|
def test_invalid_gemified_plugin
|
||
|
skip unless gemfile_for_test?
|
||
|
path = Redmine::PluginLoader.find_path(plugin_id: nil, plugin_dir: File.join(@klass.directory, 'qux_plugin'))
|
||
|
e = assert_raises Redmine::InvalidPluginId do
|
||
|
path.run_initializer
|
||
|
end
|
||
|
initrb = Rails.root.join('test/fixtures/plugins/qux_plugin/init.rb')
|
||
|
assert_equal "The location of init.rb is different from #{Rails.root.join('test/fixtures/gem_plugins/baz')}. It called from #{initrb}", e.message
|
||
|
end
|
||
|
def test_register_should_raise_error_if_plugin_directory_does_not_exist
|
||
|
e = assert_raises Redmine::PluginNotFound do
|
||
|
@klass.register(:bar_plugin) {}
|
||