Permalink
Browse files

Improve sudo support

  • Loading branch information...
1 parent f58b686 commit 16259eaebf6a4d867ed35ea02a125528fa8c9343 @wycats wycats committed Jun 8, 2010
Showing with 19 additions and 7 deletions.
  1. +4 −1 lib/bundler.rb
  2. +15 −6 lib/bundler/source.rb
View
5 lib/bundler.rb
@@ -174,10 +174,13 @@ def default_gemfile
SharedHelpers.default_gemfile
end
+ WINDOWS = Config::CONFIG["host_os"] =~ %r!(msdos|mswin|djgpp|mingw)!
+ NULL = WINDOWS ? "NUL" : "/dev/null"
+
def requires_sudo?
case
when File.writable?(bundle_path) ||
- `which sudo 2>NUL`.empty? ||
+ `which sudo 2>#{NULL}`.empty? ||
File.owned?(bundle_path)
false
else
View
21 lib/bundler/source.rb
@@ -99,14 +99,18 @@ def install(spec)
# SUDO HAX
if Bundler.requires_sudo?
- `sudo mkdir -p #{Gem.dir}/gems #{Gem.dir}/specifications`
- `sudo mv #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/`
- `sudo mv #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/`
+ sudo "mkdir -p #{Gem.dir}/gems #{Gem.dir}/specifications"
+ sudo "mv #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/"
+ sudo "mv #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/"
end
spec.loaded_from = "#{Gem.dir}/specifications/#{spec.full_name}.gemspec"
end
+ def sudo(str)
+ `sudo -E #{str}`
+ end
+
def cache(spec)
cached_path = cached_gem(spec)
raise GemNotFound, "Missing gem file '#{spec.full_name}.gem'." unless cached_path
@@ -222,8 +226,8 @@ def download_gem_from_uri(spec, uri)
Gem::RemoteFetcher.fetcher.download(spec, uri, download_path)
if Bundler.requires_sudo?
- `sudo mkdir -p #{Gem.dir}/cache`
- `sudo mv #{Bundler.tmp}/cache/#{spec.full_name}.gem #{gem_path}`
+ sudo "mkdir -p #{Gem.dir}/cache"
+ sudo "mv #{Bundler.tmp}/cache/#{spec.full_name}.gem #{gem_path}"
end
gem_path
@@ -510,7 +514,12 @@ def load_spec_files
private
def git(command)
- out = %x{git #{command}}
+ if Bundler.requires_sudo?
+ out = %x{sudo -E git #{command}}
+ else
+ out = %x{git #{command}}
+ end
+
if $? != 0
raise GitError, "An error has occurred in git. Cannot complete bundling."
end

0 comments on commit 16259ea

Please sign in to comment.