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
70
71
# File 'lib/rbs/collection/sources/git.rb', line 43

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

  gem_dir = dest.join(name, version)

  colored_io = CLI::ColoredIO.new(stdout: stdout)

  case
  when gem_dir.symlink?
    colored_io.puts_green("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)
      colored_io.puts "Using #{format_config_entry(name, version)}"
    else
      colored_io.puts_green("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?
    colored_io.puts_green("Installing #{format_config_entry(name, version)}")
    _install(dest: dest, name: name, version: version)
  else
    raise
  end
end

#load_metadata(dir:) ⇒ Object



223
224
225
226
227
# File 'lib/rbs/collection/sources/git.rb', line 223

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



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

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



207
208
209
210
211
212
213
# File 'lib/rbs/collection/sources/git.rb', line 207

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

#resolved_revisionObject



172
173
174
175
176
177
178
179
180
181
# File 'lib/rbs/collection/sources/git.rb', line 172

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



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

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



215
216
217
218
219
220
221
# File 'lib/rbs/collection/sources/git.rb', line 215

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