Class: Bundix::Source

Inherits:
Struct
  • Object
show all
Defined in:
lib/bundix/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#specObject

Returns the value of attribute spec

Returns:

  • (Object)

    the current value of spec



2
3
4
# File 'lib/bundix/source.rb', line 2

def spec
  @spec
end

Instance Method Details

#convertObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/bundix/source.rb', line 3

def convert
  case spec.source
  when Bundler::Source::Rubygems
    convert_rubygems
  when Bundler::Source::Git
    convert_git
  else
    pp spec
    fail 'unkown bundler source'
  end
end

#convert_gitObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bundix/source.rb', line 73

def convert_git
  revision = spec.source.options.fetch('revision')
  uri = spec.source.options.fetch('uri')
  hash = nix_prefetch_git(uri, revision).split.last
  hash = sh(NIX_HASH, '--type', 'sha256', '--to-base32', hash)[SHA256_32]
  fail "couldn't fetch hash for #{spec.name}-#{spec.version}" unless hash
  puts "#{hash} => #{uri}" if $VERBOSE

  { type: 'git',
    url: uri.to_s,
    rev: revision,
    sha256: hash,
    fetchSubmodules: false }
end

#convert_rubygemsObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bundix/source.rb', line 60

def convert_rubygems
  remotes = spec.source.remotes.map{|remote| remote.to_s.sub(/\/+$/, '') }
  hash = fetch_local_hash(spec)
  remote, hash = fetch_remotes_hash(spec, remotes) unless hash
  hash = sh(NIX_HASH, '--type', 'sha256', '--to-base32', hash)[SHA256_32]
  fail "couldn't fetch hash for #{spec.name}-#{spec.version}" unless hash
  puts "#{hash} => #{spec.name}-#{spec.version}.gem" if $VERBOSE

  { type: 'gem',
    remotes: (remote ? [remote] : remotes),
    sha256: hash }
end

#fetch_local_hash(spec) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/bundix/source.rb', line 33

def fetch_local_hash(spec)
  spec.source.caches.each do |cache|
    path = File.join(cache, "#{spec.name}-#{spec.version}.gem")
    next unless File.file?(path)
    hash = nix_prefetch_url("file://#{path}")[SHA256_32]
    return hash if hash
  end

  nil
end

#fetch_remote_hash(spec, remote) ⇒ Object



53
54
55
56
57
58
# File 'lib/bundix/source.rb', line 53

def fetch_remote_hash(spec, remote)
  uri = "#{remote}/gems/#{spec.name}-#{spec.version}.gem"
  nix_prefetch_url(uri)[SHA256_32]
rescue
  nil
end

#fetch_remotes_hash(spec, remotes) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/bundix/source.rb', line 44

def fetch_remotes_hash(spec, remotes)
  remotes.each do |remote|
    hash = fetch_remote_hash(spec, remote)
    return remote, hash if hash
  end

  nil
end

#nix_prefetch_git(uri, revision) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/bundix/source.rb', line 25

def nix_prefetch_git(uri, revision)
  home = ENV['HOME']
  ENV['HOME'] = '/homeless-shelter'
  sh(NIX_PREFETCH_GIT, '--url', uri, '--rev', revision, '--hash', 'sha256', '--leave-dotGit')
ensure
  ENV['HOME'] = home
end

#nix_prefetch_url(*args) ⇒ Object



19
20
21
22
23
# File 'lib/bundix/source.rb', line 19

def nix_prefetch_url(*args)
  sh(NIX_PREFETCH_URL, '--type', 'sha256', *args)
rescue
  nil
end

#sh(*args) ⇒ Object



15
16
17
# File 'lib/bundix/source.rb', line 15

def sh(*args)
  Bundix.sh(*args)
end