Class: Stylesheet::Watcher
- Inherits:
-
Object
- Object
- Stylesheet::Watcher
- Defined in:
- lib/stylesheet/watcher.rb
Class Method Summary collapse
Instance Method Summary collapse
- #core_assets_refresh(target) ⇒ Object
-
#initialize(paths) ⇒ Watcher
constructor
A new instance of Watcher.
- #plugin_assets_refresh(plugin_name, target) ⇒ Object
- #process_change(paths) ⇒ Object
- #start ⇒ Object
- #worker_loop ⇒ Object
Constructor Details
#initialize(paths) ⇒ Watcher
Returns a new instance of Watcher.
13 14 15 16 |
# File 'lib/stylesheet/watcher.rb', line 13 def initialize(paths) @paths = paths || Watcher.default_paths @queue = Queue.new end |
Class Method Details
.default_paths ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/stylesheet/watcher.rb', line 18 def self.default_paths return @default_paths if @default_paths @default_paths = ["app/assets/stylesheets"] Discourse.plugins.each do |plugin| if plugin.path.to_s.include?(Rails.root.to_s) path = File.dirname(plugin.path).sub(Rails.root.to_s, "").sub(%r{\A/}, "") path << "/assets/stylesheets" @default_paths << path if File.exist?(path) 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 end @default_paths end |
.watch(paths = nil) ⇒ Object
7 8 9 10 11 |
# File 'lib/stylesheet/watcher.rb', line 7 def self.watch(paths = nil) watcher = new(paths) watcher.start watcher end |
Instance Method Details
#core_assets_refresh(target) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/stylesheet/watcher.rb', line 94 def core_assets_refresh(target) if target&.match(/wcag|color_definitions/) Stylesheet::Manager.clear_color_scheme_cache! return end targets = target ? [target] : %w[desktop mobile admin] Stylesheet::Manager.clear_core_cache!(targets) = targets.map! { |name| Stylesheet::Manager.new.stylesheet_data(name.to_sym) }.flatten! MessageBus.publish "/file-change", end |
#plugin_assets_refresh(plugin_name, target) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/stylesheet/watcher.rb', line 107 def plugin_assets_refresh(plugin_name, target) Stylesheet::Manager.clear_plugin_cache!(plugin_name) targets = [] if target.present? if DiscoursePluginRegistry.stylesheets_exists?(plugin_name, target.to_sym) targets.push("#{plugin_name}_#{target.to_s}") end else targets.push(plugin_name) end = targets.map! { |name| Stylesheet::Manager.new.stylesheet_data(name.to_sym) }.flatten! MessageBus.publish "/file-change", end |
#process_change(paths) ⇒ Object
134 135 136 |
# File 'lib/stylesheet/watcher.rb', line 134 def process_change(paths) paths.each { |path| @queue.push path } end |
#start ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stylesheet/watcher.rb', line 36 def start Thread.new do begin worker_loop while true rescue => e STDERR.puts "CSS change notifier crashed \n#{e}" start end end listener_opts = { ignore: [/node_modules/], only: /\.s?css\z/ } listener_opts[:force_polling] = true if ENV["FORCE_POLLING"] Thread.new do begin plugins_paths = Dir .glob("#{Rails.root}/plugins/*") .map do |file| if File.symlink?(file) File.(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 end end target = nil target_match = long.match(/admin|desktop|mobile|publish|wizard|wcag|color_definitions/) target = target_match[0] if target_match&.length { basename: File.basename(long), target: target, plugin_name: plugin_name } end process_change(paths) end rescue => e STDERR.puts "Failed to listen for CSS changes: \n#{e}" end listener.start sleep end end |
#worker_loop ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/stylesheet/watcher.rb', line 122 def worker_loop path = @queue.pop @queue.pop while @queue.length > 0 if path[:plugin_name] plugin_assets_refresh(path[:plugin_name], path[:target]) else core_assets_refresh(path[:target]) end end |