Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve Gemfile to support groups #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 86 additions & 79 deletions bin/bundix
Original file line number Diff line number Diff line change
@@ -1,92 +1,99 @@
#!/usr/bin/env ruby
#! /usr/bin/env ruby
Copy link
Member

@zimbatm zimbatm Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only place where I see this space is in the nix world. Do you know the motivation behind ? It's not wrong, I'm just curious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just think it's more readable this way, don't care much either way, this is a change left over from an experiment using the nix-shell shebang for ruby, which unfortunately is still blocked by NixOS/nix#877 with a pending PR.


require 'optparse'
require 'tmpdir'
require 'pathname'

require_relative '../lib/bundix'

options = {
ruby: 'ruby',
bundle_pack_path: 'vendor/bundle',
lockfile: 'Gemfile.lock',
gemset: 'gemset.nix'
}

op = OptionParser.new do |o|
o.on '-m', '--magic', 'lock, pack, and write dependencies' do
options[:magic] = true
end

o.on '--ruby=ruby', 'ruby version to use for magic and init, defaults to latest' do |value|
options[:ruby] = value
end

o.on '--bundle-pack-path=vendor/bundle', "path to pack the magic" do |value|
options[:bundle_pack_path] = value
end

o.on '-i', '--init', "initialize a new shell.nix for nix-shell (won't overwrite old ones)" do
options[:init] = true
end

o.on '--gemset=gemset.nix', 'path to the gemset.nix' do |value|
options[:gemset] = File.expand_path(value)
end
ENV.delete('SSL_CERT_FILE') if ENV['SSL_CERT_FILE'] == '/no-cert-file.crt'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean that bundix would be inside of a sandbox anyways ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using it like nix-shell -p bundler --command ~/github/manveru/bundix/bin/bundix, and having no SSL connections means bundix can't work. If you know a more elegant solution, please inform me :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. No better solution to offer I'm afraid.


o.on '--lockfile=Gemfile.lock', 'path to the Gemfile.lock' do |value|
options[:lockfile] = File.expand_path(value)
require_relative '../lib/bundix'
require_relative '../lib/bundix/version'

class Bundix
OPTIONS = {
ruby: 'ruby',
gemset: 'gemset.nix',
cache: false,
quiet: false,
development_dependencies: false,
groups: []
}

Bundler.settings[:frozen] = true

op = OptionParser.new do |o|
o.on '--ruby=ruby', 'ruby version to use for init, defaults to latest' do |value|
OPTIONS[:ruby] = value
end

o.on '-i', '--init', "initialize a new shell.nix for nix-shell (won't overwrite old ones)" do
OPTIONS[:init] = true
end

o.on '--gemset=gemset.nix', 'path to the gemset.nix' do |value|
OPTIONS[:gemset] = File.expand_path(value)
end

o.on '--gemfile=Gemfile', 'path to the Gemfile' do |value|
Bundler.settings[:gemfile] = Pathname(value)
end

o.on '--lockfile=Gemfile.lock', 'path to the Gemfile.lock' do |value|
Bundler.settings[:lockfile] = Pathname(value)
end

o.on '-l', '--lock', 'create Gemfile.lock for given groups' do |groups|
Bundler.settings[:frozen] = false
end

o.on '-c', '--cache', 'resolve dependencies from cache' do
OPTIONS[:cache] = true
end

o.on '-g', '--groups [GROUP1,GROUP2]', Array, 'only use these groups for the lockfile' do |groups|
OPTIONS[:groups].concat groups.map(&:to_sym)
end

o.on '--development-dependencies', 'include development dependencies from gemspecs in the set' do
OPTIONS[:development_dependencies] = true
end

o.on '-q', '--quiet', 'only output errors' do
OPTIONS[:quiet] = true
end

o.on '-v', '--version', 'show the version of bundix' do
puts Bundix::VERSION
exit
end
end

o.on '-d', '--dependencies', 'include gem dependencies' do
options[:deps] = true
op.parse!
$VERBOSE = !OPTIONS[:quiet]

if OPTIONS[:init]
if File.file?('shell.nix')
warn "won't override existing shell.nix"
else
shell_nix = File.read(File.expand_path('../template/shell.nix', __dir__))
shell_nix.gsub!('PROJECT', File.basename(Dir.pwd))
shell_nix.gsub!('RUBY', OPTIONS[:ruby])
shell_nix.gsub!('GEMFILE', "./#{Bundler.settings[:gemfile].relative_path_from(Pathname('./'))}")
shell_nix.gsub!('LOCKFILE', "./#{Bundler.settings[:lockfile].relative_path_from(Pathname('./'))}")
shell_nix.gsub!('GEMSET', "./#{Pathname(OPTIONS[:gemset]).relative_path_from(Pathname('./'))}")
File.write('shell.nix', shell_nix)
end
end

