aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/upgrade.rb
blob: f0682262f7f7874a6db9640f03b973f76fcfc1e5 (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
require 'cmd/outdated'
require 'cmd/install'

class Fixnum
  def plural_s
    if self > 1 then "s" else "" end
  end
end

module Homebrew extend self
  def upgrade
    Homebrew.perform_preinstall_checks

    outdated = if ARGV.named.empty?
      Homebrew.outdated_brews
    else
      ARGV.formulae.each do |f|
        raise "#{f} already upgraded" if f.installed?
        raise "#{f} not installed" unless f.rack.exist? and not f.rack.children.empty?
      end
    end

    # Expand the outdated list to include outdated dependencies then sort and
    # reduce such that dependencies are installed first and installation is not
    # attempted twice. Sorting is implicit the way `recursive_deps` returns
    # root dependencies at the head of the list and `uniq` keeps the first
    # element it encounters and discards the rest.
    outdated.map!{ |f| f.recursive_deps.reject{ |d| d.installed?} << f }
    outdated.flatten!
    outdated.uniq!

    if outdated.length > 1
      oh1 "Upgrading #{outdated.length} outdated package#{outdated.length.plural_s}, with result:"
      puts outdated.map{ |f| "#{f.name} #{f.version}" } * ", "
    end

    outdated.each do |f|
      installer = FormulaInstaller.new f
      installer.show_header = false
      oh1 "Upgrading #{f.name}"
      installer.install
      Keg.new("#{f.rack}/#{f.version}").unlink
      installer.caveats
      installer.finish # includes link step
    end
  end
end