@@ -43,22 +43,70 @@ def execute_no_wrapper
43
43
specs = installed_gems . select { |spec | spec . name =~ /^#{ name } /i }
44
44
specs . each do |spec |
45
45
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"
57
49
end
58
50
end
59
51
end
60
52
61
53
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
+
62
110
def installed_gems
63
111
if Gem ::VERSION > '1.8' then
64
112
Gem ::Specification . to_a
0 commit comments