Patch #44016 » 0001-Enable-instantiation-of-PluginLoader.patch
| lib/redmine/plugin.rb | ||
|---|---|---|
|
#
|
||
|
# See: http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial
|
||
|
class Plugin
|
||
|
cattr_accessor :loader
|
||
|
# Absolute path to the directory where plugins are located
|
||
|
cattr_accessor :directory
|
||
|
self.directory = PluginLoader.directory
|
||
|
def self.directory
|
||
|
loader.directory
|
||
|
end
|
||
|
# Absolute path to the public directory where plugins assets are copied
|
||
|
cattr_accessor :public_directory
|
||
|
self.public_directory = PluginLoader.public_directory
|
||
|
def self.public_directory
|
||
|
loader.public_directory
|
||
|
end
|
||
|
@registered_plugins = {}
|
||
|
@used_partials = {}
|
||
| ... | ... | |
|
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}
|
||
|
p.path = loader.directories.find {|d| d.to_s == p.directory}
|
||
|
# Adds plugin locales if any
|
||
|
# YAML translation files should be found under <plugin>/config/locales/
|
||
| ... | ... | |
|
def asset_paths
|
||
|
return unless path.has_assets_dir?
|
||
|
base_dir = Pathname.new(path.assets_dir)
|
||
|
paths = base_dir.children.select(&:directory?)
|
||
|
Redmine::AssetPath.new(base_dir, paths, asset_prefix)
|
||
|
Redmine::AssetPath.new(path.base_dir, path.asset_paths, asset_prefix)
|
||
|
end
|
||
|
def <=>(plugin)
|
||
| lib/redmine/plugin_loader.rb | ||
|---|---|---|
|
module Redmine
|
||
|
class PluginPath
|
||
|
attr_reader :assets_dir, :initializer
|
||
|
attr_reader :assets_dir, :initializer, :prefix
|
||
|
def initialize(dir)
|
||
|
def initialize(dir, prefix)
|
||
|
@dir = dir
|
||
|
@assets_dir = File.join dir, 'assets'
|
||
|
@initializer = File.join dir, 'init.rb'
|
||
|
@prefix = prefix
|
||
|
@assets_dir = File.join @dir, 'assets'
|
||
|
@initializer = File.join @dir, 'init.rb'
|
||
|
end
|
||
|
def run_initializer
|
||
| ... | ... | |
|
def has_initializer?
|
||
|
File.file?(@initializer)
|
||
|
end
|
||
|
def base_dir
|
||
|
@base_dir ||= Pathname.new(assets_dir)
|
||
|
end
|
||
|
def asset_paths
|
||
|
paths = base_dir.children.select(&:directory?)
|
||
|
paths
|
||
|
end
|
||
|
end
|
||
|
class PluginLoader
|
||
|
# Absolute path to the directory where plugins are located
|
||
|
cattr_accessor :directory
|
||
|
self.directory = Rails.root.join Rails.application.config.redmine_plugins_directory
|
||
|
attr_accessor :directory
|
||
|
attr_accessor :prefix
|
||
|
# Absolute path to the public directory where plugins assets are copied
|
||
|
cattr_accessor :public_directory
|
||
|
self.public_directory = Rails.public_path.join('plugin_assets')
|
||
|
attr_accessor :public_directory
|
||
|
def self.load
|
||
|
setup
|
||
|
add_autoload_paths
|
||
|
def initialize(directory:, prefix:)
|
||
|
@directory = directory
|
||
|
@prefix = prefix
|
||
|
@plugin_directories = []
|
||
|
Rails.application.config.to_prepare do
|
||
|
PluginLoader.directories.each(&:run_initializer)
|
||
|
Dir.glob(File.join(directory, '*')).each do |directory|
|
||
|
next unless File.directory?(directory)
|
||
|
Redmine::Hook.call_hook :after_plugins_loaded
|
||
|
@plugin_directories << PluginPath.new(directory, prefix)
|
||
|
end
|
||
|
end
|
||
|
def self.setup
|
||
|
@plugin_directories = []
|
||
|
def self.load
|
||
|
directory = Rails.root.join(Rails.application.config.redmine_plugins_directory)
|
||
|
prefix = 'plugin_assets'
|
||
|
loader = new(directory: directory, prefix: prefix)
|
||
|
Dir.glob(File.join(directory, '*')).each do |directory|
|
||
|
next unless File.directory?(directory)
|
||
|
loader.public_directory = Rails.public_path.join(prefix)
|
||
|
loader.add_autoload_paths
|
||
|
Rails.application.config.to_prepare do
|
||
|
Redmine::Plugin.loader = loader
|
||
|
loader.directories.each(&:run_initializer)
|
||
|
@plugin_directories << PluginPath.new(directory)
|
||
|
Redmine::Hook.call_hook :after_plugins_loaded
|
||
|
end
|
||
|
end
|
||
|
def self.add_autoload_paths
|
||
|
def add_autoload_paths
|
||
|
directories.each do |directory|
|
||
|
# Add the plugin directories to rails autoload paths
|
||
|
engine_cfg = Rails::Engine::Configuration.new(directory.to_s)
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
def self.directories
|
||
|
def directories
|
||
|
@plugin_directories
|
||
|
end
|
||
|
end
|
||
| test/fixtures/plugins/foo_plugin/app/javascript/foo.js | ||
|---|---|---|
|
export sample() {
|
||
|
}
|
||
| test/fixtures/plugins/foo_plugin/init.rb | ||
|---|---|---|
|
Redmine::Plugin.register :foo_plugin do
|
||
|
name 'Test plugin redmine_test_plugin_foo'
|
||
|
author 'Author name'
|
||
|
description 'This is a plugin for Redmine test'
|
||
|
version '0.0.1'
|
||
|
end
|
||
| test/generators/controller_generator_test.rb | ||
|---|---|---|
|
setup do
|
||
|
@plugin_directory = Redmine::Plugin.directory
|
||
|
Redmine::Plugin.directory = TMP_DIR
|
||
|
Redmine::Plugin.loader.directory = TMP_DIR
|
||
|
end
|
||
|
teardown do
|
||
|
Redmine::Plugin.directory = @plugin_directory
|
||
|
Redmine::Plugin.loader.directory = @plugin_directory
|
||
|
end
|
||
|
def test_generates_files_from_templates
|
||
| test/generators/migration_generator_test.rb | ||
|---|---|---|
|
setup do
|
||
|
@plugin_directory = Redmine::Plugin.directory
|
||
|
Redmine::Plugin.directory = TMP_DIR
|
||
|
Redmine::Plugin.loader.directory = TMP_DIR
|
||
|
end
|
||
|
teardown do
|
||
|
Redmine::Plugin.directory = @plugin_directory
|
||
|
Redmine::Plugin.loader.directory = @plugin_directory
|
||
|
end
|
||
|
def test_migration_file_name_is_snake_case
|
||
| test/generators/model_generator_test.rb | ||
|---|---|---|
|
setup do
|
||
|
@plugin_directory = Redmine::Plugin.directory
|
||
|
Redmine::Plugin.directory = TMP_DIR
|
||
|
Redmine::Plugin.loader.directory = TMP_DIR
|
||
|
end
|
||
|
teardown do
|
||
|
Redmine::Plugin.directory = @plugin_directory
|
||
|
Redmine::Plugin.loader.directory = @plugin_directory
|
||
|
end
|
||
|
def test_generates_files_from_templates
|
||
| test/integration/routing/plugins_test.rb | ||
|---|---|---|
|
class RoutingPluginsTest < Redmine::RoutingTest
|
||
|
def setup
|
||
|
@original_plugin_dir = Redmine::PluginLoader.directory
|
||
|
@original_loader = Redmine::Plugin.loader
|
||
|
Redmine::Plugin.clear
|
||
|
Redmine::PluginLoader.directory = Rails.root.join('test/fixtures/plugins')
|
||
|
Redmine::Plugin.directory = Rails.root.join('test/fixtures/plugins')
|
||
|
Redmine::PluginLoader.load
|
||
|
Redmine::PluginLoader.directories.each(&:run_initializer) # to define relative controllers
|
||
|
loader = Redmine::PluginLoader.new(directory: Rails.root.join('test/fixtures/plugins'), prefix: @original_loader.prefix)
|
||
|
loader.public_directory = Rails.public_path.join(@original_loader.prefix)
|
||
|
loader.add_autoload_paths
|
||
|
Redmine::Plugin.loader = loader
|
||
|
loader.directories.each(&:run_initializer) # to define relative controllers
|
||
|
RedmineApp::Application.instance.routes_reloader.reload!
|
||
|
end
|
||
|
def teardown
|
||
|
Redmine::Plugin.clear
|
||
|
Redmine::PluginLoader.directory = @original_plugin_dir
|
||
|
Redmine::Plugin.directory = @original_plugin_dir
|
||
|
Redmine::Plugin.loader = @original_loader
|
||
|
Redmine::PluginLoader.load
|
||
|
RedmineApp::Application.instance.routes_reloader.reload!
|
||
|
end
|
||
| test/unit/lib/redmine/plugin_test.rb | ||
|---|---|---|
|
class Redmine::PluginTest < ActiveSupport::TestCase
|
||
|
def setup
|
||
|
@klass = Redmine::Plugin
|
||
|
@original_loader = Redmine::Plugin.loader
|
||
|
# Change plugin directory for testing to default
|
||
|
# plugins/foo => test/fixtures/plugins/foo
|
||
|
@klass.directory = Rails.root.join('test/fixtures/plugins')
|
||
|
loader = Redmine::PluginLoader.new(directory: Rails.root.join('test/fixtures/plugins'), prefix: @original_loader.prefix)
|
||
|
@klass.loader = loader
|
||
|
# In case some real plugins are installed
|
||
|
@klass.clear
|
||
|
# Change plugin loader's directory for testing
|
||
|
Redmine::PluginLoader.directory = @klass.directory
|
||
|
Redmine::PluginLoader.setup
|
||
|
end
|
||
|
def teardown
|
||
|
@klass.clear
|
||
|
Redmine::Plugin.loader = @original_loader
|
||
|
end
|
||
|
def test_register
|
||