Debian Codegraph @

lib/bundler/plugin/index.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 # Manages which plugins are installed and their sources. This also is supposed to map
5 # which plugin does what (currently the features are not implemented so this class is
6 # now a stub class).
7 module Plugin
8 class Index
9 class CommandConflict < PluginError
10 def initialize(plugin, commands)
11 msg = "Command(s) `#{commands.join("`, `")}` declared by #{plugin} are already registered."
12 super msg
13 end
14 end
15
16 class SourceConflict < PluginError
17 def initialize(plugin, sources)
18 msg = "Source(s) `#{sources.join("`, `")}` declared by #{plugin} are already registered."
19 super msg
20 end
21 end
22
23 attr_reader :commands
24
25 def initialize
26 @plugin_paths = {}
27 @commands = {}
28 @sources = {}
29 @hooks = {}
30 @load_paths = {}
31
32 begin
33 load_index(global_index_file, true)
34 rescue GenericSystemCallError
35 # no need to fail when on a read-only FS, for example
36 nil
37 end
38 load_index(local_index_file) if SharedHelpers.in_bundle?
39 end
40
41 # This function is to be called when a new plugin is installed. This
42 # function shall add the functions of the plugin to existing maps and also
43 # the name to source location.
44 #
45 # @param [String] name of the plugin to be registered
46 # @param [String] path where the plugin is installed
47 # @param [Array<String>] load_paths for the plugin
48 # @param [Array<String>] commands that are handled by the plugin
49 # @param [Array<String>] sources that are handled by the plugin
50 def register_plugin(name, path, load_paths, commands, sources, hooks)
51 old_commands = @commands.dup
52
53 common = commands & @commands.keys
54 raise CommandConflict.new(name, common) unless common.empty?
55 commands.each {|c| @commands[c] = name }
56
57 common = sources & @sources.keys
58 raise SourceConflict.new(name, common) unless common.empty?
59 sources.each {|k| @sources[k] = name }
60
61 hooks.each do |event|
62 event_hooks = (@hooks[event] ||= []) << name
63 event_hooks.uniq!
64 end
65
66 @plugin_paths[name] = path
67 @load_paths[name] = load_paths
68 save_index
69 rescue StandardError
70 @commands = old_commands
71 raise
72 end
73
74 def unregister_plugin(name)
75 @commands.delete_if {|_, v| v == name }
76 @sources.delete_if {|_, v| v == name }
77 @hooks.each do |hook, names|
78 names.delete(name)
79 @hooks.delete(hook) if names.empty?
80 end
81 @plugin_paths.delete(name)
82 @load_paths.delete(name)
83 save_index
84 end
85
86 # Path of default index file
87 def index_file
88 Plugin.root.join("index")
89 end
90
91 # Path where the global index file is stored
92 def global_index_file
93 Plugin.global_root.join("index")
94 end
95
96 # Path where the local index file is stored
97 def local_index_file
98 Plugin.local_root.join("index")
99 end
100
101 def plugin_path(name)
102 Pathname.new @plugin_paths[name]
103 end
104
105 def load_paths(name)
106 @load_paths[name]
107 end
108
109 # Fetch the name of plugin handling the command
110 def command_plugin(command)
111 @commands[command]
112 end
113
114 def installed?(name)
115 @plugin_paths[name]
116 end
117
118 def installed_plugins
119 @plugin_paths.keys
120 end
121
122 def plugin_commands(plugin)
123 @commands.find_all {|_, n| n == plugin }.map(&:first)
124 end
125
126 def source?(source)
127 @sources.key? source
128 end
129
130 def source_plugin(name)
131 @sources[name]
132 end
133
134 # Returns the list of plugin names handling the passed event
135 def hook_plugins(event)
136 @hooks[event] || []
137 end
138
139 # This plugin is installed inside the .bundle/plugin directory,
140 # and thus is managed solely by Bundler
141 def installed_in_plugin_root?(name)
142 return false unless (path = installed?(name))
143
144 path.start_with?("#{Plugin.root}/")
145 end
146
147 private
148
149 # Reads the index file from the directory and initializes the instance
150 # variables.
151 #
152 # It skips the sources if the second param is true
153 # @param [Pathname] index file path
154 # @param [Boolean] is the index file global index
155 def load_index(index_file, global = false)
156 SharedHelpers.filesystem_access(index_file, :read) do |index_f|
157 valid_file = index_f&.exist? && !index_f.size.zero?
158 break unless valid_file
159
160 data = index_f.read
161
162 require_relative "../yaml_serializer"
163 index = YAMLSerializer.load(data)
164
165 @commands.merge!(index["commands"])
166 @hooks.merge!(index["hooks"])
167 @load_paths.merge!(index["load_paths"])
168 @plugin_paths.merge!(index["plugin_paths"])
169 @sources.merge!(index["sources"]) unless global
170 end
171 end
172
173 # Should be called when any of the instance variables change. Stores the
174 # instance variables in YAML format. (The instance variables are supposed
175 # to be only String key value pairs)
176 def save_index
177 index = {
178 "commands" => @commands,
179 "hooks" => @hooks,
180 "load_paths" => @load_paths,
181 "plugin_paths" => @plugin_paths,
182 "sources" => @sources,
183 }
184
185 require_relative "../yaml_serializer"
186 SharedHelpers.filesystem_access(index_file) do |index_f|
187 FileUtils.mkdir_p(index_f.dirname)
188 File.open(index_f, "w") {|f| f.puts YAMLSerializer.dump(index) }
189 end
190 end
191 end
192 end
193 end