aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/reinstall.rb
blob: c0379b5515ebc96114b85acbb80c1468d7f88fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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*' '}"
        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?
        restore_backup keg, formula
        raise 'Reinstall failed.'
      else
        backup_path(keg).rmtree if backup_path(keg).exist?
      end
    end
  end

  def backup keg
    keg.unlink
    keg.rename backup_path(keg)
  end

  def restore_backup keg, formula
    path = backup_path(keg)
    if path.directory?
      path.rename keg
      keg.link unless formula.keg_only?
    end
  end

  def backup_path path
    Pathname.new "#{path}.reinstall"
  end
end