Skip to content

Instantly share code, notes, and snippets.

@manveru
Last active May 6, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manveru/8d5c3e88f913d617eeb4f013f6547a83 to your computer and use it in GitHub Desktop.
Save manveru/8d5c3e88f913d617eeb4f013f6547a83 to your computer and use it in GitHub Desktop.
Create bundlerApp drvs with ease.
#! /usr/bin/env nix-shell
#! nix-shell -i ruby -p ruby bundix bundler
require 'optparse'
require 'rubygems/package'
require 'rubygems/remote_fetcher'
require 'erb'
op = OptionParser.new do |o|
o.banner = "Usage: make-bundler-app.rb GEM_NAME [options]"
o.on('-h', '--help', 'Prints this help'){ puts o; exit }
o.on('--licenses a,b', Array)
o.on('--maintainers a,b', Array)
o.on('--homepage URL')
o.on('--description STRING')
o.on('--version STRING')
end
options = {
description: nil,
licenses: [],
maintainers: [],
homepage: nil,
version: nil,
}
op.parse!(into: options)
gem = ARGV[0]
dependency = Gem::Dependency.new(gem, options[:version])
gem_file = Gem::RemoteFetcher.fetcher.download_to_cache(dependency)
package = Gem::Package.new(gem_file)
options[:spec] = package.spec
default_nix = ERB.new(<<EOF, nil, '-').result_with_hash(options)
{ lib, bundlerApp }:
bundlerApp {
pname = "<%= spec.name %>";
gemdir = ./.;
exes = [ <%= spec.executables.map{ |e| e.dump }.join(' ') %> ];
meta = with lib; {
description = "<%= (description || spec.description).to_s.gsub(/\.$/, '') %>";
homepage = <%= homepage || spec.homepage %>;
<% if licenses.size == 1 -%>
license = licenses.<%= licenses.first %>;
<% elsif licenses.size > 1 -%>
license = with licenses; [ <%= licenses.join(' ') %> ];
<% end -%>
<% if maintainers.size > 0 -%>
maintainers = with maintainers; [ <%= maintainers.join(' ') %> ];
<% end -%>
};
}
EOF
FileUtils.mkdir_p(gem)
Dir.chdir gem do
File.write('default.nix', default_nix)
File.write('Gemfile', <<~EOF)
source 'https://rubygems.org' do
gem #{gem.dump}, #{dependency.requirement.to_s.dump}
end
EOF
puts 'generating lockfiles...'
system('bundle', 'lock')
system('bundix')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment