Sha256: 1dbc59d70771b22e9ca869e5b4274bd40c72354d2942f659479d5bd6031ec619

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Lolcommits
  class GemPlugin
    attr_accessor :gem_spec, :required

    def initialize(gem_spec)
      @gem_spec = gem_spec
    end

    # activate the plugin (require the gem - enables/loads the plugin
    # immediately at point of call if not already required)
    def activate!
      begin
        require gem_path unless required?
      rescue LoadError => e
        warn "Found plugin #{name}, but could not require gem '#{gem_name}'"
        warn e
      rescue StandardError => e
        warn "require gem '#{gem_name}' failed with: #{e}"
      end

      @required = true
    end

    alias required? required

    def supported?
      # false if the plugin gem does not support this version of Lolcommits
      lolcommits_version = Gem::Version.new(::Lolcommits::VERSION)
      gem_spec.dependencies.each do |dependency|
        if dependency.name == Lolcommits::GEM_NAME
          return dependency.requirement.satisfied_by?(lolcommits_version)
        end
      end
      true
    end

    def plugin_klass
      self.class.const_get(plugin_klass_name)
    rescue StandardError => e
      warn "failed to load constant from plugin gem '#{plugin_klass_name}: #{e}'"
    end

    def plugin_instance(runner)
      plugin_klass.new(runner: runner, config: runner.config.yaml[name], name: name)
    end

    def gem_name
      gem_spec.name
    end

    def name
      gem_name.split('-', 2).last
    end

    private

    def gem_path
      gem_name.tr('-', '/')
    end

    def plugin_klass_name
      # convert gem paths to plugin classes e.g.
      # lolcommits/loltext --> Lolcommits::Plugin::Loltext
      # lolcommits/term_output --> Lolcommits::Plugin::TermOutput
      gem_path.split('/').insert(1, 'plugin').collect do |c|
        c.split('_').collect(&:capitalize).join
      end.join('::')
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lolcommits-0.17.2 lib/lolcommits/gem_plugin.rb
lolcommits-0.17.1 lib/lolcommits/gem_plugin.rb
lolcommits-0.17.0 lib/lolcommits/gem_plugin.rb
lolcommits-0.16.5 lib/lolcommits/gem_plugin.rb
lolcommits-0.16.4 lib/lolcommits/gem_plugin.rb