Mike McQuaid 94148c3bc8
Fix handling unreadable casks
When casks are unreadable (e.g. have invalid syntax, the cask file
cannot be found) then it's not been possible to uninstall them, list
them or perform any operation which iterates through all casks.

Handle these various cases by falling back to creating a `Cask::Cask`
object using just the name/token and latest installed version on disk.

This provides enough functionality to be able to verbosely list these
casks, not error on listing and, most importantly, uninstall/reinstall
them.

Fixes https://github.com/Homebrew/homebrew-cask/issues/62223
2022-05-16 17:27:13 -04:00

43 lines
967 B
Ruby

# typed: false
# frozen_string_literal: true
module Cask
class Cmd
# Cask implementation for the `brew uninstall` command.
#
# @api private
class Zap < AbstractCommand
extend T::Sig
def self.parser
super do
switch "--force",
description: "Ignore errors when removing files."
end
end
sig { void }
def run
self.class.zap_casks(*casks, verbose: args.verbose?, force: args.force?)
end
sig { params(casks: Cask, force: T.nilable(T::Boolean), verbose: T.nilable(T::Boolean)).void }
def self.zap_casks(
*casks,
force: nil,
verbose: nil
)
require "cask/installer"
casks.each do |cask|
odebug "Zapping Cask #{cask}"
raise CaskNotInstalledError, cask if !cask.installed? && !force
Installer.new(cask, verbose: verbose, force: force).zap
end
end
end
end
end