Class: RBS::Collection::Config::LockfileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/collection/config/lockfile_generator.rb

Defined Under Namespace

Classes: GemfileLockMismatchError

Constant Summary collapse

ALUMNI_STDLIBS =
{
  "mutex_m" => ">= 0.3.0",
  "abbrev" => nil,
  "base64" => nil,
  "benchmark" => nil,
  "bigdecimal" => nil,
  "csv" => nil,
  "kconv" => nil,
  "logger" => nil,
  "minitest" => nil,
  "net-smtp" => nil,
  "nkf" => nil,
  "observer" => nil,
  "cgi" => nil,
  "pstore" => nil,
}
NONGEM_STDLIBS =
Set[
  "cgi-escape",
  "coverage",
  "monitor",
  "objspace",
  "pathname",
  "pty",
  "ripper",
  "socket",
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, definition:, with_lockfile:) ⇒ LockfileGenerator

Returns a new instance of LockfileGenerator.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 59

def initialize(config:, definition:, with_lockfile:)
  @config = config

  @gem_entries = config.gems.each.with_object({}) do |entry, hash| #$ Hash[String, gem_entry?]
    name = entry["name"]
    hash[name] = entry
  end

  lockfile_path = Config.to_lockfile_path(config.config_path)
  lockfile_dir = lockfile_path.parent

  @lockfile = Lockfile.new(
    lockfile_path: lockfile_path,
    path: config.repo_path_data,
    gemfile_lock_path: definition.lockfile.relative_path_from(lockfile_dir)
  )

  if with_lockfile && lockfile_path.file?
    @existing_lockfile = Lockfile.from_lockfile(lockfile_path: lockfile_path, data: YAML.load_file(lockfile_path.to_s))
    validate_gemfile_lock_path!(lock: @existing_lockfile, gemfile_lock_path: definition.lockfile)
  end

  @definition = definition
  @gem_hash = definition.locked_gems.specs.each.with_object({}) do |spec, hash|  #$ Hash[String, Bundler::LazySpecification]
    hash[spec.name] = spec
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



51
52
53
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 51

def config
  @config
end

#definitionObject (readonly)

Returns the value of attribute definition.



51
52
53
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 51

def definition
  @definition
end

#existing_lockfileObject (readonly)

Returns the value of attribute existing_lockfile.



51
52
53
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 51

def existing_lockfile
  @existing_lockfile
end

#gem_entriesObject (readonly)

Returns the value of attribute gem_entries.



51
52
53
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 51

def gem_entries
  @gem_entries
end

#gem_hashObject (readonly)

Returns the value of attribute gem_hash.



51
52
53
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 51

def gem_hash
  @gem_hash
end

#lockfileObject (readonly)

Returns the value of attribute lockfile.



51
52
53
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 51

def lockfile
  @lockfile
end

Class Method Details

.generate(config:, definition:, with_lockfile: true) ⇒ Object



53
54
55
56
57
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 53

def self.generate(config:, definition:, with_lockfile: true)
  generator = new(config: config, definition: definition, with_lockfile: with_lockfile)
  generator.generate
  generator.lockfile
end

Instance Method Details

#generateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rbs/collection/config/lockfile_generator.rb', line 87

def generate
  config.gems.each do |gem|
    case
    when gem.dig("source", "type") == "stdlib"
      unless gem.fetch("ignore", false)
        assign_stdlib(name: gem["name"])
      end
    else
      assign_gem(name: gem["name"], version: gem["version"])
    end
  end

  definition.dependencies.each do |dep|
    if dep.autorequire && dep.autorequire.empty?
      next
    end

    if spec = gem_hash[dep.name]
      assign_gem(name: dep.name, version: spec.version, skip: dep.source.is_a?(Bundler::Source::Gemspec))
    end
  end

  lockfile.lockfile_path.write(YAML.dump(lockfile.to_lockfile))
end