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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bundix/source.rb', line 105

def convert_git
  revision = spec.source.options.fetch('revision')
  uri = spec.source.options.fetch('uri')
  output = nix_prefetch_git(uri, revision)
  # FIXME: this is a hack, we should separate $stdout/$stderr in the sh call
  hash = JSON.parse(output[/({[^}]+})\s*\z/m])['sha256']
  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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bundix/source.rb', line 92

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
  fail "couldn't fetch hash for #{spec.name}-#{spec.version}" unless hash
  hash = sh(NIX_HASH, '--type', 'sha256', '--to-base32', hash)[SHA256_32]
  puts "#{hash} => #{spec.name}-#{spec.version}.gem" if $VERBOSE

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

#download(file, url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bundix/source.rb', line 19

def download(file, url)
  warn "Downloading #{file} from #{url}"
  uri = URI(url)
  open_options = {}
  if uri.user && uri.password
    open_options[:http_basic_authentication] = [uri.user, uri.password]
    uri.user = nil
    uri.password = nil
  end
  open(uri, 'r', 0600, open_options) do |net|
    File.open(file, 'wb+') { |local|
      File.copy_stream(net, local)
    }
  end
end

#fetch_local_hash(spec) ⇒ Object



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

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/bundix/source.rb', line 81

def fetch_remote_hash(spec, remote)
  uri = "#{remote}/gems/#{spec.name}-#{spec.version}.gem"
  result = nix_prefetch_url(uri)
  return unless result
  result[SHA256_32]
rescue => e
  puts "ignoring error during fetching: #{e}"
  puts e.backtrace
  nil
end

#fetch_remotes_hash(spec, remotes) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/bundix/source.rb', line 72

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



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

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(url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bundix/source.rb', line 35

def nix_prefetch_url(url)
  dir = File.expand_path('~/.cache/bundix')
  FileUtils.mkdir_p dir
  file = File.join(dir, url.gsub(/[^\w-]+/, '_'))

  unless File.size?(file)
    download(file, url)
  end

  return unless File.size?(file)

  sh('nix-prefetch-url', '--type', 'sha256', "file://#{file}")
    .force_encoding('UTF-8').strip
rescue => ex
  puts ex
  nil
end

#sh(*args, &block) ⇒ Object



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

def sh(*args, &block)
  Bundix.sh(*args, &block)
end