brew/Library/Homebrew/formula_pin.rb
apainintheneck db507be41a keg: update sorting by version logic
This updates logic to add a `#scheme_and_version` method to be used
with `.sort_by` and `.max_by`. Using `Keg#version` by itself can be
inaccurate when different version schemes are present. This also
updates the behavior of `Formula#eligible_kegs_for_cleanup` to match
the previous behavior. We were dropping the wrong keg based on the
sort being reversed in a previous PR.
2024-03-31 17:52:32 -07:00

48 lines
853 B
Ruby

# typed: true
# frozen_string_literal: true
require "keg"
# Helper functions for pinning a formula.
#
# @api private
class FormulaPin
def initialize(formula)
@formula = formula
end
def path
HOMEBREW_PINNED_KEGS/@formula.name
end
def pin_at(version)
HOMEBREW_PINNED_KEGS.mkpath
version_path = @formula.rack/version
path.make_relative_symlink(version_path) if !pinned? && version_path.exist?
end
def pin
latest_keg = @formula.installed_kegs.max_by(&:scheme_and_version)
return if latest_keg.nil?
pin_at(latest_keg.version)
end
def unpin
path.unlink if pinned?
HOMEBREW_PINNED_KEGS.rmdir_if_possible
end
def pinned?
path.symlink?
end
def pinnable?
!@formula.installed_prefixes.empty?
end
def pinned_version
Keg.new(path.resolved_path).version if pinned?
end
end