aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/switch.rb
blob: 8a378aed797babac1f3ed0de133ed2edeac9abad (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
#:  * `switch` <name> <version>:
#:    Symlink all of the specific <version> of <name>'s install to Homebrew prefix.

require "formula"
require "keg"
require "cmd/link"

module Homebrew
  module_function

  def switch
    if ARGV.named.length != 2
      onoe "Usage: brew switch <name> <version>"
      exit 1
    end

    name = ARGV.shift
    version = ARGV.shift

    rack = Formulary.to_rack(name)

    unless rack.directory?
      onoe "#{name} not found in the Cellar."
      exit 2
    end

    # Does the target version exist?
    unless (rack/version).directory?
      onoe "#{name} does not have a version \"#{version}\" in the Cellar."

      versions = rack.subdirs.map { |d| Keg.new(d).version }.sort
      puts "Versions available: #{versions.join(", ")}"

      exit 3
    end

    # Unlink all existing versions
    rack.subdirs.each do |v|
      keg = Keg.new(v)
      puts "Cleaning #{keg}"
      keg.unlink
    end

    keg = Keg.new(rack/version)

    # Link new version, if not keg-only
    if keg_only?(rack)
      keg.optlink
      puts "Opt link created for #{keg}"
    else
      puts "#{keg.link} links created for #{keg}"
    end
  end
end