o.on '-q', '--quiet', 'only output errors' do
options[:quiet] = true
end
gemset = Bundix.new(OPTIONS).convert

o.on '-v', '--version', 'show the version of bundix' do
puts Bundix::VERSION
exit
tempfile = Tempfile.new('gemset.nix', encoding: 'UTF-8')
begin
Bundix.object2nix(gemset, 2, tempfile)
tempfile.flush
FileUtils.cp(tempfile.path, OPTIONS[:gemset])
ensure
tempfile.close!
tempfile.unlink
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to move the option parsing in the lib instead ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, once we have some tests :P

end

op.parse!
$VERBOSE = !options[:quiet]

if options[:magic]
fail unless system(
Bundix::NIX_SHELL, '-p', options[:ruby],
"bundler.override { ruby = #{options[:ruby]}; }",
"--command", "bundle lock --lockfile=#{options[:lockfile]}")
fail unless system(
Bundix::NIX_SHELL, '-p', options[:ruby],
"bundler.override { ruby = #{options[:ruby]}; }",
"--command", "bundle pack --all --path #{options[:bundle_pack_path]}")
end

if options[:init]
if File.file?('shell.nix')
warn "won't override existing shell.nix"
else
shell_nix = File.read(File.expand_path('../template/shell.nix', __dir__))
shell_nix.gsub!('PROJECT', File.basename(Dir.pwd))
shell_nix.gsub!('RUBY', options[:ruby])
shell_nix.gsub!('LOCKFILE', "./#{Pathname(options[:lockfile]).relative_path_from(Pathname('./'))}")
shell_nix.gsub!('GEMSET', "./#{Pathname(options[:gemset]).relative_path_from(Pathname('./'))}")
File.write('shell.nix', shell_nix)
end
end

gemset = Bundix.new(options).convert

tempfile = Tempfile.new('gemset.nix', encoding: 'UTF-8')
begin
Bundix.object2nix(gemset, 2, tempfile)
tempfile.flush
FileUtils.cp(tempfile.path, options[:gemset])
ensure
tempfile.close!
tempfile.unlink
end
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ let
};
in stdenv.mkDerivation {
name = "bundix";
buildInputs = [bundix];
buildInputs = [bundler bundix];
}
69 changes: 35 additions & 34 deletions lib/bundix.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,75 @@
require 'bundler'
require 'json'
require 'open-uri'
require 'open3'
require 'pp'
require 'set'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where you're using set and/or tsort here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my ctrl+f was off. So I see Set in use, but not TSort.


require_relative 'bundix/version'
require_relative 'bundix/source'
require_relative 'bundix/gemfile_dependency_tree'

class Bundix
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}$)
SHA256_16 = %r(^[a-f0-9]{64}$)

attr_reader :options

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

def convert
cache = parse_gemset
lock = parse_lockfile
@cache = parse_gemset
puts "resolving dependencies..." if $VERBOSE
tree = GemfileDependencyTree.run(options)
gems = {}

tree.each do |name, node|
gems[name] = convert_one(name, node)
@cache[name] = gems[name]
end

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

if options[:deps] && spec.dependencies.any?
gems[spec.name]['dependencies'] = spec.dependencies.map(&:name) - ['bundler']
end
end
def convert_one(name, node)
find_cached_spec(node) || convert_spec(node)
end

def convert_spec(spec, cache)
{spec.name => {version: spec.version.to_s, source: Source.new(spec).convert}}
def convert_spec(spec, definition = nil)
{
'version' => spec.version.to_s,
'groups' => spec.groups,
'dependencies' => spec.dependencies,
'source' => Source.new(spec, definition).convert
}
rescue => ex
warn "Skipping #{spec.name}: #{ex}"
puts ex.backtrace
{spec.name => {}}
{}
end

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

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

{name => cached} if cached
cached
end


def parse_gemset
path = File.expand_path(options[:gemset])
return {} unless File.file?(path)
Expand All @@ -77,10 +78,6 @@ def parse_gemset
JSON.parse(json.strip.gsub(/\\"/, '"')[1..-2])
end

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

def self.object2nix(obj, level = 2, out = '')
case obj
when Hash
Expand All @@ -97,7 +94,7 @@ def self.object2nix(obj, level = 2, out = '')
out << (v.is_a?(Hash) ? "\n" : ";\n")
end
out << (' ' * (level - 2)) << (level == 2 ? '}' : '};')
when Array
when Array, Set
out << '[' << obj.sort.map{|o| o.to_str.dump }.join(' ') << ']'
when String
out << obj.dump
Expand All @@ -110,6 +107,10 @@ def self.object2nix(obj, level = 2, out = '')
end
end

def sh(*args)
self.class.sh(*args)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also define the main method here and then use module_function(:sh) to export it to the module level.


def self.sh(*args)
out, status = Open3.capture2e(*args)
unless status.success?
Expand Down
Loading