2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
require_dependency 'distributed_cache'
|
|
|
|
require_dependency 'stylesheet/compiler'
|
|
|
|
|
|
|
|
module Stylesheet; end
|
|
|
|
|
|
|
|
class Stylesheet::Manager
|
|
|
|
|
|
|
|
CACHE_PATH ||= 'tmp/stylesheet-cache'
|
|
|
|
MANIFEST_DIR ||= "#{Rails.root}/tmp/cache/assets/#{Rails.env}"
|
|
|
|
MANIFEST_FULL_PATH ||= "#{MANIFEST_DIR}/stylesheet-manifest"
|
2018-08-08 00:46:34 -04:00
|
|
|
THEME_REGEX ||= /_theme$/
|
2020-08-03 22:57:10 -04:00
|
|
|
COLOR_SCHEME_STYLESHEET ||= "color_definitions"
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
@lock = Mutex.new
|
|
|
|
|
|
|
|
def self.cache
|
|
|
|
@cache ||= DistributedCache.new("discourse_stylesheet")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_theme_cache!
|
2019-10-10 01:10:23 -04:00
|
|
|
cache.hash.keys.select { |k| k =~ /theme/ }.each { |k| cache.delete(k) }
|
|
|
|
end
|
|
|
|
|
2020-08-18 13:02:13 -04:00
|
|
|
def self.clear_color_scheme_cache!
|
|
|
|
cache.hash.keys.select { |k| k =~ /color_definitions/ }.each { |k| cache.delete(k) }
|
|
|
|
end
|
|
|
|
|
2019-10-10 01:10:23 -04:00
|
|
|
def self.clear_core_cache!(targets)
|
|
|
|
cache.hash.keys.select { |k| k =~ /#{targets.join('|')}/ }.each { |k| cache.delete(k) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_plugin_cache!(plugin)
|
|
|
|
cache.hash.keys.select { |k| k =~ /#{plugin}/ }.each { |k| cache.delete(k) }
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.stylesheet_data(target = :desktop, theme_ids = :missing)
|
|
|
|
stylesheet_details(target, "all", theme_ids)
|
2017-04-18 16:48:15 -04:00
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.stylesheet_link_tag(target = :desktop, media = 'all', theme_ids = :missing)
|
|
|
|
stylesheets = stylesheet_details(target, media, theme_ids)
|
|
|
|
stylesheets.map do |stylesheet|
|
|
|
|
href = stylesheet[:new_href]
|
|
|
|
theme_id = stylesheet[:theme_id]
|
|
|
|
data_theme_id = theme_id ? "data-theme-id=\"#{theme_id}\"" : ""
|
|
|
|
%[<link href="#{href}" media="#{media}" rel="stylesheet" data-target="#{target}" #{data_theme_id}/>]
|
|
|
|
end.join("\n").html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stylesheet_details(target = :desktop, media = 'all', theme_ids = :missing)
|
|
|
|
if theme_ids == :missing
|
|
|
|
theme_ids = [SiteSetting.default_theme_id]
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
target = target.to_sym
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
theme_ids = [theme_ids] unless Array === theme_ids
|
|
|
|
theme_ids = [theme_ids.first] unless target =~ THEME_REGEX
|
2021-02-26 12:30:23 -05:00
|
|
|
include_components = !!(target =~ THEME_REGEX)
|
|
|
|
|
|
|
|
theme_ids = Theme.transform_ids(theme_ids, extend: include_components)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-02-19 20:52:20 -05:00
|
|
|
current_hostname = Discourse.current_hostname
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
array_cache_key = "array_themes_#{theme_ids.join(",")}_#{target}_#{current_hostname}"
|
|
|
|
stylesheets = cache[array_cache_key]
|
|
|
|
return stylesheets if stylesheets.present?
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
@lock.synchronize do
|
2018-08-08 00:46:34 -04:00
|
|
|
stylesheets = []
|
|
|
|
theme_ids.each do |theme_id|
|
|
|
|
data = { target: target }
|
|
|
|
cache_key = "path_#{target}_#{theme_id}_#{current_hostname}"
|
|
|
|
href = cache[cache_key]
|
|
|
|
|
|
|
|
unless href
|
|
|
|
builder = self.new(target, theme_id)
|
|
|
|
is_theme = builder.is_theme?
|
|
|
|
has_theme = builder.theme.present?
|
|
|
|
|
|
|
|
if is_theme && !has_theme
|
|
|
|
next
|
|
|
|
else
|
2021-03-01 09:14:25 -05:00
|
|
|
next if builder.theme&.component && !builder.theme&.has_scss(target)
|
2018-08-08 00:46:34 -04:00
|
|
|
data[:theme_id] = builder.theme.id if has_theme && is_theme
|
|
|
|
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
|
|
|
href = builder.stylesheet_path(current_hostname)
|
|
|
|
end
|
|
|
|
cache[cache_key] = href
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
data[:theme_id] = theme_id if theme_id.present? && data[:theme_id].blank?
|
|
|
|
data[:new_href] = href
|
|
|
|
stylesheets << data
|
|
|
|
end
|
|
|
|
cache[array_cache_key] = stylesheets.freeze
|
|
|
|
stylesheets
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 08:49:13 -04:00
|
|
|
def self.color_scheme_stylesheet_details(color_scheme_id = nil, media, theme_id)
|
2020-08-18 13:02:13 -04:00
|
|
|
theme_id = theme_id || SiteSetting.default_theme_id
|
|
|
|
|
2020-08-03 22:57:10 -04:00
|
|
|
color_scheme = begin
|
|
|
|
ColorScheme.find(color_scheme_id)
|
|
|
|
rescue
|
2020-08-06 09:45:37 -04:00
|
|
|
# don't load fallback when requesting dark color scheme
|
|
|
|
return false if media != "all"
|
2020-08-12 08:49:13 -04:00
|
|
|
|
|
|
|
Theme.find_by_id(theme_id)&.color_scheme || ColorScheme.base
|
2020-08-03 22:57:10 -04:00
|
|
|
end
|
|
|
|
|
2020-08-06 09:45:37 -04:00
|
|
|
return false if !color_scheme
|
|
|
|
|
2020-08-03 22:57:10 -04:00
|
|
|
target = COLOR_SCHEME_STYLESHEET.to_sym
|
|
|
|
current_hostname = Discourse.current_hostname
|
2020-08-18 13:02:13 -04:00
|
|
|
cache_key = color_scheme_cache_key(color_scheme, theme_id)
|
2020-08-11 16:28:59 -04:00
|
|
|
stylesheets = cache[cache_key]
|
2020-08-03 22:57:10 -04:00
|
|
|
return stylesheets if stylesheets.present?
|
|
|
|
|
2020-08-11 16:28:59 -04:00
|
|
|
stylesheet = { color_scheme_id: color_scheme&.id }
|
2020-08-03 22:57:10 -04:00
|
|
|
|
2020-08-18 13:02:13 -04:00
|
|
|
builder = self.new(target, theme_id, color_scheme)
|
|
|
|
|
2020-08-03 22:57:10 -04:00
|
|
|
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
2020-08-11 16:28:59 -04:00
|
|
|
|
2020-08-03 22:57:10 -04:00
|
|
|
href = builder.stylesheet_path(current_hostname)
|
|
|
|
stylesheet[:new_href] = href
|
2020-08-11 16:28:59 -04:00
|
|
|
cache[cache_key] = stylesheet.freeze
|
2020-08-03 22:57:10 -04:00
|
|
|
stylesheet
|
|
|
|
end
|
|
|
|
|
2020-08-12 08:49:13 -04:00
|
|
|
def self.color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = 'all', theme_ids = nil)
|
|
|
|
theme_id = theme_ids&.first
|
|
|
|
stylesheet = color_scheme_stylesheet_details(color_scheme_id, media, theme_id)
|
2020-08-06 09:45:37 -04:00
|
|
|
return '' if !stylesheet
|
|
|
|
|
2020-08-03 22:57:10 -04:00
|
|
|
href = stylesheet[:new_href]
|
2020-09-04 16:40:40 -04:00
|
|
|
|
|
|
|
css_class = media == 'all' ? "light-scheme" : "dark-scheme"
|
|
|
|
|
|
|
|
%[<link href="#{href}" media="#{media}" rel="stylesheet" class="#{css_class}"/>].html_safe
|
2020-08-03 22:57:10 -04:00
|
|
|
end
|
|
|
|
|
2020-08-18 13:02:13 -04:00
|
|
|
def self.color_scheme_cache_key(color_scheme, theme_id = nil)
|
2020-08-11 16:28:59 -04:00
|
|
|
color_scheme_name = Slug.for(color_scheme.name) + color_scheme&.id.to_s
|
2020-08-18 13:02:13 -04:00
|
|
|
theme_string = theme_id ? "_theme#{theme_id}" : ""
|
|
|
|
"#{COLOR_SCHEME_STYLESHEET}_#{color_scheme_name}#{theme_string}_#{Discourse.current_hostname}"
|
2020-08-11 16:28:59 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def self.precompile_css
|
2020-08-18 13:02:13 -04:00
|
|
|
themes = Theme.where('user_selectable OR id = ?', SiteSetting.default_theme_id).pluck(:id, :name, :color_scheme_id)
|
2017-04-12 10:52:52 -04:00
|
|
|
themes << nil
|
2020-11-11 18:17:38 -05:00
|
|
|
|
|
|
|
color_schemes = ColorScheme.where(user_selectable: true).to_a
|
|
|
|
color_schemes << ColorScheme.find_by(id: SiteSetting.default_dark_mode_color_scheme_id)
|
|
|
|
color_schemes = color_schemes.compact.uniq
|
|
|
|
|
2020-11-18 04:36:52 -05:00
|
|
|
targets = [:desktop, :mobile, :desktop_rtl, :mobile_rtl, :desktop_theme, :mobile_theme, :admin, :wizard]
|
2020-11-11 18:17:38 -05:00
|
|
|
targets += Discourse.find_plugin_css_assets(include_disabled: true, mobile_view: true, desktop_view: true)
|
|
|
|
|
2020-08-18 13:02:13 -04:00
|
|
|
themes.each do |id, name, color_scheme_id|
|
2020-11-11 18:17:38 -05:00
|
|
|
targets.each do |target|
|
2018-07-12 00:18:21 -04:00
|
|
|
theme_id = id || SiteSetting.default_theme_id
|
2018-08-31 07:23:55 -04:00
|
|
|
next if target =~ THEME_REGEX && theme_id == -1
|
2018-07-12 00:18:21 -04:00
|
|
|
cache_key = "#{target}_#{theme_id}"
|
2017-04-12 12:46:03 -04:00
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
STDERR.puts "precompile target: #{target} #{name}"
|
2018-07-12 00:18:21 -04:00
|
|
|
builder = self.new(target, theme_id)
|
2017-04-12 12:46:03 -04:00
|
|
|
builder.compile(force: true)
|
|
|
|
cache[cache_key] = nil
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2020-08-03 22:57:10 -04:00
|
|
|
|
2020-11-11 18:17:38 -05:00
|
|
|
theme_color_scheme = ColorScheme.find_by_id(color_scheme_id) || ColorScheme.base
|
2020-08-03 22:57:10 -04:00
|
|
|
|
2020-11-11 18:17:38 -05:00
|
|
|
[theme_color_scheme, *color_schemes].uniq.each do |scheme|
|
|
|
|
STDERR.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{name} (#{scheme.name})"
|
|
|
|
|
|
|
|
builder = self.new(COLOR_SCHEME_STYLESHEET, id, scheme)
|
|
|
|
builder.compile(force: true)
|
|
|
|
end
|
2020-08-18 13:02:13 -04:00
|
|
|
clear_color_scheme_cache!
|
2020-08-03 22:57:10 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2017-04-12 12:30:24 -04:00
|
|
|
globs = ["#{Rails.root}/app/assets/stylesheets/**/*.*css",
|
|
|
|
"#{Rails.root}/app/assets/images/**/*.*"]
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
Discourse.plugins.map { |plugin| File.dirname(plugin.path) }.each do |path|
|
2017-04-12 12:30:24 -04:00
|
|
|
globs << "#{path}/plugin.rb"
|
2021-04-28 12:27:18 -04:00
|
|
|
globs << "#{path}/assets/stylesheets/**/*.*css"
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
globs.map do |pattern|
|
|
|
|
Dir.glob(pattern).map { |x| File.mtime(x) }.max
|
|
|
|
end.compact.max.to_i
|
|
|
|
end
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
def self.cache_fullpath
|
|
|
|
"#{Rails.root}/#{CACHE_PATH}"
|
|
|
|
end
|
|
|
|
|
2020-08-03 22:57:10 -04:00
|
|
|
def initialize(target = :desktop, theme_id = nil, color_scheme = nil)
|
2017-04-12 10:52:52 -04:00
|
|
|
@target = target
|
2018-07-12 00:18:21 -04:00
|
|
|
@theme_id = theme_id
|
2020-08-03 22:57:10 -04:00
|
|
|
@color_scheme = color_scheme
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def compile(opts = {})
|
|
|
|
unless opts[:force]
|
|
|
|
if File.exists?(stylesheet_fullpath)
|
|
|
|
unless StylesheetCache.where(target: qualified_target, digest: digest).exists?
|
|
|
|
begin
|
2018-03-28 04:20:08 -04:00
|
|
|
source_map = begin
|
|
|
|
File.read(source_map_fullpath)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
StylesheetCache.add(qualified_target, digest, File.read(stylesheet_fullpath), source_map)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn "Completely unexpected error adding contents of '#{stylesheet_fullpath}' to cache #{e}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rtl = @target.to_s =~ /_rtl$/
|
2021-04-27 09:33:43 -04:00
|
|
|
css, source_map = with_load_paths do |load_paths|
|
2017-04-12 10:52:52 -04:00
|
|
|
Stylesheet::Compiler.compile_asset(
|
|
|
|
@target,
|
|
|
|
rtl: rtl,
|
|
|
|
theme_id: theme&.id,
|
2021-02-02 13:09:41 -05:00
|
|
|
theme_variables: theme&.scss_variables.to_s,
|
2020-08-03 22:57:10 -04:00
|
|
|
source_map_file: source_map_filename,
|
2021-02-02 13:09:41 -05:00
|
|
|
color_scheme_id: @color_scheme&.id,
|
2021-04-27 09:33:43 -04:00
|
|
|
load_paths: load_paths
|
2017-04-12 10:52:52 -04:00
|
|
|
)
|
|
|
|
rescue SassC::SyntaxError => e
|
2019-09-17 05:19:59 -04:00
|
|
|
if Stylesheet::Importer::THEME_TARGETS.include?(@target.to_s)
|
2017-04-19 16:46:28 -04:00
|
|
|
# no special errors for theme, handled in theme editor
|
|
|
|
["", nil]
|
2020-09-09 11:43:34 -04:00
|
|
|
elsif @target.to_s == COLOR_SCHEME_STYLESHEET
|
|
|
|
# log error but do not crash for errors in color definitions SCSS
|
|
|
|
Rails.logger.error "SCSS compilation error: #{e.message}"
|
|
|
|
["", nil]
|
2017-04-19 16:46:28 -04:00
|
|
|
else
|
2019-02-07 09:27:42 -05:00
|
|
|
raise Discourse::ScssError, e.message
|
2017-04-19 16:46:28 -04:00
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(cache_fullpath)
|
|
|
|
|
|
|
|
File.open(stylesheet_fullpath, "w") do |f|
|
|
|
|
f.puts css
|
|
|
|
end
|
|
|
|
|
|
|
|
if source_map.present?
|
|
|
|
File.open(source_map_fullpath, "w") do |f|
|
|
|
|
f.puts source_map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
StylesheetCache.add(qualified_target, digest, css, source_map)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn "Completely unexpected error adding item to cache #{e}"
|
|
|
|
end
|
|
|
|
css
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_fullpath
|
|
|
|
self.class.cache_fullpath
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_fullpath
|
|
|
|
"#{cache_fullpath}/#{stylesheet_filename}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_map_fullpath
|
|
|
|
"#{cache_fullpath}/#{source_map_filename}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_map_filename
|
|
|
|
"#{stylesheet_filename}.map"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_fullpath_no_digest
|
|
|
|
"#{cache_fullpath}/#{stylesheet_filename_no_digest}"
|
|
|
|
end
|
|
|
|
|
2018-02-19 20:52:20 -05:00
|
|
|
def stylesheet_cdnpath(hostname)
|
|
|
|
"#{GlobalSetting.cdn_url}#{stylesheet_relpath}?__ws=#{hostname}"
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-02-19 20:52:20 -05:00
|
|
|
def stylesheet_path(hostname)
|
|
|
|
stylesheet_cdnpath(hostname)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def root_path
|
|
|
|
"#{GlobalSetting.relative_url_root}/"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_relpath
|
|
|
|
"#{root_path}stylesheets/#{stylesheet_filename}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_relpath_no_digest
|
|
|
|
"#{root_path}stylesheets/#{stylesheet_filename_no_digest}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def qualified_target
|
|
|
|
if is_theme?
|
|
|
|
"#{@target}_#{theme.id}"
|
2020-08-03 22:57:10 -04:00
|
|
|
elsif @color_scheme
|
2020-09-08 15:00:16 -04:00
|
|
|
"#{@target}_#{scheme_slug}_#{@color_scheme&.id.to_s}"
|
2017-04-12 10:52:52 -04:00
|
|
|
else
|
|
|
|
scheme_string = theme && theme.color_scheme ? "_#{theme.color_scheme.id}" : ""
|
|
|
|
"#{@target}#{scheme_string}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_filename(with_digest = true)
|
|
|
|
digest_string = "_#{self.digest}" if with_digest
|
|
|
|
"#{qualified_target}#{digest_string}.css"
|
|
|
|
end
|
|
|
|
|
|
|
|
def stylesheet_filename_no_digest
|
|
|
|
stylesheet_filename(_with_digest = false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_theme?
|
2018-08-08 00:46:34 -04:00
|
|
|
!!(@target.to_s =~ THEME_REGEX)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2020-09-08 15:00:16 -04:00
|
|
|
def scheme_slug
|
|
|
|
Slug.for(ActiveSupport::Inflector.transliterate(@color_scheme.name), 'scheme')
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
# digest encodes the things that trigger a recompile
|
|
|
|
def digest
|
|
|
|
@digest ||= begin
|
|
|
|
if is_theme?
|
|
|
|
theme_digest
|
|
|
|
else
|
|
|
|
color_scheme_digest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def theme
|
2018-08-08 00:46:34 -04:00
|
|
|
@theme ||= Theme.find_by(id: @theme_id) || :nil
|
2017-04-12 10:52:52 -04:00
|
|
|
@theme == :nil ? nil : @theme
|
|
|
|
end
|
|
|
|
|
2021-04-27 09:33:43 -04:00
|
|
|
def with_load_paths
|
|
|
|
if theme
|
|
|
|
theme.with_scss_load_paths { |p| yield p }
|
|
|
|
else
|
|
|
|
yield nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def theme_digest
|
|
|
|
if [:mobile_theme, :desktop_theme].include?(@target)
|
2019-04-12 06:36:08 -04:00
|
|
|
scss_digest = theme.resolve_baked_field(:common, :scss)
|
|
|
|
scss_digest += theme.resolve_baked_field(@target.to_s.sub("_theme", ""), :scss)
|
2017-04-12 10:52:52 -04:00
|
|
|
elsif @target == :embedded_theme
|
2019-04-12 06:36:08 -04:00
|
|
|
scss_digest = theme.resolve_baked_field(:common, :embedded_scss)
|
2017-04-12 10:52:52 -04:00
|
|
|
else
|
|
|
|
raise "attempting to look up theme digest for invalid field"
|
|
|
|
end
|
|
|
|
|
2019-04-12 06:36:08 -04:00
|
|
|
Digest::SHA1.hexdigest(scss_digest.to_s + color_scheme_digest.to_s + settings_digest + plugins_digest + uploads_digest)
|
2018-05-31 03:02:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# this protects us from situations where new versions of a plugin removed a file
|
|
|
|
# old instances may still be serving CSS and not aware of the change
|
|
|
|
# so we could end up poisoning the cache with a bad file that can not be removed
|
|
|
|
def plugins_digest
|
|
|
|
assets = []
|
2019-08-20 12:39:52 -04:00
|
|
|
DiscoursePluginRegistry.stylesheets.each { |_, paths| assets += paths.to_a }
|
|
|
|
DiscoursePluginRegistry.mobile_stylesheets.each { |_, paths| assets += paths.to_a }
|
|
|
|
DiscoursePluginRegistry.desktop_stylesheets.each { |_, paths| assets += paths.to_a }
|
2018-05-31 03:02:27 -04:00
|
|
|
Digest::SHA1.hexdigest(assets.sort.join)
|
2018-03-04 19:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def settings_digest
|
2018-08-30 06:53:03 -04:00
|
|
|
theme_ids = Theme.components_for(@theme_id).dup
|
|
|
|
theme_ids << @theme_id
|
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
fields = ThemeField.where(
|
|
|
|
name: "yaml",
|
|
|
|
type_id: ThemeField.types[:yaml],
|
2018-08-30 06:53:03 -04:00
|
|
|
theme_id: theme_ids
|
2018-08-08 00:46:34 -04:00
|
|
|
).pluck(:updated_at)
|
|
|
|
|
2018-08-30 06:53:03 -04:00
|
|
|
settings = ThemeSetting.where(theme_id: theme_ids).pluck(:updated_at)
|
2018-08-08 00:46:34 -04:00
|
|
|
timestamps = fields.concat(settings).map!(&:to_f).sort!.join(",")
|
|
|
|
|
|
|
|
Digest::SHA1.hexdigest(timestamps)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-06-13 09:54:26 -04:00
|
|
|
def uploads_digest
|
|
|
|
Digest::SHA1.hexdigest(ThemeField.joins(:upload).where(id: theme&.all_theme_variables).pluck(:sha1).join(","))
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def color_scheme_digest
|
|
|
|
|
2020-08-11 16:28:59 -04:00
|
|
|
cs = @color_scheme || theme&.color_scheme
|
|
|
|
|
2017-10-04 17:04:29 -04:00
|
|
|
category_updated = Category.where("uploaded_background_id IS NOT NULL").pluck(:updated_at).map(&:to_i).sum
|
2020-10-05 13:40:41 -04:00
|
|
|
fonts = "#{SiteSetting.base_font}-#{SiteSetting.heading_font}"
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
if cs || category_updated > 0
|
2020-08-18 13:02:13 -04:00
|
|
|
theme_color_defs = theme&.resolve_baked_field(:common, :color_definitions)
|
2020-10-05 13:40:41 -04:00
|
|
|
Digest::SHA1.hexdigest "#{RailsMultisite::ConnectionManagement.current_db}-#{cs&.id}-#{cs&.version}-#{theme_color_defs}-#{Stylesheet::Manager.last_file_updated}-#{category_updated}-#{fonts}"
|
2017-04-12 10:52:52 -04:00
|
|
|
else
|
2020-10-05 13:40:41 -04:00
|
|
|
digest_string = "defaults-#{Stylesheet::Manager.last_file_updated}-#{fonts}"
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
if cdn_url = GlobalSetting.cdn_url
|
|
|
|
digest_string = "#{digest_string}-#{cdn_url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
Digest::SHA1.hexdigest digest_string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|