Class: RBS::Collection::Config

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

Overview

This class represent the configration file.

Defined Under Namespace

Classes: CollectionNotAvailable, LockfileGenerator

Constant Summary collapse

PATH =
Pathname('rbs_collection.yaml')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, config_path:) ⇒ Config

Returns a new instance of Config.



36
37
38
39
# File 'lib/rbs/collection/config.rb', line 36

def initialize(data, config_path:)
  @data = data
  @config_path = config_path
end

Class Method Details

.from_path(path) ⇒ Object



23
24
25
# File 'lib/rbs/collection/config.rb', line 23

def self.from_path(path)
  new(YAML.load(path.read), config_path: path)
end

.generate_lockfile(config_path:, gemfile_lock_path:, with_lockfile: true) ⇒ Object

Generate a rbs lockfile from Gemfile.lock to ‘config_path`. If `with_lockfile` is true, it respects existing rbs lockfile.



19
20
21
# File 'lib/rbs/collection/config.rb', line 19

def self.generate_lockfile(config_path:, gemfile_lock_path:, with_lockfile: true)
  LockfileGenerator.generate(config_path: config_path, gemfile_lock_path: gemfile_lock_path, with_lockfile: with_lockfile)
end

.lockfile_of(config_path) ⇒ Object



27
28
29
30
# File 'lib/rbs/collection/config.rb', line 27

def self.lockfile_of(config_path)
  lock_path = to_lockfile_path(config_path)
  from_path lock_path if lock_path.exist?
end

.to_lockfile_path(config_path) ⇒ Object



32
33
34
# File 'lib/rbs/collection/config.rb', line 32

def self.to_lockfile_path(config_path)
  config_path.sub_ext('.lock' + config_path.extname)
end

Instance Method Details

#add_gem(gem) ⇒ Object



41
42
43
# File 'lib/rbs/collection/config.rb', line 41

def add_gem(gem)
  gems << gem
end

#check_rbs_availability!Object

It raises an error when there are non-available libraries



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbs/collection/config.rb', line 71

def check_rbs_availability!
  raise CollectionNotAvailable unless repo_path.exist?

  gems.each do |gem|
    case gem['source']['type']
    when 'git'
      meta_path = repo_path.join(gem['name'], gem['version'], Sources::Git::METADATA_FILENAME)
      raise CollectionNotAvailable unless meta_path.exist?
      raise CollectionNotAvailable unless gem == YAML.load(meta_path.read)
    end
  end
end

#dump_to(io) ⇒ Object



62
63
64
# File 'lib/rbs/collection/config.rb', line 62

def dump_to(io)
  YAML.dump(@data, io)
end

#gem(gem_name) ⇒ Object



45
46
47
# File 'lib/rbs/collection/config.rb', line 45

def gem(gem_name)
  gems.find { |gem| gem['name'] == gem_name }
end

#gemsObject



66
67
68
# File 'lib/rbs/collection/config.rb', line 66

def gems
  @data['gems'] ||= []
end

#repo_pathObject



49
50
51
# File 'lib/rbs/collection/config.rb', line 49

def repo_path
  @config_path.dirname.join @data['path']
end

#sourcesObject



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

def sources
  @sources ||= (
    @data['sources']
      .map { |c| Sources.from_config_entry(c) }
      .push(Sources::Stdlib.instance)
      .push(Sources::Rubygems.instance)
  )
end