cask/artifact: check the bundle version when using --adopt.

This makes `--adopt` considerably faster and more useful for application
bundles by checking the bundle version before failing to adopt the
bundle.

This could be further extended by e.g. checking if auto-updates are
enabled.

While we're here, also allow `adopt` to act a bit more like `force` in
a few other places assuming this initial check passes.
This commit is contained in:
Mike McQuaid 2024-03-14 10:31:31 +00:00
parent a7d77485f1
commit a3702a28c4
No known key found for this signature in database
2 changed files with 33 additions and 11 deletions

View File

@ -52,12 +52,34 @@ module Cask
else else
if adopt if adopt
ohai "Adopting existing #{self.class.english_name} at '#{target}'" ohai "Adopting existing #{self.class.english_name} at '#{target}'"
same = command.run(
"/usr/bin/diff", source_plist = Pathname("#{source}/Contents/Info.plist")
args: ["--recursive", "--brief", source, target], target_plist = Pathname("#{target}/Contents/Info.plist")
verbose:, same = if source_plist.size? &&
print_stdout: verbose, (source_bundle_version = Homebrew::BundleVersion.from_info_plist(source_plist)) &&
).success? target_plist.size? &&
(target_bundle_version = Homebrew::BundleVersion.from_info_plist(target_plist))
if source_bundle_version.short_version == target_bundle_version.short_version
if source_bundle_version.version == target_bundle_version.version
true
else
onoe "The bundle version of #{source} is #{source_bundle_version.version} but " \
"is #{target_bundle_version.version} for #{target}!"
false
end
else
onoe "The bundle short version of #{source} is #{source_bundle_version.short_version} but " \
"is #{target_bundle_version.short_version} for #{target}!"
false
end
else
command.run(
"/usr/bin/diff",
args: ["--recursive", "--brief", source, target],
verbose:,
print_stdout: verbose,
).success?
end
unless same unless same
raise CaskError, raise CaskError,
@ -73,7 +95,7 @@ module Cask
message = "It seems there is already #{self.class.english_article} " \ message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{target}'" "#{self.class.english_name} at '#{target}'"
raise CaskError, "#{message}." unless force raise CaskError, "#{message}." if !force && !adopt
opoo "#{message}; overwriting." opoo "#{message}; overwriting."
delete(target, force:, command:, **options) delete(target, force:, command:, **options)
@ -120,13 +142,13 @@ module Cask
end end
end end
def move_back(skip: false, force: false, command: nil, **options) def move_back(skip: false, force: false, adopt: false, command: nil, **options)
FileUtils.rm source if source.symlink? && source.dirname.join(source.readlink) == target FileUtils.rm source if source.symlink? && source.dirname.join(source.readlink) == target
if Utils.path_occupied?(source) if Utils.path_occupied?(source)
message = "It seems there is already #{self.class.english_article} " \ message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{source}'" "#{self.class.english_name} at '#{source}'"
raise CaskError, "#{message}." unless force raise CaskError, "#{message}." if !force && !adopt
opoo "#{message}; overwriting." opoo "#{message}; overwriting."
delete(source, force:, command:, **options) delete(source, force:, command:, **options)

View File

@ -43,7 +43,7 @@ module Cask
private private
def link(force: false, command: nil, **_options) def link(force: false, adopt: false, command: nil, **_options)
unless source.exist? unless source.exist?
raise CaskError, raise CaskError,
"It seems the #{self.class.link_type_english_name.downcase} " \ "It seems the #{self.class.link_type_english_name.downcase} " \
@ -54,7 +54,7 @@ module Cask
message = "It seems there is already #{self.class.english_article} " \ message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{target}'" "#{self.class.english_name} at '#{target}'"
if force && target.symlink? && if (force || adopt) && target.symlink? &&
(target.realpath == source.realpath || target.realpath.to_s.start_with?("#{cask.caskroom_path}/")) (target.realpath == source.realpath || target.realpath.to_s.start_with?("#{cask.caskroom_path}/"))
opoo "#{message}; overwriting." opoo "#{message}; overwriting."
Utils.gain_permissions_remove(target, command:) Utils.gain_permissions_remove(target, command:)