Class: Belt::CLI::PathGemMaterializer

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/path_gem_materializer.rb

Overview

Turns Gemfile path: gems into real .gem files in vendor/cache and rewrites the Gemfile/lock so Docker bundle install produces a normal gem install (with specifications/). Required for Lambda bare require without bundler/setup.

Only mutates files under build_dir — never the app's real Gemfile.

Defined Under Namespace

Classes: PathGem, PathSource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build_dir, project_root:) ⇒ PathGemMaterializer

Returns a new instance of PathGemMaterializer.



23
24
25
26
27
28
# File 'lib/belt/cli/path_gem_materializer.rb', line 23

def initialize(build_dir, project_root:)
  @build_dir = build_dir
  @project_root = project_root
  @gemfile = File.join(build_dir, 'Gemfile')
  @lockfile = File.join(build_dir, 'Gemfile.lock')
end

Class Method Details

.materialize!(build_dir, project_root:) ⇒ Object



19
20
21
# File 'lib/belt/cli/path_gem_materializer.rb', line 19

def self.materialize!(build_dir, project_root:)
  new(build_dir, project_root: project_root).materialize!
end

Instance Method Details

#materialize!Array<String>

Returns names of gems materialized (empty if none).

Returns:

  • (Array<String>)

    names of gems materialized (empty if none)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/belt/cli/path_gem_materializer.rb', line 31

def materialize!
  return [] unless File.exist?(@gemfile) && File.exist?(@lockfile)

  sources = parse_path_sources(File.read(@lockfile))
  return [] if sources.empty?

  cache_dir = File.join(@build_dir, 'vendor', 'cache')
  FileUtils.mkdir_p(cache_dir)

  materialized = []
  sources.each do |source|
    source_path = resolve_remote(source.remote)
    abort "✗ path gem source missing: #{source.remote} (resolved #{source_path})" unless Dir.exist?(source_path)

    source.gems.each do |gem|
      gem_file = build_gem(source_path, gem)
      dest = File.join(cache_dir, File.basename(gem_file))
      FileUtils.cp(gem_file, dest)
      FileUtils.rm_f(gem_file)
      rewrite_gemfile_path_to_version!(gem.name, gem.version)
      materialized << gem.name
    end
  end

  return [] if materialized.empty?

  relock!(materialized)
  materialized
end