2013-08-23 14:21:52 +08:00
|
|
|
require 'digest/sha1'
|
|
|
|
require 'fileutils'
|
|
|
|
require_dependency 'plugin/metadata'
|
|
|
|
require_dependency 'plugin/auth_provider'
|
|
|
|
|
|
|
|
class Plugin::Instance
|
|
|
|
|
2014-04-10 14:30:22 +08:00
|
|
|
attr_reader :auth_providers, :assets, :styles
|
2013-08-26 09:04:16 +08:00
|
|
|
attr_accessor :path, :metadata
|
2013-08-23 14:21:52 +08:00
|
|
|
|
|
|
|
def self.find_all(parent_path)
|
|
|
|
[].tap { |plugins|
|
2013-10-21 17:18:24 +08:00
|
|
|
# also follows symlinks - http://stackoverflow.com/q/357754
|
|
|
|
Dir["#{parent_path}/**/*/**/plugin.rb"].each do |path|
|
2013-08-23 14:21:52 +08:00
|
|
|
source = File.read(path)
|
|
|
|
metadata = Plugin::Metadata.parse(source)
|
|
|
|
plugins << self.new(metadata, path)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-26 09:04:16 +08:00
|
|
|
def initialize(metadata=nil, path=nil)
|
2013-08-23 14:21:52 +08:00
|
|
|
@metadata = metadata
|
|
|
|
@path = path
|
|
|
|
@assets = []
|
|
|
|
end
|
|
|
|
|
2013-08-26 09:04:16 +08:00
|
|
|
def name
|
|
|
|
metadata.name
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:21:52 +08:00
|
|
|
# will make sure all the assets this plugin needs are registered
|
|
|
|
def generate_automatic_assets!
|
|
|
|
paths = []
|
|
|
|
automatic_assets.each do |path, contents|
|
|
|
|
unless File.exists? path
|
|
|
|
ensure_directory path
|
|
|
|
File.open(path,"w") do |f|
|
|
|
|
f.write(contents)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
paths << path
|
|
|
|
end
|
|
|
|
|
|
|
|
delete_extra_automatic_assets(paths)
|
|
|
|
|
|
|
|
paths
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_extra_automatic_assets(good_paths)
|
2013-09-21 05:39:14 +08:00
|
|
|
return unless Dir.exists? auto_generated_path
|
|
|
|
|
2013-08-23 14:21:52 +08:00
|
|
|
filenames = good_paths.map{|f| File.basename(f)}
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def auto_generated_path
|
|
|
|
File.dirname(path) << "/auto_generated"
|
|
|
|
end
|
|
|
|
|
2013-09-17 08:23:21 +08:00
|
|
|
def after_initialize(&block)
|
|
|
|
@after_initialize ||= []
|
|
|
|
@after_initialize << block
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_after_initialize
|
|
|
|
if @after_initialize
|
|
|
|
@after_initialize.each do |callback|
|
|
|
|
callback.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:21:52 +08:00
|
|
|
def register_css(style)
|
|
|
|
@styles ||= []
|
|
|
|
@styles << style
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_javascript(js)
|
|
|
|
@javascripts ||= []
|
|
|
|
@javascripts << js
|
|
|
|
end
|
|
|
|
|
2014-04-07 22:33:35 +08:00
|
|
|
def register_asset(file, opts=nil)
|
2013-08-23 14:21:52 +08:00
|
|
|
full_path = File.dirname(path) << "/assets/" << file
|
2014-04-10 14:30:22 +08:00
|
|
|
assets << [full_path, opts]
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def automatic_assets
|
|
|
|
css = ""
|
2013-09-20 08:59:17 +08:00
|
|
|
js = ""
|
2013-08-23 14:21:52 +08:00
|
|
|
|
|
|
|
css = @styles.join("\n") if @styles
|
|
|
|
js = @javascripts.join("\n") if @javascripts
|
|
|
|
|
|
|
|
unless auth_providers.blank?
|
|
|
|
auth_providers.each do |auth|
|
|
|
|
overrides = ""
|
|
|
|
overrides = ", titleOverride: '#{auth.title}'" if auth.title
|
|
|
|
overrides << ", messageOverride: '#{auth.message}'" if auth.message
|
|
|
|
overrides << ", frameWidth: '#{auth.frame_width}'" if auth.frame_width
|
|
|
|
overrides << ", frameHeight: '#{auth.frame_height}'" if auth.frame_height
|
|
|
|
|
|
|
|
js << "Discourse.LoginMethod.register(Discourse.LoginMethod.create({name: '#{auth.name}'#{overrides}}));\n"
|
|
|
|
|
|
|
|
if auth.glyph
|
|
|
|
css << ".btn-social.#{auth.name}:before{ content: '#{auth.glyph}'; }\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
if auth.background_color
|
|
|
|
css << ".btn-social.#{auth.name}{ background: #{auth.background_color}; }\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-20 08:59:17 +08:00
|
|
|
# Generate an IIFE for the JS
|
|
|
|
js = "(function(){#{js}})();" if js.present?
|
2013-08-23 14:21:52 +08:00
|
|
|
|
2013-09-20 08:59:17 +08:00
|
|
|
result = []
|
|
|
|
result << [css, 'css'] if css.present?
|
|
|
|
result << [js, 'js'] if js.present?
|
|
|
|
|
|
|
|
result.map do |asset, extension|
|
2013-08-23 14:21:52 +08:00
|
|
|
hash = Digest::SHA1.hexdigest asset
|
|
|
|
["#{auto_generated_path}/plugin_#{hash}.#{extension}", asset]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-04-10 14:30:22 +08:00
|
|
|
|
2013-08-23 14:21:52 +08: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!
|
2013-09-12 09:27:13 +08:00
|
|
|
self.instance_eval File.read(path), path
|
2013-08-23 14:21:52 +08:00
|
|
|
if auto_assets = generate_automatic_assets!
|
2014-04-10 14:30:22 +08:00
|
|
|
assets.concat auto_assets.map{|a| [a]}
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|
|
|
|
unless assets.blank?
|
2014-04-10 14:30:22 +08:00
|
|
|
register_assets!
|
2013-08-23 14:21:52 +08:00
|
|
|
# TODO possibly amend this to a rails engine
|
|
|
|
Rails.configuration.assets.paths << auto_generated_path
|
|
|
|
Rails.configuration.assets.paths << File.dirname(path) + "/assets"
|
|
|
|
end
|
|
|
|
|
2013-11-20 11:38:21 +08:00
|
|
|
|
|
|
|
public_data = File.dirname(path) + "/public"
|
|
|
|
if Dir.exists?(public_data)
|
|
|
|
target = Rails.root.to_s + "/public/plugins/"
|
|
|
|
`mkdir -p #{target}`
|
2014-01-18 07:35:52 +08:00
|
|
|
target << name.gsub(/\s/,"_")
|
2013-11-20 11:38:21 +08:00
|
|
|
# TODO a cleaner way of registering and unregistering
|
2013-11-20 14:31:58 +08:00
|
|
|
`rm -f #{target}`
|
|
|
|
`ln -s #{public_data} #{target}`
|
2013-11-20 11:38:21 +08:00
|
|
|
end
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|
|
|
|
|
2014-04-10 14:30:22 +08:00
|
|
|
|
2013-08-26 10:52:36 +08:00
|
|
|
def auth_provider(opts)
|
2013-08-23 14:21:52 +08:00
|
|
|
@auth_providers ||= []
|
|
|
|
provider = Plugin::AuthProvider.new
|
2013-08-26 10:52:36 +08:00
|
|
|
[:glyph, :background_color, :title, :message, :frame_width, :frame_height, :authenticator].each do |sym|
|
2013-08-23 14:21:52 +08:00
|
|
|
provider.send "#{sym}=", opts.delete(sym)
|
|
|
|
end
|
|
|
|
@auth_providers << provider
|
|
|
|
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 = {})
|
|
|
|
gems_path = File.dirname(path) + "/gems/#{RUBY_VERSION}"
|
|
|
|
spec_path = gems_path + "/specifications"
|
|
|
|
spec_file = spec_path + "/#{name}-#{version}.gemspec"
|
|
|
|
unless File.exists? spec_file
|
2014-01-17 09:27:09 +08:00
|
|
|
command = "gem install #{name} -v #{version} -i #{gems_path} --no-document --ignore-dependencies"
|
2013-12-31 06:57:04 +08:00
|
|
|
if opts[:source]
|
|
|
|
command << " --source #{opts[:source]}"
|
|
|
|
end
|
2013-08-23 14:21:52 +08:00
|
|
|
puts command
|
2013-08-26 11:52:15 +08:00
|
|
|
puts `#{command}`
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|
|
|
|
if File.exists? spec_file
|
|
|
|
spec = Gem::Specification.load spec_file
|
|
|
|
spec.activate
|
|
|
|
unless opts[:require] == false
|
2014-02-12 07:53:54 +08:00
|
|
|
require opts[:require_name] ? opts[:require_name] : name
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "You are specifying the gem #{name} in #{path}, however it does not exist!"
|
2013-11-20 11:38:21 +08:00
|
|
|
exit(-1)
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-10 14:30:22 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def register_assets!
|
|
|
|
assets.each do |asset, opts|
|
|
|
|
if asset =~ /\.js$|\.js\.erb$/
|
|
|
|
if opts == :admin
|
|
|
|
DiscoursePluginRegistry.admin_javascripts << asset
|
|
|
|
else
|
|
|
|
if opts == :server_side
|
|
|
|
DiscoursePluginRegistry.server_side_javascripts << asset
|
|
|
|
end
|
|
|
|
DiscoursePluginRegistry.javascripts << asset
|
|
|
|
end
|
|
|
|
elsif asset =~ /\.css$|\.scss$/
|
2014-04-25 16:26:37 +08:00
|
|
|
if opts == :mobile
|
2014-04-10 14:30:22 +08:00
|
|
|
DiscoursePluginRegistry.mobile_stylesheets << asset
|
2014-04-25 16:26:37 +08:00
|
|
|
elsif opts == :desktop
|
|
|
|
DiscoursePluginRegistry.desktop_stylesheets << asset
|
|
|
|
elsif opts == :variables
|
|
|
|
DiscoursePluginRegistry.sass_variables << asset
|
|
|
|
else
|
|
|
|
DiscoursePluginRegistry.stylesheets << asset
|
2014-04-10 14:30:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
elsif asset =~ /\.js\.handlebars$/
|
|
|
|
DiscoursePluginRegistry.handlebars << asset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:21:52 +08:00
|
|
|
end
|