2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 09:52:52 -05: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-07 23:46:34 -05:00
|
|
|
THEME_REGEX ||= /_theme$/
|
2020-08-03 21:57:10 -05:00
|
|
|
COLOR_SCHEME_STYLESHEET ||= "color_definitions"
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
@@lock = Mutex.new
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
def self.cache
|
|
|
|
@cache ||= DistributedCache.new("discourse_stylesheet")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_theme_cache!
|
2019-10-10 00:10:23 -05:00
|
|
|
cache.hash.keys.select { |k| k =~ /theme/ }.each { |k| cache.delete(k) }
|
|
|
|
end
|
|
|
|
|
2020-08-18 12:02:13 -05:00
|
|
|
def self.clear_color_scheme_cache!
|
|
|
|
cache.hash.keys.select { |k| k =~ /color_definitions/ }.each { |k| cache.delete(k) }
|
|
|
|
end
|
|
|
|
|
2019-10-10 00:10:23 -05: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 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2020-08-18 12:02:13 -05:00
|
|
|
def self.color_scheme_cache_key(color_scheme, theme_id = nil)
|
2020-08-11 15:28:59 -05:00
|
|
|
color_scheme_name = Slug.for(color_scheme.name) + color_scheme&.id.to_s
|
2020-08-18 12:02:13 -05:00
|
|
|
theme_string = theme_id ? "_theme#{theme_id}" : ""
|
|
|
|
"#{COLOR_SCHEME_STYLESHEET}_#{color_scheme_name}#{theme_string}_#{Discourse.current_hostname}"
|
2020-08-11 15:28:59 -05:00
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def self.precompile_css
|
2021-06-30 08:33:15 -05:00
|
|
|
themes = Theme.where('user_selectable OR id = ?', SiteSetting.default_theme_id).pluck(:id, :name, :color_scheme_id)
|
|
|
|
themes << nil
|
2020-11-11 17:17:38 -06: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
|
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
targets = [:desktop, :mobile, :desktop_rtl, :mobile_rtl, :desktop_theme, :mobile_theme, :admin, :wizard]
|
|
|
|
targets += Discourse.find_plugin_css_assets(include_disabled: true, mobile_view: true, desktop_view: true)
|
2020-11-11 17:17:38 -06:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
themes.each do |id, name, color_scheme_id|
|
|
|
|
theme_id = id || SiteSetting.default_theme_id
|
2021-06-15 01:57:17 -05:00
|
|
|
manager = self.new(theme_id: theme_id)
|
2017-04-12 11:46:03 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
targets.each do |target|
|
2021-06-30 08:33:15 -05:00
|
|
|
if target =~ THEME_REGEX
|
|
|
|
next if theme_id == -1
|
2021-06-30 03:06:13 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
scss_checker = ScssChecker.new(target, manager.theme_ids)
|
2021-06-30 03:06:13 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
manager.load_themes(manager.theme_ids).each do |theme|
|
|
|
|
builder = Stylesheet::Manager::Builder.new(
|
|
|
|
target: target, theme: theme, manager: manager
|
|
|
|
)
|
2021-06-30 03:06:13 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
STDERR.puts "precompile target: #{target} #{builder.theme.name}"
|
|
|
|
next if theme.component && !scss_checker.has_scss(theme.id)
|
|
|
|
builder.compile(force: true)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
STDERR.puts "precompile target: #{target} #{name}"
|
2021-06-30 03:06:13 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
Stylesheet::Manager::Builder.new(
|
|
|
|
target: target, theme: manager.get_theme(theme_id), manager: manager
|
|
|
|
).compile(force: true)
|
2021-05-20 01:01:54 -05:00
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2020-08-03 21:57:10 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
theme_color_scheme = ColorScheme.find_by_id(color_scheme_id) || ColorScheme.base
|
2020-08-03 21:57:10 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
[theme_color_scheme, *color_schemes].uniq.each do |scheme|
|
|
|
|
STDERR.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{name} (#{scheme.name})"
|
2020-11-11 17:17:38 -06:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
Stylesheet::Manager::Builder.new(
|
|
|
|
target: COLOR_SCHEME_STYLESHEET,
|
2021-06-30 08:33:15 -05:00
|
|
|
theme: manager.get_theme(theme_id),
|
2021-06-15 01:57:17 -05:00
|
|
|
color_scheme: scheme,
|
|
|
|
manager: manager
|
|
|
|
).compile(force: true)
|
2020-11-11 17:17:38 -06:00
|
|
|
end
|
2020-08-18 12:02:13 -05:00
|
|
|
clear_color_scheme_cache!
|
2020-08-03 21:57:10 -05:00
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05: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 11:30:24 -05:00
|
|
|
globs = ["#{Rails.root}/app/assets/stylesheets/**/*.*css",
|
|
|
|
"#{Rails.root}/app/assets/images/**/*.*"]
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
Discourse.plugins.map { |plugin| File.dirname(plugin.path) }.each do |path|
|
2017-04-12 11:30:24 -05:00
|
|
|
globs << "#{path}/plugin.rb"
|
2021-04-28 11:27:18 -05:00
|
|
|
globs << "#{path}/assets/stylesheets/**/*.*css"
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
globs.map do |pattern|
|
|
|
|
Dir.glob(pattern).map { |x| File.mtime(x) }.max
|
|
|
|
end.compact.max.to_i
|
|
|
|
end
|
|
|
|
|
2018-08-07 23:46:34 -05:00
|
|
|
def self.cache_fullpath
|
|
|
|
"#{Rails.root}/#{CACHE_PATH}"
|
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
attr_reader :theme_ids
|
|
|
|
|
|
|
|
def initialize(theme_id: nil)
|
2021-06-17 21:16:26 -05:00
|
|
|
@theme_id = theme_id
|
2021-06-15 01:57:17 -05:00
|
|
|
@theme_ids = Theme.transform_ids(@theme_id)
|
|
|
|
@themes_cache = {}
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def cache
|
|
|
|
self.class.cache
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def get_theme(theme_id)
|
|
|
|
if theme = @themes_cache[theme_id]
|
|
|
|
theme
|
|
|
|
else
|
|
|
|
load_themes([theme_id]).first
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2021-06-15 01:57:17 -05:00
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def load_themes(theme_ids)
|
|
|
|
themes = []
|
|
|
|
to_load_theme_ids = []
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
theme_ids.each do |theme_id|
|
|
|
|
if @themes_cache[theme_id]
|
|
|
|
themes << @themes_cache[theme_id]
|
|
|
|
else
|
|
|
|
to_load_theme_ids << theme_id
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
Theme
|
|
|
|
.where(id: to_load_theme_ids)
|
|
|
|
.includes(:yaml_theme_fields, :theme_settings, :upload_fields, :builder_theme_fields)
|
|
|
|
.each do |theme|
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
@themes_cache[theme.id] = theme
|
|
|
|
themes << theme
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
themes
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def stylesheet_data(target = :desktop)
|
|
|
|
stylesheet_details(target, "all")
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def stylesheet_link_tag(target = :desktop, media = 'all')
|
|
|
|
stylesheets = stylesheet_details(target, media)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
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
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def stylesheet_details(target = :desktop, media = 'all')
|
|
|
|
target = target.to_sym
|
|
|
|
current_hostname = Discourse.current_hostname
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
array_cache_key = "array_themes_#{@theme_ids.join(",")}_#{target}_#{current_hostname}"
|
2021-06-15 01:57:17 -05:00
|
|
|
stylesheets = cache[array_cache_key]
|
|
|
|
return stylesheets if stylesheets.present?
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
@@lock.synchronize do
|
|
|
|
stylesheets = []
|
|
|
|
stale_theme_ids = []
|
2021-06-30 08:33:15 -05:00
|
|
|
theme_ids = target.to_s =~ THEME_REGEX ? @theme_ids : [@theme_id]
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-21 03:21:11 -05:00
|
|
|
theme_ids.each do |theme_id|
|
2021-06-15 01:57:17 -05:00
|
|
|
cache_key = "path_#{target}_#{theme_id}_#{current_hostname}"
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
if href = cache[cache_key]
|
|
|
|
stylesheets << {
|
|
|
|
target: target,
|
|
|
|
theme_id: theme_id,
|
|
|
|
new_href: href
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stale_theme_ids << theme_id
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
scss_checker = ScssChecker.new(target, stale_theme_ids)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
themes = @theme_id.blank? ? [nil] : load_themes(stale_theme_ids)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
themes.each do |theme|
|
|
|
|
theme_id = theme&.id
|
|
|
|
data = { target: target, theme_id: theme_id }
|
|
|
|
builder = Builder.new(target: target, theme: theme, manager: self)
|
|
|
|
is_theme = builder.is_theme?
|
|
|
|
has_theme = builder.theme.present?
|
2021-06-21 19:27:11 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
if is_theme && !has_theme
|
|
|
|
next
|
|
|
|
else
|
|
|
|
next if is_theme && builder.theme&.component && !scss_checker.has_scss(theme_id)
|
|
|
|
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
|
|
|
href = builder.stylesheet_path(current_hostname)
|
|
|
|
cache.defer_set("path_#{target}_#{theme_id}_#{current_hostname}", href)
|
|
|
|
end
|
2020-09-08 14:00:16 -05:00
|
|
|
|
2021-06-30 08:33:15 -05:00
|
|
|
data[:new_href] = href
|
2021-06-15 01:57:17 -05:00
|
|
|
stylesheets << data
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2021-06-15 01:57:17 -05:00
|
|
|
|
|
|
|
cache.defer_set(array_cache_key, stylesheets.freeze)
|
|
|
|
stylesheets
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def color_scheme_stylesheet_details(color_scheme_id = nil, media)
|
2021-06-17 21:16:26 -05:00
|
|
|
theme_id = @theme_id || SiteSetting.default_theme_id
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
color_scheme = begin
|
|
|
|
ColorScheme.find(color_scheme_id)
|
|
|
|
rescue
|
|
|
|
# don't load fallback when requesting dark color scheme
|
|
|
|
return false if media != "all"
|
2021-04-27 08:33:43 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
get_theme(theme_id)&.color_scheme || ColorScheme.base
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
return false if !color_scheme
|
2018-03-04 18:04:23 -06:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
target = COLOR_SCHEME_STYLESHEET.to_sym
|
|
|
|
current_hostname = Discourse.current_hostname
|
|
|
|
cache_key = self.class.color_scheme_cache_key(color_scheme, theme_id)
|
|
|
|
stylesheets = cache[cache_key]
|
|
|
|
return stylesheets if stylesheets.present?
|
2018-08-30 05:53:03 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
stylesheet = { color_scheme_id: color_scheme.id }
|
2018-08-07 23:46:34 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
theme = get_theme(theme_id)
|
2018-08-07 23:46:34 -05:00
|
|
|
|
2021-06-20 23:44:41 -05:00
|
|
|
builder = Builder.new(
|
|
|
|
target: target,
|
|
|
|
theme: get_theme(theme_id),
|
|
|
|
color_scheme: color_scheme,
|
|
|
|
manager: self
|
|
|
|
)
|
|
|
|
|
|
|
|
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
|
|
|
|
|
|
|
href = builder.stylesheet_path(current_hostname)
|
|
|
|
stylesheet[:new_href] = href
|
|
|
|
cache.defer_set(cache_key, stylesheet.freeze)
|
|
|
|
stylesheet
|
2018-06-13 08:54:26 -05:00
|
|
|
end
|
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
def color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = 'all')
|
|
|
|
stylesheet = color_scheme_stylesheet_details(color_scheme_id, media)
|
2020-08-11 15:28:59 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
return '' if !stylesheet
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
href = stylesheet[:new_href]
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
css_class = media == 'all' ? "light-scheme" : "dark-scheme"
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-06-15 01:57:17 -05:00
|
|
|
%[<link href="#{href}" media="#{media}" rel="stylesheet" class="#{css_class}"/>].html_safe
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
end
|