2014-05-02 17:46:03 -04:00
|
|
|
require_dependency 'sass/discourse_sass_compiler'
|
|
|
|
|
|
|
|
class DiscourseStylesheets
|
|
|
|
|
|
|
|
CACHE_PATH = 'uploads/stylesheet-cache'
|
2014-05-29 15:42:50 -04:00
|
|
|
MANIFEST_DIR = "#{Rails.root}/tmp/cache/assets/#{Rails.env}"
|
|
|
|
MANIFEST_FULL_PATH = "#{MANIFEST_DIR}/stylesheet-manifest"
|
2014-05-02 17:46:03 -04:00
|
|
|
|
|
|
|
@lock = Mutex.new
|
2014-11-10 23:32:44 -05:00
|
|
|
@links = {}
|
|
|
|
|
|
|
|
def self.ensure_subscribed!
|
|
|
|
unless @subscribed
|
|
|
|
@lock.synchronize do
|
|
|
|
MessageBus.subscribe("/discourse_stylesheet") do |message|
|
|
|
|
@lock.synchronize do
|
|
|
|
@links[message.site_id] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@subscribed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-02 17:46:03 -04:00
|
|
|
|
|
|
|
def self.stylesheet_link_tag(target = :desktop)
|
2014-11-10 23:32:44 -05:00
|
|
|
ensure_subscribed!
|
2014-05-02 17:46:03 -04:00
|
|
|
@lock.synchronize do
|
2014-11-10 23:32:44 -05:00
|
|
|
links = (@links[RailsMultisite::ConnectionManagement.current_db] ||= {})
|
|
|
|
tag = links[target]
|
|
|
|
|
|
|
|
if !tag
|
|
|
|
builder = self.new(target)
|
|
|
|
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
|
|
|
builder.ensure_digestless_file
|
|
|
|
tag = %[<link href="#{Rails.env.production? ? builder.stylesheet_relpath : builder.stylesheet_relpath_no_digest + '?body=1'}" media="all" rel="stylesheet" />].html_safe
|
|
|
|
|
|
|
|
links[target] = tag
|
|
|
|
end
|
|
|
|
|
|
|
|
tag
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-29 15:42:50 -04:00
|
|
|
def self.compile(target = :desktop, opts={})
|
2014-05-02 17:46:03 -04:00
|
|
|
@lock.synchronize do
|
2014-05-29 15:42:50 -04:00
|
|
|
FileUtils.rm(MANIFEST_FULL_PATH, force: true) if opts[:force] # Force a recompile, even in production env
|
2014-05-02 17:46:03 -04:00
|
|
|
builder = self.new(target)
|
|
|
|
builder.compile
|
|
|
|
builder.stylesheet_filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-29 15:42:50 -04:00
|
|
|
def self.last_file_updated
|
|
|
|
if Rails.env.production?
|
|
|
|
@last_file_updated ||= if File.exists?(MANIFEST_FULL_PATH)
|
|
|
|
File.readlines(MANIFEST_FULL_PATH, 'r')[0]
|
|
|
|
else
|
|
|
|
mtime = max_file_mtime
|
|
|
|
FileUtils.mkdir_p(MANIFEST_DIR)
|
|
|
|
File.open(MANIFEST_FULL_PATH, "w") { |f| f.print(mtime) }
|
|
|
|
mtime
|
|
|
|
end
|
|
|
|
else
|
|
|
|
max_file_mtime
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.max_file_mtime
|
2014-06-03 16:46:32 -04:00
|
|
|
[ "#{Rails.root}/app/assets/stylesheets/**/*.*css",
|
|
|
|
"#{Rails.root}/plugins/**/*.*css",
|
|
|
|
"#{Rails.root}/plugins/**/plugin.rb" ].map do |pattern|
|
|
|
|
Dir.glob(pattern).map { |x| File.mtime(x) }.max
|
|
|
|
end.compact.max.to_i
|
2014-05-29 15:42:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
|
|
|
|
def initialize(target = :desktop)
|
|
|
|
@target = target
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile
|
|
|
|
scss = File.read("#{Rails.root}/app/assets/stylesheets/#{@target}.scss")
|
|
|
|
css = begin
|
|
|
|
DiscourseSassCompiler.compile(scss, @target)
|
|
|
|
rescue Sass::SyntaxError => e
|
|
|
|
Rails.logger.error "Stylesheet failed to compile for '#{@target}'! Recompiling without plugins and theming."
|
|
|
|
Rails.logger.error e.sass_backtrace_str("#{@target} stylesheet")
|
|
|
|
DiscourseSassCompiler.compile(scss + DiscourseSassCompiler.error_as_css(e, "#{@target} stylesheet"), @target, safe: true)
|
|
|
|
end
|
|
|
|
FileUtils.mkdir_p(cache_fullpath)
|
|
|
|
File.open(stylesheet_fullpath, "w") do |f|
|
|
|
|
f.puts css
|
|
|
|
end
|
|
|
|
css
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_digestless_file
|
2014-05-27 16:08:47 -04:00
|
|
|
# file without digest is only for auto-reloading css in dev env
|
|
|
|
unless Rails.env.production? || (File.exist?(stylesheet_fullpath_no_digest) && File.mtime(stylesheet_fullpath) == File.mtime(stylesheet_fullpath_no_digest))
|
2014-05-02 17:46:03 -04:00
|
|
|
FileUtils.cp(stylesheet_fullpath, stylesheet_fullpath_no_digest)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_fullpath
|
|
|
|
"#{Rails.root}/public/#{CACHE_PATH}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_fullpath
|
|
|
|
"#{cache_fullpath}/#{stylesheet_filename}"
|
|
|
|
end
|
|
|
|
def stylesheet_fullpath_no_digest
|
|
|
|
"#{cache_fullpath}/#{stylesheet_filename_no_digest}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_relpath
|
|
|
|
"/#{CACHE_PATH}/#{stylesheet_filename}"
|
|
|
|
end
|
|
|
|
def stylesheet_relpath_no_digest
|
|
|
|
"/#{CACHE_PATH}/#{stylesheet_filename_no_digest}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_filename
|
|
|
|
"#{@target}_#{digest}.css"
|
|
|
|
end
|
|
|
|
def stylesheet_filename_no_digest
|
|
|
|
"#{@target}.css"
|
|
|
|
end
|
|
|
|
|
2014-05-29 15:42:50 -04:00
|
|
|
# digest encodes the things that trigger a recompile
|
2014-05-02 17:46:03 -04:00
|
|
|
def digest
|
|
|
|
@digest ||= begin
|
|
|
|
theme = (cs = ColorScheme.enabled) ? "#{cs.id}-#{cs.version}" : 0
|
2014-06-27 17:06:59 -04:00
|
|
|
category_updated = Category.last_updated_at
|
|
|
|
Digest::SHA1.hexdigest("#{RailsMultisite::ConnectionManagement.current_db}-#{theme}-#{DiscourseStylesheets.last_file_updated}-#{category_updated}")
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|