A platform for community discussion. Free, open, simple.
DEV: Allow loading specific plugins in development (#39975)
David Taylor49c7da3c156c
diff --git a/app/models/global_setting.rb b/app/models/global_setting.rbindex f34e5bb1f7a..fce1c211714 100644--- a/app/models/global_setting.rb+++ b/app/models/global_setting.rb@@ -388,14 +388,29 @@ class GlobalSettingenddef self.load_plugins?- if ENV["LOAD_PLUGINS"] == "1"- true- elsif ENV["LOAD_PLUGINS"] == "0"- false- elsif Rails.env.test?- false+ load_plugins_filter != :none+ end++ def self.plugins_to_load+ filter = load_plugins_filter+ filter.is_a?(Array) ? filter : nil+ end++ # Returns `:all`, `:none`, or an array of plugin directory names.+ def self.load_plugins_filter+ case ENV["LOAD_PLUGINS"]+ when "0"+ :none+ when "1"+ :all+ when nil, ""+ Rails.env.test? ? :none : :allelse- true+ unless Rails.env.local?+ raise "LOAD_PLUGINS=#{ENV["LOAD_PLUGINS"].inspect} is only supported in development/test"+ end+ ENV["LOAD_PLUGINS"].split(",").map { |p| File.basename(p.strip) }.reject(&:empty?)endend+ private_class_method :load_plugins_filterenddiff --git a/app/models/site_setting.rb b/app/models/site_setting.rbindex 74bbf6fc7f7..62763180c34 100644--- a/app/models/site_setting.rb+++ b/app/models/site_setting.rb@@ -133,15 +133,22 @@ class SiteSetting < ActiveRecord::Baseendif GlobalSetting.load_plugins?+ allowed_plugins = GlobalSetting.plugins_to_load+ plugin_allowed = ->(name) { allowed_plugins.nil? || allowed_plugins.include?(name) }+Dir[File.join(Rails.root, "plugins", "*", "config", "settings.yml")].each do |file|- load_settings(file, plugin: file.split("/")[-3])+ plugin_name = file.split("/")[-3]+ load_settings(file, plugin: plugin_name) if plugin_allowed.call(plugin_name)end# Sometimes plugins need to define their own fake site settings for testingif Rails.env.test?Dir[File.join(Rails.root, "plugins", "*", "spec", "support", "dummy_plugin_site_settings.yml")- ].each { |file| load_settings(file, plugin: file.split("/")[-4]) }+ ].each do |file|+ plugin_name = file.split("/")[-4]+ load_settings(file, plugin: plugin_name) if plugin_allowed.call(plugin_name)+ endendenddiff --git a/config/application.rb b/config/application.rbindex ec970f07e0e..f62b5dc0992 100644--- a/config/application.rb+++ b/config/application.rb@@ -20,8 +20,9 @@ if GlobalSetting.load_plugins?# Support for plugins to register custom setting providers. They can do this# by having a file, `register_provider.rb` in their root that will be run# at this point.-+ allowed_plugins = GlobalSetting.plugins_to_loadDir.glob(File.join(File.dirname(__FILE__), "../plugins", "*", "register_provider.rb")) do |p|+ next if allowed_plugins && !allowed_plugins.include?(File.basename(File.dirname(p)))require pendenddiff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rbindex 02fbb55d577..b6544fe6291 100644--- a/lib/discourse_plugin_registry.rb+++ b/lib/discourse_plugin_registry.rb@@ -242,9 +242,7 @@ class DiscoursePluginRegistrydef self.seed_pathsresult = SeedFu.fixture_paths.dup- unless Rails.env.test? && ENV["LOAD_PLUGINS"] != "1"- seed_path_builders.each { |b| result += b.call }- end+ seed_path_builders.each { |b| result += b.call } if GlobalSetting.load_plugins?result.uniqenddiff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 59e9c85d7ae..98a1f5ec6c9 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -94,9 +94,13 @@ class Plugin::Instanceenddef self.find_all(parent_path)+ allowed = GlobalSetting.plugins_to_load[].tap do |plugins|# also follows symlinks - http://stackoverflow.com/q/357754- Dir["#{parent_path}/*/plugin.rb"].sort.each { |path| plugins << parse_from_source(path) }+ Dir["#{parent_path}/*/plugin.rb"].sort.each do |path|+ next if allowed && !allowed.include?(File.basename(File.dirname(path)))+ plugins << parse_from_source(path)+ endendenddiff --git a/spec/models/global_setting_spec.rb b/spec/models/global_setting_spec.rbindex d539b199e24..d8d0b84ab7c 100644--- a/spec/models/global_setting_spec.rb+++ b/spec/models/global_setting_spec.rb@@ -179,4 +179,43 @@ RSpec.describe GlobalSetting::FileProvider dof.unlinkend++ describe ".load_plugins? and .plugins_to_load" do+ around do |example|+ original = ENV["LOAD_PLUGINS"]+ example.run+ ensure+ original.nil? ? ENV.delete("LOAD_PLUGINS") : ENV["LOAD_PLUGINS"] = original+ end++ it "treats `1` as load-all" do+ ENV["LOAD_PLUGINS"] = "1"+ expect(GlobalSetting.load_plugins?).to eq(true)+ expect(GlobalSetting.plugins_to_load).to be_nil+ end++ it "treats `0` as load-none" do+ ENV["LOAD_PLUGINS"] = "0"+ expect(GlobalSetting.load_plugins?).to eq(false)+ expect(GlobalSetting.plugins_to_load).to be_nil+ end++ it "parses a comma-separated list, stripping whitespace and empty entries" do+ ENV["LOAD_PLUGINS"] = " chat , automation ,, "+ expect(GlobalSetting.load_plugins?).to eq(true)+ expect(GlobalSetting.plugins_to_load).to eq(%w[chat automation])+ end++ it "tolerates `plugins/foo` entries by taking the basename" do+ ENV["LOAD_PLUGINS"] = "plugins/chat,plugins/automation"+ expect(GlobalSetting.plugins_to_load).to eq(%w[chat automation])+ end++ it "raises when given a list outside of dev/test" do+ ENV["LOAD_PLUGINS"] = "chat,automation"+ allow(Rails.env).to receive(:local?).and_return(false)+ expect { GlobalSetting.load_plugins? }.to raise_error(RuntimeError, /only supported/)+ expect { GlobalSetting.plugins_to_load }.to raise_error(RuntimeError, /only supported/)+ end+ endend