Class: RBS::Collection::Config::Lockfile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_path:, path:, gemfile_lock_path:) ⇒ Lockfile

Returns a new instance of Lockfile.



9
10
11
12
13
14
15
16
17
# File 'lib/rbs/collection/config/lockfile.rb', line 9

def initialize(lockfile_path:, path:, gemfile_lock_path:)
  @lockfile_path = lockfile_path
  @lockfile_dir = lockfile_path.parent
  @path = path
  @gemfile_lock_path = gemfile_lock_path

  @sources = {}
  @gems = {}
end

Instance Attribute Details

#gemfile_lock_pathObject (readonly)

Returns the value of attribute gemfile_lock_path.



7
8
9
# File 'lib/rbs/collection/config/lockfile.rb', line 7

def gemfile_lock_path
  @gemfile_lock_path
end

#gemsObject (readonly)

Returns the value of attribute gems.



7
8
9
# File 'lib/rbs/collection/config/lockfile.rb', line 7

def gems
  @gems
end

#lockfile_dirObject (readonly)

Returns the value of attribute lockfile_dir.



7
8
9
# File 'lib/rbs/collection/config/lockfile.rb', line 7

def lockfile_dir
  @lockfile_dir
end

#lockfile_pathObject (readonly)

Returns the value of attribute lockfile_path.



7
8
9
# File 'lib/rbs/collection/config/lockfile.rb', line 7

def lockfile_path
  @lockfile_path
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/rbs/collection/config/lockfile.rb', line 7

def path
  @path
end

#sourcesObject (readonly)

Returns the value of attribute sources.



7
8
9
# File 'lib/rbs/collection/config/lockfile.rb', line 7

def sources
  @sources
end

Class Method Details

.from_lockfile(lockfile_path:, data:) ⇒ Object



55
56
57
58
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
86
87
88
# File 'lib/rbs/collection/config/lockfile.rb', line 55

def self.from_lockfile(lockfile_path:, data:)
  path = Pathname(data["path"])
  if p = data["gemfile_lock_path"]
    gemfile_lock_path = Pathname(p)
  end

  lockfile = Lockfile.new(lockfile_path: lockfile_path, path: path, gemfile_lock_path: gemfile_lock_path)

  if sources = data["sources"]
    sources.each do |src|
      git = Sources::Git.new(
        name: src["name"],
        revision: src["revision"],
        remote: src["remote"],
        repo_dir: src["repo_dir"]
      )
      lockfile.sources[git.name] = git
    end
  end

  if gems = data["gems"]
    gems.each do |gem|
      src = gem["source"]
      source = Sources.from_config_entry(src, base_directory: lockfile_path.dirname)
      lockfile.gems[gem["name"]] = {
        name: gem["name"],
        version: gem["version"],
        source: source
      }
    end
  end

  lockfile
end

Instance Method Details

#check_rbs_availability!Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rbs/collection/config/lockfile.rb', line 98

def check_rbs_availability!
  raise CollectionNotAvailable unless fullpath.exist?

  gems.each_value do |gem|
    source = gem[:source]

    case source
    when Sources::Git
      meta_path = fullpath.join(gem[:name], gem[:version], Sources::Git::METADATA_FILENAME)
      raise CollectionNotAvailable unless meta_path.exist?
      raise CollectionNotAvailable unless library_data(gem) == YAML.load(meta_path.read)
    when Sources::Local
      raise CollectionNotAvailable unless fullpath.join(gem[:name], gem[:version]).symlink?
    end
  end
end

#each_source(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/rbs/collection/config/lockfile.rb', line 29

def each_source(&block)
  if block
    sources.each_value(&block)
    yield Sources::Rubygems.instance
    yield Sources::Stdlib.instance
  else
    enum_for :each_source
  end
end

#fullpathObject



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

def fullpath
  lockfile_dir + path
end

#gemfile_lock_fullpathObject



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

def gemfile_lock_fullpath
  if gemfile_lock_path
    lockfile_dir + gemfile_lock_path
  end
end

#library_data(lib) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/rbs/collection/config/lockfile.rb', line 90

def library_data(lib)
  {
    "name" => lib[:name],
    "version" => lib[:version],
    "source" => lib[:source].to_lockfile
  }
end

#to_lockfileObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rbs/collection/config/lockfile.rb', line 39

def to_lockfile
  # @type var data: lockfile_data

  data = {
    "sources" => sources.each_value.sort_by {|s| s.name }.map {|source| source.to_lockfile },
    "path" => path.to_s,
    "gems" => gems.each_value.sort_by {|g| g[:name] }.map {|hash| library_data(hash) },
    "gemfile_lock_path" => gemfile_lock_path.to_s
  }

  data.delete("sources") if sources.empty?
  data.delete("gems") if gems.empty?

  data
end