2013-08-23 01:21:52 -05:00
|
|
|
require 'digest/sha1'
|
|
|
|
require 'fileutils'
|
|
|
|
require_dependency 'plugin/metadata'
|
|
|
|
require_dependency 'plugin/auth_provider'
|
|
|
|
|
2016-07-22 11:59:43 -05:00
|
|
|
class Plugin::CustomEmoji
|
|
|
|
def self.cache_key
|
|
|
|
@@cache_key ||= "plugin-emoji"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.emojis
|
|
|
|
@@emojis ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.register(name, url)
|
|
|
|
@@cache_key = Digest::SHA1.hexdigest(cache_key + name)[0..10]
|
|
|
|
emojis[name] = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
class Plugin::Instance
|
|
|
|
|
2013-08-25 20:04:16 -05:00
|
|
|
attr_accessor :path, :metadata
|
2015-02-06 16:32:59 -06:00
|
|
|
attr_reader :admin_route
|
2013-08-23 01:21:52 -05:00
|
|
|
|
2015-02-04 11:59:18 -06:00
|
|
|
# Memoized array readers
|
2017-01-12 14:43:09 -06:00
|
|
|
[:assets,
|
|
|
|
:auth_providers,
|
|
|
|
:color_schemes,
|
|
|
|
:initializers,
|
|
|
|
:javascripts,
|
|
|
|
:styles,
|
|
|
|
:themes].each do |att|
|
2015-02-04 11:59:18 -06:00
|
|
|
class_eval %Q{
|
|
|
|
def #{att}
|
|
|
|
@#{att} ||= []
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-07-22 11:59:43 -05:00
|
|
|
def seed_data
|
|
|
|
@seed_data ||= HashWithIndifferentAccess.new({})
|
2015-06-04 14:56:17 -05:00
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
def self.find_all(parent_path)
|
|
|
|
[].tap { |plugins|
|
2013-10-21 04:18:24 -05:00
|
|
|
# also follows symlinks - http://stackoverflow.com/q/357754
|
2017-05-16 16:28:45 -05:00
|
|
|
Dir["#{parent_path}/*/plugin.rb"].sort.each do |path|
|
2016-04-25 14:55:15 -05:00
|
|
|
|
|
|
|
# tagging is included in core, so don't load it
|
|
|
|
next if path =~ /discourse-tagging/
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
source = File.read(path)
|
|
|
|
metadata = Plugin::Metadata.parse(source)
|
|
|
|
plugins << self.new(metadata, path)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(metadata = nil, path = nil)
|
2013-08-23 01:21:52 -05:00
|
|
|
@metadata = metadata
|
|
|
|
@path = path
|
2015-08-21 10:28:17 -05:00
|
|
|
@idx = 0
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
2015-02-06 16:32:59 -06:00
|
|
|
def add_admin_route(label, location)
|
2017-07-27 20:20:09 -05:00
|
|
|
@admin_route = { label: label, location: location }
|
2015-02-06 16:32:59 -06:00
|
|
|
end
|
|
|
|
|
2015-02-04 15:23:39 -06:00
|
|
|
def enabled?
|
2015-04-23 12:33:29 -05:00
|
|
|
@enabled_site_setting ? SiteSetting.send(@enabled_site_setting) : true
|
2013-08-25 20:04:16 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 15:23:39 -06:00
|
|
|
delegate :name, to: :metadata
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def add_to_serializer(serializer, attr, define_include_method = true, &block)
|
2017-08-09 15:22:18 -05:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 11:28:32 -05:00
|
|
|
klass = "#{serializer.to_s.classify}Serializer".constantize rescue "#{serializer.to_s}Serializer".constantize
|
2015-04-23 12:33:29 -05:00
|
|
|
|
2017-08-09 15:22:18 -05:00
|
|
|
unless attr.to_s.start_with?("include_")
|
|
|
|
klass.attributes(attr)
|
2015-04-23 12:33:29 -05:00
|
|
|
|
2017-08-09 15:22:18 -05:00
|
|
|
if define_include_method
|
|
|
|
# Don't include serialized methods if the plugin is disabled
|
|
|
|
klass.send(:define_method, "include_#{attr}?") { plugin.enabled? }
|
|
|
|
end
|
2017-08-09 11:28:32 -05:00
|
|
|
end
|
2017-08-09 15:22:18 -05:00
|
|
|
|
|
|
|
klass.send(:define_method, attr, &block)
|
2017-08-09 11:28:32 -05:00
|
|
|
end
|
2015-02-04 15:23:39 -06:00
|
|
|
end
|
|
|
|
|
2017-10-17 12:31:45 -05:00
|
|
|
def replace_flags
|
|
|
|
settings = ::FlagSettings.new
|
|
|
|
yield settings
|
|
|
|
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
::PostActionType.replace_flag_settings(settings) if plugin.enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 14:52:18 -06:00
|
|
|
def whitelist_staff_user_custom_field(field)
|
2017-08-11 21:21:02 -05:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-16 16:00:52 -05:00
|
|
|
::User.register_plugin_staff_custom_field(field, plugin) if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
2016-03-11 14:52:18 -06:00
|
|
|
end
|
|
|
|
|
2017-08-30 13:24:03 -05:00
|
|
|
def custom_avatar_column(column)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
AvatarLookup.lookup_columns << column
|
|
|
|
AvatarLookup.lookup_columns.uniq!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-28 12:16:51 -05:00
|
|
|
def add_body_class(class_name)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
::ApplicationHelper.extra_body_classes << class_name if plugin.enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-02 11:04:59 -05:00
|
|
|
def rescue_from(exception, &block)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
::ApplicationController.rescue_from(exception, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-04 15:23:39 -06:00
|
|
|
# Extend a class but check that the plugin is enabled
|
2015-08-21 10:28:17 -05:00
|
|
|
# for class methods use `add_class_method`
|
2017-08-09 11:28:32 -05:00
|
|
|
def add_to_class(class_name, attr, &block)
|
2017-08-09 15:22:18 -05:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 11:28:32 -05:00
|
|
|
klass = class_name.to_s.classify.constantize rescue class_name.to_s.constantize
|
|
|
|
hidden_method_name = :"#{attr}_without_enable_check"
|
|
|
|
klass.send(:define_method, hidden_method_name, &block)
|
|
|
|
|
|
|
|
klass.send(:define_method, attr) do |*args|
|
|
|
|
send(hidden_method_name, *args) if plugin.enabled?
|
|
|
|
end
|
2015-02-04 15:23:39 -06:00
|
|
|
end
|
2015-01-12 09:52:55 -06:00
|
|
|
end
|
|
|
|
|
2015-08-21 10:28:17 -05:00
|
|
|
# Adds a class method to a class, respecting if plugin is enabled
|
2017-08-09 11:28:32 -05:00
|
|
|
def add_class_method(klass_name, attr, &block)
|
2017-08-09 15:22:18 -05:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 11:28:32 -05:00
|
|
|
klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
|
2015-08-21 10:28:17 -05:00
|
|
|
|
2017-08-09 11:28:32 -05:00
|
|
|
hidden_method_name = :"#{attr}_without_enable_check"
|
|
|
|
klass.send(:define_singleton_method, hidden_method_name, &block)
|
2015-08-21 10:28:17 -05:00
|
|
|
|
2017-08-09 11:28:32 -05:00
|
|
|
klass.send(:define_singleton_method, attr) do |*args|
|
|
|
|
send(hidden_method_name, *args) if plugin.enabled?
|
|
|
|
end
|
2015-08-21 10:28:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 11:28:32 -05:00
|
|
|
def add_model_callback(klass_name, callback, options = {}, &block)
|
2017-08-09 15:22:18 -05:00
|
|
|
reloadable_patch do |plugin|
|
2017-08-09 11:28:32 -05:00
|
|
|
klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
|
2015-08-21 10:28:17 -05:00
|
|
|
|
2017-08-09 11:28:32 -05:00
|
|
|
# generate a unique method name
|
|
|
|
method_name = "#{plugin.name}_#{klass.name}_#{callback}#{@idx}".underscore
|
|
|
|
@idx += 1
|
|
|
|
hidden_method_name = :"#{method_name}_without_enable_check"
|
|
|
|
klass.send(:define_method, hidden_method_name, &block)
|
2015-08-21 10:28:17 -05:00
|
|
|
|
2017-08-09 11:28:32 -05:00
|
|
|
klass.send(callback, options) do |*args|
|
|
|
|
send(hidden_method_name, *args) if plugin.enabled?
|
|
|
|
end
|
2015-08-21 10:28:17 -05:00
|
|
|
|
2017-08-09 11:28:32 -05:00
|
|
|
hidden_method_name
|
|
|
|
end
|
2015-08-21 10:28:17 -05:00
|
|
|
end
|
|
|
|
|
2017-08-11 21:21:02 -05:00
|
|
|
def topic_view_post_custom_fields_whitelister(&block)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-13 12:19:50 -05:00
|
|
|
::TopicView.add_post_custom_fields_whitelister(&block) if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_preloaded_group_custom_field(field)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-13 12:19:50 -05:00
|
|
|
::Group.preloaded_custom_field_names << field if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_preloaded_topic_list_custom_field(field)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-13 12:19:50 -05:00
|
|
|
::TopicList.preloaded_custom_fields << field if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_permitted_post_create_param(name)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-13 12:19:50 -05:00
|
|
|
::Post.permitted_create_params << name if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-23 12:33:29 -05:00
|
|
|
# Add validation method but check that the plugin is enabled
|
2015-04-25 17:12:19 -05:00
|
|
|
def validate(klass, name, &block)
|
2015-04-23 12:33:29 -05:00
|
|
|
klass = klass.to_s.classify.constantize
|
2015-04-25 17:12:19 -05:00
|
|
|
klass.send(:define_method, name, &block)
|
2015-04-23 12:33:29 -05:00
|
|
|
|
|
|
|
plugin = self
|
2015-04-25 17:12:19 -05:00
|
|
|
klass.validate(name, if: -> { plugin.enabled? })
|
2015-04-23 12:33:29 -05:00
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
# will make sure all the assets this plugin needs are registered
|
|
|
|
def generate_automatic_assets!
|
|
|
|
paths = []
|
2015-11-06 08:02:40 -06:00
|
|
|
assets = []
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
automatic_assets.each do |path, contents|
|
2015-11-06 08:02:40 -06:00
|
|
|
write_asset(path, contents)
|
|
|
|
paths << path
|
|
|
|
assets << [path]
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
delete_extra_automatic_assets(paths)
|
|
|
|
|
2015-11-06 08:02:40 -06:00
|
|
|
assets
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_extra_automatic_assets(good_paths)
|
2013-09-20 16:39:14 -05:00
|
|
|
return unless Dir.exists? auto_generated_path
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
filenames = good_paths.map { |f| File.basename(f) }
|
2013-08-23 01:21:52 -05:00
|
|
|
# nuke old files
|
|
|
|
Dir.foreach(auto_generated_path) do |p|
|
|
|
|
next if [".", ".."].include?(p)
|
|
|
|
next if filenames.include?(p)
|
|
|
|
File.delete(auto_generated_path + "/#{p}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_directory(path)
|
|
|
|
dirname = File.dirname(path)
|
|
|
|
unless File.directory?(dirname)
|
|
|
|
FileUtils.mkdir_p(dirname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-12 14:43:09 -06:00
|
|
|
def directory
|
|
|
|
File.dirname(path)
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
def auto_generated_path
|
|
|
|
File.dirname(path) << "/auto_generated"
|
|
|
|
end
|
|
|
|
|
2013-09-16 19:23:21 -05:00
|
|
|
def after_initialize(&block)
|
2015-02-04 11:59:18 -06:00
|
|
|
initializers << block
|
2013-09-16 19:23:21 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 15:23:39 -06:00
|
|
|
# A proxy to `DiscourseEvent.on` which does nothing if the plugin is disabled
|
|
|
|
def on(event_name, &block)
|
|
|
|
DiscourseEvent.on(event_name) do |*args|
|
|
|
|
block.call(*args) if enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-16 19:23:21 -05:00
|
|
|
def notify_after_initialize
|
2014-06-03 11:36:34 -05:00
|
|
|
color_schemes.each do |c|
|
2017-08-11 21:21:02 -05:00
|
|
|
unless ColorScheme.where(name: c[:name]).exists?
|
|
|
|
ColorScheme.create_from_base(name: c[:name], colors: c[:colors])
|
|
|
|
end
|
2014-06-03 11:36:34 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 11:59:18 -06:00
|
|
|
initializers.each do |callback|
|
2015-08-25 15:38:25 -05:00
|
|
|
begin
|
|
|
|
callback.call(self)
|
|
|
|
rescue ActiveRecord::StatementInvalid => e
|
2017-08-11 21:21:02 -05:00
|
|
|
# When running `db:migrate` for the first time on a new database,
|
|
|
|
# plugin initializers might try to use models.
|
|
|
|
# Tolerate it.
|
2015-08-25 15:38:25 -05:00
|
|
|
raise e unless e.message.try(:include?, "PG::UndefinedTable")
|
|
|
|
end
|
2013-09-16 19:23:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 01:59:31 -05:00
|
|
|
def register_category_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
Category.register_custom_field_type(name, type) if plugin.enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-11 21:21:02 -05:00
|
|
|
def register_topic_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-16 16:00:52 -05:00
|
|
|
::Topic.register_custom_field_type(name, type) if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_post_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-16 16:00:52 -05:00
|
|
|
::Post.register_custom_field_type(name, type) if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_group_custom_field_type(name, type)
|
|
|
|
reloadable_patch do |plugin|
|
2017-08-16 16:00:52 -05:00
|
|
|
::Group.register_custom_field_type(name, type) if plugin.enabled?
|
2017-08-11 21:21:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-25 01:55:53 -05:00
|
|
|
def register_seedfu_fixtures(paths)
|
|
|
|
paths = [paths] if !paths.kind_of?(Array)
|
|
|
|
SeedFu.fixture_paths.concat(paths)
|
|
|
|
end
|
|
|
|
|
2014-12-11 10:08:47 -06:00
|
|
|
def listen_for(event_name)
|
|
|
|
return unless self.respond_to?(event_name)
|
|
|
|
DiscourseEvent.on(event_name, &self.method(event_name))
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
def register_css(style)
|
2015-02-04 11:59:18 -06:00
|
|
|
styles << style
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def register_javascript(js)
|
2015-02-04 11:59:18 -06:00
|
|
|
javascripts << js
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
2014-06-04 20:39:33 -05:00
|
|
|
def register_custom_html(hash)
|
|
|
|
DiscoursePluginRegistry.custom_html ||= {}
|
|
|
|
DiscoursePluginRegistry.custom_html.merge!(hash)
|
|
|
|
end
|
|
|
|
|
2017-04-17 14:47:21 -05:00
|
|
|
def register_html_builder(name, &block)
|
|
|
|
DiscoursePluginRegistry.register_html_builder(name, &block)
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def register_asset(file, opts = nil)
|
2013-08-23 01:21:52 -05:00
|
|
|
full_path = File.dirname(path) << "/assets/" << file
|
2014-04-10 01:30:22 -05:00
|
|
|
assets << [full_path, opts]
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
2014-06-03 11:36:34 -05:00
|
|
|
def register_color_scheme(name, colors)
|
2017-07-27 20:20:09 -05:00
|
|
|
color_schemes << { name: name, colors: colors }
|
2015-06-04 14:56:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def register_seed_data(key, value)
|
|
|
|
seed_data[key] = value
|
|
|
|
end
|
2014-06-03 11:36:34 -05:00
|
|
|
|
2017-11-16 13:42:38 -06:00
|
|
|
def register_seed_path_builder(&block)
|
|
|
|
DiscoursePluginRegistry.register_seed_path_builder(&block)
|
|
|
|
end
|
|
|
|
|
2015-11-05 10:25:26 -06:00
|
|
|
def register_emoji(name, url)
|
2016-07-22 11:59:43 -05:00
|
|
|
Plugin::CustomEmoji.register(name, url)
|
2015-11-05 10:25:26 -06:00
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
def automatic_assets
|
2015-02-04 11:59:18 -06:00
|
|
|
css = styles.join("\n")
|
|
|
|
js = javascripts.join("\n")
|
2013-08-23 01:21:52 -05:00
|
|
|
|
2015-02-04 11:59:18 -06:00
|
|
|
auth_providers.each do |auth|
|
2013-08-23 01:21:52 -05:00
|
|
|
|
2016-06-30 11:26:49 -05:00
|
|
|
auth_json = auth.to_json
|
|
|
|
hash = Digest::SHA1.hexdigest(auth_json)
|
|
|
|
js << <<JS
|
|
|
|
define("discourse/initializers/login-method-#{hash}",
|
|
|
|
["discourse/models/login-method", "exports"],
|
|
|
|
function(module, __exports__) {
|
|
|
|
"use strict";
|
|
|
|
__exports__["default"] = {
|
|
|
|
name: "login-method-#{hash}",
|
|
|
|
after: "inject-objects",
|
2016-07-13 15:11:48 -05:00
|
|
|
initialize: function(container) {
|
2016-06-30 12:55:44 -05:00
|
|
|
if (Ember.testing) { return; }
|
2016-07-13 15:11:48 -05:00
|
|
|
|
|
|
|
var authOpts = #{auth_json};
|
|
|
|
authOpts.siteSettings = container.lookup('site-settings:main');
|
|
|
|
module.register(authOpts);
|
2016-06-30 11:26:49 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
JS
|
2013-08-23 01:21:52 -05:00
|
|
|
|
2015-02-04 11:59:18 -06:00
|
|
|
if auth.glyph
|
|
|
|
css << ".btn-social.#{auth.name}:before{ content: '#{auth.glyph}'; }\n"
|
|
|
|
end
|
2013-08-23 01:21:52 -05:00
|
|
|
|
2015-02-04 11:59:18 -06:00
|
|
|
if auth.background_color
|
|
|
|
css << ".btn-social.#{auth.name}{ background: #{auth.background_color}; }\n"
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-06 08:02:40 -06:00
|
|
|
# Generate an IIFE for the JS
|
|
|
|
js = "(function(){#{js}})();" if js.present?
|
|
|
|
|
|
|
|
result = []
|
|
|
|
result << [css, 'css'] if css.present?
|
|
|
|
result << [js, 'js'] if js.present?
|
|
|
|
|
|
|
|
result.map do |asset, extension|
|
|
|
|
hash = Digest::SHA1.hexdigest asset
|
|
|
|
["#{auto_generated_path}/plugin_#{hash}.#{extension}", asset]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
# note, we need to be able to parse seperately to activation.
|
|
|
|
# this allows us to present information about a plugin in the UI
|
|
|
|
# prior to activations
|
|
|
|
def activate!
|
2015-04-27 12:06:53 -05:00
|
|
|
|
|
|
|
if @path
|
|
|
|
# Automatically include all ES6 JS and hbs files
|
|
|
|
root_path = "#{File.dirname(@path)}/assets/javascripts"
|
|
|
|
DiscoursePluginRegistry.register_glob(root_path, 'js.es6')
|
|
|
|
DiscoursePluginRegistry.register_glob(root_path, 'hbs')
|
2015-08-17 14:03:55 -05:00
|
|
|
|
|
|
|
admin_path = "#{File.dirname(@path)}/admin/assets/javascripts"
|
|
|
|
DiscoursePluginRegistry.register_glob(admin_path, 'js.es6', admin: true)
|
|
|
|
DiscoursePluginRegistry.register_glob(admin_path, 'hbs', admin: true)
|
2015-04-27 12:06:53 -05:00
|
|
|
end
|
|
|
|
|
2013-09-11 20:27:13 -05:00
|
|
|
self.instance_eval File.read(path), path
|
2013-08-23 01:21:52 -05:00
|
|
|
if auto_assets = generate_automatic_assets!
|
2015-11-06 08:02:40 -06:00
|
|
|
assets.concat(auto_assets)
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
2014-12-30 15:29:28 -06:00
|
|
|
|
|
|
|
register_assets! unless assets.blank?
|
|
|
|
|
2015-06-04 14:56:17 -05:00
|
|
|
seed_data.each do |key, value|
|
|
|
|
DiscoursePluginRegistry.register_seed_data(key, value)
|
|
|
|
end
|
|
|
|
|
2015-05-04 09:01:57 -05:00
|
|
|
# TODO: possibly amend this to a rails engine
|
|
|
|
|
|
|
|
# Automatically include assets
|
2014-12-30 15:29:28 -06:00
|
|
|
Rails.configuration.assets.paths << auto_generated_path
|
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/assets"
|
2015-08-17 14:03:55 -05:00
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/admin/assets"
|
2015-08-27 15:59:36 -05:00
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/test/javascripts"
|
2013-08-23 01:21:52 -05:00
|
|
|
|
2015-05-04 09:01:57 -05:00
|
|
|
# Automatically include rake tasks
|
|
|
|
Rake.add_rakelib(File.dirname(path) + "/lib/tasks")
|
|
|
|
|
|
|
|
# Automatically include migrations
|
|
|
|
Rails.configuration.paths["db/migrate"] << File.dirname(path) + "/db/migrate"
|
|
|
|
|
2013-11-19 21:38:21 -06:00
|
|
|
public_data = File.dirname(path) + "/public"
|
|
|
|
if Dir.exists?(public_data)
|
|
|
|
target = Rails.root.to_s + "/public/plugins/"
|
2017-03-17 01:21:30 -05:00
|
|
|
|
|
|
|
Discourse::Utils.execute_command('mkdir', '-p', target)
|
2017-07-27 20:20:09 -05:00
|
|
|
target << name.gsub(/\s/, "_")
|
2013-11-19 21:38:21 -06:00
|
|
|
# TODO a cleaner way of registering and unregistering
|
2017-03-17 01:21:30 -05:00
|
|
|
Discourse::Utils.execute_command('rm', '-f', target)
|
|
|
|
Discourse::Utils.execute_command('ln', '-s', public_data, target)
|
2013-11-19 21:38:21 -06:00
|
|
|
end
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
2013-08-25 21:52:36 -05:00
|
|
|
def auth_provider(opts)
|
2013-08-23 01:21:52 -05:00
|
|
|
provider = Plugin::AuthProvider.new
|
2015-09-25 10:29:05 -05:00
|
|
|
|
|
|
|
Plugin::AuthProvider.auth_attributes.each do |sym|
|
2013-08-23 01:21:52 -05:00
|
|
|
provider.send "#{sym}=", opts.delete(sym)
|
|
|
|
end
|
2015-02-04 11:59:18 -06:00
|
|
|
auth_providers << provider
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# shotgun approach to gem loading, in future we need to hack bundler
|
|
|
|
# to at least determine dependencies do not clash before loading
|
|
|
|
#
|
|
|
|
# Additionally we want to support multiple ruby versions correctly and so on
|
|
|
|
#
|
|
|
|
# This is a very rough initial implementation
|
|
|
|
def gem(name, version, opts = {})
|
2017-01-09 16:10:14 -06:00
|
|
|
PluginGem.load(path, name, version, opts)
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def enabled_site_setting(setting = nil)
|
2015-07-02 11:45:17 -05:00
|
|
|
if setting
|
|
|
|
@enabled_site_setting = setting
|
|
|
|
else
|
|
|
|
@enabled_site_setting
|
|
|
|
end
|
2015-02-04 15:23:39 -06:00
|
|
|
end
|
|
|
|
|
2016-11-14 18:42:55 -06:00
|
|
|
def handlebars_includes
|
|
|
|
assets.map do |asset, opts|
|
|
|
|
next if opts == :admin
|
|
|
|
next unless asset =~ DiscoursePluginRegistry::HANDLEBARS_REGEX
|
|
|
|
asset
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def javascript_includes
|
|
|
|
assets.map do |asset, opts|
|
|
|
|
next if opts == :admin
|
|
|
|
next unless asset =~ DiscoursePluginRegistry::JS_REGEX
|
|
|
|
asset
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_globbed_asset
|
|
|
|
if @path
|
|
|
|
# Automatically include all ES6 JS and hbs files
|
|
|
|
root_path = "#{File.dirname(@path)}/assets/javascripts"
|
|
|
|
|
|
|
|
Dir.glob("#{root_path}/**/*") do |f|
|
|
|
|
if File.directory?(f)
|
2017-07-27 20:20:09 -05:00
|
|
|
yield [f, true]
|
2016-11-14 18:42:55 -06:00
|
|
|
elsif f.to_s.ends_with?(".js.es6") || f.to_s.ends_with?(".hbs")
|
2017-07-27 20:20:09 -05:00
|
|
|
yield [f, false]
|
2016-11-14 18:42:55 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-10 01:30:22 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def register_assets!
|
|
|
|
assets.each do |asset, opts|
|
2014-12-09 13:20:53 -06:00
|
|
|
DiscoursePluginRegistry.register_asset(asset, opts)
|
2014-04-10 01:30:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-06 08:02:40 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def write_asset(path, contents)
|
|
|
|
unless File.exists?(path)
|
|
|
|
ensure_directory(path)
|
2017-07-27 20:20:09 -05:00
|
|
|
File.open(path, "w") { |f| f.write(contents) }
|
2015-11-06 08:02:40 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 15:22:18 -05:00
|
|
|
def reloadable_patch(plugin = self)
|
2017-08-30 23:06:56 -05:00
|
|
|
if Rails.env.development? && defined?(ActiveSupport::Reloader)
|
|
|
|
ActiveSupport::Reloader.to_prepare do
|
2017-08-09 15:22:18 -05:00
|
|
|
# reload the patch
|
2017-08-09 11:28:32 -05:00
|
|
|
yield plugin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# apply the patch
|
|
|
|
yield plugin
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:21:52 -05:00
|
|
|
end
|