class Bundler::Plugin::Index
def command_plugin(command)
def command_plugin(command) @commands[command] end
def global_index_file
def global_index_file Plugin.global_root.join("index") end
def hook_plugins(event)
def hook_plugins(event) @hooks[event] || [] end
def index_file
def index_file Plugin.root.join("index") end
def initialize
def initialize @plugin_paths = {} @commands = {} @sources = {} @hooks = {} @load_paths = {} begin load_index(global_index_file, true) rescue GenericSystemCallError # no need to fail when on a read-only FS, for example nil end load_index(local_index_file) if SharedHelpers.in_bundle? end
def installed?(name)
def installed?(name) @plugin_paths[name] end
def installed_plugins
def installed_plugins @plugin_paths.keys end
def load_index(index_file, global = false)
-
is
(Boolean
) -- the index file global index -
index
(Pathname
) -- file path
def load_index(index_file, global = false) SharedHelpers.filesystem_access(index_file, :read) do |index_f| valid_file = index_f && index_f.exist? && !index_f.size.zero? break unless valid_file data = index_f.read require_relative "../yaml_serializer" index = YAMLSerializer.load(data) @commands.merge!(index["commands"]) @hooks.merge!(index["hooks"]) @load_paths.merge!(index["load_paths"]) @plugin_paths.merge!(index["plugin_paths"]) @sources.merge!(index["sources"]) unless global end end
def load_paths(name)
def load_paths(name) @load_paths[name] end
def local_index_file
def local_index_file Plugin.local_root.join("index") end
def plugin_commands(plugin)
def plugin_commands(plugin) @commands.find_all {|_, n| n == plugin }.map(&:first) end
def plugin_path(name)
def plugin_path(name) Pathname.new @plugin_paths[name] end
def register_plugin(name, path, load_paths, commands, sources, hooks)
-
sources
(Array
) -- that are handled by the plugin -
commands
(Array
) -- that are handled by the plugin -
load_paths
(Array
) -- for the plugin -
path
(String
) -- where the plugin is installed -
name
(String
) -- of the plugin to be registered
def register_plugin(name, path, load_paths, commands, sources, hooks) old_commands = @commands.dup common = commands & @commands.keys raise CommandConflict.new(name, common) unless common.empty? commands.each {|c| @commands[c] = name } common = sources & @sources.keys raise SourceConflict.new(name, common) unless common.empty? sources.each {|k| @sources[k] = name } hooks.each do |event| event_hooks = (@hooks[event] ||= []) << name event_hooks.uniq! end @plugin_paths[name] = path @load_paths[name] = load_paths save_index rescue StandardError @commands = old_commands raise end
def save_index
instance variables in YAML format. (The instance variables are supposed
Should be called when any of the instance variables change. Stores the
def save_index index = { "commands" => @commands, "hooks" => @hooks, "load_paths" => @load_paths, "plugin_paths" => @plugin_paths, "sources" => @sources, } require_relative "../yaml_serializer" SharedHelpers.filesystem_access(index_file) do |index_f| FileUtils.mkdir_p(index_f.dirname) File.open(index_f, "w") {|f| f.puts YAMLSerializer.dump(index) } end end
def source?(source)
def source?(source) @sources.key? source end
def source_plugin(name)
def source_plugin(name) @sources[name] end
def unregister_plugin(name)
def unregister_plugin(name) @commands.delete_if {|_, v| v == name } @sources.delete_if {|_, v| v == name } @hooks.each do |hook, names| names.delete(name) @hooks.delete(hook) if names.empty? end @plugin_paths.delete(name) @load_paths.delete(name) save_index end