Module: PuppetX::Chocolatey::ChocolateyCommon

Defined in:
lib/puppet_x/chocolatey/chocolatey_common.rb

Overview

Module used for general Chocolatey commands and constants

Constant Summary collapse

FIRST_COMPILED_CHOCO_VERSION =

First C# version of Chocolatey

'0.9.9.0'
MINIMUM_SUPPORTED_CHOCO_VERSION_EXIT_CODES =

Specifes the minimum version that allows the ‘ignore-package-exit-codes` flag

'0.9.10.0'
MINIMUM_SUPPORTED_CHOCO_UNINSTALL_SOURCE =

Specifies the minimum version that allows uninstalling with a source argument

'0.9.10.0'
MINIMUM_SUPPORTED_CHOCO_VERSION_NO_PROGRESS =

Specifies the minimum version that allows the ‘–no-progress’ flag

'0.10.4.0'

Class Method Summary collapse

Class Method Details

.choco_config_fileString

Retrieves the path of the Chocolatey configuration file.

Returns:

  • (String)

    Path to config file



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 83

def choco_config_file
  choco_install_path = Facter.value('choco_install_path') || PuppetX::Chocolatey::ChocolateyInstall.install_path
  choco_config = "#{choco_install_path}\\config\\chocolatey.config"

  # choco may be installed, but a config file doesn't exist until the
  # first run of choco - trigger that by checking the version
  _choco_run_ensure_config = choco_version

  return choco_config if file_exists?(choco_config)

  old_choco_config = "#{choco_install_path}\\chocolateyinstall\\chocolatey.config"

  return old_choco_config if file_exists?(old_choco_config)

  nil
end

.choco_versionString

Retrieves version of currently installed Chocolatey package.

Returns:

  • (String)

    Semver string of Chocolatey version



63
64
65
66
67
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 63

def choco_version
  version_fact = Facter.value('chocolateyversion')
  @chocoversion ||= strip_beta_from_version((version_fact == '0' ? nil : version_fact) || PuppetX::Chocolatey::ChocolateyVersion.version)
  @chocoversion
end

.chocolatey_commandString

Retrieves path of Chocolatey executable.

Returns:

  • (String)

    Path to Chocolatey executable



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 26

def chocolatey_command
  if Puppet::Util::Platform.windows?
    # When attempting to find the choco command executable, the following
    # paths are checked:
    # - Start with the install_path. If choco is found with environment
    #   variables through the registry or a check on the
    #   ChocolateyInstall env var (first install of Choco may only have
    #   this), then use that path.
    # - Next look to the most commonly used install location (ProgramData)
    # - Fall back to the older install location for older installations
    # - If all else fails, attempt to find Chocolatey in the default place
    #   it installs
    choco_install_path = Facter.value('choco_install_path') || PuppetX::Chocolatey::ChocolateyInstall.install_path

    chocopath =  (choco_install_path if choco_install_path && file_exists?("#{choco_install_path}\\choco.exe")) ||
                 ('C:\ProgramData\chocolatey' if file_exists?('C:\ProgramData\chocolatey\choco.exe')) ||
                 ('C:\Chocolatey' if file_exists?('C:\Chocolatey\choco.exe')) ||
                 "#{ENV['ALLUSERSPROFILE']}\\chocolatey"

    chocopath += '\choco.exe'
  else
    chocopath = 'choco.exe'
  end

  chocopath
end

.clear_cached_valuesObject

Clears global common constants.



102
103
104
105
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 102

def clear_cached_values
  @chocoversion = nil
  @compiled_choco = nil
end

.file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 18

def file_exists?(path)
  File.exist?(path)
end

.set_env_chocolateyinstallObject

Sets the ‘ChocolateyInstall` environment variables to the current Chocolatey install path



55
56
57
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 55

def set_env_chocolateyinstall
  ENV['ChocolateyInstall'] = Facter.value('choco_install_path') || PuppetX::Chocolatey::ChocolateyInstall.install_path
end

.strip_beta_from_version(value) ⇒ String

Removes the beta section of the version string.

Parameters:

  • value (String)

    Semver string

Returns:

  • (String)

    Semver string with beta postfix removed



74
75
76
77
78
# File 'lib/puppet_x/chocolatey/chocolatey_common.rb', line 74

def self.strip_beta_from_version(value)
  return nil if value.nil?

  value.split(%r{-})[0]
end