Class: Itamae::Resource::GemPackage

Inherits:
Base
  • Object
show all
Defined in:
lib/itamae/resource/gem_package.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #current_attributes, #notifications, #recipe, #resource_name, #subscriptions, #updated

Instance Method Summary collapse

Methods inherited from Base

#action_nothing, define_attribute, #do_not_run_because_of_not_if?, #do_not_run_because_of_only_if?, inherited, #initialize, #resource_type, #run

Constructor Details

This class inherits a constructor from Itamae::Resource::Base

Instance Method Details

#action_install(action_options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/itamae/resource/gem_package.rb', line 34

def action_install(action_options)
  if current.installed
    if attributes.version && current.version != attributes.version
      install!
      updated!
    end
  else
    install!
    updated!
  end
end

#action_uninstall(action_options) ⇒ Object



46
47
48
# File 'lib/itamae/resource/gem_package.rb', line 46

def action_uninstall(action_options)
  uninstall! if current.installed
end

#action_upgrade(action_options) ⇒ Object



50
51
52
53
54
55
# File 'lib/itamae/resource/gem_package.rb', line 50

def action_upgrade(action_options)
  return if current.installed && attributes.version && current.version == attributes.version

  install!
  updated!
end

#install!Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/itamae/resource/gem_package.rb', line 71

def install!
  cmd = [*Array(attributes.gem_binary), 'install', *Array(attributes.options)]
  if attributes.version
    cmd << '-v' << attributes.version
  end
  if attributes.source
    cmd << '--source' << attributes.source
  end
  cmd << attributes.package_name

  run_command(cmd)
end

#installed_gemsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/itamae/resource/gem_package.rb', line 57

def installed_gems
  gems = []
  run_command([*Array(attributes.gem_binary), 'list', '-l']).stdout.each_line do |line|
    if /\A([^ ]+) \(([^\)]+)\)\z/ =~ line.strip
      name = $1
      versions = $2.split(', ')
      gems << {name: name, versions: versions}
    end
  end
  gems
rescue Backend::CommandExecutionError
  []
end

#pre_actionObject



11
12
13
14
15
16
17
18
# File 'lib/itamae/resource/gem_package.rb', line 11

def pre_action
  case @current_action
  when :install
    attributes.installed = true
  when :uninstall
    attributes.installed = false
  end
end

#set_current_attributesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/itamae/resource/gem_package.rb', line 20

def set_current_attributes
  installed = installed_gems.find {|g| g[:name] == attributes.package_name }
  current.installed = !!installed

  if current.installed
    versions = installed[:versions]
    if versions.include?(attributes.version)
      current.version = attributes.version
    else
      current.version = versions.first
    end
  end
end

#uninstall!Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/itamae/resource/gem_package.rb', line 84

def uninstall!
  cmd = [*Array(attributes.gem_binary), 'uninstall', '--ignore-dependencies', '--executables', *Array(attributes.options)]
  if attributes.version
    cmd << '-v' << attributes.version
  else
    cmd << '--all'
  end
  cmd << attributes.package_name

  run_command(cmd)
end