Better error when linking fails

Refs http://stackoverflow.com/questions/9762943

The system ln no longer outputs anything. Though the user can force its output with a --verbose of course. So in cases where it's not the usual of: not writable or existing file, we can ask the user to run with --verbose. I don't particularly like hiding its output, but it just confused the error IMO since it is creating a relative symlink the output was weird every time I've seen it in tickets.

I made a print wrapper so that the brew-link output doesn't get mucked up if an exception is thrown.
This commit is contained in:
Max Howell 2012-03-19 00:15:40 +00:00
parent 8088f6c17b
commit c3370c48ce
2 changed files with 33 additions and 6 deletions

View File

@ -1,4 +1,5 @@
module Homebrew extend self module Homebrew extend self
def link def link
raise KegUnspecifiedError if ARGV.named.empty? raise KegUnspecifiedError if ARGV.named.empty?
@ -9,9 +10,30 @@ module Homebrew extend self
end end
ARGV.kegs.each do |keg| ARGV.kegs.each do |keg|
print "Linking #{keg}... " print "Linking #{keg}... " do
puts if ARGV.verbose? puts if ARGV.verbose?
puts "#{keg.link} symlinks created" puts "#{keg.link} symlinks created"
end end
end end
end end
private
# Allows us to ensure a puts happens before the block exits so that if say,
# an exception is thrown, its output starts on a new line.
def print str, &block
Kernel.print str
puts_capture = Class.new do
def self.puts str
$did_puts = true
Kernel.puts str
end
end
puts_capture.instance_eval &block
ensure
puts unless $did_puts
end
end

View File

@ -283,9 +283,14 @@ class Pathname
self.dirname.mkpath self.dirname.mkpath
Dir.chdir self.dirname do Dir.chdir self.dirname do
# NOTE only system ln -s will create RELATIVE symlinks # NOTE only system ln -s will create RELATIVE symlinks
system 'ln', '-s', src.relative_path_from(self.dirname), self.basename quiet_system 'ln', '-s', src.relative_path_from(self.dirname), self.basename
# ln outputs useful error message for us if not $?.success?
raise "Could not create symlink: #{to_s}." unless $?.success? raise <<-EOS.undent
Could not symlink file: #{src.expand_path}
Check #{self} does not already exist.
Check #{dirname} is writable.
EOS
end
end end
end end