123456789_123456789_123456789_123456789_123456789_

Class: Bundler::Plugin::Index

Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/bundler/plugin/index.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newIndex

[ GitHub ]

  
# 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

Instance Attribute Details

#commands (readonly)

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 23

attr_reader :commands

Instance Method Details

#command_plugin(command)

Fetch the name of plugin handling the command

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 110

def command_plugin(command)
  @commands[command]
end

#global_index_file

Path where the global index file is stored

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 92

def global_index_file
  Plugin.global_root.join("index")
end

#hook_plugins(event)

Returns the list of plugin names handling the passed event

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 135

def hook_plugins(event)
  @hooks[event] || []
end

#index_file

Path of default index file

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 87

def index_file
  Plugin.root.join("index")
end

#installed?(name) ⇒ Boolean

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 114

def installed?(name)
  @plugin_paths[name]
end

#installed_in_plugin_root?(name) ⇒ Boolean

This plugin is installed inside the .bundle/plugin directory, and thus is managed solely by ::Bundler

[ GitHub ]

  
# File 'lib/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

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 118

def installed_plugins
  @plugin_paths.keys
end

#load_index(index_file, global = false) (private)

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

It skips the sources if the second param is true

Parameters:

  • index (Pathname)

    file path

  • is (Boolean)

    the index file global index

[ GitHub ]

  
# File 'lib/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

#load_paths(name)

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 105

def load_paths(name)
  @load_paths[name]
end

#local_index_file

Path where the local index file is stored

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 97

def local_index_file
  Plugin.local_root.join("index")
end

#plugin_commands(plugin)

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 122

def plugin_commands(plugin)
  @commands.find_all {|_, n| n == plugin }.map(&:first)
end

#plugin_path(name)

[ GitHub ]

  
# File 'lib/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)

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.

Parameters:

  • name (String)

    of the plugin to be registered

  • path (String)

    where the plugin is installed

  • load_paths (Array<String>)

    for the plugin

  • commands (Array<String>)

    that are handled by the plugin

  • sources (Array<String>)

    that are handled by the plugin

[ GitHub ]

  
# 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

#save_index (private)

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)

[ GitHub ]

  
# File 'lib/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

#source?(source) ⇒ Boolean

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 126

def source?(source)
  @sources.key? source
end

#source_plugin(name)

[ GitHub ]

  
# File 'lib/bundler/plugin/index.rb', line 130

def source_plugin(name)
  @sources[name]
end

#unregister_plugin(name)

[ GitHub ]

  
# File 'lib/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