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
|
|
|
|
|
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)
|
2023-01-21 02:52:49 +08:00
|
|
|
|
@default_paths << File.dirname(plugin.path).sub(Rails.root.to_s, "").sub(%r{\A/}, "")
|
2021-04-16 21:25:20 +08:00
|
|
|
|
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
|
|
|
|
|
worker_loop while true
|
|
|
|
|
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
|
|
|
|
|
|
2023-01-21 02:52:49 +08:00
|
|
|
|
listener_opts = { ignore: /xxxx/, only: /\.(css|scss)\z/ }
|
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
|
2019-10-10 13:10:23 +08:00
|
|
|
|
end
|
2021-03-13 05:18:00 +08:00
|
|
|
|
end
|
2023-01-09 20:10:19 +08:00
|
|
|
|
.compact
|
|
|
|
|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
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)
|
2023-01-09 20:10:19 +08:00
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-10-10 13:10:23 +08:00
|
|
|
|
|
2021-03-13 05:18:00 +08:00
|
|
|
|
target = nil
|
2022-05-03 06:49:47 +08:00
|
|
|
|
target_match =
|
|
|
|
|
long.match(/admin|desktop|mobile|publish|wizard|wcag|color_definitions/)
|
2021-03-13 05:18:00 +08:00
|
|
|
|
target = target_match[0] if target_match&.length
|
2023-01-09 20:10:19 +08:00
|
|
|
|
|
2020-09-02 01:56:40 +08:00
|
|
|
|
{ basename: File.basename(long), target: target, plugin_name: plugin_name }
|
2019-10-10 13:10:23 +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)
|
2022-05-04 23:58:52 +08:00
|
|
|
|
if target&.match(/wcag|color_definitions/)
|
2022-05-03 06:49:47 +08:00
|
|
|
|
Stylesheet::Manager.clear_color_scheme_cache!
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-10 13:10:23 +08:00
|
|
|
|
targets = target ? [target] : %w[desktop mobile admin]
|
|
|
|
|
Stylesheet::Manager.clear_core_cache!(targets)
|
|
|
|
|
message =
|
2021-07-07 01:11:10 +08:00
|
|
|
|
targets.map! { |name| Stylesheet::Manager.new.stylesheet_data(name.to_sym) }.flatten!
|
2019-10-10 13:10:23 +08:00
|
|
|
|
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?
|
|
|
|
|
if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, target.to_sym)
|
|
|
|
|
targets.push("#{plugin_name}_#{target.to_s}")
|
2023-01-09 20:10:19 +08:00
|
|
|
|
end
|
2020-09-02 01:56:40 +08:00
|
|
|
|
else
|
|
|
|
|
targets.push(plugin_name)
|
|
|
|
|
end
|
2020-01-09 02:25:36 +08:00
|
|
|
|
message =
|
2021-07-07 01:11:10 +08:00
|
|
|
|
targets.map! { |name| Stylesheet::Manager.new.stylesheet_data(name.to_sym) }.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
|
|
|
|
@queue.pop while @queue.length > 0
|
|
|
|
|
|
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 { |path| @queue.push path }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|