Class: RBS::Collection::Sources::Git

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rbs/collection/sources/git.rb

Defined Under Namespace

Classes: CommandError

Constant Summary collapse

METADATA_FILENAME =
'.rbs_meta.yaml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#dependencies_of

Constructor Details

#initialize(name:, revision:, remote:, repo_dir:) ⇒ Git

Returns a new instance of Git.



18
19
20
21
22
23
24
# File 'lib/rbs/collection/sources/git.rb', line 18

def initialize(name:, revision:, remote:, repo_dir:)
  @name = name
  @remote = remote
  @repo_dir = repo_dir || 'gems'
  @revision = revision
  @need_setup = true
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/rbs/collection/sources/git.rb', line 16

def name
  @name
end

#remoteObject (readonly)

Returns the value of attribute remote.



16
17
18
# File 'lib/rbs/collection/sources/git.rb', line 16

def remote
  @remote
end

#repo_dirObject (readonly)

Returns the value of attribute repo_dir.



16
17
18
# File 'lib/rbs/collection/sources/git.rb', line 16

def repo_dir
  @repo_dir
end

#revisionObject (readonly)

Returns the value of attribute revision.



16
17
18
# File 'lib/rbs/collection/sources/git.rb', line 16

def revision
  @revision
end

Instance Method Details

#has?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/rbs/collection/sources/git.rb', line 26

def has?(name, version)
  setup! do
    if version
      (gems_versions[name] || Set[]).include?(version)
    else
      gems_versions.key?(name)
    end
  end
end

#install(dest:, name:, version:, stdout:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbs/collection/sources/git.rb', line 43

def install(dest:, name:, version:, stdout:)
  setup!()

  gem_dir = dest.join(name, version)

  case
  when gem_dir.symlink?
    stdout.puts "Updating to #{format_config_entry(name, version)} from a local source"
    gem_dir.unlink
    _install(dest: dest, name: name, version: version)
  when gem_dir.directory?
    prev = (dir: gem_dir)

    if prev == (name: name, version: version)
      stdout.puts "Using #{format_config_entry(name, version)}"
    else
      stdout.puts "Updating to #{format_config_entry(name, version)} from #{format_config_entry(prev["name"], prev["version"])}"
      FileUtils.remove_entry_secure(gem_dir.to_s)
      _install(dest: dest, name: name, version: version)
    end
  when !gem_dir.exist?
    stdout.puts "Installing #{format_config_entry(name, version)}"
    _install(dest: dest, name: name, version: version)
  else
    raise
  end
end

#load_metadata(dir:) ⇒ Object



226
227
228
229
230
# File 'lib/rbs/collection/sources/git.rb', line 226

def (dir:)
  # @type var content: Hash[String, untyped]
  content = YAML.load_file(dir.join(METADATA_FILENAME).to_s)
  _ = content.slice("name", "version", "source")
end

#manifest_of(name, version) ⇒ Object



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

def manifest_of(name, version)
  setup! do
    path = File.join(repo_dir, name, version, 'manifest.yaml')
    content = git('cat-file', '-p', "#{resolved_revision}:#{path}")
    YAML.safe_load(content)
  rescue CommandError
    if has?(name, version)
      nil
    else
      raise
    end
  end
end

#metadata_content(name:, version:) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/rbs/collection/sources/git.rb', line 210

def (name:, version:)
  {
    "name" => name,
    "version" => version,
    "source" => to_lockfile
  }
end

#resolved_revisionObject



175
176
177
178
179
180
181
182
183
184
# File 'lib/rbs/collection/sources/git.rb', line 175

def resolved_revision
  @resolved_revision ||=
    begin
      if commit_hash?
        revision
      else
        setup! { git('rev-parse', "refs/remotes/origin/#{revision}").chomp }
      end
    end
end

#to_lockfileObject



111
112
113
114
115
116
117
118
119
# File 'lib/rbs/collection/sources/git.rb', line 111

def to_lockfile
  {
    'type' => 'git',
    'name' => name,
    'revision' => resolved_revision,
    'remote' => remote,
    'repo_dir' => repo_dir,
  }
end

#versions(name) ⇒ Object



36
37
38
39
40
41
# File 'lib/rbs/collection/sources/git.rb', line 36

def versions(name)
  setup! do
    versions = gems_versions[name] or raise "Git source `#{name}` doesn't have `#{name}`"
    versions.sort
  end
end

#write_metadata(dir:, name:, version:) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/rbs/collection/sources/git.rb', line 218

def (dir:, name:, version:)
  dir.join(METADATA_FILENAME).write(
    YAML.dump(
      (name: name, version: version)
    )
  )
end