lib/chronicle/etl/registry/plugin_registry.rb in chronicle-etl-0.4.4 vs lib/chronicle/etl/registry/plugin_registry.rb in chronicle-etl-0.5.0

- old
+ new

@@ -11,12 +11,12 @@ # @todo Better validation for whether a gem is actually a plugin # @todo Add ways to load a plugin that don't require a gem on rubygems.org module PluginRegistry # Does this plugin exist? def self.exists?(name) - # TODO: implement this. Could query rubygems.org or have a - # hardcoded approved list + # TODO: implement this. Could query rubygems.org or use a hardcoded + # list somewhere true end # All versions of all plugins currently installed def self.all_installed @@ -29,25 +29,34 @@ all_installed.group_by(&:name) .transform_values { |versions| versions.sort_by(&:version).reverse.first } .values end + # Check whether a given plugin is installed + def self.installed?(name) + gem_name = "chronicle-#{name}" + all_installed.map(&:name).include?(gem_name) + end + # Activate a plugin with given name by `require`ing it def self.activate(name) # By default, activates the latest available version of a gem # so don't have to run Kernel#gem separately require "chronicle/#{name}" rescue Gem::ConflictError => e # TODO: figure out if there's more we can do here raise Chronicle::ETL::PluginConflictError.new(name), "Plugin '#{name}' couldn't be loaded. #{e.message}" - rescue LoadError => e - raise Chronicle::ETL::PluginLoadError.new(name), "Plugin '#{name}' couldn't be loaded" if exists?(name) - - raise Chronicle::ETL::PluginNotAvailableError.new(name), "Plugin #{name} doesn't exist" + rescue StandardError, LoadError => e + # StandardError to catch random non-loading problems that might occur + # when requiring the plugin (eg class macro invoked the wrong way) + # TODO: decide if this should be separated + raise Chronicle::ETL::PluginLoadError.new(name), "Plugin '#{name}' couldn't be loaded" end # Install a plugin to local gems def self.install(name) + return if installed?(name) + gem_name = "chronicle-#{name}" raise(Chronicle::ETL::PluginNotAvailableError.new(gem_name), "Plugin #{name} doesn't exist") unless exists?(gem_name) Gem::DefaultUserInteraction.ui = Gem::SilentUI.new Gem.install(gem_name)