W3cubDocs

/Ruby 2.7

class Bundler::Plugin::Index

Parent:
Object

Attributes

commands[R]

Public Class Methods

new() Show source
# File lib/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

Public Instance Methods

command_plugin(command) Show source
# File lib/bundler/plugin/index.rb, line 98
def command_plugin(command)
  @commands[command]
end

Fetch the name of plugin handling the command

global_index_file() Show source
# File lib/bundler/plugin/index.rb, line 80
def global_index_file
  Plugin.global_root.join("index")
end

Path where the global index file is stored

hook_plugins(event) Show source
# File lib/bundler/plugin/index.rb, line 123
def hook_plugins(event)
  @hooks[event] || []
end

Returns the list of plugin names handling the passed event

index_file() Show source
# File lib/bundler/plugin/index.rb, line 75
def index_file
  Plugin.root.join("index")
end

Path of default index file

installed?(name) Show source
# File lib/bundler/plugin/index.rb, line 102
def installed?(name)
  @plugin_paths[name]
end
installed_plugins() Show source
# File lib/bundler/plugin/index.rb, line 106
def installed_plugins
  @plugin_paths.keys
end
load_paths(name) Show source
# File lib/bundler/plugin/index.rb, line 93
def load_paths(name)
  @load_paths[name]
end
local_index_file() Show source
# File lib/bundler/plugin/index.rb, line 85
def local_index_file
  Plugin.local_root.join("index")
end

Path where the local index file is stored

plugin_commands(plugin) Show source
# File lib/bundler/plugin/index.rb, line 110
def plugin_commands(plugin)
  @commands.find_all {|_, n| n == plugin }.map(&:first)
end
plugin_path(name) Show source
# File lib/bundler/plugin/index.rb, line 89
def plugin_path(name)
  Pathname.new @plugin_paths[name]
end
register_plugin(name, path, load_paths, commands, sources, hooks) Show source
# File lib/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

This function is to be called when a new plugin is installed. This function shall add the functions of the plugin to existing maps and also the name to source location.

@param [String] name of the plugin to be registered @param [String] path where the plugin is installed @param [Array<String>] load_paths for the plugin @param [Array<String>] commands that are handled by the plugin @param [Array<String>] sources that are handled by the plugin

source?(source) Show source
# File lib/bundler/plugin/index.rb, line 114
def source?(source)
  @sources.key? source
end
source_plugin(name) Show source
# File lib/bundler/plugin/index.rb, line 118
def source_plugin(name)
  @sources[name]
end

Private Instance Methods

load_index(index_file, global = false) Show source
# File lib/bundler/plugin/index.rb, line 135
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

Reads the index file from the directory and initializes the instance variables.

It skips the sources if the second param is true @param [Pathname] index file path @param [Boolean] is the index file global index

save_index() Show source
# File lib/bundler/plugin/index.rb, line 156
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

Should be called when any of the instance variables change. Stores the instance variables in YAML format. (The instance variables are supposed to be only String key value pairs)

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.