22
33require "bundler/vendored_thor" unless defined? ( Thor )
44require "bundler"
5+ require "shellwords"
56
67module Bundler
78 class GemHelper
@@ -75,7 +76,7 @@ def install
7576 def build_gem
7677 file_name = nil
7778 gem = ENV [ "BUNDLE_GEM" ] ? ENV [ "BUNDLE_GEM" ] : "gem"
78- sh ( " #{ gem } build -V ' #{ spec_path } '" ) do
79+ sh ( %W[ #{ gem } build -V #{ spec_path } ] ) do
7980 file_name = File . basename ( built_gem_path )
8081 SharedHelpers . filesystem_access ( File . join ( base , "pkg" ) ) { |p | FileUtils . mkdir_p ( p ) }
8182 FileUtils . mv ( built_gem_path , "pkg" )
@@ -87,17 +88,21 @@ def build_gem
8788 def install_gem ( built_gem_path = nil , local = false )
8889 built_gem_path ||= build_gem
8990 gem = ENV [ "BUNDLE_GEM" ] ? ENV [ "BUNDLE_GEM" ] : "gem"
90- out , _ = sh_with_code ( "#{ gem } install '#{ built_gem_path } '#{ " --local" if local } " )
91- raise "Couldn't install gem, run `gem install #{ built_gem_path } ' for more detailed output" unless out [ /Successfully installed/ ]
91+ cmd = %W[ #{ gem } install #{ built_gem_path } ]
92+ cmd << "--local" if local
93+ out , status = sh_with_status ( cmd )
94+ unless status . success? && out [ /Successfully installed/ ]
95+ raise "Couldn't install gem, run `gem install #{ built_gem_path } ' for more detailed output"
96+ end
9297 Bundler . ui . confirm "#{ name } (#{ version } ) installed."
9398 end
9499
95100 protected
96101
97102 def rubygem_push ( path )
98- gem_command = " gem push ' #{ path } '"
99- gem_command += " --key #{ gem_key } " if gem_key
100- gem_command += " --host #{ allowed_push_host } " if allowed_push_host
103+ gem_command = %W[ gem push #{ path } ]
104+ gem_command << " --key" << gem_key if gem_key
105+ gem_command << " --host" << allowed_push_host if allowed_push_host
101106 unless allowed_push_host || Bundler . user_home . join ( ".gem/credentials" ) . file?
102107 raise "Your rubygems.org credentials aren't set. Run `gem push` to set them."
103108 end
@@ -129,12 +134,14 @@ def gem_push_host
129134
130135 def perform_git_push ( options = "" )
131136 cmd = "git push #{ options } "
132- out , code = sh_with_code ( cmd )
133- raise "Couldn't git push. `#{ cmd } ' failed with the following output:\n \n #{ out } \n " unless code == 0
137+ out , status = sh_with_status ( cmd )
138+ return if status . success?
139+ cmd = cmd . shelljoin if cmd . respond_to? ( :shelljoin )
140+ raise "Couldn't git push. `#{ cmd } ' failed with the following output:\n \n #{ out } \n "
134141 end
135142
136143 def already_tagged?
137- return false unless sh ( " git tag" ) . split ( /\n / ) . include? ( version_tag )
144+ return false unless sh ( %w[ git tag ] ) . split ( /\n / ) . include? ( version_tag )
138145 Bundler . ui . confirm "Tag #{ version_tag } has already been created."
139146 true
140147 end
@@ -144,20 +151,20 @@ def guard_clean
144151 end
145152
146153 def clean?
147- sh_with_code ( " git diff --exit-code" ) [ 1 ] == 0
154+ sh_with_status ( %w[ git diff --exit-code ] ) [ 1 ] . success?
148155 end
149156
150157 def committed?
151- sh_with_code ( " git diff-index --quiet --cached HEAD" ) [ 1 ] == 0
158+ sh_with_status ( %w[ git diff-index --quiet --cached HEAD ] ) [ 1 ] . success?
152159 end
153160
154161 def tag_version
155- sh " git tag -m \" Version #{ version } \" #{ version_tag } "
162+ sh %W[ git tag -m Version\ #{ version } #{ version_tag } ]
156163 Bundler . ui . confirm "Tagged #{ version_tag } ."
157164 yield if block_given?
158165 rescue RuntimeError
159166 Bundler . ui . error "Untagging #{ version_tag } due to error."
160- sh_with_code " git tag -d #{ version_tag } "
167+ sh_with_status %W[ git tag -d #{ version_tag } ]
161168 raise
162169 end
163170
@@ -174,21 +181,20 @@ def name
174181 end
175182
176183 def sh ( cmd , &block )
177- out , code = sh_with_code ( cmd , &block )
178- unless code . zero?
184+ out , status = sh_with_status ( cmd , &block )
185+ unless status . success?
186+ cmd = cmd . shelljoin if cmd . respond_to? ( :shelljoin )
179187 raise ( out . empty? ? "Running `#{ cmd } ` failed. Run this command directly for more detailed output." : out )
180188 end
181189 out
182190 end
183191
184- def sh_with_code ( cmd , &block )
185- cmd += " 2>&1"
186- outbuf = String . new
192+ def sh_with_status ( cmd , &block )
187193 Bundler . ui . debug ( cmd )
188194 SharedHelpers . chdir ( base ) do
189- outbuf = ` #{ cmd } `
190- status = $?. exitstatus
191- block . call ( outbuf ) if status . zero ? && block
195+ outbuf = IO . popen ( cmd , :err => [ :child , :out ] , & :read )
196+ status = $?
197+ block . call ( outbuf ) if status . success ? && block
192198 [ outbuf , status ]
193199 end
194200 end
0 commit comments