Class: PDK::Util::Bundler::BundleHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/util/bundler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gemfile_env(gem_overrides) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/pdk/util/bundler.rb', line 224

def self.gemfile_env(gem_overrides)
  gemfile_env = {}

  return gemfile_env unless gem_overrides.respond_to?(:each)

  gem_overrides.each do |gem, version|
    gemfile_env['PUPPET_GEM_VERSION'] = version if gem.respond_to?(:to_s) && gem.to_s == 'puppet' && !version.nil?
    gemfile_env['FACTER_GEM_VERSION'] = version if gem.respond_to?(:to_s) && gem.to_s == 'facter' && !version.nil?
    gemfile_env['HIERA_GEM_VERSION'] = version if gem.respond_to?(:to_s) && gem.to_s == 'hiera' && !version.nil?
  end

  gemfile_env
end

Instance Method Details

#binstubs!(gems) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/pdk/util/bundler.rb', line 207

def binstubs!(gems)
  raise PDK::CLI::FatalError, 'Unable to install requested binstubs as the Gemfile is missing' if gemfile.nil?

  binstub_dir = File.join(File.dirname(gemfile), 'bin')
  return true if gems.all? { |gem| PDK::Util::Filesystem.file?(File.join(binstub_dir, gem)) }

  cmd = bundle_command('binstubs', *gems, '--force')
  result = cmd.execute!

  unless result[:exit_code].zero?
    PDK.logger.fatal(format("Failed to generate binstubs for '%{gems}':\n%{output}", gems: gems.join(' '), output: result.values_at(:stdout, :stderr).join("\n"))) unless PDK.logger.debug?
    raise PDK::CLI::FatalError, 'Unable to install requested binstubs.'
  end

  true
end

#gemfileObject



75
76
77
78
# File 'lib/pdk/util/bundler.rb', line 75

def gemfile
  require 'pdk/util'
  @gemfile ||= PDK::Util.find_upwards('Gemfile')
end

#gemfile?Boolean

Returns:

  • (Boolean)


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

def gemfile?
  !gemfile.nil?
end

#gemfile_lockObject



80
81
82
83
84
# File 'lib/pdk/util/bundler.rb', line 80

def gemfile_lock
  return if gemfile.nil?

  @gemfile_lock ||= File.join(File.dirname(gemfile), 'Gemfile.lock')
end

#install!(gem_overrides = {}) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/pdk/util/bundler.rb', line 186

def install!(gem_overrides = {})
  require 'pdk/util/ruby_version'

  argv = ['install', "--gemfile=#{gemfile}"]
  argv << '-j4' unless Gem.win_platform? && Gem::Version.new(PDK::Util::RubyVersion.active_ruby_version) < Gem::Version.new('2.3.5')

  cmd = bundle_command(*argv).tap do |c|
    c.add_spinner('Installing missing Gemfile dependencies.')
    c.update_environment(gemfile_env(gem_overrides)) unless gem_overrides.empty?
  end

  result = cmd.execute!

  unless result[:exit_code].zero?
    PDK.logger.fatal(result.values_at(:stdout, :stderr).join("\n")) unless PDK.logger.debug?
    raise PDK::CLI::FatalError, 'Unable to install missing Gemfile dependencies.'
  end

  true
end

#installed?(gem_overrides = {}) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pdk/util/bundler.rb', line 94

def installed?(gem_overrides = {})
  PDK.logger.debug('Checking for missing Gemfile dependencies.')

  argv = ['check', "--gemfile=#{gemfile}", '--dry-run']

  cmd = bundle_command(*argv).tap do |c|
    c.update_environment(gemfile_env(gem_overrides)) unless gem_overrides.empty?
  end

  result = cmd.execute!

  result[:exit_code].zero?
end

#lock!Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pdk/util/bundler.rb', line 108

def lock!
  require 'pdk/util'
  require 'pdk/util/ruby_version'

  if PDK::Util.package_install?
    # In packaged installs, use vendored Gemfile.lock as a starting point.
    # Subsequent 'bundle install' will still pick up any new dependencies.
    vendored_lockfiles = [
      File.join(PDK::Util.package_cachedir, "Gemfile-#{PDK::Util::RubyVersion.active_ruby_version}.lock"),
      File.join(PDK::Util.package_cachedir, 'Gemfile.lock')
    ]

    vendored_gemfile_lock = vendored_lockfiles.find do |lockfile|
      PDK::Util::Filesystem.exist?(lockfile)
    end

    raise PDK::CLI::FatalError, format('Vendored Gemfile.lock (%{source}) not found.', source: vendored_gemfile_lock) unless vendored_gemfile_lock

    PDK.logger.debug(format('Using vendored Gemfile.lock from %{source}.', source: vendored_gemfile_lock))
    PDK::Util::Filesystem.cp(vendored_gemfile_lock, File.join(PDK::Util.module_root, 'Gemfile.lock'))
  else
    argv = ['lock']

    cmd = bundle_command(*argv).tap do |c|
      c.add_spinner('Resolving default Gemfile dependencies.')
    end

    result = cmd.execute!

    unless result[:exit_code].zero?
      PDK.logger.fatal(result.values_at(:stdout, :stderr).join("\n")) unless PDK.logger.debug?
      raise PDK::CLI::FatalError, 'Unable to resolve default Gemfile dependencies.'
    end

    # After initial lockfile generation, re-resolve json gem to built-in
    # version to avoid unncessary native compilation attempts. For packaged
    # installs this is done during the generation of the vendored Gemfile.lock
    update_lock!(only: { json: nil }, local: true)
  end

  true
end

#locked?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/pdk/util/bundler.rb', line 90

def locked?
  !gemfile_lock.nil? && PDK::Util::Filesystem.file?(gemfile_lock)
end

#update_lock!(options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/pdk/util/bundler.rb', line 151

def update_lock!(options = {})
  PDK.logger.debug('Updating Gemfile dependencies.')

  argv = ['lock', "--lockfile=#{gemfile_lock}", '--update']

  overrides = nil

  if options && options[:only]
    update_gems = options[:only].keys.map(&:to_s)
    argv << update_gems
    argv.flatten!

    overrides = options[:only]
  elsif options && options[:with]
    overrides = options[:with]
  end

  argv << '--local' if options && options[:local]
  argv << '--conservative' if options && options[:conservative]

  cmd = bundle_command(*argv).tap do |c|
    c.update_environment('BUNDLE_GEMFILE' => gemfile)
    c.update_environment(gemfile_env(overrides)) if overrides
  end

  result = cmd.execute!

  unless result[:exit_code].zero?
    PDK.logger.fatal(result.values_at(:stdout, :stderr).join("\n")) unless PDK.logger.debug?
    raise PDK::CLI::FatalError, 'Unable to resolve Gemfile dependencies.'
  end

  true
end