1#!/usr/bin/env ruby
2require 'rbconfig'
3require 'rubygems'
4require 'rubygems/specification'
5require 'fileutils'
6
7# args/settings
8out = ENV["out"]
9ruby = ARGV[0]
10gemfile = ARGV[1]
11bundle_path = ARGV[2]
12bundler_path = ARGV[3]
13paths = ARGV[4].split
14groups = ARGV[5].split
15
16# generate binstubs
17FileUtils.mkdir_p("#{out}/bin")
18paths.each do |path|
19 next unless File.directory?("#{path}/nix-support/gem-meta")
20
21 name = File.read("#{path}/nix-support/gem-meta/name")
22 executables = File.read("#{path}/nix-support/gem-meta/executables")
23 .force_encoding('UTF-8').split
24 executables.each do |exe|
25 File.open("#{out}/bin/#{exe}", "w") do |f|
26 f.write(<<-EOF)
27#!#{ruby}
28#
29# This file was generated by Nix.
30#
31# The application '#{exe}' is installed as part of a gem, and
32# this file is here to facilitate running it.
33#
34
35ENV["BUNDLE_GEMFILE"] = #{gemfile.dump}
36ENV.delete 'BUNDLE_PATH'
37ENV['BUNDLE_FROZEN'] = '1'
38ENV['BUNDLE_IGNORE_CONFIG'] = '1'
39
40Gem.paths = { 'GEM_HOME' => #{bundle_path.dump} }
41
42$LOAD_PATH.unshift #{File.join(bundler_path, "/lib").dump}
43
44require 'bundler'
45# Monkey-patch out the check that Bundler performs to determine
46# whether the bundler env is writable. It's not writable, even for
47# root! And for this use of Bundler, it shouldn't be necessary since
48# we're not trying to perform any package management operations, only
49# produce a Gem path. Thus, we replace it with a method that will
50# always return false, to squelch a warning from Bundler saying that
51# sudo may be required.
52module Bundler
53 class <<self
54 def requires_sudo?
55 return false
56 end
57 end
58end
59Bundler.setup(#{groups.map(&:dump).join(', ')})
60
61load Gem.bin_path(#{name.dump}, #{exe.dump})
62EOF
63 FileUtils.chmod("+x", "#{out}/bin/#{exe}")
64 end
65 end
66end