Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit f54469e

Browse files
author
Colby Swandale
authored
Merge pull request #7109 from bundler/colby/port-gem-helper
Backport `GemHelper` changes for #7108
2 parents da0ff0c + e8ea176 commit f54469e

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

lib/bundler/gem_helper.rb

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "bundler/vendored_thor" unless defined?(Thor)
44
require "bundler"
5+
require "shellwords"
56

67
module 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

spec/bundler/gem_helper_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def mock_build_message(name, version)
181181
`git init`
182182
`git config user.email "you@example.com"`
183183
`git config user.name "name"`
184+
`git config commit.gpgsign false`
184185
`git config push.default simple`
185186
end
186187

@@ -208,11 +209,12 @@ def mock_build_message(name, version)
208209
end
209210

210211
context "succeeds" do
212+
let(:repo) { build_git("foo", :bare => true) }
213+
211214
before do
212-
Dir.chdir(gem_repo1) { `git init --bare` }
213215
Dir.chdir(app_path) do
214-
`git remote add origin file://#{gem_repo1}`
215-
`git commit -a -m "initial commit"`
216+
sys_exec("git remote add origin file://#{repo.path}")
217+
sys_exec('git commit -a -m "initial commit"')
216218
end
217219
end
218220

0 commit comments

Comments
 (0)