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
|
2019-02-23 04:53:09 +08:00
|
|
|
REDIS_KEY = "dev_last_used_theme_id"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-07-12 12:18:21 +08:00
|
|
|
def self.theme_id=(v)
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.set(REDIS_KEY, v)
|
2018-05-16 15:25:49 +08:00
|
|
|
end
|
|
|
|
|
2018-07-12 12:18:21 +08:00
|
|
|
def self.theme_id
|
2019-12-03 17:05:53 +08:00
|
|
|
(Discourse.redis.get(REDIS_KEY) || SiteSetting.default_theme_id).to_i
|
2018-05-16 15:25:49 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.watch(paths = nil)
|
2017-04-12 22:52:52 +08:00
|
|
|
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"]
|
|
|
|
Discourse.plugins.each do |p|
|
|
|
|
@default_paths << File.dirname(p.path).sub(Rails.root.to_s, '').sub(/^\//, '')
|
|
|
|
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
|
|
|
|
STDERR.puts "CSS change notifier crashed #{e}"
|
2019-10-05 01:20:51 +08:00
|
|
|
start
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
root = Rails.root.to_s
|
2017-08-17 00:59:21 +08:00
|
|
|
|
|
|
|
listener_opts = { ignore: /xxxx/ }
|
|
|
|
listener_opts[:force_polling] = true if ENV['FORCE_POLLING']
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@paths.each do |watch|
|
|
|
|
Thread.new do
|
|
|
|
begin
|
2019-10-10 13:10:23 +08:00
|
|
|
plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file|
|
2020-03-15 19:01:24 +08:00
|
|
|
if File.symlink?(file)
|
|
|
|
File.expand_path(File.readlink(file), "#{Rails.root}/plugins")
|
|
|
|
else
|
|
|
|
file
|
|
|
|
end
|
2019-10-10 13:10:23 +08:00
|
|
|
end.compact
|
|
|
|
|
2017-08-17 00:59:21 +08:00
|
|
|
listener = Listen.to("#{root}/#{watch}", listener_opts) do |modified, added, _|
|
2017-04-12 22:52:52 +08:00
|
|
|
paths = [modified, added].flatten
|
|
|
|
paths.compact!
|
2019-10-10 13:10:23 +08:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
target = nil
|
|
|
|
if !plugin_name
|
2020-04-09 00:52:36 +08:00
|
|
|
target_match = long.match(/admin|desktop|mobile|publish/)
|
2019-10-10 13:10:23 +08:00
|
|
|
if target_match&.length
|
|
|
|
target = target_match[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
basename: File.basename(long),
|
|
|
|
target: target,
|
|
|
|
plugin_name: plugin_name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
process_change(paths)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
STDERR.puts "Failed to listen for CSS changes at: #{watch}\n#{e}"
|
|
|
|
end
|
2017-04-13 22:26:07 +08:00
|
|
|
listener.start
|
|
|
|
sleep
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
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|
|
|
|
|
Stylesheet::Manager.stylesheet_data(name.to_sym, Stylesheet::Watcher.theme_id)
|
|
|
|
end.flatten!
|
|
|
|
MessageBus.publish '/file-change', message
|
|
|
|
end
|
|
|
|
|
|
|
|
def plugin_assets_refresh(plugin_name)
|
|
|
|
Stylesheet::Manager.clear_plugin_cache!(plugin_name)
|
2020-01-09 02:25:36 +08:00
|
|
|
targets = [plugin_name]
|
|
|
|
targets.push("#{plugin_name}_mobile") if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, :mobile)
|
|
|
|
targets.push("#{plugin_name}_desktop") if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, :desktop)
|
|
|
|
|
|
|
|
message = targets.map! do |name|
|
|
|
|
Stylesheet::Manager.stylesheet_data(name.to_sym, Stylesheet::Watcher.theme_id)
|
|
|
|
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]
|
|
|
|
plugin_assets_refresh(path[:plugin_name])
|
|
|
|
else
|
|
|
|
core_assets_refresh(path[:target])
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_change(paths)
|
|
|
|
paths.each do |path|
|
2019-10-10 13:10:23 +08:00
|
|
|
if path[:basename] =~ /\.(css|scss)$/
|
2017-04-12 22:52:52 +08:00
|
|
|
@queue.push path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|