aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/formula_pin.rb
blob: c9a195fdd97ce1c92c0f6ac293e39b8d66fd554b (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
require "keg"

class FormulaPin
  PINDIR = Pathname.new("#{HOMEBREW_LIBRARY}/PinnedKegs")

  def initialize(f)
    @f = f
  end

  def path
    Pathname.new("#{PINDIR}/#{@f.name}")
  end

  def pin_at(version)
    PINDIR.mkpath
    version_path = @f.rack.join(version)
    path.make_relative_symlink(version_path) unless pinned? || !version_path.exist?
  end

  def pin
    pin_at(@f.installed_kegs.map { |keg| keg.version }.max)
  end

  def unpin
    path.unlink if pinned?
    PINDIR.rmdir_if_possible
  end

  def pinned?
    path.symlink?
  end

  def pinnable?
    @f.installed_prefixes.any?
  end

  def pinned_version
    Keg.new(path.resolved_path).version if pinned?
  end
end