|
1 | | -# redmine_lightbox3/init.rb |
2 | | - |
3 | | -require 'redmine' |
4 | | -require 'fileutils' # <-- 引入文件操作模組 |
5 | | - |
6 | | -# === 資產複製邏輯:確保靜態檔案存在於 public/plugin_assets === |
7 | | -plugin_dir = Rails.root.join('plugins', 'redmine_lightbox3') |
8 | | -public_dir = Rails.root.join('public', 'plugin_assets', 'redmine_lightbox3') |
9 | | - |
10 | | -# 確保目標目錄存在 |
11 | | -FileUtils.mkdir_p(public_dir) unless File.directory?(public_dir.to_s) |
12 | | - |
13 | | -# 複製 assets/javascripts, assets/stylesheets, 和 assets/images |
14 | | -['javascripts', 'stylesheets', 'images'].each do |asset_type| |
15 | | - source = plugin_dir.join('app', 'assets', asset_type) |
16 | | - target = public_dir.join(asset_type) |
17 | | - |
18 | | - if File.directory?(source.to_s) |
19 | | - # 如果目標子目錄不存在,則執行複製 |
20 | | - unless File.directory?(target.to_s) |
21 | | - # 使用 cp_r 複製整個目錄內容 |
22 | | - FileUtils.cp_r(source.to_s, public_dir.to_s) |
23 | | - end |
24 | | - end |
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'fileutils' |
| 4 | + |
| 5 | +# --- Asset Deployment Logic with Pure Ruby Pathing --- |
| 6 | +# This version uses pure Ruby methods (`__dir__`) to be immune to Rails' boot sequence issues. |
| 7 | + |
| 8 | +# 1. Define key paths using pure Ruby |
| 9 | +plugin_name = 'redmine_lightbox3' |
| 10 | + |
| 11 | +# THE ONLY FIX IS HERE: `__dir__` itself is the plugin's root directory. |
| 12 | +# `File.expand_path('..', __dir__)` was incorrect as it went up one level. |
| 13 | +plugin_root = __dir__ |
| 14 | + |
| 15 | +source_assets = File.join(plugin_root, 'app', 'assets') |
| 16 | +public_dest = File.join(Rails.root.to_s, 'public', 'plugin_assets', plugin_name) |
| 17 | +version_file = File.join(public_dest, 'version.txt') |
| 18 | + |
| 19 | +# 2. Get the "Code Version" by parsing this init.rb file itself. |
| 20 | +code_version = nil |
| 21 | +init_rb_content = File.read(File.join(plugin_root, 'init.rb')) |
| 22 | +if match = init_rb_content.match(/version\s+['"]([^'"]+)['"]/) |
| 23 | + code_version = match[1] |
| 24 | +end |
| 25 | + |
| 26 | +# 3. Read the version of the assets currently on disk |
| 27 | +public_version = File.exist?(version_file) ? File.read(version_file).strip : nil |
| 28 | + |
| 29 | +# 4. Compare versions and redeploy if they don't match. |
| 30 | +if code_version.to_s != public_version.to_s && !code_version.nil? |
| 31 | + puts "Redmine Lightbox 3: Version mismatch (code: #{code_version}, public: #{public_version}). Redeploying assets." |
| 32 | + FileUtils.rm_rf(public_dest) |
| 33 | + FileUtils.mkdir_p(public_dest) |
| 34 | + FileUtils.cp_r(File.join(source_assets, '.'), public_dest) |
| 35 | + File.open(version_file, 'w') { |f| f.write(code_version) } |
25 | 36 | end |
26 | | -# ============================================================= |
27 | 37 |
|
28 | 38 |
|
| 39 | +# --- Plugin Registration --- |
29 | 40 | Redmine::Plugin.register :redmine_lightbox3 do |
30 | 41 | name 'Lightbox3 Plugin (Support Redmine 6 or higher)' |
31 | 42 | author 'tomy' |
|
34 | 45 | url 'https://redmine-tw.net' |
35 | 46 | author_url 'https://github.com/tomy-shen' |
36 | 47 |
|
37 | | - requires_redmine :version_or_higher => '6.0.0' |
| 48 | + requires_redmine version_or_higher: '6.0.0' |
38 | 49 |
|
39 | | - # 確保 Hook 檔案被載入 |
40 | 50 | require_relative 'lib/hooks/view_layouts_base_html_head_hook' |
41 | 51 |
|
42 | | - |
43 | | - # Rails 7/Redmine 6 兼容性修正:使用 ActiveSupport::Reloader 載入 Patch |
44 | 52 | ActiveSupport::Reloader.to_prepare do |
45 | 53 | require_relative 'lib/patches/attachments_patch' |
46 | | - |
47 | | - # 應用 Patch |
48 | 54 | unless Attachment.included_modules.include?(Patches::AttachmentsPatch) |
49 | 55 | Attachment.send(:include, Patches::AttachmentsPatch) |
50 | 56 | end |
|
0 commit comments