2012-10-20 20:54:11 -05:00
|
|
|
require 'ostruct'
|
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
module Homebrew extend self
|
2012-03-19 00:15:40 +00:00
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
def link
|
2012-02-04 00:01:29 -06:00
|
|
|
raise KegUnspecifiedError if ARGV.named.empty?
|
|
|
|
|
2012-10-20 20:54:11 -05:00
|
|
|
mode = OpenStruct.new
|
|
|
|
|
|
|
|
mode.overwrite = true if ARGV.include? '--overwrite'
|
|
|
|
mode.dry_run = true if ARGV.dry_run?
|
2012-06-17 16:54:20 -05:00
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
ARGV.kegs.each do |keg|
|
2012-07-11 20:13:46 -07:00
|
|
|
if keg.linked?
|
2012-03-19 12:24:13 +00:00
|
|
|
opoo "Already linked: #{keg}"
|
2012-10-22 12:27:07 -07:00
|
|
|
puts "To relink: brew unlink #{keg.fname} && brew link #{keg.fname}"
|
2012-03-19 12:24:13 +00:00
|
|
|
next
|
2013-05-20 21:55:01 -05:00
|
|
|
elsif keg_only?(keg.fname) && !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
|
|
|
|
elsif mode.dry_run && mode.overwrite
|
2014-03-27 15:50:06 -05:00
|
|
|
puts "Would remove:"
|
|
|
|
keg.link(mode)
|
2012-06-17 16:54:20 -05:00
|
|
|
|
2012-10-20 20:54:11 -05:00
|
|
|
next
|
|
|
|
elsif mode.dry_run
|
2014-03-27 15:50:06 -05:00
|
|
|
puts "Would link:"
|
|
|
|
keg.link(mode)
|
2012-10-20 20:54:11 -05:00
|
|
|
|
2012-06-17 16:54:20 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:25 -06:00
|
|
|
keg.lock do
|
|
|
|
print "Linking #{keg}... " do
|
2014-03-26 21:51:27 -05:00
|
|
|
puts if ARGV.verbose?
|
2013-01-23 00:26:25 -06:00
|
|
|
puts "#{keg.link(mode)} symlinks created"
|
|
|
|
end
|
2012-03-19 00:15:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-05-20 21:55:01 -05:00
|
|
|
def keg_only?(name)
|
|
|
|
Formula.factory(name).keg_only?
|
|
|
|
rescue FormulaUnavailableError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2012-03-19 00:15:40 +00:00
|
|
|
# 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
|
2014-03-26 21:51:27 -05:00
|
|
|
def self.puts(*args)
|
2012-03-19 00:15:40 +00:00
|
|
|
$did_puts = true
|
2014-03-26 21:51:27 -05:00
|
|
|
Kernel.puts(*args)
|
2012-03-19 00:15:40 +00:00
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
2012-03-19 00:15:40 +00:00
|
|
|
|
2013-04-06 00:14:03 -05:00
|
|
|
puts_capture.instance_eval(&block)
|
2012-03-19 00:15:40 +00:00
|
|
|
|
|
|
|
ensure
|
|
|
|
puts unless $did_puts
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
2012-03-19 00:15:40 +00:00
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|