Class: Stylesheet::Manager
- Inherits:
-
Object
- Object
- Stylesheet::Manager
- Defined in:
- lib/stylesheet/manager.rb
Defined Under Namespace
Classes: Builder, ScssChecker
Constant Summary collapse
- BASE_COMPILER_VERSION =
1
- @@lock =
Mutex.new
Instance Attribute Summary collapse
-
#theme_ids ⇒ Object
readonly
Returns the value of attribute theme_ids.
Class Method Summary collapse
- .cache ⇒ Object
- .cache_fullpath ⇒ Object
- .clear_color_scheme_cache! ⇒ Object
- .clear_core_cache!(targets) ⇒ Object
- .clear_plugin_cache!(plugin) ⇒ Object
- .clear_theme_cache! ⇒ Object
- .color_scheme_cache_key(color_scheme, theme_id = nil) ⇒ Object
- .fs_asset_cachebuster ⇒ Object
- .precompile_css ⇒ Object
- .precompile_theme_css ⇒ Object
- .recalculate_fs_asset_cachebuster! ⇒ Object
Instance Method Summary collapse
- #cache ⇒ Object
- #color_scheme_stylesheet_details(color_scheme_id = nil, media) ⇒ Object
- #color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = "all", preload_callback = nil) ⇒ Object
- #color_scheme_stylesheet_preload_tag(color_scheme_id = nil, media = "all") ⇒ Object
- #get_theme(theme_id) ⇒ Object
-
#initialize(theme_id: nil) ⇒ Manager
constructor
A new instance of Manager.
- #load_themes(theme_ids) ⇒ Object
- #stylesheet_data(target = :desktop) ⇒ Object
- #stylesheet_details(target = :desktop, media = "all") ⇒ Object
- #stylesheet_link_tag(target = :desktop, media = "all", preload_callback = nil) ⇒ Object
- #stylesheet_preload_tag(target = :desktop, media = "all") ⇒ Object
Constructor Details
#initialize(theme_id: nil) ⇒ Manager
Returns a new instance of Manager.
196 197 198 199 200 |
# File 'lib/stylesheet/manager.rb', line 196 def initialize(theme_id: nil) @theme_id = theme_id @theme_ids = Theme.transform_ids(@theme_id) @themes_cache = {} end |
Instance Attribute Details
#theme_ids ⇒ Object (readonly)
Returns the value of attribute theme_ids.
194 195 196 |
# File 'lib/stylesheet/manager.rb', line 194 def theme_ids @theme_ids end |
Class Method Details
.cache ⇒ Object
19 20 21 |
# File 'lib/stylesheet/manager.rb', line 19 def self.cache @cache ||= DistributedCache.new("discourse_stylesheet") end |
.cache_fullpath ⇒ Object
188 189 190 191 192 |
# File 'lib/stylesheet/manager.rb', line 188 def self.cache_fullpath path = "#{Rails.root}/#{CACHE_PATH}" return path if !Rails.env.test? File.join(path, "test_#{ENV["TEST_ENV_NUMBER"].presence || "0"}") end |
.clear_color_scheme_cache! ⇒ Object
27 28 29 |
# File 'lib/stylesheet/manager.rb', line 27 def self.clear_color_scheme_cache! cache.hash.keys.select { |k| k =~ /color_definitions/ }.each { |k| cache.delete(k) } end |
.clear_core_cache!(targets) ⇒ Object
31 32 33 |
# File 'lib/stylesheet/manager.rb', line 31 def self.clear_core_cache!(targets) cache.hash.keys.select { |k| k =~ /#{targets.join("|")}/ }.each { |k| cache.delete(k) } end |
.clear_plugin_cache!(plugin) ⇒ Object
35 36 37 |
# File 'lib/stylesheet/manager.rb', line 35 def self.clear_plugin_cache!(plugin) cache.hash.keys.select { |k| k =~ /#{plugin}/ }.each { |k| cache.delete(k) } end |
.clear_theme_cache! ⇒ Object
23 24 25 |
# File 'lib/stylesheet/manager.rb', line 23 def self.clear_theme_cache! cache.hash.keys.select { |k| k =~ /theme/ }.each { |k| cache.delete(k) } end |
.color_scheme_cache_key(color_scheme, theme_id = nil) ⇒ Object
39 40 41 42 43 |
# File 'lib/stylesheet/manager.rb', line 39 def self.color_scheme_cache_key(color_scheme, theme_id = nil) color_scheme_name = Slug.for(color_scheme.name) + color_scheme&.id.to_s theme_string = theme_id ? "_theme#{theme_id}" : "" "#{COLOR_SCHEME_STYLESHEET}_#{color_scheme_name}_#{theme_string}_#{Discourse.current_hostname}" end |
.fs_asset_cachebuster ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/stylesheet/manager.rb', line 127 def self.fs_asset_cachebuster if use_file_hash_for_cachebuster? @cachebuster ||= if File.exist?(manifest_full_path) File.readlines(manifest_full_path, "r")[0] else cachebuster = "#{BASE_COMPILER_VERSION}:#{fs_assets_hash}" FileUtils.mkdir_p(MANIFEST_DIR) File.open(manifest_full_path, "w") { |f| f.print(cachebuster) } cachebuster end else "#{BASE_COMPILER_VERSION}:#{max_file_mtime}" end end |
.precompile_css ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/stylesheet/manager.rb', line 45 def self.precompile_css targets = %i[desktop mobile admin wizard desktop_rtl mobile_rtl admin_rtl wizard_rtl] targets += Discourse.find_plugin_css_assets( include_disabled: true, mobile_view: true, desktop_view: true, ) targets += Discourse.find_plugin_css_assets( include_disabled: true, mobile_view: true, desktop_view: true, rtl: true, ) targets.each do |target| $stderr.puts "precompile target: #{target}" Stylesheet::Manager::Builder.new(target: target, manager: nil).compile(force: true) end end |
.precompile_theme_css ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/stylesheet/manager.rb', line 69 def self.precompile_theme_css themes = Theme.where("user_selectable OR id = ?", SiteSetting.default_theme_id).pluck( :id, :color_scheme_id, ) color_schemes = ColorScheme.where(user_selectable: true).to_a color_schemes << ColorScheme.find_by(id: SiteSetting.default_dark_mode_color_scheme_id) color_schemes << ColorScheme.base color_schemes = color_schemes.compact.uniq targets = %i[desktop_theme mobile_theme] compiled = Set.new themes.each do |theme_id, color_scheme_id| manager = self.new(theme_id: theme_id) targets.each do |target| next if theme_id == -1 scss_checker = ScssChecker.new(target, manager.theme_ids) manager .load_themes(manager.theme_ids) .each do |theme| next if compiled.include?("#{target}_#{theme.id}") builder = Stylesheet::Manager::Builder.new(target: target, theme: theme, manager: manager) next if theme.component && !scss_checker.has_scss(theme.id) $stderr.puts "precompile target: #{target} #{theme.name}" builder.compile(force: true) compiled << "#{target}_#{theme.id}" end end theme_color_scheme = ColorScheme.find_by_id(color_scheme_id) theme = manager.get_theme(theme_id) [theme_color_scheme, *color_schemes].compact.uniq.each do |scheme| $stderr.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{theme.name} (#{scheme.name})" Stylesheet::Manager::Builder.new( target: COLOR_SCHEME_STYLESHEET, theme: theme, color_scheme: scheme, manager: manager, ).compile(force: true) end clear_color_scheme_cache! end nil end |
.recalculate_fs_asset_cachebuster! ⇒ Object
143 144 145 146 147 |
# File 'lib/stylesheet/manager.rb', line 143 def self.recalculate_fs_asset_cachebuster! File.delete(manifest_full_path) if File.exist?(manifest_full_path) @cachebuster = nil fs_asset_cachebuster end |
Instance Method Details
#cache ⇒ Object
202 203 204 |
# File 'lib/stylesheet/manager.rb', line 202 def cache self.class.cache end |
#color_scheme_stylesheet_details(color_scheme_id = nil, media) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/stylesheet/manager.rb', line 329 def color_scheme_stylesheet_details(color_scheme_id = nil, media) theme_id = @theme_id || SiteSetting.default_theme_id color_scheme = begin ColorScheme.find(color_scheme_id) rescue StandardError # don't load fallback when requesting dark color scheme return false if media != "all" get_theme(theme_id)&.color_scheme || ColorScheme.base end return false if !color_scheme 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? stylesheet = { color_scheme_id: color_scheme.id } theme = get_theme(theme_id) builder = Builder.new( target: target, theme: get_theme(theme_id), color_scheme: color_scheme, manager: self, ) builder.compile unless File.exist?(builder.stylesheet_fullpath) href = builder.stylesheet_absolute_url stylesheet[:new_href] = href cache.defer_set(cache_key, stylesheet.freeze) stylesheet end |
#color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = "all", preload_callback = nil) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/stylesheet/manager.rb', line 380 def color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = "all", preload_callback = nil) stylesheet = color_scheme_stylesheet_details(color_scheme_id, media) return "" if !stylesheet href = stylesheet[:new_href] preload_callback.call(href, "style") if preload_callback css_class = media == "all" ? "light-scheme" : "dark-scheme" %[<link href="#{href}" media="#{media}" rel="stylesheet" class="#{css_class}"/>].html_safe end |
#color_scheme_stylesheet_preload_tag(color_scheme_id = nil, media = "all") ⇒ Object
370 371 372 373 374 375 376 377 378 |
# File 'lib/stylesheet/manager.rb', line 370 def color_scheme_stylesheet_preload_tag(color_scheme_id = nil, media = "all") stylesheet = color_scheme_stylesheet_details(color_scheme_id, media) return "" if !stylesheet href = stylesheet[:new_href] %[<link href="#{href}" rel="preload" as="style"/>].html_safe end |
#get_theme(theme_id) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/stylesheet/manager.rb', line 206 def get_theme(theme_id) if theme = @themes_cache[theme_id] theme else load_themes([theme_id]).first end end |
#load_themes(theme_ids) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/stylesheet/manager.rb', line 214 def load_themes(theme_ids) themes = [] to_load_theme_ids = [] theme_ids.each do |theme_id| if @themes_cache[theme_id] themes << @themes_cache[theme_id] else to_load_theme_ids << theme_id end end Theme .where(id: to_load_theme_ids) .includes(:yaml_theme_fields, :theme_settings, :upload_fields, :builder_theme_fields) .each do |theme| @themes_cache[theme.id] = theme themes << theme end themes end |
#stylesheet_data(target = :desktop) ⇒ Object
237 238 239 |
# File 'lib/stylesheet/manager.rb', line 237 def stylesheet_data(target = :desktop) stylesheet_details(target, "all") end |
#stylesheet_details(target = :desktop, media = "all") ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/stylesheet/manager.rb', line 268 def stylesheet_details(target = :desktop, media = "all") target = target.to_sym current_hostname = Discourse.current_hostname is_theme_target = !!(target.to_s =~ THEME_REGEX) array_cache_key = ( if is_theme_target "array_themes_#{@theme_ids.join(",")}_#{target}_#{current_hostname}" else "array_#{target}_#{current_hostname}" end ) stylesheets = cache[array_cache_key] return stylesheets if stylesheets.present? @@lock.synchronize do stylesheets = [] if is_theme_target scss_checker = ScssChecker.new(target, @theme_ids) themes = load_themes(@theme_ids) themes.each do |theme| theme_id = theme&.id data = { target: target, theme_id: theme_id, theme_name: theme&.name.downcase, remote: theme.remote_theme_id?, } builder = Builder.new(target: target, theme: theme, manager: self) next if builder.theme&.component && !scss_checker.has_scss(theme_id) builder.compile unless File.exist?(builder.stylesheet_fullpath) href = builder.stylesheet_absolute_url data[:new_href] = href stylesheets << data end if stylesheets.size > 1 stylesheets = stylesheets.sort_by do |s| [s[:remote] ? 0 : 1, s[:theme_id] == @theme_id ? 1 : 0, s[:theme_name]] end end else builder = Builder.new(target: target, manager: self) builder.compile unless File.exist?(builder.stylesheet_fullpath) href = builder.stylesheet_absolute_url data = { target: target, new_href: href } stylesheets << data end cache.defer_set(array_cache_key, stylesheets.freeze) stylesheets end end |
#stylesheet_link_tag(target = :desktop, media = "all", preload_callback = nil) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/stylesheet/manager.rb', line 252 def stylesheet_link_tag(target = :desktop, media = "all", preload_callback = nil) stylesheets = stylesheet_details(target, media) stylesheets .map do |stylesheet| href = stylesheet[:new_href] preload_callback.call(href, "style") if preload_callback theme_id = stylesheet[:theme_id] data_theme_id = theme_id ? "data-theme-id=\"#{theme_id}\"" : "" theme_name = stylesheet[:theme_name] data_theme_name = theme_name ? "data-theme-name=\"#{CGI.escapeHTML(theme_name)}\"" : "" %[<link href="#{href}" media="#{media}" rel="stylesheet" data-target="#{target}" #{data_theme_id} #{data_theme_name}/>] end .join("\n") .html_safe end |
#stylesheet_preload_tag(target = :desktop, media = "all") ⇒ Object
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/stylesheet/manager.rb', line 241 def stylesheet_preload_tag(target = :desktop, media = "all") stylesheets = stylesheet_details(target, media) stylesheets .map do |stylesheet| href = stylesheet[:new_href] %[<link href="#{href}" rel="preload" as="style"/>] end .join("\n") .html_safe end |