Class: GitCommander::Plugin::Loader Abstract

Inherits:
Loader
  • Object
show all
Defined in:
lib/git_commander/plugin/loader.rb

Overview

This class is abstract.

Handles loading native plugins by name.

Defined Under Namespace

Classes: LoadError, NotFoundError

Constant Summary collapse

NATIVE_PLUGIN_DIR =
File.expand_path(File.join(__dir__, "..", "plugins"))

Instance Attribute Summary collapse

Attributes inherited from Loader

#registry, #result

Instance Method Summary collapse

Methods inherited from Loader

#system

Constructor Details

#initialize(registry) ⇒ Loader

Returns a new instance of Loader.



17
18
19
20
# File 'lib/git_commander/plugin/loader.rb', line 17

def initialize(registry)
  @commands = []
  super
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



15
16
17
# File 'lib/git_commander/plugin/loader.rb', line 15

def commands
  @commands
end

#contentObject (readonly)

Returns the value of attribute content.



15
16
17
# File 'lib/git_commander/plugin/loader.rb', line 15

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/git_commander/plugin/loader.rb', line 15

def name
  @name
end

Instance Method Details

#command(name, &block) ⇒ Object



51
52
53
54
55
56
# File 'lib/git_commander/plugin/loader.rb', line 51

def command(name, &block)
  GitCommander.logger.debug("Loading command :#{name} from plugin #{@name}")
  @commands << Command::Configurator.new(registry).configure("#{plugin_name_formatted_for_cli}:#{name}".to_sym, &block)
rescue Command::Configurator::ConfigurationError => e
  result.errors << e
end

#load(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git_commander/plugin/loader.rb', line 22

def load(name)
  @plugin = GitCommander::Plugin.new(
    resolve_plugin_name(name),
    source_instance: instance_eval(resolve_content(name))
  )
  @plugin.commands = @commands
  result.plugins << @plugin
  result.commands |= @commands
  result
rescue Errno::ENOENT, Errno::EACCES => e
  handle_error LoadError, e
rescue StandardError => e
  handle_error NotFoundError, e
end

#plugin(name, **options) ⇒ Object



58
59
60
61
# File 'lib/git_commander/plugin/loader.rb', line 58

def plugin(name, **options)
  plugin_result = GitCommander::Plugin::Loader.new(registry).load(name, **options)
  result.plugins |= plugin_result.plugins
end

#resolve_content(native_name_or_filename) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/git_commander/plugin/loader.rb', line 43

def resolve_content(native_name_or_filename)
  if native_name_or_filename.is_a? Symbol
    return @content = File.read("#{NATIVE_PLUGIN_DIR}/#{native_name_or_filename}.rb")
  end

  @content = File.read(native_name_or_filename)
end

#resolve_plugin_name(native_name_or_filename) ⇒ Object



37
38
39
40
41
# File 'lib/git_commander/plugin/loader.rb', line 37

def resolve_plugin_name(native_name_or_filename)
  return @name = native_name_or_filename if native_name_or_filename.is_a? Symbol

  @name = File.basename(native_name_or_filename).split(".").first.to_sym
end