Class: Rbs::Src::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/src/gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, version:, repository_prefix:, repository_dir:, rbs_prefix:) ⇒ Gem

Returns a new instance of Gem.



6
7
8
9
10
11
12
# File 'lib/rbs/src/gem.rb', line 6

def initialize(name:, version:, repository_prefix:, repository_dir:, rbs_prefix:)
  @name = name
  @version = version
  @repository_prefix = repository_prefix
  @repository_dir = repository_dir
  @rbs_prefix = rbs_prefix
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rbs/src/gem.rb', line 4

def name
  @name
end

#rbs_prefixObject (readonly)

Returns the value of attribute rbs_prefix.



4
5
6
# File 'lib/rbs/src/gem.rb', line 4

def rbs_prefix
  @rbs_prefix
end

#repository_dirObject (readonly)

Returns the value of attribute repository_dir.



4
5
6
# File 'lib/rbs/src/gem.rb', line 4

def repository_dir
  @repository_dir
end

#repository_prefixObject (readonly)

Returns the value of attribute repository_prefix.



4
5
6
# File 'lib/rbs/src/gem.rb', line 4

def repository_prefix
  @repository_prefix
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/rbs/src/gem.rb', line 4

def version
  @version
end

Instance Method Details

#checkout(runner, repository_url:, commit:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbs/src/gem.rb', line 38

def checkout(runner, repository_url:, commit:)
  unless runner.query!("git", "status", "-s", "-z", chdir: repository_root).split("\0").empty?
    runner.puts "📣 Stashing uncommited changes... Restore the changes by: `git stash pop`"
    runner.execute!("git", "stash", "-u", "-m", "Stash by rbs-src on #{name}-#{version}", chdir: repository_root)
  end

  return if runner.query!("git", "rev-parse", "HEAD", chdir: repository_root).chomp == commit

  unless runner.query?("git", "cat-file", "-e", commit, chdir: repository_root)
    runner.puts "💾 Fetching from #{repository_url}..."
    runner.execute!("git", "remote", "set-url", "origin", repository_url, chdir: repository_root)
    runner.execute!("git", "fetch", "origin", chdir: repository_root)
  end

  runner.puts "💪 Checking out the commit #{commit}..."
  runner.execute!("git", "checkout", commit, chdir: repository_root)
end

#clone(runner, repository_url:, commit:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rbs/src/gem.rb', line 56

def clone(runner, repository_url:, commit:)
  runner.puts "git clone..."
  runner.execute!("git", "clone", "--filter=blob:none", "--sparse", repository_url, repository_root.to_s)
  dirs = runner.query!("git", "ls-tree", "-d", "--name-only", "-z", "HEAD", chdir: repository_root).split("\0")
  dirs.delete(repository_dir.to_s)
  runner.execute!("git", "sparse-checkout", "set", *dirs, repository_dir.join(name).to_s, chdir: repository_root)
  if commit
    runner.puts "git checkout..."
    runner.execute!("git", "checkout", commit, chdir: repository_root)
  end
end


68
69
70
71
72
73
# File 'lib/rbs/src/gem.rb', line 68

def link
  Pathname.glob(rbs_path("*").to_s).each do |path|
    File.unlink(path.to_s)
  end
  File.symlink(repository_path.relative_path_from(rbs_path.parent), rbs_path)
end

#rbs_path(suffix = version) ⇒ Object



22
23
24
# File 'lib/rbs/src/gem.rb', line 22

def rbs_path(suffix = version)
  rbs_prefix + "#{name}-#{suffix}"
end

#repository_pathObject



18
19
20
# File 'lib/rbs/src/gem.rb', line 18

def repository_path
  repository_root.join(repository_dir, name, version)
end

#repository_rootObject



14
15
16
# File 'lib/rbs/src/gem.rb', line 14

def repository_root
  repository_prefix + "#{name}-#{version}"
end

#status(runner, commit:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rbs/src/gem.rb', line 26

def status(runner, commit:)
  unless runner.query!("git", "status", "-s", "-z", chdir: repository_root).split("\0").empty?
    return :dirty
  end

  if runner.query!("git", "rev-parse", "HEAD", chdir: repository_root).chomp != commit
    return :commit_mismatch
  end

  :ok
end