1require 'rbconfig'
2require 'bundler/vendored_thor'
3require 'bundler'
4require 'rubygems/command'
5require 'fileutils'
6require 'pathname'
7require 'tmpdir'
8require 'shellwords'
9
10if defined?(Encoding.default_internal)
11 Encoding.default_internal = Encoding::UTF_8
12 Encoding.default_external = Encoding::UTF_8
13end
14
15# Options:
16#
17# type - installation type, either "git" or "path"
18# name - the gem name
19# version - gem version
20# build-flags - build arguments
21#
22# Git-only options:
23#
24# uri - git repo uri
25# repo - path to local checkout
26# ref - the commit hash
27
28ruby = File.join(ENV["ruby"], "bin", RbConfig::CONFIG['ruby_install_name'])
29out = ENV["out"]
30bin_dir = File.join(ENV["out"], "bin")
31
32type = ARGV[0]
33name = ARGV[1]
34version = ARGV[2]
35build_flags = Shellwords.split(ARGV[3])
36if type == "git"
37 uri = ARGV[4]
38 REPO = ARGV[5]
39 ref = ARGV[6]
40end
41
42# options to pass to bundler
43options = {
44 "name" => name,
45 "version" => version,
46}
47if type == "path"
48 options.merge!({
49 "path" => Dir.pwd,
50 })
51elsif type == "git"
52 options.merge!({
53 "uri" => uri,
54 "ref" => ref,
55 })
56end
57
58# Monkey-patch Bundler to use our local checkout.
59# I wish we didn't have to do this, but bundler does not expose an API to do
60# these kinds of things.
61Bundler.module_eval do
62 def self.requires_sudo?
63 false
64 end
65
66 def self.root
67 # we don't have a Gemfile, so it doesn't make sense to try to make paths
68 # relative to the (non existent) parent directory thereof, so we give a
69 # nonsense path here.
70 Pathname.new("/no-root-path")
71 end
72
73 def self.bundle_path
74 Pathname.new(ENV["GEM_HOME"])
75 end
76
77 def self.locked_gems
78 nil
79 end
80end
81
82if type == "git"
83 Bundler::Source::Git.class_eval do
84 def allow_git_ops?
85 true
86 end
87 end
88
89 Bundler::Source::Git::GitProxy.class_eval do
90 def checkout
91 unless path.exist?
92 FileUtils.mkdir_p(path.dirname)
93 FileUtils.cp_r(File.join(REPO, ".git"), path)
94 system("chmod -R +w #{path}")
95 end
96 end
97
98 def copy_to(destination, submodules=false)
99 unless File.exist?(destination.join(".git"))
100 FileUtils.mkdir_p(destination.dirname)
101 FileUtils.cp_r(REPO, destination)
102 system("chmod -R +w #{destination}")
103 end
104 end
105 end
106end
107
108# UI
109verbose = false
110no_color = false
111Bundler.ui = Bundler::UI::Shell.new({"no-color" => no_color})
112Bundler.ui.level = "debug" if verbose
113
114# Install
115if type == "git"
116 source = Bundler::Source::Git.new(options)
117else
118 source = Bundler::Source::Path.new(options)
119end
120spec = source.specs.search_all(name).first
121Bundler.rubygems.with_build_args build_flags do
122 source.install(spec)
123end
124
125msg = spec.post_install_message
126if msg
127 Bundler.ui.confirm "Post-install message from #{name}:"
128 Bundler.ui.info msg
129end
130
131# Write out the binstubs
132if spec.executables.any?
133 FileUtils.mkdir_p(bin_dir)
134 spec.executables.each do |exe|
135 wrapper = File.join(bin_dir, exe)
136 File.open(wrapper, "w") do |f|
137 f.write(<<-EOF)
138#!#{ruby}
139#
140# This file was generated by Nix.
141#
142# The application '#{exe}' is installed as part of a gem, and
143# this file is here to facilitate running it.
144#
145
146require 'rubygems'
147require 'bundler/setup'
148
149load Gem.bin_path(#{spec.name.inspect}, #{exe.inspect})
150EOF
151 end
152
153 FileUtils.chmod("+x", wrapper)
154 end
155end
156
157# Write out metadata
158meta = "#{out}/nix-support/gem-meta"
159FileUtils.mkdir_p(meta)
160FileUtils.ln_s(spec.loaded_from.to_s, "#{meta}/spec")
161File.open("#{meta}/name", "w") do |f|
162 f.write spec.name
163end
164if type == "git"
165 File.open("#{meta}/install-path", "w") do |f|
166 f.write source.install_path.to_s
167 end
168end
169File.open("#{meta}/require-paths", "w") do |f|
170 f.write spec.require_paths.join(" ")
171end
172File.open("#{meta}/executables", "w") do |f|
173 f.write spec.executables.join(" ")
174end
175
176# make the lib available during bundler/git installs
177if type == "git"
178 File.open("#{out}/nix-support/setup-hook", "a") do |f|
179 spec.require_paths.each do |dir|
180 f.puts("addToSearchPath RUBYLIB #{source.install_path}/#{dir}")
181 end
182 end
183end