2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-07-09 22:16:56 -04:00
|
|
|
module Autospec
|
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
|
2013-07-09 22:16:56 -04:00
|
|
|
class Autospec::ReloadCss
|
2013-11-01 18:57:50 -04:00
|
|
|
WATCHERS = {}
|
2013-07-09 22:16:56 -04:00
|
|
|
def self.watch(pattern, &blk)
|
2013-11-01 18:57:50 -04:00
|
|
|
WATCHERS[pattern] = blk
|
2013-07-09 22:16:56 -04:00
|
|
|
end
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
# css, scss, sass or handlebars
|
2013-07-09 22:16:56 -04:00
|
|
|
watch(/\.css$/)
|
2013-11-01 18:57:50 -04:00
|
|
|
watch(/\.ca?ss\.erb$/)
|
|
|
|
watch(/\.s[ac]ss$/)
|
2014-09-26 15:23:15 -04:00
|
|
|
watch(/\.hbs$/)
|
2020-02-11 14:38:12 -05:00
|
|
|
watch(/\.hbr$/)
|
2013-07-09 22:16:56 -04:00
|
|
|
|
|
|
|
def self.message_bus
|
|
|
|
MessageBus::Instance.new.tap do |bus|
|
|
|
|
bus.site_id_lookup do
|
2013-11-01 18:57:50 -04:00
|
|
|
# this is going to be dev the majority of the time
|
|
|
|
# if you have multisite configured in dev stuff may be different
|
2013-07-09 22:16:56 -04:00
|
|
|
"default"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run_on_change(paths)
|
2014-05-02 17:46:03 -04:00
|
|
|
if paths.any? { |p| p =~ /\.(css|s[ac]ss)/ }
|
2015-02-27 01:19:30 -05:00
|
|
|
# todo connect to dev instead?
|
|
|
|
ActiveRecord::Base.establish_connection
|
2015-06-22 00:35:11 -04:00
|
|
|
%i[desktop mobile].each do |style|
|
|
|
|
s = DiscourseStylesheets.new(style)
|
|
|
|
s.compile
|
|
|
|
paths << "public" + s.stylesheet_relpath_no_digest
|
|
|
|
end
|
2015-02-27 01:19:30 -05:00
|
|
|
ActiveRecord::Base.clear_active_connections!
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
2013-07-09 22:16:56 -04:00
|
|
|
paths.map! do |p|
|
|
|
|
hash = nil
|
2013-11-01 18:57:50 -04:00
|
|
|
fullpath = "#{Rails.root}/#{p}"
|
2022-01-05 12:45:08 -05:00
|
|
|
hash = Digest::MD5.hexdigest(File.read(fullpath)) if File.exist?(fullpath)
|
2015-02-27 01:19:30 -05:00
|
|
|
p = p.sub(/\.sass\.erb/, "")
|
|
|
|
p = p.sub(/\.sass/, "")
|
|
|
|
p = p.sub(/\.scss/, "")
|
|
|
|
p = p.sub(%r{^app/assets/stylesheets}, "assets")
|
2015-06-22 00:35:11 -04:00
|
|
|
{ name: p, hash: hash || SecureRandom.hex }
|
2013-07-09 22:16:56 -04:00
|
|
|
end
|
|
|
|
message_bus.publish "/file-change", paths
|
|
|
|
end
|
|
|
|
end
|