discourse/lib/autospec/reload_css.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Autospec; end
2013-11-02 06:57:50 +08:00
class Autospec::ReloadCss
2013-11-02 06:57:50 +08:00
WATCHERS = {}
def self.watch(pattern, &blk)
2013-11-02 06:57:50 +08:00
WATCHERS[pattern] = blk
end
2013-11-02 06:57:50 +08:00
# css, scss, sass or handlebars
watch(/\.css$/)
2013-11-02 06:57:50 +08:00
watch(/\.ca?ss\.erb$/)
watch(/\.s[ac]ss$/)
watch(/\.hbs$/)
watch(/\.hbr$/)
def self.message_bus
MessageBus::Instance.new.tap do |bus|
bus.site_id_lookup do
2013-11-02 06:57:50 +08:00
# 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
def self.run_on_change(paths)
if paths.any? { |p| p =~ /\.(css|s[ac]ss)/ }
2015-02-27 14:19:30 +08:00
# todo connect to dev instead?
ActiveRecord::Base.establish_connection
2015-06-22 12:35:11 +08:00
[:desktop, :mobile].each do |style|
s = DiscourseStylesheets.new(style)
s.compile
paths << "public" + s.stylesheet_relpath_no_digest
end
2015-02-27 14:19:30 +08:00
ActiveRecord::Base.clear_active_connections!
end
paths.map! do |p|
hash = nil
2013-11-02 06:57:50 +08:00
fullpath = "#{Rails.root}/#{p}"
hash = Digest::MD5.hexdigest(File.read(fullpath)) if File.exists?(fullpath)
2015-02-27 14:19:30 +08:00
p = p.sub(/\.sass\.erb/, "")
p = p.sub(/\.sass/, "")
p = p.sub(/\.scss/, "")
p = p.sub(/^app\/assets\/stylesheets/, "assets")
2015-06-22 12:35:11 +08:00
{ name: p, hash: hash || SecureRandom.hex }
end
message_bus.publish "/file-change", paths
end
end