Class: Minfra::Cli::Plugins::Plugin

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/minfra/cli/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #deprecated, #error, #exit_error, #info, #warn

Constructor Details

#initialize(name:, version:, opts:, disabled:) ⇒ Plugin

Returns a new instance of Plugin.



10
11
12
13
14
15
16
17
18
# File 'lib/minfra/cli/plugin.rb', line 10

def initialize(name:, version:, opts:, disabled:)
  @name = name
  @version = version
  @opts = opts.merge(require: false)
  @disabled = disabled
  return unless opts['path']

  @path = Minfra::Cli.config.base_path.join(opts['path'])
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/minfra/cli/plugin.rb', line 8

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/minfra/cli/plugin.rb', line 8

def opts
  @opts
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/minfra/cli/plugin.rb', line 8

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/minfra/cli/plugin.rb', line 8

def version
  @version
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/minfra/cli/plugin.rb', line 20

def disabled?
  @disabled
end

#installObject



24
25
26
27
28
29
30
# File 'lib/minfra/cli/plugin.rb', line 24

def install
  if path
    system("cd #{path}; bundle install")
  else
    system("gem install #{name} --version #{version}")
  end
end

#prepareObject

adds the plugin to the



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/minfra/cli/plugin.rb', line 33

def prepare
  debug("plugin prepare: #{name}, #{version}, disabled: #{disabled?}")
  return if disabled?

  if path
    begin
      lib_path = path.join('lib')
      $LOAD_PATH.unshift lib_path
      require name
    rescue Gem::Requirement::BadRequirementError, LoadError
      warn("plugin prepare path: #{name} (#{$ERROR_INFO})")
    end
  else
    begin
      @gem_spec = Gem::Specification.find_by_name(name)
      gem name, version
    rescue Gem::MissingSpecError
      warn("plugin prepare gem: #{name}, #{version} (#{$ERROR_INFO})")
    end
  end
end

#setupObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/minfra/cli/plugin.rb', line 55

def setup
  return if disabled?

  if path
    minfra_path = Pathname.new(path).join('minfracs', 'init.rb')
    if minfra_path.exist?
      begin
        require minfra_path # this should register the command
      rescue LoadError
        logger.warn("Minfra plugin detected but dependencies not installed: #{minfra_path} (#{$ERROR_INFO}). TRY: minfra plugin install")
      end
    end
  else
    error('Gem based plugins not supported yet')
  end
end