Skip to content

Commit

Permalink
Update commands to use cask install receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
Rylan12 committed Jul 3, 2024
1 parent 71602ec commit 90c4cd0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
11 changes: 8 additions & 3 deletions Library/Homebrew/cask/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.get_info(cask)
output << "#{Formatter.url(cask.homepage)}\n" if cask.homepage
deprecate_disable = DeprecateDisable.message(cask)
output << "#{deprecate_disable.capitalize}\n" if deprecate_disable
output << installation_info(cask)
output << "#{installation_info(cask)}\n"
repo = repo_info(cask)
output << "#{repo}\n" if repo
output << name_info(cask)
Expand All @@ -37,7 +37,7 @@ def self.title_info(cask)
end

def self.installation_info(cask)
return "Not installed\n" unless cask.installed?
return "Not installed" unless cask.installed?

versioned_staged_path = cask.caskroom_path.join(cask.installed_version)
path_details = if versioned_staged_path.exist?
Expand All @@ -46,7 +46,12 @@ def self.installation_info(cask)
Formatter.error("does not exist")
end

"Installed\n#{versioned_staged_path} (#{path_details})\n"
tab = Tab.for_cask(cask)

info = ["Installed"]
info << "#{versioned_staged_path} (#{path_details})"
info << " #{tab.to_cask_install_message}" if tab.tabfile&.exist?
info.join("\n")
end

def self.name_info(cask)
Expand Down
56 changes: 37 additions & 19 deletions Library/Homebrew/cmd/tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Cmd
class TabCmd < AbstractCommand
cmd_args do
description <<~EOS
Edit tab information for installed formulae.
Edit tab information for installed formulae or casks.
This can be useful when you want to control whether an installed
formula should be removed by `brew autoremove`.
Expand All @@ -19,13 +19,18 @@ class TabCmd < AbstractCommand
EOS

switch "--installed-on-request",
description: "Mark <formula> as installed on request."
description: "Mark <installed_formula> or <installed_cask> as installed on request."
switch "--no-installed-on-request",
description: "Mark <formula> as not installed on request."
description: "Mark <installed_formula> or <installed_cask> as not installed on request."
switch "--formula", "--formulae",
description: "Only mark formulae."
switch "--cask", "--casks",
description: "Only mark casks."

conflicts "--formula", "--cask"
conflicts "--installed-on-request", "--no-installed-on-request"

named_args :formula, min: 1
named_args [:installed_formula, :installed_cask], min: 1
end

sig { override.void }
Expand All @@ -37,38 +42,51 @@ def run
end
raise UsageError, "No marking option specified." if installed_on_request.nil?

formulae = args.named.to_formulae
if (formulae_not_installed = formulae.reject(&:any_version_installed?)).any?
formula_names = formulae_not_installed.map(&:name)
is_or_are = (formula_names.length == 1) ? "is" : "are"
odie "#{formula_names.to_sentence} #{is_or_are} not installed."
formulae, casks = args.named.to_formulae_to_casks
formulae_not_installed = formulae.reject(&:any_version_installed?)
casks_not_installed = casks.reject(&:installed?)
if formulae_not_installed.any? || casks_not_installed.any?
names = formulae_not_installed.map(&:name) + casks_not_installed.map(&:token)
is_or_are = (names.length == 1) ? "is" : "are"
odie "#{names.to_sentence} #{is_or_are} not installed."
end

formulae.each do |formula|
update_tab formula, installed_on_request:
[*formulae, *casks].each do |formula_or_cask|
update_tab formula_or_cask, installed_on_request:
end
end

private

sig { params(formula: Formula, installed_on_request: T::Boolean).void }
def update_tab(formula, installed_on_request:)
tab = Tab.for_formula(formula)
unless tab.tabfile.exist?
sig { params(formula_or_cask: T.any(Formula, Cask::Cask), installed_on_request: T::Boolean).void }
def update_tab(formula_or_cask, installed_on_request:)
name, tab, type = if formula_or_cask.is_a?(Formula)
[formula_or_cask.name, Tab.for_formula(formula_or_cask), :formula]
else
[formula_or_cask.token, Tab.for_cask(formula_or_cask), :cask]
end

if tab.tabfile.blank? || !tab.tabfile.exist?
raise ArgumentError,
"Tab file for #{formula.name} does not exist."
"Tab file for #{name} does not exist."
end

installed_on_request_str = "#{"not " unless installed_on_request}installed on request"
if (tab.installed_on_request && installed_on_request) ||
(!tab.installed_on_request && !installed_on_request)
ohai "#{formula.name} is already marked as #{installed_on_request_str}."
ohai "#{name} is already marked as #{installed_on_request_str}."
return
end

tab.installed_on_request = installed_on_request
tab.write
ohai "#{formula.name} is now marked as #{installed_on_request_str}."

if type == :formula
tab.write_formula_file
else
tab.write_cask_file
end

ohai "#{name} is now marked as #{installed_on_request_str}."
end
end
end
Expand Down

0 comments on commit 90c4cd0

Please sign in to comment.