summary refs log tree commit diff
path: root/pkgs/development/ruby-modules/gem/nix-bundle-install.rb
blob: b0be71102c38ef0142ecc9b87659fd5a19e53d22 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'rbconfig'
require 'bundler/vendored_thor'
require 'bundler'
require 'rubygems/command'
require 'fileutils'
require 'pathname'
require 'tmpdir'
require 'shellwords'

if defined?(Encoding.default_internal)
  Encoding.default_internal = Encoding::UTF_8
  Encoding.default_external = Encoding::UTF_8
end

# Options:
#
#   type        - installation type, either "git" or "path"
#   name        - the gem name
#   version     - gem version
#   build-flags - build arguments
#
# Git-only options:
#
#   uri         - git repo uri
#   repo        - path to local checkout
#   ref         - the commit hash

ruby = File.join(ENV["ruby"], "bin", RbConfig::CONFIG['ruby_install_name'])
out = ENV["out"]
bin_dir = File.join(ENV["out"], "bin")

type        = ARGV[0]
name        = ARGV[1]
version     = ARGV[2]
build_flags = Shellwords.split(ARGV[3])
if type == "git"
  uri         = ARGV[4]
  REPO        = ARGV[5]
  ref         = ARGV[6]
end

# options to pass to bundler
options = {
  "name" => name,
  "version" => version,
}
if type == "path"
  options.merge!({
    "path" => Dir.pwd,
  })
elsif type == "git"
  options.merge!({
    "uri"  => uri,
    "ref"  => ref,
  })
end

# Monkey-patch Bundler to use our local checkout.
# I wish we didn't have to do this, but bundler does not expose an API to do
# these kinds of things.
Bundler.module_eval do
  def self.requires_sudo?
    false
  end

  def self.root
    # we don't have a Gemfile, so it doesn't make sense to try to make paths
    # relative to the (non existent) parent directory thereof, so we give a
    # nonsense path here.
    Pathname.new("/no-root-path")
  end

  def self.bundle_path
    Pathname.new(ENV["GEM_HOME"])
  end

  def self.locked_gems
    nil
  end
end

if type == "git"
  Bundler::Source::Git.class_eval do
    def allow_git_ops?
      true
    end
  end

  Bundler::Source::Git::GitProxy.class_eval do
    def checkout
      unless path.exist?
        FileUtils.mkdir_p(path.dirname)
        FileUtils.cp_r(File.join(REPO, ".git"), path)
        system("chmod -R +w #{path}")
      end
    end

    def copy_to(destination, submodules=false)
      unless File.exist?(destination.join(".git"))
        FileUtils.mkdir_p(destination.dirname)
        FileUtils.cp_r(REPO, destination)
        system("chmod -R +w #{destination}")
      end
    end
  end
end

# UI
verbose = false
no_color = false
Bundler.ui = Bundler::UI::Shell.new({"no-color" => no_color})
Bundler.ui.level = "debug" if verbose

# Install
if type == "git"
  source = Bundler::Source::Git.new(options)
else
  source = Bundler::Source::Path.new(options)
end
spec = source.specs.search_all(name).first
source.install(spec, :build_args => build_flags)

msg = spec.post_install_message
if msg
  Bundler.ui.confirm "Post-install message from #{name}:"
  Bundler.ui.info msg
end

# Write out the binstubs
if spec.executables.any?
  FileUtils.mkdir_p(bin_dir)
  spec.executables.each do |exe|
    wrapper = File.join(bin_dir, exe)
    File.open(wrapper, "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.
#

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path(#{spec.name.inspect}, #{exe.inspect})
EOF
    end

    FileUtils.chmod("+x", wrapper)
  end
end

# Write out metadata
meta = "#{out}/nix-support/gem-meta"
FileUtils.mkdir_p(meta)
FileUtils.ln_s(spec.loaded_from.to_s, "#{meta}/spec")
File.open("#{meta}/name", "w") do |f|
  f.write spec.name
end
if type == "git"
  File.open("#{meta}/install-path", "w") do |f|
    f.write source.install_path.to_s
  end
end
File.open("#{meta}/require-paths", "w") do |f|
  f.write spec.require_paths.join(" ")
end
File.open("#{meta}/executables", "w") do |f|
  f.write spec.executables.join(" ")
end

# make the lib available during bundler/git installs
if type == "git"
  File.open("#{out}/nix-support/setup-hook", "a") do |f|
    spec.require_paths.each do |dir|
      f.puts("addToSearchPath RUBYLIB #{source.install_path}/#{dir}")
    end
  end
end