76 lines
1.7 KiB
Ruby
Raw Normal View History

require 'ostruct'
module Homebrew extend self
def link
raise KegUnspecifiedError if ARGV.named.empty?
if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero?
raise "Cowardly refusing to `sudo brew link'\n#{SUDO_BAD_ERRMSG}"
end
mode = OpenStruct.new
mode.overwrite = true if ARGV.include? '--overwrite'
mode.dry_run = true if ARGV.dry_run?
ARGV.kegs.each do |keg|
2012-07-11 20:13:46 -07:00
if keg.linked?
opoo "Already linked: #{keg}"
puts "To relink: brew unlink #{keg.fname} && brew link #{keg.fname}"
next
end
if mode.dry_run and mode.overwrite
print "Would remove:\n" do
keg.link(mode)
end
next
elsif mode.dry_run
print "Would link:\n" do
keg.link(mode)
end
next
end
begin
f = Formula.factory(keg.fname)
if f.keg_only? and not ARGV.force?
opoo "#{keg.fname} is keg-only and must be linked with --force"
puts "Note that doing so can interfere with building software."
next
end
2013-04-10 12:02:35 -05:00
rescue FormulaUnavailableError
end
keg.lock do
print "Linking #{keg}... " do
puts "#{keg.link(mode)} symlinks created"
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
2013-04-06 00:14:03 -05:00
puts_capture.instance_eval(&block)
ensure
puts unless $did_puts
end
end