2019-05-02 18:17:27 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-04-12 10:52:52 -04: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 11:06:27 -04:00
|
|
|
|
@paths = paths || Watcher.default_paths
|
2017-04-12 10:52:52 -04:00
|
|
|
|
@queue = Queue.new
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-09 11:06:27 -04:00
|
|
|
|
def self.default_paths
|
|
|
|
|
return @default_paths if @default_paths
|
|
|
|
|
|
|
|
|
|
@default_paths = ["app/assets/stylesheets"]
|
2021-04-16 09:25:20 -04:00
|
|
|
|
Discourse.plugins.each do |plugin|
|
|
|
|
|
if plugin.path.to_s.include?(Rails.root.to_s)
|
2023-01-20 13:52:49 -05:00
|
|
|
|
@default_paths << File.dirname(plugin.path).sub(Rails.root.to_s, "").sub(%r{\A/}, "")
|
2021-04-16 09:25:20 -04: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 11:06:27 -04:00
|
|
|
|
end
|
|
|
|
|
@default_paths
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
def start
|
|
|
|
|
Thread.new do
|
|
|
|
|
begin
|
|
|
|
|
worker_loop while true
|
|
|
|
|
rescue => e
|
2021-03-12 16:18:00 -05:00
|
|
|
|
STDERR.puts "CSS change notifier crashed \n#{e}"
|
2019-10-04 13:20:51 -04:00
|
|
|
|
start
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-20 13:52:49 -05:00
|
|
|
|
listener_opts = { ignore: /xxxx/, only: /\.(css|scss)\z/ }
|
2017-08-16 12:59:21 -04:00
|
|
|
|
listener_opts[:force_polling] = true if ENV["FORCE_POLLING"]
|
|
|
|
|
|
2021-03-12 16:18:00 -05: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 01:10:23 -04:00
|
|
|
|
end
|
2021-03-12 16:18:00 -05:00
|
|
|
|
end
|
2023-01-09 07:10:19 -05:00
|
|
|
|
.compact
|
|
|
|
|
|
2021-03-12 16:18:00 -05: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 07:10:19 -05:00
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-10-10 01:10:23 -04:00
|
|
|
|
|
2021-03-12 16:18:00 -05:00
|
|
|
|
target = nil
|
2022-05-02 18:49:47 -04:00
|
|
|
|
target_match =
|
|
|
|
|
long.match(/admin|desktop|mobile|publish|wizard|wcag|color_definitions/)
|
2021-03-12 16:18:00 -05:00
|
|
|
|
target = target_match[0] if target_match&.length
|
2023-01-09 07:10:19 -05:00
|
|
|
|
|
2020-09-01 13:56:40 -04:00
|
|
|
|
{ basename: File.basename(long), target: target, plugin_name: plugin_name }
|
2019-10-10 01:10:23 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-12 16:18:00 -05:00
|
|
|
|
process_change(paths)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
2021-03-12 16:18:00 -05:00
|
|
|
|
rescue => e
|
|
|
|
|
STDERR.puts "Failed to listen for CSS changes: \n#{e}"
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
2021-03-12 16:18:00 -05:00
|
|
|
|
listener.start
|
|
|
|
|
sleep
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-10 01:10:23 -04:00
|
|
|
|
def core_assets_refresh(target)
|
2022-05-04 11:58:52 -04:00
|
|
|
|
if target&.match(/wcag|color_definitions/)
|
2022-05-02 18:49:47 -04:00
|
|
|
|
Stylesheet::Manager.clear_color_scheme_cache!
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-10 01:10:23 -04:00
|
|
|
|
targets = target ? [target] : %w[desktop mobile admin]
|
|
|
|
|
Stylesheet::Manager.clear_core_cache!(targets)
|
|
|
|
|
message =
|
2021-07-06 13:11:10 -04:00
|
|
|
|
targets.map! { |name| Stylesheet::Manager.new.stylesheet_data(name.to_sym) }.flatten!
|
2019-10-10 01:10:23 -04:00
|
|
|
|
MessageBus.publish "/file-change", message
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-01 13:56:40 -04:00
|
|
|
|
def plugin_assets_refresh(plugin_name, target)
|
2019-10-10 01:10:23 -04:00
|
|
|
|
Stylesheet::Manager.clear_plugin_cache!(plugin_name)
|
2020-09-01 13:56:40 -04:00
|
|
|
|
targets = []
|
|
|
|
|
if target.present?
|
|
|
|
|
if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, target.to_sym)
|
|
|
|
|
targets.push("#{plugin_name}_#{target.to_s}")
|
2023-01-09 07:10:19 -05:00
|
|
|
|
end
|
2020-09-01 13:56:40 -04:00
|
|
|
|
else
|
|
|
|
|
targets.push(plugin_name)
|
|
|
|
|
end
|
2020-01-08 13:25:36 -05:00
|
|
|
|
message =
|
2021-07-06 13:11:10 -04:00
|
|
|
|
targets.map! { |name| Stylesheet::Manager.new.stylesheet_data(name.to_sym) }.flatten!
|
2019-10-10 01:10:23 -04:00
|
|
|
|
MessageBus.publish "/file-change", message
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
def worker_loop
|
2019-10-10 01:10:23 -04:00
|
|
|
|
path = @queue.pop
|
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
@queue.pop while @queue.length > 0
|
|
|
|
|
|
2019-10-10 01:10:23 -04:00
|
|
|
|
if path[:plugin_name]
|
2020-09-01 13:56:40 -04:00
|
|
|
|
plugin_assets_refresh(path[:plugin_name], path[:target])
|
2019-10-10 01:10:23 -04:00
|
|
|
|
else
|
|
|
|
|
core_assets_refresh(path[:target])
|
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def process_change(paths)
|
|
|
|
|
paths.each { |path| @queue.push path }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|