Class: CLAide::Command::PluginManager
- Inherits:
-
Object
- Object
- CLAide::Command::PluginManager
- Defined in:
- lib/claide/command/plugin_manager.rb
Overview
Handles plugin related logic logic for the Command
class.
Plugins are loaded the first time a command run and are identified by the prefix specified in the command class. Plugins must adopt the following conventions:
- Support being loaded by a file located under the
lib/#{plugin_prefix}_plugin
relative path. - Be stored in a folder named after the plugin.
Helper Methods collapse
-
.plugin_gems_for_prefix(prefix) ⇒ Array<[Gem::Specification, Array<String>]>
Returns an array of tuples containing the specifications and plugin files to require for a given plugin prefix.
-
.safe_require(paths) ⇒ Bool
Requires the given paths.
Class Method Summary collapse
-
.installed_specifications_for_prefix(plugin_prefix) ⇒ Array<Specification>
The RubyGems specifications for the installed plugins that match the given
plugin_prefix
. -
.load_plugins(plugin_prefix) ⇒ Array<Gem::Specification>
Loads plugins via RubyGems looking for files named after the
PLUGIN_PREFIX_plugin
and returns the specifications of the gems loaded successfully. -
.loaded_plugins ⇒ Hash<String,Gem::Specification>
The loaded plugins, grouped by plugin prefix.
-
.plugins_involved_in_exception(exception) ⇒ Array<String>
The list of the plugins whose root path appears in the backtrace of an exception.
-
.specifications ⇒ Array<Specification>
The RubyGems specifications for the loaded plugins.
Class Method Details
.installed_specifications_for_prefix(plugin_prefix) ⇒ Array<Specification>
45 46 47 48 |
# File 'lib/claide/command/plugin_manager.rb', line 45 def self.installed_specifications_for_prefix(plugin_prefix) loaded_plugins[plugin_prefix] || plugin_gems_for_prefix(plugin_prefix).map(&:first) end |
.load_plugins(plugin_prefix) ⇒ Array<Gem::Specification>
28 29 30 31 32 33 |
# File 'lib/claide/command/plugin_manager.rb', line 28 def self.load_plugins(plugin_prefix) loaded_plugins[plugin_prefix] ||= plugin_gems_for_prefix(plugin_prefix).map do |spec, paths| spec if safe_require(paths) end.compact end |
.loaded_plugins ⇒ Hash<String,Gem::Specification>
19 20 21 |
# File 'lib/claide/command/plugin_manager.rb', line 19 def self.loaded_plugins @loaded_plugins ||= {} end |
.plugin_gems_for_prefix(prefix) ⇒ Array<[Gem::Specification, Array<String>]>
72 73 74 75 76 77 78 |
# File 'lib/claide/command/plugin_manager.rb', line 72 def self.plugin_gems_for_prefix(prefix) glob = "#{prefix}_plugin#{Gem.suffix_pattern}" Gem::Specification.latest_specs(true).map do |spec| matches = spec.matches_for_glob(glob) [spec, matches] unless matches.empty? end.compact end |
.plugins_involved_in_exception(exception) ⇒ Array<String>
56 57 58 59 60 61 62 63 64 |
# File 'lib/claide/command/plugin_manager.rb', line 56 def self.plugins_involved_in_exception(exception) specifications.select do |gemspec| exception.backtrace.any? do |line| full_require_paths_for(gemspec).any? do |plugin_path| line.include?(plugin_path) end end end.map(&:name) end |
.safe_require(paths) ⇒ Bool
Requires the given paths. If any exception occurs it is caught and an informative message is printed.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/claide/command/plugin_manager.rb', line 89 def self.safe_require(paths) paths.each do |path| begin require(path) rescue Exception => exception # rubocop:disable RescueException = "\n---------------------------------------------" << "\nError loading plugin file `#{path}`.\n" << "\n#{exception.class} - #{exception.}" << "\n#{exception.backtrace.join("\n")}" << "\n---------------------------------------------\n" warn .ansi.yellow return false end end true end |
.specifications ⇒ Array<Specification>
38 39 40 |
# File 'lib/claide/command/plugin_manager.rb', line 38 def self.specifications loaded_plugins.values.flatten.uniq end |