-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathdk.rb.erb
332 lines (286 loc) · 9.72 KB
/
dk.rb.erb
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# encoding: UTF-8
require 'win32/registry'
require 'yaml'
require 'fileutils'
module DevKitInstaller
DEVKIT_ROOT = File.expand_path(File.dirname(__FILE__))
DEVKIT_START = ':DK-BEG:'
DEVKIT_END = ':DK-END:'
REG_KEYS = [
'Software\RubyInstaller\MRI',
'SOFTWARE\Wow6432Node\RubyInstaller\MRI'
]
CONFIG_FILE = 'config.yml'
def self.usage
<<-EOT
Configures an MSYS/MinGW based Development Kit (DevKit) for
each of the Ruby installations on your Windows system. The
DevKit enables you to build many of the available native
RubyGems that don't yet have a binary gem.
Usage: ruby dk.rb COMMAND [options]
where COMMAND is one of:
init prepare DevKit for installation
review review DevKit install plan
install install required DevKit executables
and 'install' [options] are:
-f, --force overwrite existing helper scripts
EOT
end
def self.timestamp
Time.now.strftime('%Y%m%d%H%M%S')
end
private_class_method :timestamp
def self.gem_override(dk_root=DEVKIT_ROOT)
d = dk_root.gsub('/', '\\\\\\')
<<-EOT
# #{DEVKIT_START} override 'gem install' to enable RubyInstaller DevKit usage
Gem.pre_install do |gem_installer|
load 'devkit.rb' unless gem_installer.spec.extensions.empty?
end
# #{DEVKIT_END}
EOT
end
private_class_method :gem_override
def self.devkit_lib(dk_root=DEVKIT_ROOT)
d = dk_root.gsub('/', '\\\\\\')
<<-EOT
# enable RubyInstaller DevKit usage as a vendorable helper library
unless ENV['PATH'].include?('#{d}\\\\mingw\\\\bin') then
phrase = 'Temporarily enhancing PATH to include DevKit...'
if defined?(Gem)
Gem.ui.say(phrase) if Gem.configuration.verbose
else
puts phrase
end
puts "Prepending ENV['PATH'] to include DevKit..." if $DEBUG
ENV['PATH'] = '#{d}\\\\bin;#{d}\\\\mingw\\\\bin;' + ENV['PATH']
end
ENV['RI_DEVKIT'] = '#{d}'
<% tool_names.each_pair do |k,v| -%>
<%= 'ENV[\'%s\'] = \'%s\'' % [k,v] %>
<% end -%>
EOT
end
private_class_method :devkit_lib
def self.update_gem_override(target)
in_devkit = false
bkup = "#{target}.#{timestamp}"
File.rename(target, bkup)
# copy existing gem override except for old DevKit content
begin
File.open(bkup, 'r') do |src|
File.open(target, 'w') do |tgt|
src.each_line do |src_line|
case src_line
when /^# #{DEVKIT_START}/
in_devkit = true
when /^# #{DEVKIT_END}/
in_devkit = false
next
end
tgt.puts(src_line) unless in_devkit
end
# append new DevKit content
tgt.write(gem_override)
end
end
rescue
# restore backup if anything went wrong
FileUtils.cp(bkup, target)
end
end
private_class_method :update_gem_override
def self.scan_for(key)
ris = []
[Win32::Registry::HKEY_LOCAL_MACHINE, Win32::Registry::HKEY_CURRENT_USER].each do |hive|
begin
hive.open(key) do |ri_key|
ri_key.each_key do |skey, wtime|
# read the install location if a version subkey
if skey =~ /\d\.\d\.\d/
ri_key.open(skey) do |ver_key|
ri_root = ver_key['InstallLocation'].gsub('\\', '/')
puts '[INFO] found RubyInstaller v%s at %s' % [ skey, ri_root ]
ris << ri_root
end
end
end
end
rescue Win32::Registry::Error
end
end
ris
end
private_class_method :scan_for
def self.installed_rubies
rubies = REG_KEYS.collect { |key| scan_for(key) }
rubies.flatten.uniq
end
private_class_method :installed_rubies
def self.init
# get all known installed Ruby root dirs and write the root dirs
# to 'config.yml', overwriting any existing config file.
ir = installed_rubies
puts <<-EOT
Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all
of the installed Rubies you want enhanced by the DevKit.
EOT
File.open(CONFIG_FILE, 'w') do |f|
f.write <<-EOT
# This configuration file contains the absolute path locations of all
# installed Rubies to be enhanced to work with the DevKit. This config
# file is generated by the 'ruby dk.rb init' step and may be modified
# before running the 'ruby dk.rb install' step. To include any installed
# Rubies that were not automagically discovered, simply add a line below
# the triple hyphens with the absolute path to the Ruby root directory,
# prefixed with a hyphen and a space.
#
# Example:
#
# ---
# - C:/ruby19trunk
# - C:/ruby192dev
#
EOT
unless ir.empty? then f.write(ir.to_yaml) else f.write("---\n") end
end
end
private_class_method :init
def self.review
if File.exists?(File.expand_path(CONFIG_FILE))
File.open(CONFIG_FILE, 'r') do |f|
puts <<-EOT
Based upon the settings in the '#{CONFIG_FILE}' file generated
from running 'ruby dk.rb init' and any of your customizations,
DevKit functionality will be injected into the following Rubies
when you run 'ruby dk.rb install'.
EOT
rubies = YAML.load(f.read)
if rubies.is_a?(Array)
rubies.each { |i| puts File.expand_path(i) }
else
puts "Invalid configuration. Please fix '#{CONFIG_FILE}.'"
exit(-2)
end
end
else
puts <<-EOT
Unable to find '#{CONFIG_FILE}'. Have you run 'ruby dk.rb init' yet?
EOT
exit(-2)
end
end
def self.install
begin
rubies = YAML.load_file(CONFIG_FILE)
rescue
puts <<-EOT
Error loading '#{CONFIG_FILE}'. Have you run 'ruby dk.rb init' yet?
EOT
exit(-2)
end
unless rubies.is_a?(Array) && !rubies.empty?
puts <<-EOT
Invalid configuration or no Rubies listed. Please fix '#{CONFIG_FILE}'
and rerun 'ruby dk.rb install'
EOT
exit(-2)
end
rubies.each do |path|
path = File.expand_path(path)
unless File.directory?(path)
puts "[ERROR] Skipping invalid directory '#{path}'"
next
end
site_ruby = Dir.glob("#{path}/lib/ruby/site_ruby")
site_rubygems = Dir.glob("#{path}/lib/ruby/site_ruby/[1-9].*/rubygems")
core_rubygems = Dir.glob("#{path}/lib/ruby/[1-9].*/rubygems")
# Warn and exit if unable to find a RubyGems installation
if site_rubygems.empty? && core_rubygems.empty?
puts <<-EOT
[ERROR] Unable to find RubyGems in site_ruby or core Ruby. Please
install RubyGems and rerun 'ruby dk.rb install'.
EOT
exit(-2)
else
# either (or both) site_rubygems or core_rubygems contains RubyGems;
# favor injecting override into site_rubygems over core_rubygems
target_ruby = site_rubygems.empty? ? core_rubygems : site_rubygems
# inject RubyGems override file into proper site_ruby location
# appending an existing override file if it doesn't already contain
# DevKit specific code.
target_ruby.each do |folder|
target = File.join(folder, 'defaults', 'operating_system.rb')
FileUtils.mkdir_p File.dirname(target)
if File.exist?(target)
content = File.read(target)
case
when content !~ /^#.*DevKit/o
# handle original and new token-based comments
puts "[INFO] Updating existing gem override for '#{path}'"
File.open(target, 'a') { |f| f.write(gem_override) }
when content =~ /^# #{DEVKIT_START} missing DevKit/o
# replace missing DevKit/build tool convenience notice
puts "[INFO] Updating convenience notice gem override for '#{path}'"
update_gem_override(target)
else
puts "[INFO] Skipping existing gem override for '#{path}'" unless $options[:force]
if $options[:force]
puts "[WARN] Updating (with backup) existing gem override for '#{path}'"
update_gem_override(target)
end
end
else
puts "[INFO] Installing '#{target}'"
File.open(target, 'w') { |f| f.write(gem_override) }
end
end
end
# inject DevKit PATH helper into site_ruby (allows for overriding)
# for the 'ruby -rdevkit extconf.rb' use case.
# TODO more robust JRuby check since can't assume JRuby is running
# this script?
jruby_site_shared = File.join(site_ruby, 'shared')
if File.directory?(jruby_site_shared) && File.exist?(File.join(path, 'bin', 'jruby.bat'))
site_ruby = jruby_site_shared
end
target = File.join(site_ruby, 'devkit.rb')
if File.exist?(target)
# Be paranoid about our 'site_ruby/devkit.rb' namespace. Either
# someone else has collided with it, or we've already written the
# helper lib. Warn the developer and skip rather than overwriting
# or appending.
puts "[WARN] Skipping existing DevKit helper library for '#{path}'" unless $options[:force]
if $options[:force]
puts "[WARN] Updating (with backup) DevKit helper library for '#{path}'"
File.rename(target, "#{target}.#{timestamp}")
File.open(target, 'w') { |f| f.write(devkit_lib) }
end
else
puts "[INFO] Installing '#{target}'"
File.open(target, 'w') { |f| f.write(devkit_lib) }
end
end
end
private_class_method :install
def self.usage_and_exit
$stderr.puts usage
exit(-1)
end
def self.run(*args)
send(args.first)
end
end
if __FILE__ == $0
if ARGV.empty? || ARGV.delete('--help') || ARGV.delete('-h')
DevKitInstaller.usage_and_exit
end
cmd = ARGV.delete('init') ||
ARGV.delete('review') ||
ARGV.delete('install')
$options ||= {}
$options[:force] = ARGV.delete('--force') || ARGV.delete('-f')
DevKitInstaller.usage_and_exit unless ARGV.empty?
DevKitInstaller.run(cmd)
end