Skip to content

Commit 96491f1

Browse files
committed
First try to fix binstubs before installing them, fix rvm/rvm#2536
1 parent b729564 commit 96491f1

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.3.0
4+
date: 2014-01-11
5+
6+
- First try to fix binstubs before installing them, fix wayneeseguin/rvm#2536
7+
38
## 1.2.6
49
date: 2013-10-21
510

lib/executable-hooks/regenerate_binstubs_command.rb

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,70 @@ def execute_no_wrapper
4343
specs = installed_gems.select{|spec| spec.name =~ /^#{name}/i }
4444
specs.each do |spec|
4545
unless spec.executables.empty?
46-
org_gem_path = Gem.path.find{|path|
47-
File.exists? File.join path, 'gems', spec.full_name
48-
} || Gem.dir
49-
cache_gem = File.join(org_gem_path, 'cache', spec.file_name)
50-
if File.exist? cache_gem
51-
puts "#{spec.name} #{spec.version}"
52-
inst = Gem::Installer.new Dir[cache_gem].first, :wrappers => true, :force => true, :install_dir => org_gem_path
53-
ExecutableHooksInstaller.bundler_generate_bin(inst)
54-
else
55-
$stderr.puts "##{spec.name} #{spec.version} not found in GEM_PATH"
56-
end
46+
try_to_fix_binstubs(spec) or
47+
try_to_install_binstubs(spec) or
48+
$stderr.puts "##{spec.name} #{spec.version} not found in GEM_PATH"
5749
end
5850
end
5951
end
6052

6153
private
54+
55+
def try_to_fix_binstubs(spec)
56+
executable_paths =
57+
spec.executables.map do |executable|
58+
path = expanded_bin_paths.detect{|path| File.exist?(File.join(path, executable)) }
59+
File.join(path, executable) if path
60+
end
61+
return false if executable_paths.include?(nil) # not found
62+
executable_shebangs =
63+
executable_paths.map do |path|
64+
[path, File.readlines(path).map(&:chomp)]
65+
end
66+
return false if executable_shebangs.detect{|path, lines| !lines[0] =~ /^#!\// }
67+
puts "#{spec.name} #{spec.version}"
68+
executable_shebangs.map do |path, lines|
69+
lines[0] = "#!/usr/bin/env ruby_executable_hooks"
70+
File.open(path, "w") do |file|
71+
file.puts(lines)
72+
end
73+
end
74+
end
75+
76+
def expanded_bin_paths
77+
@expanded_bin_paths ||= begin
78+
paths = expanded_gem_paths.map{|path| File.join(path, "bin") }
79+
paths << RbConfig::CONFIG["bindir"]
80+
paths
81+
end
82+
end
83+
84+
def try_to_install_binstubs(spec)
85+
org_gem_path = expanded_gem_paths.find{|path|
86+
File.exists? File.join path, 'gems', spec.full_name
87+
} || Gem.dir
88+
cache_gem = File.join(org_gem_path, 'cache', spec.file_name)
89+
if File.exist? cache_gem
90+
puts "#{spec.name} #{spec.version}"
91+
inst = Gem::Installer.new Dir[cache_gem].first, :wrappers => true, :force => true, :install_dir => org_gem_path
92+
ExecutableHooksInstaller.bundler_generate_bin(inst)
93+
else
94+
false
95+
end
96+
end
97+
98+
def expanded_gem_paths
99+
@expanded_gem_paths ||=
100+
Gem.path.map do |path|
101+
paths = [path]
102+
while File.symlink?(path)
103+
path = File.readlink(path)
104+
paths << path
105+
end
106+
paths
107+
end.flatten
108+
end
109+
62110
def installed_gems
63111
if Gem::VERSION > '1.8' then
64112
Gem::Specification.to_a

0 commit comments

Comments
 (0)