You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin_loader.rb 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. class PluginPath
  20. attr_reader :assets_dir, :initializer
  21. def initialize(dir)
  22. @dir = dir
  23. @assets_dir = File.join dir, 'assets'
  24. @initializer = File.join dir, 'init.rb'
  25. end
  26. def run_initializer
  27. load initializer if has_initializer?
  28. end
  29. def to_s
  30. @dir
  31. end
  32. def mirror_assets
  33. return unless has_assets_dir?
  34. destination = File.join(PluginLoader.public_directory, File.basename(@dir))
  35. source_files = Dir["#{assets_dir}/**/*"]
  36. source_dirs = source_files.select { |d| File.directory?(d)}
  37. source_files -= source_dirs
  38. unless source_files.empty?
  39. base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(assets_dir, ''))
  40. begin
  41. FileUtils.mkdir_p(base_target_dir)
  42. rescue => e
  43. raise "Could not create directory #{base_target_dir}: " + e.message
  44. end
  45. end
  46. source_dirs.each do |dir|
  47. # strip down these paths so we have simple, relative paths we can
  48. # add to the destination
  49. target_dir = File.join(destination, dir.gsub(assets_dir, ''))
  50. begin
  51. FileUtils.mkdir_p(target_dir)
  52. rescue => e
  53. raise "Could not create directory #{target_dir}: " + e.message
  54. end
  55. end
  56. source_files.each do |file|
  57. target = File.join(destination, file.gsub(assets_dir, ''))
  58. unless File.exist?(target) && FileUtils.identical?(file, target)
  59. FileUtils.cp(file, target)
  60. end
  61. rescue => e
  62. raise "Could not copy #{file} to #{target}: " + e.message
  63. end
  64. end
  65. def has_assets_dir?
  66. File.directory?(@assets_dir)
  67. end
  68. def has_initializer?
  69. File.file?(@initializer)
  70. end
  71. end
  72. class PluginLoader
  73. # Absolute path to the directory where plugins are located
  74. cattr_accessor :directory
  75. self.directory = Rails.root.join('plugins')
  76. # Absolute path to the public directory where plugins assets are copied
  77. cattr_accessor :public_directory
  78. self.public_directory = Rails.public_path.join('plugin_assets')
  79. def self.create_assets_reloader
  80. plugin_assets_dirs = {}
  81. directories.each do |dir|
  82. plugin_assets_dirs[dir.assets_dir] = ['*']
  83. end
  84. ActiveSupport::FileUpdateChecker.new([], plugin_assets_dirs) do
  85. mirror_assets
  86. end
  87. end
  88. def self.load
  89. setup
  90. add_autoload_paths
  91. Rails.application.config.to_prepare do
  92. PluginLoader.directories.each(&:run_initializer)
  93. Redmine::Hook.call_hook :after_plugins_loaded
  94. end
  95. end
  96. def self.setup
  97. @plugin_directories = []
  98. Dir.glob(File.join(directory, '*')).sort.each do |directory|
  99. next unless File.directory?(directory)
  100. @plugin_directories << PluginPath.new(directory)
  101. end
  102. end
  103. def self.add_autoload_paths
  104. directories.each do |directory|
  105. # Add the plugin directories to rails autoload paths
  106. engine_cfg = Rails::Engine::Configuration.new(directory.to_s)
  107. engine_cfg.paths.add 'lib', eager_load: true
  108. engine_cfg.all_eager_load_paths.each do |dir|
  109. Rails.autoloaders.main.push_dir dir
  110. Rails.application.config.watchable_dirs[dir] = [:rb]
  111. end
  112. end
  113. end
  114. def self.directories
  115. @plugin_directories
  116. end
  117. def self.mirror_assets(name=nil)
  118. if name.present?
  119. directories.find{|d| d.to_s == File.join(directory, name)}.mirror_assets
  120. else
  121. directories.each(&:mirror_assets)
  122. end
  123. end
  124. end
  125. end