Skip to content

Commit df52c28

Browse files
committed
add asset deployment logic
1 parent d599351 commit df52c28

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The above copyright notice and this permission notice shall be included in all
1515
copies or substantial portions of the Software.
1616

1717
Additional Modifications Notice:
18-
This version includes modifications made by Tomy Shen since 2025-10-08.
18+
This version includes modifications made by Tomy Shen since 2025-10-10.
1919
The main modification is support for Redmine 6.0 or higher.
2020

2121
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ $ rake redmine:plugins:migrate RAILS_ENV=production
2323
```
2424
* Restart Redmine
2525

26-
> Due to the update of redmine 6, CSS or javascript probably need to be precompiled after the initial installation. Reference [here](https://www.redmine.org/issues/41754).
27-
2826
## Screenshots
2927

3028
![screenshot01](screenshots/lightbox3-attachment-preview.png)

init.rb

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
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) }
2536
end
26-
# =============================================================
2737

2838

39+
# --- Plugin Registration ---
2940
Redmine::Plugin.register :redmine_lightbox3 do
3041
name 'Lightbox3 Plugin (Support Redmine 6 or higher)'
3142
author 'tomy'
@@ -34,17 +45,12 @@
3445
url 'https://redmine-tw.net'
3546
author_url 'https://github.com/tomy-shen'
3647

37-
requires_redmine :version_or_higher => '6.0.0'
48+
requires_redmine version_or_higher: '6.0.0'
3849

39-
# 確保 Hook 檔案被載入
4050
require_relative 'lib/hooks/view_layouts_base_html_head_hook'
4151

42-
43-
# Rails 7/Redmine 6 兼容性修正:使用 ActiveSupport::Reloader 載入 Patch
4452
ActiveSupport::Reloader.to_prepare do
4553
require_relative 'lib/patches/attachments_patch'
46-
47-
# 應用 Patch
4854
unless Attachment.included_modules.include?(Patches::AttachmentsPatch)
4955
Attachment.send(:include, Patches::AttachmentsPatch)
5056
end

0 commit comments

Comments
 (0)