2017-04-12 09:52:52 -05:00
|
|
|
require 'listen'
|
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
class Watcher
|
2019-02-22 14:53:09 -06:00
|
|
|
REDIS_KEY = "dev_last_used_theme_id"
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2018-07-11 23:18:21 -05:00
|
|
|
def self.theme_id=(v)
|
2019-02-22 14:53:09 -06:00
|
|
|
$redis.set(REDIS_KEY, v)
|
2018-05-16 02:25:49 -05:00
|
|
|
end
|
|
|
|
|
2018-07-11 23:18:21 -05:00
|
|
|
def self.theme_id
|
2019-02-22 14:53:09 -06:00
|
|
|
($redis.get(REDIS_KEY) || SiteSetting.default_theme_id).to_i
|
2018-05-16 02:25:49 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.watch(paths = nil)
|
2017-04-12 09:52:52 -05:00
|
|
|
watcher = new(paths)
|
|
|
|
watcher.start
|
|
|
|
watcher
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(paths)
|
2017-08-09 10:06:27 -05:00
|
|
|
@paths = paths || Watcher.default_paths
|
2017-04-12 09:52:52 -05:00
|
|
|
@queue = Queue.new
|
|
|
|
end
|
|
|
|
|
2017-08-09 10:06:27 -05: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 09:52:52 -05:00
|
|
|
def start
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
begin
|
|
|
|
while true
|
|
|
|
worker_loop
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
STDERR.puts "CSS change notifier crashed #{e}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
root = Rails.root.to_s
|
2017-08-16 11:59:21 -05:00
|
|
|
|
|
|
|
listener_opts = { ignore: /xxxx/ }
|
|
|
|
listener_opts[:force_polling] = true if ENV['FORCE_POLLING']
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
@paths.each do |watch|
|
|
|
|
Thread.new do
|
|
|
|
begin
|
2017-08-16 11:59:21 -05:00
|
|
|
listener = Listen.to("#{root}/#{watch}", listener_opts) do |modified, added, _|
|
2017-04-12 09:52:52 -05:00
|
|
|
paths = [modified, added].flatten
|
|
|
|
paths.compact!
|
2017-07-27 20:20:09 -05:00
|
|
|
paths.map! { |long| long[(root.length + 1)..-1] }
|
2017-04-12 09:52:52 -05:00
|
|
|
process_change(paths)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
STDERR.puts "Failed to listen for CSS changes at: #{watch}\n#{e}"
|
|
|
|
end
|
2017-04-13 09:26:07 -05:00
|
|
|
listener.start
|
|
|
|
sleep
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def worker_loop
|
|
|
|
@queue.pop
|
|
|
|
while @queue.length > 0
|
|
|
|
@queue.pop
|
|
|
|
end
|
|
|
|
|
2017-04-18 15:48:15 -05:00
|
|
|
Stylesheet::Manager.cache.clear
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
message = ["desktop", "mobile", "admin"].map do |name|
|
2018-08-07 23:46:34 -05:00
|
|
|
Stylesheet::Manager.stylesheet_data(name.to_sym, Stylesheet::Watcher.theme_id)
|
|
|
|
end.flatten
|
2017-04-12 09:52:52 -05:00
|
|
|
MessageBus.publish '/file-change', message
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_change(paths)
|
|
|
|
paths.each do |path|
|
|
|
|
if path =~ /\.(css|scss)$/
|
|
|
|
@queue.push path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|