Class: Autospec::ReloadCss

Inherits:
Object
  • Object
show all
Defined in:
lib/autospec/reload_css.rb

Constant Summary collapse

WATCHERS =
{}

Class Method Summary collapse

Class Method Details

.message_busObject



19
20
21
22
23
24
25
26
27
# File 'lib/autospec/reload_css.rb', line 19

def self.message_bus
  MessageBus::Instance.new.tap do |bus|
    bus.site_id_lookup do
      # this is going to be dev the majority of the time
      # if you have multisite configured in dev stuff may be different
      "default"
    end
  end
end

.run_on_change(paths) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/autospec/reload_css.rb', line 29

def self.run_on_change(paths)
  if paths.any? { |p| p =~ /\.(css|s[ac]ss)/ }
    # todo connect to dev instead?
    ActiveRecord::Base.establish_connection
    %i[desktop mobile].each do |style|
      s = DiscourseStylesheets.new(style)
      s.compile
      paths << "public" + s.stylesheet_relpath_no_digest
    end
    ActiveRecord::Base.clear_active_connections!
  end
  paths.map! do |p|
    hash = nil
    fullpath = "#{Rails.root}/#{p}"
    hash = Digest::MD5.hexdigest(File.read(fullpath)) if File.exist?(fullpath)
    p = p.sub(/\.sass\.erb/, "")
    p = p.sub(/\.sass/, "")
    p = p.sub(/\.scss/, "")
    p = p.sub(%r{\Aapp/assets/stylesheets}, "assets")
    { name: p, hash: hash || SecureRandom.hex }
  end
  message_bus.publish "/file-change", paths
end

.watch(pattern, &blk) ⇒ Object



8
9
10
# File 'lib/autospec/reload_css.rb', line 8

def self.watch(pattern, &blk)
  WATCHERS[pattern] = blk
end