<commit_before>provides :inspec_gem
resource_name :inspec_gem

property :gem_name, String, name_property: true
property :version, String
property :source, String

default_action :install

action :install do
  # detect if installation is required
  installation_required = inspec_info.nil? || !version.nil?

  # detect if the same version is already installed
  if !inspec_info.nil?
    installed_version = inspec_info.version.to_s
    Chef::Log.debug("Installed InSpec version: #{installed_version}")
    installation_required = false if version == installed_version
  end
  Chef::Log.info("Installation of InSpec required: #{installation_required}")

  # only uninstall if InSpec is installed
  if installation_required && !inspec_info.nil?
    converge_by 'uninstall all inspec and train gem versions' do
      uninstall_inspec_gem
    end
  end

  if installation_required
    converge_by 'install latest InSpec version' do
      install_inspec_gem(version: version, source: source)
    end
  else
    Chef::Log.info("inspec_gem: not installing InSpec. It's already installed or an explicit version was not supplied.")
  end
end

action_class do
  def install_inspec_gem(options)
    gem_source  = options[:source]
    gem_version = options[:version]

    chef_gem 'inspec' do
      version gem_version if !gem_version.nil? && gem_version != 'latest'
      clear_sources true unless gem_source.nil?
      source gem_source unless gem_source.nil?
      action :install
    end
  end

  def uninstall_inspec_gem<commit_msg>Disable default source when using user-supplied gem source (#265)<commit_after>provides :inspec_gem
resource_name :inspec_gem

property :gem_name, String, name_property: true
property :version, String
property :source, String

default_action :install

action :install do
  # detect if installation is required
  installation_required = inspec_info.nil? || !version.nil?

  # detect if the same version is already installed
  if !inspec_info.nil?
    installed_version = inspec_info.version.to_s
    Chef::Log.debug("Installed InSpec version: #{installed_version}")
    installation_required = false if version == installed_version
  end
  Chef::Log.info("Installation of InSpec required: #{installation_required}")

  # only uninstall if InSpec is installed
  if installation_required && !inspec_info.nil?
    converge_by 'uninstall all inspec and train gem versions' do
      uninstall_inspec_gem
    end
  end

  if installation_required
    converge_by 'install latest InSpec version' do
      install_inspec_gem(version: version, source: source)
    end
  else
    Chef::Log.info("inspec_gem: not installing InSpec. It's already installed or an explicit version was not supplied.")
  end
end

action_class do
  def install_inspec_gem(options)
    gem_source  = options[:source]
    gem_version = options[:version]

    chef_gem 'inspec' do
      version gem_version if !gem_version.nil? && gem_version != 'latest'
      unless gem_source.nil?
        clear_sources true
        include_default_source false if respond_to?(:include_default_source)
        source gem_source
      end
      action :install
    end
  end

  def uninstall_inspec_gem
{'fraction_non_alphanumeric': 0.06994328922495274, 'fraction_numerical': 0.000945179584120983, 'mean_word_length': 4.204918032786885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7284764', 'n_tokens_mistral': 937, 'n_tokens_neox': 930, 'n_words': 284}