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
|
2018-05-16 03:25:49 -04:00
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
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)
|
|
|
|
|
@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 11:06:27 -04:00
|
|
|
|
end
|
|
|
|
|
@default_paths
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
def start
|
|
|
|
|
Thread.new do
|
|
|
|
|
begin
|
|
|
|
|
while true
|
|
|
|
|
worker_loop
|
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
2021-03-12 16:18:00 -05:00
|
|
|
|
listener_opts = { ignore: /xxxx/, only: /\.(css|scss)$/ }
|
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
|
|
|
|
|
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 01:10:23 -04:00
|
|
|
|
end
|
2021-03-12 16:18:00 -05:00
|
|
|
|
end
|
2019-10-10 01:10:23 -04:00
|
|
|
|
|
2021-03-12 16:18:00 -05:00
|
|
|
|
target = nil
|
|
|
|
|
target_match = long.match(/admin|desktop|mobile|publish/)
|
|
|
|
|
if target_match&.length
|
|
|
|
|
target = target_match[0]
|
2019-10-10 01:10:23 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-12 16:18:00 -05:00
|
|
|
|
{
|
|
|
|
|
basename: File.basename(long),
|
|
|
|
|
target: target,
|
|
|
|
|
plugin_name: plugin_name
|
|
|
|
|
}
|
2017-04-12 10:52:52 -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)
|
|
|
|
|
targets = target ? [target] : ["desktop", "mobile", "admin"]
|
|
|
|
|
Stylesheet::Manager.clear_core_cache!(targets)
|
|
|
|
|
message = targets.map! do |name|
|
2020-07-30 19:03:24 -04:00
|
|
|
|
msgs = []
|
|
|
|
|
active_themes.each do |theme_id|
|
2021-06-21 03:48:30 -04:00
|
|
|
|
msgs << Stylesheet::Manager.new(theme_id: theme_id).stylesheet_data(name.to_sym)
|
2020-07-30 19:03:24 -04:00
|
|
|
|
end
|
|
|
|
|
msgs
|
2019-10-10 01:10:23 -04:00
|
|
|
|
end.flatten!
|
|
|
|
|
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?
|
|
|
|
|
targets.push("#{plugin_name}_#{target.to_s}") if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, target.to_sym)
|
|
|
|
|
else
|
|
|
|
|
targets.push(plugin_name)
|
|
|
|
|
end
|
2020-01-08 13:25:36 -05:00
|
|
|
|
message = targets.map! do |name|
|
2020-07-30 19:03:24 -04:00
|
|
|
|
msgs = []
|
|
|
|
|
active_themes.each do |theme_id|
|
2021-06-21 03:48:30 -04:00
|
|
|
|
msgs << Stylesheet::Manager.new(theme_id: theme_id).stylesheet_data(name.to_sym)
|
2020-07-30 19:03:24 -04:00
|
|
|
|
end
|
2020-08-04 14:10:25 -04:00
|
|
|
|
msgs
|
2020-01-08 13:25:36 -05:00
|
|
|
|
end.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
|
|
|
|
while @queue.length > 0
|
|
|
|
|
@queue.pop
|
|
|
|
|
end
|
|
|
|
|
|
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 do |path|
|
2021-03-12 16:18:00 -05:00
|
|
|
|
@queue.push path
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2020-07-30 19:03:24 -04:00
|
|
|
|
|
|
|
|
|
def active_themes
|
|
|
|
|
@active_themes ||= Theme.user_selectable.pluck(:id)
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
end
|
|
|
|
|
end
|