class Bundler::Plugin::Index

属性

commands[R]

公共类方法

new() 点击切换源代码
# File bundler/plugin/index.rb, line 25
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

公共实例方法

command_plugin(command) 点击切换源代码

获取处理命令的插件名称

# File bundler/plugin/index.rb, line 110
def command_plugin(command)
  @commands[command]
end
global_index_file() 点击切换源代码

全局索引文件的存储路径

# File bundler/plugin/index.rb, line 92
def global_index_file
  Plugin.global_root.join("index")
end
hook_plugins(event) 点击切换源代码

返回处理传递事件的插件名称列表

# File bundler/plugin/index.rb, line 135
def hook_plugins(event)
  @hooks[event] || []
end
index_file() 点击切换源代码

默认索引文件的路径

# File bundler/plugin/index.rb, line 87
def index_file
  Plugin.root.join("index")
end
installed?(name) 点击切换源代码
# File bundler/plugin/index.rb, line 114
def installed?(name)
  @plugin_paths[name]
end
installed_in_plugin_root?(name) 点击切换源代码

此插件安装在 .bundle/plugin 目录中,因此仅由 Bundler 管理

# File bundler/plugin/index.rb, line 141
def installed_in_plugin_root?(name)
  return false unless (path = installed?(name))

  path.start_with?("#{Plugin.root}/")
end
installed_plugins() 点击切换源代码
# File bundler/plugin/index.rb, line 118
def installed_plugins
  @plugin_paths.keys
end
load_paths(name) 点击切换源代码
# File bundler/plugin/index.rb, line 105
def load_paths(name)
  @load_paths[name]
end
local_index_file() 点击切换源代码

本地索引文件的存储路径

# File bundler/plugin/index.rb, line 97
def local_index_file
  Plugin.local_root.join("index")
end
plugin_commands(plugin) 点击切换源代码
# File bundler/plugin/index.rb, line 122
def plugin_commands(plugin)
  @commands.find_all {|_, n| n == plugin }.map(&:first)
end
plugin_path(name) 点击切换源代码
# File bundler/plugin/index.rb, line 101
def plugin_path(name)
  Pathname.new @plugin_paths[name]
end
register_plugin(name, path, load_paths, commands, sources, hooks) 点击切换源代码

当安装新插件时,应调用此函数。此函数应将插件的功能添加到现有映射,并将名称添加到源位置。

@param [String] 要注册的插件的名称 @param [String] 插件的安装路径 @param [Array<String>] 插件的 load_paths @param [Array<String>] 由插件处理的命令 @param [Array<String>] 由插件处理的源

# File bundler/plugin/index.rb, line 50
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
source?(source) 点击切换源代码
# File bundler/plugin/index.rb, line 126
def source?(source)
  @sources.key? source
end
source_plugin(name) 点击切换源代码
# File bundler/plugin/index.rb, line 130
def source_plugin(name)
  @sources[name]
end
unregister_plugin(name) 点击切换源代码
# File bundler/plugin/index.rb, line 74
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

私有实例方法

load_index(index_file, global = false) 点击切换源代码

从目录读取索引文件并初始化实例变量。

如果第二个参数为 true,则跳过源 @param [Pathname] 索引文件路径 @param [Boolean] 索引文件是否为全局索引

# File bundler/plugin/index.rb, line 155
def load_index(index_file, global = false)
  SharedHelpers.filesystem_access(index_file, :read) do |index_f|
    valid_file = 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
save_index() 点击切换源代码

当任何实例变量发生更改时,应调用此方法。将实例变量以 YAML 格式存储。(实例变量应该只是字符串键值对)

# File bundler/plugin/index.rb, line 176
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