1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-02 13:24:20 +01:00
fab-manager/lib/plugin/instance.rb

101 lines
2.4 KiB
Ruby
Raw Normal View History

2019-01-31 17:15:26 +01:00
# frozen_string_literal: true
2016-05-10 16:50:01 +02:00
require 'fileutils'
require 'plugin_registry'
2020-02-26 10:19:43 +01:00
# Fab-manager extensible functionalities
2019-02-11 13:45:43 +01:00
module Plugin; end
2019-01-31 17:15:26 +01:00
class Plugin::Instance
attr_accessor :path
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
%i[assets initializers javascripts styles].each do |att|
class_eval %(
def #{att}
@#{att} ||= []
end
), __FILE__, __LINE__ - 4
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
def self.find_all(parent_path)
[].tap { |plugins|
# also follows symlinks - http://stackoverflow.com/q/357754
Dir["#{parent_path}/**/*/**/plugin.rb"].sort.each do |path|
plugins << new(nil, path)
end
}
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
def initialize(metadata=nil, path=nil)
@metadata = metadata
@path = path
@idx = 0
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
def activate!
if @path
root_path = "#{File.dirname(@path)}/assets/javascripts"
PluginRegistry.register_glob(root_path, 'coffee.erb')
2016-05-10 16:50:01 +02:00
end
2019-01-31 17:15:26 +01:00
instance_eval File.read(path), path # execute all code of the plugin main file ! (named plugin.rb)
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
register_assets! unless assets.blank?
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
Rails.configuration.assets.paths << "#{File.dirname(path)}/assets"
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
Rails.configuration.assets.precompile += [lambda do |_filename, path|
(Dir['plugins/*/assets/templates'].any? { |p| path.include?(p) })
end]
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
Rails.configuration.sass.load_paths += Dir['plugins/*/assets/stylesheets']
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
# Automatically include rake tasks
Rake.add_rakelib("#{File.dirname(path)}/lib/tasks")
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
# Automatically include migrations
Rails.configuration.paths['db/migrate'] << "#{File.dirname(path)}/db/migrate"
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
# to be used by the plugin !
def register_asset(file, opts=nil)
full_path = File.dirname(path) << '/assets/' << file
assets << [full_path, opts]
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
def register_code_insertion(key, code)
PluginRegistry.code_insertions[key] ||= []
PluginRegistry.code_insertions[key] << code
end
2019-01-31 17:15:26 +01:00
# useless ?
def register_css(style)
styles << style
end
2019-01-31 17:15:26 +01:00
def after_initialize(&block)
initializers << block
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
def notify_after_initialize
initializers.each do |callback|
begin
callback.call(self)
rescue ActiveRecord::StatementInvalid => e
# When running db:migrate for the first time on a new database, plugin initializers might
# try to use models. Tolerate it.
raise e unless e.message.try(:include?, 'PG::UndefinedTable')
2016-05-10 16:50:01 +02:00
end
end
2019-01-31 17:15:26 +01:00
end
2016-05-10 16:50:01 +02:00
2019-01-31 17:15:26 +01:00
protected
def register_assets!
assets.each do |asset, opts|
PluginRegistry.register_asset(asset, opts)
end
2016-05-10 16:50:01 +02:00
end
end