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

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gemfile_env(gem_overrides) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/pdk/util/bundler.rb', line 221

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



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

def binstubs!(gems)
  binstub_dir = File.join(File.dirname(gemfile), 'bin')
  return true if gems.all? { |gem| File.file?(File.join(binstub_dir, gem)) }

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

  unless result[:exit_code].zero?
    PDK.logger.fatal(_("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



79
80
81
# File 'lib/pdk/util/bundler.rb', line 79

def gemfile
  @gemfile ||= PDK::Util.find_upwards('Gemfile')
end

#gemfile?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/pdk/util/bundler.rb', line 88

def gemfile?
  !gemfile.nil?
end

#gemfile_lockObject



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

def gemfile_lock
  return if gemfile.nil?
  @gemfile_lock ||= File.join(File.dirname(gemfile), 'Gemfile.lock')
end

#install!(gem_overrides = {}) ⇒ Object



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

def install!(gem_overrides = {})
  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)


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

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



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
150
# File 'lib/pdk/util/bundler.rb', line 110

def lock!
  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 { |lockfile| File.exist?(lockfile) }

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

    PDK.logger.debug(_('Using vendored Gemfile.lock from %{source}.') % { source: vendored_gemfile_lock })
    FileUtils.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)


92
93
94
# File 'lib/pdk/util/bundler.rb', line 92

def locked?
  !gemfile_lock.nil? && File.file?(gemfile_lock)
end

#update_lock!(options = {}) ⇒ Object



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
185
# File 'lib/pdk/util/bundler.rb', line 152

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