brew/Library/Homebrew/dev-cmd/livecheck.rb

128 lines
4.5 KiB
Ruby
Raw Normal View History

2020-11-25 17:03:23 +01:00
# typed: true
# frozen_string_literal: true
require "cli/parser"
require "formula"
require "livecheck/livecheck"
require "livecheck/strategy"
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
module_function
WATCHLIST_PATH = File.expand_path(Homebrew::EnvConfig.livecheck_watchlist).freeze
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def livecheck_args
Homebrew::CLI::Parser.new do
description <<~EOS
2020-09-03 21:18:04 -07:00
Check for newer versions of formulae and/or casks from upstream.
2020-12-12 10:12:58 -05:00
If no formula or cask argument is passed, the list of formulae and
casks to check is taken from `HOMEBREW_LIVECHECK_WATCHLIST` or
`~/.brew_livecheck_watchlist`.
EOS
switch "--full-name",
2020-09-03 21:18:04 -07:00
description: "Print formulae/casks with fully-qualified names."
flag "--tap=",
2020-09-03 21:18:04 -07:00
description: "Check formulae/casks within the given tap, specified as <user>`/`<repo>."
switch "--all",
2020-09-03 21:18:04 -07:00
description: "Check all available formulae/casks."
switch "--installed",
2020-09-03 21:18:04 -07:00
description: "Check formulae/casks that are currently installed."
switch "--newer-only",
2020-09-03 21:18:04 -07:00
description: "Show the latest version only if it's newer than the formula/cask."
switch "--json",
description: "Output information in JSON format."
switch "-r", "--resources",
description: "Check resources with livecheck blocks."
switch "-q", "--quiet",
description: "Suppress warnings, don't print a progress bar for JSON output."
switch "--formula", "--formulae",
description: "Only check formulae."
switch "--cask", "--casks",
description: "Only check casks."
2020-11-27 11:41:08 -05:00
conflicts "--debug", "--json"
conflicts "--tap=", "--all", "--installed"
conflicts "--cask", "--formula"
2021-01-10 14:26:40 -05:00
named_args [:formula, :cask]
end
end
def livecheck
args = livecheck_args.parse
if args.debug? && args.verbose?
puts args
puts Homebrew::EnvConfig.livecheck_watchlist if Homebrew::EnvConfig.livecheck_watchlist.present?
end
package_and_resource_to_check = if args.tap
tap = Tap.fetch(args.tap)
formulae = args.cask? ? [] : tap.formula_files.map { |path| Formulary.factory(path) }
casks = args.formula? ? [] : tap.cask_files.map { |path| Cask::CaskLoader.load(path) }
formulae + casks
elsif args.installed?
formulae = args.cask? ? [] : Formula.installed
casks = args.formula? ? [] : Cask::Caskroom.casks
formulae + casks
2022-07-17 12:47:20 +02:00
elsif args.resources?
2022-07-24 15:47:15 +02:00
# formula_with_resources = Formula.all.select { |formula| formula.resources.any? }
formula_with_resources = Formula.all.select { |formula| formula.resources.any? { |resource| resource.livecheckable? } }
2022-07-24 14:14:10 +02:00
formula_with_resources
elsif args.all?
formulae = args.cask? ? [] : Formula.all
casks = args.formula? ? [] : Cask::Cask.all
formulae + casks
elsif args.named.present?
if args.formula?
args.named.to_formulae
elsif args.cask?
args.named.to_casks
2020-12-18 21:59:31 +09:00
else
args.named.to_package_and_resource
end
elsif File.exist?(WATCHLIST_PATH)
begin
names = Pathname.new(WATCHLIST_PATH).read.lines
.reject { |line| line.start_with?("#") || line.blank? }
.map(&:strip)
named_args = T.unsafe(CLI::NamedArgs).new(*names, parent: args)
named_args.to_package_and_resource(ignore_unavailable: true)
rescue Errno::ENOENT => e
onoe e
end
else
raise UsageError, "A watchlist file is required when no arguments are given."
end
2022-07-24 14:14:10 +02:00
# p package_and_resource_to_check.class
# p package_and_resource_to_check.length
# p package_and_resource_to_check[0].class
# p package_and_resource_to_check.map { |d| d.name }
2022-07-17 12:47:20 +02:00
package_and_resource_to_check = package_and_resource_to_check.sort_by do |package_or_resource|
package_or_resource.respond_to?(:token) ? package_or_resource.token : package_or_resource.name
end
raise UsageError, "No formulae or casks to check." if package_and_resource_to_check.blank?
2020-12-14 14:30:36 +01:00
options = {
json: args.json?,
full_name: args.full_name?,
2021-03-04 01:01:56 +05:30
handle_name_conflict: !args.formula? && !args.cask?,
2022-07-24 15:47:15 +02:00
resources_only: args.resources?,
newer_only: args.newer_only?,
quiet: args.quiet?,
debug: args.debug?,
verbose: args.verbose?,
2020-12-14 14:30:36 +01:00
}.compact
2022-07-24 15:47:15 +02:00
Livecheck.run_checks(package_and_resource_to_check, **options)
end
end