Class: PDK::Util::RubyVersion

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pdk/util/ruby_version.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_version = nil) ⇒ RubyVersion

Returns a new instance of RubyVersion.



86
87
88
# File 'lib/pdk/util/ruby_version.rb', line 86

def initialize(ruby_version = nil)
  @ruby_version = ruby_version || default_ruby_version
end

Class Attribute Details

.instance(version = nil) ⇒ Object (readonly)

TODO: resolve this rubocop:disable Lint/DuplicateMethods



14
15
16
# File 'lib/pdk/util/ruby_version.rb', line 14

def instance
  @instance
end

Instance Attribute Details

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



84
85
86
# File 'lib/pdk/util/ruby_version.rb', line 84

def ruby_version
  @ruby_version
end

Class Method Details

.active_ruby_versionObject

rubocop:enable Lint/DuplicateMethods



29
30
31
# File 'lib/pdk/util/ruby_version.rb', line 29

def active_ruby_version
  @active_ruby_version || default_ruby_version
end

.default_ruby_versionObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pdk/util/ruby_version.rb', line 49

def default_ruby_version
  require 'pdk/util'
  require 'pdk/util/puppet_version'

  @default_ruby_version ||= if PDK::Util.package_install?
                              # Default to the ruby that supports the latest puppet gem. If you wish to default to a
                              # specific Puppet Gem version use the following example;
                              #
                              # PDK::Util::PuppetVersion.find_gem_for('5.5.10')[:ruby_version]
                              #
                              # For using the latest puppet gem:
                              PDK::Util::PuppetVersion.latest_available[:ruby_version]
                            else
                              # TODO: may not be a safe assumption that highest available version should be default
                              # WARNING Do NOT use PDK::Util::PuppetVersion.*** methods as it can recurse into this
                              # method and cause Stack Level Too Deep errors.
                              latest_ruby_version
                            end
end

.latest_ruby_versionObject



69
70
71
# File 'lib/pdk/util/ruby_version.rb', line 69

def latest_ruby_version
  versions.keys.min { |a, b| Gem::Version.new(b) <=> Gem::Version.new(a) }
end

.scan_for_packaged_rubiesObject



39
40
41
42
43
44
45
46
47
# File 'lib/pdk/util/ruby_version.rb', line 39

def scan_for_packaged_rubies
  require 'pdk/util'

  ruby_basedir = File.join(PDK::Util.pdk_package_basedir, 'private', 'ruby', '*')
  PDK::Util::Filesystem.glob(ruby_basedir).sort.map do |ruby_dir|
    version = File.basename(ruby_dir)
    [version, version.split('.').take(2).push('0').join('.')]
  end.reverse.to_h
end

.use(version) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/pdk/util/ruby_version.rb', line 33

def use(version)
  raise ArgumentError, format('Unknown Ruby version "%{ruby_version}"', ruby_version: version) unless versions.key?(version)

  @active_ruby_version = version
end

.versionsObject



73
74
75
76
77
78
79
80
81
# File 'lib/pdk/util/ruby_version.rb', line 73

def versions
  require 'pdk/util'

  @versions ||= if PDK::Util.package_install?
                  scan_for_packaged_rubies
                else
                  { RbConfig::CONFIG['RUBY_PROGRAM_VERSION'] => RbConfig::CONFIG['ruby_version'] }
                end
end

Instance Method Details

#available_puppet_versionsObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pdk/util/ruby_version.rb', line 138

def available_puppet_versions
  return @available_puppet_versions unless @available_puppet_versions.nil?

  puppet_spec_files = PDK::Util::Filesystem.glob(File.join(gem_home, 'specifications', '**', 'puppet*.gemspec'))

  gem_path.split(File::PATH_SEPARATOR).each do |path|
    puppet_spec_files += PDK::Util::Filesystem.glob(File.join(path, 'specifications', '**', 'puppet*.gemspec'))
  end

  puppet_specs = []

  puppet_spec_files.each do |specfile|
    spec = Gem::Specification.load(specfile)
    puppet_specs << spec if spec.name == 'puppet'
  end

  @available_puppet_versions = puppet_specs.map(&:version).sort.reverse
end

#bin_pathObject



90
91
92
93
94
95
96
97
98
# File 'lib/pdk/util/ruby_version.rb', line 90

def bin_path
  require 'pdk/util'

  if PDK::Util.package_install?
    File.join(PDK::Util.pdk_package_basedir, 'private', 'ruby', ruby_version, 'bin')
  else
    RbConfig::CONFIG['bindir']
  end
end

#gem_homeObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pdk/util/ruby_version.rb', line 123

def gem_home
  require 'pdk/util'

  # TODO: bundle install --path is deprecated
  # `bundle install --path` ignores all "system" installed gems and
  # causes unnecessary package installs. `bundle install` (without
  # --path) installs into GEM_HOME, which by default is non-user
  # writeable.
  # To still use the pre-installed packages, but allow folks to install
  # additional gems, we set GEM_HOME to the user's cachedir and put all
  # other cache locations onto GEM_PATH.
  # See https://stackoverflow.com/a/11277228 for background
  File.join(PDK::Util.cachedir, 'ruby', versions[ruby_version])
end

#gem_pathObject



119
120
121
# File 'lib/pdk/util/ruby_version.rb', line 119

def gem_path
  gem_paths_raw.join(File::PATH_SEPARATOR)
end

#gem_paths_rawObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pdk/util/ruby_version.rb', line 100

def gem_paths_raw
  require 'pdk/util'

  if PDK::Util.package_install?
    # Subprocesses use their own set of gems which are managed by pdk or
    # installed with the package. We also include the separate gem path
    # where our packaged multi-puppet installations live.
    [
      File.join(PDK::Util.pdk_package_basedir, 'private', 'ruby', ruby_version, 'lib', 'ruby', 'gems', versions[ruby_version]),
      File.join(PDK::Util.package_cachedir, 'ruby', versions[ruby_version]),
      File.join(PDK::Util.pdk_package_basedir, 'private', 'puppet', 'ruby', versions[ruby_version])
    ]
  else
    # This allows the subprocess to find the 'bundler' gem, which isn't
    # in GEM_HOME for gem installs.
    [File.absolute_path(File.join(bundler_basedir, '..', '..', '..'))]
  end
end