Module: PDK::Util::Bundler

Defined in:
lib/pdk/util/bundler.rb

Defined Under Namespace

Classes: BundleHelper

Class Method Summary collapse

Class Method Details

.already_bundled?(gemfile, gem_overrides) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pdk/util/bundler.rb', line 58

def self.already_bundled?(gemfile, gem_overrides)
  !(@bundled ||= {})[bundle_cache_key(gemfile, gem_overrides)].nil?
end

.ensure_binstubs!(*gems) ⇒ Object



52
53
54
55
56
# File 'lib/pdk/util/bundler.rb', line 52

def self.ensure_binstubs!(*gems)
  bundle = BundleHelper.new

  bundle.binstubs!(gems)
end

.ensure_bundle!(gem_overrides = {}) ⇒ Object

class BundleHelper; end



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/pdk/util/bundler.rb', line 8

def self.ensure_bundle!(gem_overrides = {})
  bundle = BundleHelper.new

  # This will default ensure_bundle! to re-resolving everything to latest
  gem_overrides ||= { puppet: nil, hiera: nil, facter: nil }

  if already_bundled?(bundle.gemfile, gem_overrides)
    PDK.logger.debug('Bundler managed gems already up to date.')
    return
  end

  unless bundle.gemfile?
    PDK.logger.debug(format("No Gemfile found in '%{cwd}'. Skipping bundler management.", cwd: Dir.pwd))
    return
  end

  unless bundle.locked?
    # Generate initial default Gemfile.lock, either from package cache or
    # by invoking `bundle lock`
    bundle.lock!
  end

  # Check if all dependencies will be available once we update the lockfile.
  begin
    original_lockfile = bundle.gemfile_lock
    temp_lockfile = "#{original_lockfile}.tmp"

    PDK::Util::Filesystem.mv(original_lockfile, temp_lockfile)

    all_deps_available = bundle.installed?(gem_overrides)
  ensure
    PDK::Util::Filesystem.mv(temp_lockfile, original_lockfile, force: true)
  end

  bundle.update_lock!(with: gem_overrides, local: all_deps_available)

  # If there are missing dependencies after updating the lockfile, let `bundle install`
  # go out and get them. If the specified puppet gem version points to a remote location
  # or local filepath, then run bundle install as well.
  bundle.install!(gem_overrides) if !bundle.installed?(gem_overrides) || gem_overrides[:puppet]&.start_with?('file://', 'git://', 'https://')

  mark_as_bundled!(bundle.gemfile, gem_overrides)
end

.mark_as_bundled!(gemfile, gem_overrides) ⇒ Object



62
63
64
# File 'lib/pdk/util/bundler.rb', line 62

def self.mark_as_bundled!(gemfile, gem_overrides)
  (@bundled ||= {})[bundle_cache_key(gemfile, gem_overrides)] = true
end