Module: Bundler::Plugin
- Defined in:
- lib/bundler/plugin/api.rb,
lib/bundler/plugin.rb,
lib/bundler/plugin/dsl.rb,
lib/bundler/plugin/index.rb,
lib/bundler/plugin/events.rb,
lib/bundler/plugin/installer.rb,
lib/bundler/plugin/api/source.rb,
lib/bundler/plugin/source_list.rb,
lib/bundler/plugin/installer/git.rb,
lib/bundler/plugin/installer/rubygems.rb
Overview
SourceList object to be used while parsing the Gemfile, setting the approptiate options to be used with Source classes for plugin installation
Defined Under Namespace
Modules: Events Classes: API, DSL, Index, Installer, MalformattedPlugin, PluginInstallError, SourceList, UndefinedCommandError, UnknownSourceError
Constant Summary collapse
- PLUGIN_FILE_NAME =
"plugins.rb"
Class Method Summary collapse
-
.add_command(command, cls) ⇒ Object
To be called via the API to register to handle a command.
-
.add_hook(event, &block) ⇒ Object
To be called via the API to register a hooks and corresponding block that will be called to handle the hook.
-
.add_source(source, cls) ⇒ Object
To be called via the API to register to handle a source plugin.
-
.cache ⇒ Object
The cache directory for plugin stuffs.
-
.command?(command) ⇒ Boolean
Checks if any plugin handles the command.
-
.exec_command(command, args) ⇒ Object
To be called from Cli class to pass the command and argument to appropriate plugin class.
-
.gemfile_install(gemfile = nil, &inline) ⇒ Object
Evaluates the Gemfile with a limited DSL and installs the plugins specified by plugin method.
-
.global_root ⇒ Object
The global directory root for all plugin related data.
-
.hook(event, *args, &arg_blk) ⇒ Object
Runs all the hooks that are registered for the passed event.
-
.index ⇒ Object
The index object used to store the details about the plugin.
-
.install(names, options) ⇒ Object
Installs a new plugin by the given name.
-
.installed?(plugin) ⇒ String?
currently only intended for specs.
-
.list ⇒ Object
List installed plugins and commands.
-
.load_plugin(name) ⇒ Object
Executes the plugins.rb file.
- .local_root ⇒ Object
-
.register_plugin(name, spec, optional_plugin = false) ⇒ Object
Runs the plugins.rb file in an isolated namespace, records the plugin actions it registers for and then passes the data to index to be stored.
- .reset! ⇒ Object
-
.root ⇒ Object
The directory root for all plugin related data.
-
.save_plugin(name, spec, optional_plugin = false) ⇒ Object
Validates and registers a plugin.
-
.save_plugins(plugins, specs, optional_plugins = []) ⇒ Object
Post installation processing and registering with index.
-
.source(name) ⇒ Class
That handles the source.
-
.source?(name) ⇒ Boolean
Checks if any plugin declares the source.
-
.source_from_lock(locked_opts) ⇒ API::Source
The instance of the class that handles the source type passed in locked_opts.
-
.uninstall(names, options) ⇒ Object
Uninstalls plugins by the given names.
-
.validate_plugin!(plugin_path) ⇒ Object
Checks if the gem is good to be a plugin.
Class Method Details
.add_command(command, cls) ⇒ Object
To be called via the API to register to handle a command
159 160 161 |
# File 'lib/bundler/plugin.rb', line 159 def add_command(command, cls) @commands[command] = cls end |
.add_hook(event, &block) ⇒ Object
To be called via the API to register a hooks and corresponding block that will be called to handle the hook
208 209 210 211 212 213 |
# File 'lib/bundler/plugin.rb', line 208 def add_hook(event, &block) unless Events.defined_event?(event) raise ArgumentError, "Event '#{event}' not defined in Bundler::Plugin::Events" end @hooks_by_event[event.to_s] << block end |
.add_source(source, cls) ⇒ Object
To be called via the API to register to handle a source plugin
179 180 181 |
# File 'lib/bundler/plugin.rb', line 179 def add_source(source, cls) @sources[source] = cls end |
.cache ⇒ Object
The cache directory for plugin stuffs
154 155 156 |
# File 'lib/bundler/plugin.rb', line 154 def cache @cache ||= root.join("cache") end |
.command?(command) ⇒ Boolean
Checks if any plugin handles the command
164 165 166 |
# File 'lib/bundler/plugin.rb', line 164 def command?(command) !index.command_plugin(command).nil? end |
.exec_command(command, args) ⇒ Object
To be called from Cli class to pass the command and argument to appropriate plugin class
170 171 172 173 174 175 176 |
# File 'lib/bundler/plugin.rb', line 170 def exec_command(command, args) raise UndefinedCommandError, "Command `#{command}` not found" unless command? command load_plugin index.command_plugin(command) unless @commands.key? command @commands[command].new.exec(command, args) end |
.gemfile_install(gemfile = nil, &inline) ⇒ Object
Evaluates the Gemfile with a limited DSL and installs the plugins specified by plugin method
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bundler/plugin.rb', line 102 def gemfile_install(gemfile = nil, &inline) Bundler.settings.temporary(:frozen => false, :deployment => false) do builder = DSL.new if block_given? builder.instance_eval(&inline) else builder.eval_gemfile(gemfile) end builder.check_primary_source_safety definition = builder.to_definition(nil, true) return if definition.dependencies.empty? plugins = definition.dependencies.map(&:name).reject {|p| index.installed? p } installed_specs = Installer.new.install_definition(definition) save_plugins plugins, installed_specs, builder.inferred_plugins end rescue RuntimeError => e unless e.is_a?(GemfileError) Bundler.ui.error "Failed to install plugin: #{e.}\n #{e.backtrace[0]}" end raise end |
.global_root ⇒ Object
The global directory root for all plugin related data
149 150 151 |
# File 'lib/bundler/plugin.rb', line 149 def global_root Bundler.user_bundle_path("plugin") end |
.hook(event, *args, &arg_blk) ⇒ Object
Runs all the hooks that are registered for the passed event
It passes the passed arguments and block to the block registered with the api.
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/bundler/plugin.rb', line 221 def hook(event, *args, &arg_blk) return unless Bundler.feature_flag.plugins? unless Events.defined_event?(event) raise ArgumentError, "Event '#{event}' not defined in Bundler::Plugin::Events" end plugins = index.hook_plugins(event) return unless plugins.any? (plugins - @loaded_plugin_names).each {|name| load_plugin(name) } @hooks_by_event[event].each {|blk| blk.call(*args, &arg_blk) } end |
.index ⇒ Object
The index object used to store the details about the plugin
128 129 130 |
# File 'lib/bundler/plugin.rb', line 128 def index @index ||= Index.new end |
.install(names, options) ⇒ Object
Installs a new plugin by the given name
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bundler/plugin.rb', line 38 def install(names, ) raise InvalidOption, "You cannot specify `--branch` and `--ref` at the same time." if ["branch"] && ["ref"] specs = Installer.new.install(names, ) save_plugins names, specs rescue PluginError specs_to_delete = specs.select {|k, _v| names.include?(k) && !index.commands.values.include?(k) } specs_to_delete.each_value {|spec| Bundler.rm_rf(spec.full_gem_path) } raise end |
.installed?(plugin) ⇒ String?
currently only intended for specs
238 239 240 |
# File 'lib/bundler/plugin.rb', line 238 def installed?(plugin) Index.new.installed?(plugin) end |
.list ⇒ Object
List installed plugins and commands
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bundler/plugin.rb', line 79 def list installed_plugins = index.installed_plugins if installed_plugins.any? output = String.new installed_plugins.each do |plugin| output << "#{plugin}\n" output << "-----\n" index.plugin_commands(plugin).each do |command| output << " #{command}\n" end output << "\n" end else output = "No plugins installed" end Bundler.ui.info output end |
.load_plugin(name) ⇒ Object
Executes the plugins.rb file
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/bundler/plugin.rb', line 330 def load_plugin(name) return unless name && !name.empty? # Need to ensure before this that plugin root where the rest of gems # are installed to be on load path to support plugin deps. Currently not # done to avoid conflicts path = index.plugin_path(name) Bundler.rubygems.add_to_load_path(index.load_paths(name)) load path.join(PLUGIN_FILE_NAME) @loaded_plugin_names << name rescue RuntimeError => e Bundler.ui.error "Failed loading plugin #{name}: #{e.}" raise end |
.local_root ⇒ Object
144 145 146 |
# File 'lib/bundler/plugin.rb', line 144 def local_root Bundler.app_config_path.join("plugin") end |
.register_plugin(name, spec, optional_plugin = false) ⇒ Object
Runs the plugins.rb file in an isolated namespace, records the plugin actions it registers for and then passes the data to index to be stored.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/bundler/plugin.rb', line 294 def register_plugin(name, spec, optional_plugin = false) commands = @commands sources = @sources hooks = @hooks_by_event @commands = {} @sources = {} @hooks_by_event = Hash.new {|h, k| h[k] = [] } load_paths = spec.load_paths Bundler.rubygems.add_to_load_path(load_paths) path = Pathname.new spec.full_gem_path begin load path.join(PLUGIN_FILE_NAME), true rescue StandardError => e raise MalformattedPlugin, "#{e.class}: #{e.}" end if optional_plugin && @sources.keys.any? {|s| source? s } Bundler.rm_rf(path) false else index.register_plugin(name, path.to_s, load_paths, @commands.keys, @sources.keys, @hooks_by_event.keys) true end ensure @commands = commands @sources = sources @hooks_by_event = hooks end |
.reset! ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/bundler/plugin.rb', line 22 def reset! instance_variables.each {|i| remove_instance_variable(i) } @sources = {} @commands = {} @hooks_by_event = Hash.new {|h, k| h[k] = [] } @loaded_plugin_names = [] end |
.root ⇒ Object
The directory root for all plugin related data
If run in an app, points to local root, in app_config_path Otherwise, points to global root, in Bundler.user_bundle_path(“plugin”)
136 137 138 139 140 141 142 |
# File 'lib/bundler/plugin.rb', line 136 def root @root ||= if SharedHelpers.in_bundle? local_root else global_root end end |
.save_plugin(name, spec, optional_plugin = false) ⇒ Object
Validates and registers a plugin.
277 278 279 280 281 282 283 |
# File 'lib/bundler/plugin.rb', line 277 def save_plugin(name, spec, optional_plugin = false) validate_plugin! Pathname.new(spec.full_gem_path) installed = register_plugin(name, spec, optional_plugin) Bundler.ui.info "Installed plugin #{name}" if installed rescue PluginError => e raise PluginInstallError, "Failed to install plugin `#{spec.name}`, due to #{e.class} (#{e.})" end |
.save_plugins(plugins, specs, optional_plugins = []) ⇒ Object
Post installation processing and registering with index
248 249 250 251 252 253 254 255 256 |
# File 'lib/bundler/plugin.rb', line 248 def save_plugins(plugins, specs, optional_plugins = []) plugins.each do |name| next if index.installed?(name) spec = specs[name] save_plugin(name, spec, optional_plugins.include?(name)) end end |
.source(name) ⇒ Class
Returns that handles the source. The class includes API::Source.
189 190 191 192 193 194 195 |
# File 'lib/bundler/plugin.rb', line 189 def source(name) raise UnknownSourceError, "Source #{name} not found" unless source? name load_plugin(index.source_plugin(name)) unless @sources.key? name @sources[name] end |
.source?(name) ⇒ Boolean
Checks if any plugin declares the source
184 185 186 |
# File 'lib/bundler/plugin.rb', line 184 def source?(name) !index.source_plugin(name.to_s).nil? end |
.source_from_lock(locked_opts) ⇒ API::Source
Returns the instance of the class that handles the source type passed in locked_opts.
200 201 202 203 204 |
# File 'lib/bundler/plugin.rb', line 200 def source_from_lock(locked_opts) src = source(locked_opts["type"]) src.new(locked_opts.merge("uri" => locked_opts["remote"])) end |
.uninstall(names, options) ⇒ Object
Uninstalls plugins by the given names
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bundler/plugin.rb', line 54 def uninstall(names, ) if names.empty? && ![:all] Bundler.ui.error "No plugins to uninstall. Specify at least 1 plugin to uninstall.\n"\ "Use --all option to uninstall all the installed plugins." return end names = index.installed_plugins if [:all] if names.any? names.each do |name| if index.installed?(name) Bundler.rm_rf(index.plugin_path(name)) index.unregister_plugin(name) Bundler.ui.info "Uninstalled plugin #{name}" else Bundler.ui.error "Plugin #{name} is not installed \n" end end else Bundler.ui.info "No plugins installed" end end |
.validate_plugin!(plugin_path) ⇒ Object
Checks if the gem is good to be a plugin
At present it only checks whether it contains plugins.rb file
264 265 266 267 |
# File 'lib/bundler/plugin.rb', line 264 def validate_plugin!(plugin_path) plugin_file = plugin_path.join(PLUGIN_FILE_NAME) raise MalformattedPlugin, "#{PLUGIN_FILE_NAME} was not found in the plugin." unless plugin_file.file? end |