Class: Kreator::PluginManager
- Inherits:
-
Object
- Object
- Kreator::PluginManager
- Defined in:
- lib/kreator/plugin_manager.rb
Constant Summary collapse
- MANIFEST_NAME =
"plugin.json"- DEFAULT_HOME =
File.("~/.kreator")
- BUNDLED_PLUGINS_DIR =
File.("bundled_plugins", __dir__)
Instance Attribute Summary collapse
-
#cwd ⇒ Object
readonly
Returns the value of attribute cwd.
-
#enabled_names ⇒ Object
readonly
Returns the value of attribute enabled_names.
-
#home_dir ⇒ Object
readonly
Returns the value of attribute home_dir.
-
#plugins_enabled ⇒ Object
readonly
Returns the value of attribute plugins_enabled.
Instance Method Summary collapse
- #available_plugins ⇒ Object
-
#initialize(cwd: Dir.pwd, home_dir: DEFAULT_HOME, enabled_names: nil, plugins_enabled: true) ⇒ PluginManager
constructor
A new instance of PluginManager.
- #install(path:, name: nil) ⇒ Object
- #plugin(name) ⇒ Object
- #plugins ⇒ Object
- #remove(name) ⇒ Object
- #update(name:, path:) ⇒ Object
- #validate(path_or_name) ⇒ Object
Constructor Details
#initialize(cwd: Dir.pwd, home_dir: DEFAULT_HOME, enabled_names: nil, plugins_enabled: true) ⇒ PluginManager
Returns a new instance of PluginManager.
14 15 16 17 18 19 |
# File 'lib/kreator/plugin_manager.rb', line 14 def initialize(cwd: Dir.pwd, home_dir: DEFAULT_HOME, enabled_names: nil, plugins_enabled: true) @cwd = File.(cwd) @home_dir = File.(home_dir) @enabled_names = enabled_names&.map(&:to_s) @plugins_enabled = plugins_enabled end |
Instance Attribute Details
#cwd ⇒ Object (readonly)
Returns the value of attribute cwd.
12 13 14 |
# File 'lib/kreator/plugin_manager.rb', line 12 def cwd @cwd end |
#enabled_names ⇒ Object (readonly)
Returns the value of attribute enabled_names.
12 13 14 |
# File 'lib/kreator/plugin_manager.rb', line 12 def enabled_names @enabled_names end |
#home_dir ⇒ Object (readonly)
Returns the value of attribute home_dir.
12 13 14 |
# File 'lib/kreator/plugin_manager.rb', line 12 def home_dir @home_dir end |
#plugins_enabled ⇒ Object (readonly)
Returns the value of attribute plugins_enabled.
12 13 14 |
# File 'lib/kreator/plugin_manager.rb', line 12 def plugins_enabled @plugins_enabled end |
Instance Method Details
#available_plugins ⇒ Object
34 35 36 37 38 |
# File 'lib/kreator/plugin_manager.rb', line 34 def available_plugins Dir.glob(File.join(BUNDLED_PLUGINS_DIR, "*")) .select { |path| File.directory?(path) } .filter_map { |path| load_plugin(path) } end |
#install(path:, name: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kreator/plugin_manager.rb', line 52 def install(path:, name: nil) source = source_path(path) plugin = load_plugin(source) raise ArgumentError, "invalid plugin: #{path}" unless plugin install_name = (name || plugin.name).to_s validate_plugin_name!(install_name) destination = File.join(home_dir, "plugins", install_name) raise ArgumentError, "plugin already installed: #{install_name}" if File.exist?(destination) validate(source).fetch("ok") || raise(ArgumentError, "plugin validation failed") FileUtils.mkdir_p(File.dirname(destination)) FileUtils.cp_r(source, destination) write_manifest_name(destination, install_name) if name load_plugin(destination) end |
#plugin(name) ⇒ Object
30 31 32 |
# File 'lib/kreator/plugin_manager.rb', line 30 def plugin(name) plugins.find { |candidate| candidate.name == name.to_s } end |
#plugins ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/kreator/plugin_manager.rb', line 21 def plugins return [] unless plugins_enabled discovered = merge_plugins(plugin_dirs.filter_map { |path| load_plugin(path) }) selected = discovered.select(&:enabled?) selected = selected.select { |plugin| enabled_names.include?(plugin.name) } if enabled_names selected end |
#remove(name) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/kreator/plugin_manager.rb', line 85 def remove(name) destination = File.join(home_dir, "plugins", name.to_s) raise ArgumentError, "plugin is not installed: #{name}" unless File.directory?(destination) FileUtils.rm_rf(destination) destination end |
#update(name:, path:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kreator/plugin_manager.rb', line 69 def update(name:, path:) source = source_path(path) existing = File.join(home_dir, "plugins", name.to_s) raise ArgumentError, "plugin is not installed: #{name}" unless File.directory?(existing) raise ArgumentError, "invalid plugin: #{path}" unless load_plugin(source) validation = validate(source) raise ArgumentError, "plugin validation failed: #{validation.fetch('errors').join('; ')}" unless validation.fetch("ok") FileUtils.rm_rf(existing) FileUtils.mkdir_p(File.dirname(existing)) FileUtils.cp_r(source, existing) write_manifest_name(existing, name.to_s) load_plugin(existing) end |
#validate(path_or_name) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kreator/plugin_manager.rb', line 40 def validate(path_or_name) plugin = plugin(path_or_name) || available_plugin(path_or_name) || load_plugin(File.(path_or_name)) raise ArgumentError, "plugin not found: #{path_or_name}" unless plugin errors = PluginToolLoader.new.validate(plugin) { "ok" => errors.empty?, "plugin" => plugin.to_h, "errors" => errors } end |