2019-05-03 06:17:27 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
|
require 'listen'
|
|
|
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
|
class Watcher
|
2018-05-16 15:25:49 +08:00
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
|
def self.watch(paths = nil)
|
|
|
|
|
watcher = new(paths)
|
|
|
|
|
watcher.start
|
|
|
|
|
watcher
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def initialize(paths)
|
2017-08-09 23:06:27 +08:00
|
|
|
|
@paths = paths || Watcher.default_paths
|
2017-04-12 22:52:52 +08:00
|
|
|
|
@queue = Queue.new
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-09 23:06:27 +08:00
|
|
|
|
def self.default_paths
|
|
|
|
|
return @default_paths if @default_paths
|
|
|
|
|
|
|
|
|
|
@default_paths = ["app/assets/stylesheets"]
|
2021-04-16 21:25:20 +08:00
|
|
|
|
Discourse.plugins.each do |plugin|
|
|
|
|
|
if plugin.path.to_s.include?(Rails.root.to_s)
|
|
|
|
|
@default_paths << File.dirname(plugin.path).sub(Rails.root.to_s, '').sub(/^\//, '')
|
|
|
|
|
else
|
|
|
|
|
# if plugin doesn’t seem to be in our app, consider it as outside of the app
|
|
|
|
|
# and ignore it
|
|
|
|
|
warn("[stylesheet watcher] Ignoring outside of rails root plugin: #{plugin.path.to_s}")
|
|
|
|
|
end
|
2017-08-09 23:06:27 +08:00
|
|
|
|
end
|
|
|
|
|
@default_paths
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
|
def start
|
|
|
|
|
Thread.new do
|
|
|
|
|
begin
|
|
|
|
|
while true
|
|
|
|
|
worker_loop
|
|
|
|
|
end
|
|
|
|
|
rescue => e
|
2021-03-13 05:18:00 +08:00
|
|
|
|
STDERR.puts "CSS change notifier crashed \n#{e}"
|
2019-10-05 01:20:51 +08:00
|
|
|
|
start
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
listener_opts = { ignore: /xxxx/, only: /\.(css|scss)$/ }
|
2017-08-17 00:59:21 +08:00
|
|
|
|
listener_opts[:force_polling] = true if ENV['FORCE_POLLING']
|
|
|
|
|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
Thread.new do
|
|
|
|
|
begin
|
|
|
|
|
plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file|
|
|
|
|
|
if File.symlink?(file)
|
|
|
|
|
File.expand_path(File.readlink(file), "#{Rails.root}/plugins")
|
|
|
|
|
else
|
|
|
|
|
file
|
|
|
|
|
end
|
|
|
|
|
end.compact
|
|
|
|
|
|
|
|
|
|
listener = Listen.to(*@paths, listener_opts) do |modified, added, _|
|
|
|
|
|
paths = [modified, added].flatten
|
|
|
|
|
paths.compact!
|
|
|
|
|
paths.map! do |long|
|
|
|
|
|
plugin_name = nil
|
|
|
|
|
plugins_paths.each do |plugin_path|
|
|
|
|
|
if long.include?("#{plugin_path}/")
|
|
|
|
|
plugin_name = File.basename(plugin_path)
|
|
|
|
|
break
|
2019-10-10 13:10:23 +08:00
|
|
|
|
end
|
2021-03-13 05:18:00 +08:00
|
|
|
|
end
|
2019-10-10 13:10:23 +08:00
|
|
|
|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
target = nil
|
|
|
|
|
target_match = long.match(/admin|desktop|mobile|publish/)
|
|
|
|
|
if target_match&.length
|
|
|
|
|
target = target_match[0]
|
2019-10-10 13:10:23 +08:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
{
|
|
|
|
|
basename: File.basename(long),
|
|
|
|
|
target: target,
|
|
|
|
|
plugin_name: plugin_name
|
|
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
2021-03-13 05:18:00 +08:00
|
|
|
|
|
|
|
|
|
process_change(paths)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
2021-03-13 05:18:00 +08:00
|
|
|
|
rescue => e
|
|
|
|
|
STDERR.puts "Failed to listen for CSS changes: \n#{e}"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
2021-03-13 05:18:00 +08:00
|
|
|
|
listener.start
|
|
|
|
|
sleep
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-10 13:10:23 +08:00
|
|
|
|
def core_assets_refresh(target)
|
|
|
|
|
targets = target ? [target] : ["desktop", "mobile", "admin"]
|
|
|
|
|
Stylesheet::Manager.clear_core_cache!(targets)
|
|
|
|
|
message = targets.map! do |name|
|
2020-07-31 07:03:24 +08:00
|
|
|
|
msgs = []
|
|
|
|
|
active_themes.each do |theme_id|
|
|
|
|
|
msgs << Stylesheet::Manager.stylesheet_data(name.to_sym, theme_id)
|
|
|
|
|
end
|
|
|
|
|
msgs
|
2019-10-10 13:10:23 +08:00
|
|
|
|
end.flatten!
|
|
|
|
|
MessageBus.publish '/file-change', message
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-02 01:56:40 +08:00
|
|
|
|
def plugin_assets_refresh(plugin_name, target)
|
2019-10-10 13:10:23 +08:00
|
|
|
|
Stylesheet::Manager.clear_plugin_cache!(plugin_name)
|
2020-09-02 01:56:40 +08:00
|
|
|
|
targets = []
|
|
|
|
|
if target.present?
|
|
|
|
|
targets.push("#{plugin_name}_#{target.to_s}") if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, target.to_sym)
|
|
|
|
|
else
|
|
|
|
|
targets.push(plugin_name)
|
|
|
|
|
end
|
2020-01-09 02:25:36 +08:00
|
|
|
|
message = targets.map! do |name|
|
2020-07-31 07:03:24 +08:00
|
|
|
|
msgs = []
|
|
|
|
|
active_themes.each do |theme_id|
|
|
|
|
|
msgs << Stylesheet::Manager.stylesheet_data(name.to_sym, theme_id)
|
|
|
|
|
end
|
2020-08-05 02:10:25 +08:00
|
|
|
|
msgs
|
2020-01-09 02:25:36 +08:00
|
|
|
|
end.flatten!
|
2019-10-10 13:10:23 +08:00
|
|
|
|
MessageBus.publish '/file-change', message
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
|
def worker_loop
|
2019-10-10 13:10:23 +08:00
|
|
|
|
path = @queue.pop
|
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
|
while @queue.length > 0
|
|
|
|
|
@queue.pop
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-10 13:10:23 +08:00
|
|
|
|
if path[:plugin_name]
|
2020-09-02 01:56:40 +08:00
|
|
|
|
plugin_assets_refresh(path[:plugin_name], path[:target])
|
2019-10-10 13:10:23 +08:00
|
|
|
|
else
|
|
|
|
|
core_assets_refresh(path[:target])
|
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def process_change(paths)
|
|
|
|
|
paths.each do |path|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
@queue.push path
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2020-07-31 07:03:24 +08:00
|
|
|
|
|
|
|
|
|
def active_themes
|
|
|
|
|
@active_themes ||= Theme.user_selectable.pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
|
end
|
|
|
|
|
end
|