summary refs log tree commit diff
path: root/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb
blob: add5db56d1d05383a3253e44fcae5ba85a33dbc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'rbconfig'
require 'rubygems'
require 'rubygems/specification'
require 'fileutils'

# args/settings
out = ENV["out"]
ruby = ARGV[0]
gemfile = ARGV[1]
bundle_path = ARGV[2]
bundler_path = ARGV[3]
paths = ARGV[4].split
groups = ARGV[5].split

# generate binstubs
FileUtils.mkdir_p("#{out}/bin")
paths.each do |path|
  next unless File.directory?("#{path}/nix-support/gem-meta")

  name = File.read("#{path}/nix-support/gem-meta/name")
  executables = File.read("#{path}/nix-support/gem-meta/executables")
    .force_encoding('UTF-8').split
  executables.each do |exe|
    File.open("#{out}/bin/#{exe}", "w") do |f|
      f.write(<<-EOF)
#!#{ruby}
#
# This file was generated by Nix.
#
# The application '#{exe}' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] = #{gemfile.dump}
ENV["BUNDLE_PATH"] = #{bundle_path.dump}
ENV['BUNDLE_FROZEN'] = '1'

$LOAD_PATH.unshift #{bundler_path.dump} + "/lib"

require 'bundler'
Bundler.setup(#{groups.map(&:dump).join(', ')})

load Gem.bin_path(#{name.dump}, #{exe.dump})
EOF
      FileUtils.chmod("+x", "#{out}/bin/#{exe}")
    end
  end
end