Class: RBS::Collection::Config

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

Overview

This class represent the configuration file.

Defined Under Namespace

Classes: CollectionNotAvailable, Lockfile, LockfileGenerator

Constant Summary collapse

PATH =

Returns:

  • (Pathname)
Pathname('rbs_collection.yaml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, config_path:) ⇒ Config

config_path is necessary to resolve relative repo_path

Parameters:

  • data (Object)
  • config_path: (Pathname)


33
34
35
36
# File 'sig/collection/config.rbs', line 33

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

Instance Attribute Details

#config_pathPathname (readonly)

Returns the value of attribute config_path.

Returns:

  • (Pathname)


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

def config_path
  @config_path
end

#dataObject (readonly)

Returns the value of attribute data.

Returns:

  • (Object)


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

def data
  @data
end

Class Method Details

.find_config_pathPathname?

Returns:

  • (Pathname, nil)


21
22
23
24
25
26
27
28
29
30
# File 'lib/rbs/collection/config.rb', line 21

def self.find_config_path
  current = Pathname.pwd

  loop do
    config_path = current.join(PATH)
    return config_path if config_path.exist?
    current = current.join('..')
    return nil if current.root?
  end
end

.from_path(path) ⇒ Config

Parameters:

  • path (Pathname)

Returns:



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

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

.generate_lockfile(config_path:, definition:, with_lockfile: true) ⇒ [Config, Lockfile]

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

Parameters:

  • config_path: (Pathname)
  • definition: (Bundler::Definition)
  • with_lockfile: (boolish) (defaults to: true)

Returns:



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

def self.generate_lockfile(config_path:, definition:, with_lockfile: true)
  config = from_path(config_path)
  lockfile = LockfileGenerator.generate(config: config, definition: definition, with_lockfile: with_lockfile)

  [config, lockfile]
end

.to_lockfile_path(config_path) ⇒ Pathname

Parameters:

  • config_path (Pathname)

Returns:

  • (Pathname)


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

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

Instance Method Details

#gem(gem_name) ⇒ gem_entry?

Parameters:

  • gem_name (String)

Returns:

  • (gem_entry, nil)


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

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

#gemsArray[gem_entry]

Returns:

  • (Array[gem_entry])


74
75
76
77
78
# File 'lib/rbs/collection/config.rb', line 74

def gems
  @data['gems'] ||= (
    [] #: Array[gem_entry]
  )
end

#repo_pathPathname

Returns:

  • (Pathname)


58
59
60
# File 'lib/rbs/collection/config.rb', line 58

def repo_path
  @config_path.dirname.join repo_path_data
end

#repo_path_dataPathname

Returns:

  • (Pathname)


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

def repo_path_data
  Pathname(@data["path"])
end

#sourcesArray[Sources::t]

Returns:

  • (Array[Sources::t])


66
67
68
69
70
71
72
# File 'lib/rbs/collection/config.rb', line 66

def sources
  @sources ||= [
    Sources::Rubygems.instance,
    *@data['sources'].map { |c| Sources.from_config_entry(c, base_directory: @config_path.dirname) },
    Sources::Stdlib.instance
  ]
end