Class: Bundix

Inherits:
Object
  • Object
show all
Defined in:
lib/bundix.rb,
lib/bundix/nixer.rb,
lib/bundix/source.rb,
lib/bundix/version.rb,
lib/bundix/commandline.rb,
lib/bundix/shell_nix_context.rb

Defined Under Namespace

Classes: CommandLine, Dependency, Fetcher, Nixer, ShellNixContext, Source

Constant Summary collapse

NIX_INSTANTIATE =
'nix-instantiate'
NIX_PREFETCH_URL =
'nix-prefetch-url'
NIX_PREFETCH_GIT =
'nix-prefetch-git'
NIX_HASH =
'nix-hash'
NIX_SHELL =
'nix-shell'
SHA256_32 =
%r(^[a-z0-9]{52}$)
PLATFORM_MAPPING =
{}
VERSION =
'2.5.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Bundix

Returns a new instance of Bundix.



33
34
35
36
# File 'lib/bundix.rb', line 33

def initialize(options)
  @options = { quiet: false, tempfile: nil }.merge(options)
  @fetcher = Fetcher.new
end

Instance Attribute Details

#fetcherObject

Returns the value of attribute fetcher.



22
23
24
# File 'lib/bundix.rb', line 22

def fetcher
  @fetcher
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.sh(*args, &block) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/bundix.rb', line 175

def self.sh(*args, &block)
  out, status = Open3.capture2(*args)
  unless block_given? ? block.call(status, out) : status.success?
    puts "$ #{args.join(' ')}" if $VERBOSE
    puts out if $VERBOSE
    fail "command execution failed: #{status}"
  end
  out
end

Instance Method Details

#build_depcache(lock) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/bundix.rb', line 121

def build_depcache(lock)
  definition = Bundler::Definition.build(options[:gemfile], options[:lockfile], false)
  dep_cache = {}

  definition.dependencies.each do |dep|
    dep_cache[dep.name] = dep
  end

  lock.specs.each do |spec|
    dep_cache[spec.name] ||= Dependency.new(spec.name, nil, {})
  end

  begin
    changed = false
    lock.specs.each do |spec|
      as_dep = dep_cache.fetch(spec.name)

      spec.dependencies.each do |dep|
        cached = dep_cache.fetch(dep.name) do |name|
          if name != "bundler"
            raise KeyError, "Gem dependency '#{name}' not specified in #{lockfile}"
          end
          dep_cache[name] = Dependency.new(name, lock.bundler_version, {})
        end

        if !((as_dep.groups - cached.groups) - [:default]).empty? or !(as_dep.platforms - cached.platforms).empty?
          changed = true
          dep_cache[cached.name] = (Dependency.new(cached.name, nil, {
            "group" => as_dep.groups | cached.groups,
            "platforms" => as_dep.platforms | cached.platforms
          }))

          cc = dep_cache[cached.name]
        end
      end
    end
  end while changed

  return dep_cache
end

#convertObject



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

def convert
  cache = parse_gemset
  lock = parse_lockfile
  dep_cache = build_depcache(lock)

  # reverse so git comes last
  lock.specs.reverse_each.with_object({}) do |spec, gems|
    gem = find_cached_spec(spec, cache) || convert_spec(spec, cache, dep_cache)
    gems.merge!(gem)

    if spec.dependencies.any?
      gems[spec.name]['dependencies'] = spec.dependencies.map(&:name) - ['bundler']
    end
  end
end

#convert_spec(spec, cache, dep_cache) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bundix.rb', line 88

def convert_spec(spec, cache, dep_cache)
  {
    spec.name => {
      version: spec.version.to_s,
      source: Source.new(spec, fetcher).convert
    }.merge(platforms(spec, dep_cache)).merge(groups(spec, dep_cache))
  }
rescue => ex
  warn "Skipping #{spec.name}: #{ex}"
  puts ex.backtrace
  {spec.name => {}}
end

#find_cached_spec(spec, cache) ⇒ Object



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

def find_cached_spec(spec, cache)
  name, cached = cache.find{|k, v|
    next unless k == spec.name
    next unless cached_source = v['source']

    case spec_source = spec.source
    when Bundler::Source::Git
      next unless cached_source['type'] == 'git'
      next unless cached_rev = cached_source['rev']
      next unless spec_rev = spec_source.options['revision']
      spec_rev == cached_rev
    when Bundler::Source::Rubygems
      next unless cached_source['type'] == 'gem'
      v['version'] == spec.version.to_s
    end
  }

  {name => cached} if cached
end

#groups(spec, dep_cache) ⇒ Object



54
55
56
# File 'lib/bundix.rb', line 54

def groups(spec, dep_cache)
  {groups: dep_cache.fetch(spec.name).groups}
end

#parse_gemsetObject



162
163
164
165
166
167
168
169
# File 'lib/bundix.rb', line 162

def parse_gemset
  path = File.expand_path(options[:gemset])
  return {} unless File.file?(path)
  json = Bundix.sh(NIX_INSTANTIATE, '--eval', '-E', %(
    builtins.toJSON (import #{Nixer.serialize(path)}))
  )
  JSON.parse(json.strip.gsub(/\\"/, '"')[1..-2])
end

#parse_lockfileObject



171
172
173
# File 'lib/bundix.rb', line 171

def parse_lockfile
  Bundler::LockfileParser.new(File.read(options[:lockfile]))
end

#platforms(spec, dep_cache) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/bundix.rb', line 79

def platforms(spec, dep_cache)
  # c.f. Bundler::CurrentRuby
  platforms = dep_cache.fetch(spec.name).platforms.map do |platform_name|
    PLATFORM_MAPPING[platform_name.to_s]
  end.flatten

  {platforms: platforms}
end