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.



89
90
91
# File 'lib/pdk/util/ruby_version.rb', line 89

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.



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

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pdk/util/ruby_version.rb', line 53

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]
                              #
                              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



72
73
74
# File 'lib/pdk/util/ruby_version.rb', line 72

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

.scan_for_packaged_rubiesObject



43
44
45
46
47
48
49
50
51
# File 'lib/pdk/util/ruby_version.rb', line 43

def scan_for_packaged_rubies
  require 'pdk/util'

  ruby_basedir = File.join(PDK::Util.pdk_package_basedir, 'private', 'ruby', '*')
  Dir[ruby_basedir].sort.map { |ruby_dir|
    version = File.basename(ruby_dir)
    [version, version.split('.').take(2).concat(['0']).join('.')]
  }.reverse.to_h
end

.use(version) ⇒ Object



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

def use(version)
  if versions.key?(version)
    @active_ruby_version = version
  else
    raise ArgumentError, _('Unknown Ruby version "%{ruby_version}"') % {
      ruby_version: version,
    }
  end
end

.versionsObject



76
77
78
79
80
81
82
83
84
# File 'lib/pdk/util/ruby_version.rb', line 76

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/pdk/util/ruby_version.rb', line 142

def available_puppet_versions
  return @available_puppet_versions unless @available_puppet_versions.nil?

  puppet_spec_files = Dir[File.join(gem_home, 'specifications', '**', 'puppet*.gemspec')]

  gem_path.split(File::PATH_SEPARATOR).each do |path|
    puppet_spec_files += Dir[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 { |a, b| b <=> a }
end

#bin_pathObject



93
94
95
96
97
98
99
100
101
# File 'lib/pdk/util/ruby_version.rb', line 93

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



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pdk/util/ruby_version.rb', line 128

def gem_home
  require 'pdk/util'

  # `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



124
125
126
# File 'lib/pdk/util/ruby_version.rb', line 124

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

#gem_paths_rawObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pdk/util/ruby_version.rb', line 103

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.
    # TODO: There must be a better way to do this than shelling out to
    # gem... Perhaps can be replaced with "Gem.path"?
    [File.absolute_path(File.join(`gem which bundler`, '..', '..', '..', '..'))]
  end
end