Class: RuboCop::Cop::Chef::Modernize::UseMultipackageInstalls

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb

Overview

Pass an array of packages to package resources instead of iterating over an array of packages when using multi-package capable package subsystem such as apt, yum, chocolatey, dnf, or zypper. Multi-package installs are faster and simplify logs.

Examples:


### incorrect
%w(bmon htop vim curl).each do |pkg|
  package pkg do
    action :install
  end
end

### correct
package %w(bmon htop vim curl)

Constant Summary collapse

MSG =
'Pass an array of packages to package resources instead of iterating over an array of packages when using multi-package capable package subsystem such as apt, yum, chocolatey, dnf, or zypper. Multi-package installs are faster and simplify logs.'
MULTIPACKAGE_PLATS =
%w(debian redhat suse amazon fedora scientific oracle rhel ubuntu centos redhat).freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#multipackage_platforms?(condition_obj) ⇒ Boolean

see if all platforms in the when condition are multi-package compliant

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb', line 79

def multipackage_platforms?(condition_obj)
  condition_obj.all? do |p|
    # make sure it's a string (not a regex) and it's in the array
    p.str_type? && MULTIPACKAGE_PLATS.include?(p.value)
  end
end

#on_if(node) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb', line 98

def on_if(node)
  platform_helper?(node) do |plats, blk, _pkgs|
    return unless multipackage_platforms?(plats)

    add_offense(blk, severity: :refactor) do |corrector|
      package_array_install(blk) do |install_block, pkgs|
        corrector.replace(install_block, "package #{pkgs.source}")
      end
    end
  end
end

#on_when(node) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb', line 86

def on_when(node)
  return unless platform_or_platform_family?(node.parent.condition) &&
                multipackage_platforms?(node.conditions)
  return if node.body.nil? # don't blow up on empty whens

  package_array_install(node.body) do |install_block, pkgs|
    add_offense(install_block, severity: :refactor) do |corrector|
      corrector.replace(install_block, "package #{pkgs.source}")
    end
  end
end