Skip to content

Commit

Permalink
cask: read installed_version from Info.plist when sensible.
Browse files Browse the repository at this point in the history
If you're trying to use `brew info --json=v2` to get an installed
version and figure out if it is outdated: you're going to have a bad
time with `auto_updates` casks because `installed_version` is not
actually consistent with the installed version of the app.

Instead, in these cases, try to read from `Info.plist` if there is one
and use that version.

While we're here, add a `blank?` method to `Version` so we can use it
for `present?` checks (making a `null?` `Version` object `blank?`).
  • Loading branch information
MikeMcQuaid committed Mar 5, 2024
1 parent a436f7b commit ee6b7ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "attrable"
require "bundle_version"
require "cask/cask_loader"
require "cask/config"
require "cask/dsl"
Expand Down Expand Up @@ -176,8 +177,19 @@ def installed_caskfile

sig { returns(T.nilable(String)) }
def installed_version
if auto_updates &&
(bundle = artifacts.find { |a| a.is_a?(Artifact::App) }&.target) &&
(plist = Pathname("#{bundle}/Contents/Info.plist")) &&
plist.exist? &&
(bundle_version = ::Homebrew::BundleVersion.from_info_plist(plist)) &&
(bundle_short_version = bundle_version.short_version.presence)

Check warning on line 185 in Library/Homebrew/cask/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/cask.rb#L184-L185

Added lines #L184 - L185 were not covered by tests
return bundle_short_version
end

return unless (installed_caskfile = self.installed_caskfile)

# <caskroom_path>/.metadata/<version>/<timestamp>/Casks/<token>.{rb,json} -> <version>
installed_caskfile&.dirname&.dirname&.dirname&.basename&.to_s
installed_caskfile.dirname.dirname.dirname.basename.to_s
end

def config_path
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def numeric?
def null?
false
end

sig { returns(T::Boolean) }
def blank? = null?
end

# A pseudo-token representing the absence of a token.
Expand Down Expand Up @@ -127,6 +130,9 @@ def null?
true
end

sig { returns(T::Boolean) }
def blank? = true

sig { override.returns(String) }
def inspect
"#<#{self.class.name}>"
Expand Down

0 comments on commit ee6b7ea

Please sign in to comment.