From 87e2c9de2478b5e33f48d894bd91ab35620a23c3 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 6 Aug 2020 09:46:17 -0400 Subject: [PATCH] DEV: Plugins can extend color definitions (#10383) --- lib/discourse_plugin_registry.rb | 3 +++ lib/stylesheet/compiler.rb | 4 ++++ lib/stylesheet/importer.rb | 11 ++++++++++ .../discourse_plugin_registry_spec.rb | 6 ++++++ spec/components/plugin/instance_spec.rb | 4 ++-- spec/components/stylesheet/compiler_spec.rb | 21 +++++++++++++++++++ .../assets/stylesheets/colors.scss | 3 +++ .../plugins/color_definition/plugin.rb | 8 +++++++ 8 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss create mode 100644 spec/fixtures/plugins/color_definition/plugin.rb diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 8beb5c33123..335d102fd70 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -54,6 +54,7 @@ class DiscoursePluginRegistry define_register :stylesheets, Hash define_register :mobile_stylesheets, Hash define_register :desktop_stylesheets, Hash + define_register :color_definition_stylesheets, Hash define_register :sass_variables, Set define_register :handlebars, Set define_register :serialized_current_user_fields, Set @@ -153,6 +154,8 @@ class DiscoursePluginRegistry elsif opts == :desktop self.desktop_stylesheets[plugin_directory_name] ||= Set.new self.desktop_stylesheets[plugin_directory_name] << asset + elsif opts == :color_definitions + self.color_definition_stylesheets[plugin_directory_name] = asset elsif opts == :variables self.sass_variables << asset else diff --git a/lib/stylesheet/compiler.rb b/lib/stylesheet/compiler.rb index 54675699bb6..b5575cda494 100644 --- a/lib/stylesheet/compiler.rb +++ b/lib/stylesheet/compiler.rb @@ -19,6 +19,10 @@ module Stylesheet filename = "#{asset}.scss" path = "#{Stylesheet::Common::ASSET_ROOT}/#{filename}" file = File.read path + + if asset.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET + file += Stylesheet::Importer.import_color_definitions + end end compile(file, filename, options) diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rb index 7798f9cf90e..f1901969ae7 100644 --- a/lib/stylesheet/importer.rb +++ b/lib/stylesheet/importer.rb @@ -115,6 +115,17 @@ module Stylesheet register_imports! + def self.import_color_definitions + return "" unless DiscoursePluginRegistry.color_definition_stylesheets.length + contents = +"" + DiscoursePluginRegistry.color_definition_stylesheets.each do |name, path| + contents << "// Color definitions from #{name}\n\n" + contents << File.read(path.to_s) + contents << "\n\n" + end + contents + end + def initialize(options) @theme = options[:theme] @theme_id = options[:theme_id] diff --git a/spec/components/discourse_plugin_registry_spec.rb b/spec/components/discourse_plugin_registry_spec.rb index 3dc1bce8821..186dc69d270 100644 --- a/spec/components/discourse_plugin_registry_spec.rb +++ b/spec/components/discourse_plugin_registry_spec.rb @@ -227,6 +227,12 @@ describe DiscoursePluginRegistry do expect(registry.stylesheets[plugin_directory_name]).to eq(nil) end + it "registers color definitions properly" do + registry.register_asset("test.css", :color_definitions, plugin_directory_name) + expect(registry.color_definition_stylesheets[plugin_directory_name]).to eq('test.css') + expect(registry.stylesheets[plugin_directory_name]).to eq(nil) + end + it "registers sass variable properly" do registry.register_asset("test.css", :variables) diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index 1f317903628..1d7cc086734 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -11,8 +11,8 @@ describe Plugin::Instance do context "find_all" do it "can find plugins correctly" do plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins") - expect(plugins.count).to eq(3) - plugin = plugins[2] + expect(plugins.count).to eq(4) + plugin = plugins[3] expect(plugin.name).to eq("plugin-name") expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb") diff --git a/spec/components/stylesheet/compiler_spec.rb b/spec/components/stylesheet/compiler_spec.rb index 1d93aec2415..37aefc25e83 100644 --- a/spec/components/stylesheet/compiler_spec.rb +++ b/spec/components/stylesheet/compiler_spec.rb @@ -84,5 +84,26 @@ describe Stylesheet::Compiler do expect(css).to include("--header_primary: #88af8e") expect(css).to include("--header_background-rgb: 248,116,92") end + + context "with a plugin" do + before do + plugin = Plugin::Instance.new + plugin.path = "#{Rails.root}/spec/fixtures/plugins/color_definition/plugin.rb" + Discourse.plugins << plugin + plugin.activate! + end + + after do + Discourse.plugins.pop + DiscoursePluginRegistry.reset! + end + + it "includes color definitions from plugins" do + css, _map = Stylesheet::Compiler.compile_asset("color_definitions") + + expect(css).to include("--plugin-color") + end + end + end end diff --git a/spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss b/spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss new file mode 100644 index 00000000000..a4a156e042d --- /dev/null +++ b/spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss @@ -0,0 +1,3 @@ +:root { + --plugin-color: #{$primary}; +} diff --git a/spec/fixtures/plugins/color_definition/plugin.rb b/spec/fixtures/plugins/color_definition/plugin.rb new file mode 100644 index 00000000000..d7044b3d858 --- /dev/null +++ b/spec/fixtures/plugins/color_definition/plugin.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# name: color_definition +# about: Fixture plugin that extends color definitions +# version: 1.0 +# authors: pmusaraj + +register_asset "stylesheets/colors.scss", :color_definitions