brew/Library/Homebrew/cmd/reinstall.rb

59 lines
1.5 KiB
Ruby
Raw Normal View History

2013-02-17 13:23:41 +00:00
require 'cmd/install'
module Homebrew extend self
def reinstall
# At first save the named formulae and remove them from ARGV
named = ARGV.named
ARGV.delete_if { |arg| named.include? arg }
clean_ARGV = ARGV.clone
# Add the used_options for each named formula separately so
# that the options apply to the right formula.
named.each do |name|
ARGV.replace(clean_ARGV)
ARGV << name
tab = Tab.for_name(name)
tab.used_options.each { |option| ARGV << option.to_s }
if tab.built_as_bottle and not tab.poured_from_bottle
ARGV << '--build-bottle'
end
canonical_name = Formula.canonical_name(name)
formula = Formula.factory(canonical_name)
begin
oh1 "Reinstalling #{name} #{ARGV.options_only*' '}"
2013-09-05 14:59:33 +02:00
opt_link = HOMEBREW_PREFIX/'opt'/canonical_name
if opt_link.exist?
keg = Keg.new(opt_link.realpath)
backup keg
end
self.install_formula formula
rescue Exception => e
ofail e.message unless e.message.empty?
2013-09-05 14:59:33 +02:00
restore_backup keg, formula
raise 'Reinstall failed.'
else
2013-09-05 14:59:33 +02:00
backup_path(keg).rmtree if backup_path(keg).exist?
end
end
end
2013-09-05 14:59:33 +02:00
def backup keg
keg.unlink
2013-09-05 14:59:33 +02:00
keg.rename backup_path(keg)
end
2013-09-05 14:59:33 +02:00
def restore_backup keg, formula
path = backup_path(keg)
if path.directory?
2013-09-05 14:59:33 +02:00
path.rename keg
keg.link unless formula.keg_only?
end
2013-02-17 13:23:41 +00:00
end
2013-09-05 14:59:33 +02:00
def backup_path path
Pathname.new "#{path}.reinstall"
end
2013-02-17 13:23:41 +00:00
end