From 210d22e8199f2482b28f550838a33cca47380ce4 Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 6 Jul 2020 12:49:20 -0400 Subject: [PATCH 01/16] Integrate upgrade with cask --- Library/Homebrew/cmd/upgrade.rb | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index 66a804916513e..b313b57bf440f 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -4,6 +4,9 @@ require "formula_installer" require "install" require "upgrade" +require "cask/cmd" +require "cask/utils" +require "cask/macos" module Homebrew module_function @@ -50,6 +53,8 @@ def upgrade_args description: "Print install times for each formula at the end of the run." switch "-n", "--dry-run", description: "Show what would be upgraded, but do not actually upgrade anything." + switch "--greedy", + description: "Upgrade casks with `auto_updates` or `version :latest`" conflicts "--build-from-source", "--force-bottle" formula_options end @@ -66,14 +71,14 @@ def upgrade outdated = Formula.installed.select do |f| f.outdated?(fetch_head: args.fetch_HEAD?) end - - exit 0 if outdated.empty? + casks = [] # Upgrade all installed casks else - outdated = args.resolved_formulae.select do |f| + formulae, casks = args.resolved_formulae_casks + outdated, not_outdated = formulae.partition do |f| f.outdated?(fetch_head: args.fetch_HEAD?) end - (args.resolved_formulae - outdated).each do |f| + not_outdated.each do |f| versions = f.installed_kegs.map(&:version) if versions.empty? ofail "#{f.full_specified_name} not installed" @@ -82,9 +87,15 @@ def upgrade opoo "#{f.full_specified_name} #{version} already installed" end end - return if outdated.empty? end + upgrade_outdated_formulae(outdated) + upgrade_outdated_casks(casks) + end + + def upgrade_outdated_formulae(outdated) + return if outdated.empty? + pinned = outdated.select(&:pinned?) outdated -= pinned formulae_to_install = outdated.map(&:latest_formula) @@ -115,4 +126,12 @@ def upgrade Homebrew.messages.display_messages end + + def upgrade_outdated_casks(casks) + cask_upgrade = Cask::Cmd::Upgrade.new(casks) + cask_upgrade.force = args.force? + cask_upgrade.dry_run = args.dry_run? + cask_upgrade.greedy = args.greedy? + cask_upgrade.run + end end From ad04284a200a017615db466bf633a4299740d79a Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 6 Jul 2020 17:23:31 -0400 Subject: [PATCH 02/16] upgrade: Move logic to find outdated formulae into upgrade method --- Library/Homebrew/cmd/upgrade.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index b313b57bf440f..6d4730759e6d0 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -63,17 +63,22 @@ def upgrade_args def upgrade upgrade_args.parse + formulae, casks = args.resolved_formulae_casks + + upgrade_outdated_formulae(formulae) + upgrade_outdated_casks(casks) + end + + def upgrade_outdated_formulae(formulae) FormulaInstaller.prevent_build_flags unless DevelopmentTools.installed? Install.perform_preinstall_checks - if args.no_named? + if formulae.blank? outdated = Formula.installed.select do |f| f.outdated?(fetch_head: args.fetch_HEAD?) end - casks = [] # Upgrade all installed casks else - formulae, casks = args.resolved_formulae_casks outdated, not_outdated = formulae.partition do |f| f.outdated?(fetch_head: args.fetch_HEAD?) end @@ -89,11 +94,6 @@ def upgrade end end - upgrade_outdated_formulae(outdated) - upgrade_outdated_casks(casks) - end - - def upgrade_outdated_formulae(outdated) return if outdated.empty? pinned = outdated.select(&:pinned?) From bdc99ebd987a867de5e86c96e73217de276d5a1a Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 6 Jul 2020 17:24:20 -0400 Subject: [PATCH 03/16] outdated: Allow references to cask --- Library/Homebrew/cmd/outdated.rb | 68 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index ae065b1bcf0ea..ec647d84c15b4 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -3,6 +3,8 @@ require "formula" require "keg" require "cli/parser" +require "cask/cmd" +require "cask/caskroom" module Homebrew module_function @@ -22,43 +24,67 @@ def outdated_args flag "--json", description: "Print output in JSON format. Currently the default and only accepted "\ "value for is `v1`. See the docs for examples of using the JSON "\ - "output: " + "output: ."\ + "By default, this option treats all arguments as formulae."\ + "To treat arguments as casks, use the --cask-only option." switch "--fetch-HEAD", description: "Fetch the upstream repository to detect if the HEAD installation of the "\ "formula is outdated. Otherwise, the repository's HEAD will only be checked for "\ "updates when a new stable or development version has been released." + switch "--greedy", + description: "Print outdated casks with `auto_updates` or `version :latest`" + switch "--formula-only", + description: "Treat all arguments as formulae" + switch "--cask-only", + description: "Treat all arguments as casks" switch :debug conflicts "--quiet", "--verbose", "--json" + conflicts "--formula-only", "--cask-only" end end def outdated outdated_args.parse - formulae = if args.resolved_formulae.blank? - Formula.installed + if args.formula_only? || !args.cask_only? && args.json + formulae = args.resolved_formulae.blank? ? Formula.installed : args.resolved_formulae + outdated = print_outdated_formulae(formulae) + + Homebrew.failed = !formulae.blank? && !outdated.blank? + elsif args.cask_only? + casks = args.named.blank? ? Cask::Caskroom.casks : args.named + outdated = print_outdated_casks(casks) + + Homebrew.failed = !casks.blank? && !outdated.blank? else - args.resolved_formulae + formulae, casks = args.resolved_formulae_casks + + if formulae.blank? && casks.blank? + formulae = Formula.installed + casks = Cask::Caskroom.casks + end + + outdated_formulae = print_outdated_formulae(formulae) + outdated_casks = print_outdated_casks(casks) + + Homebrew.failed = !formulae.blank? && !outdated_formulae.blank? || !casks.blank? && !outdated_casks.blank? end + end + + def print_outdated_formulae(formulae) if args.json raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json - outdated = print_outdated_json(formulae) - else - outdated = print_outdated(formulae) + return print_outdated_formulae_json(formulae) end - Homebrew.failed = args.resolved_formulae.present? && !outdated.empty? - end - def print_outdated(formulae) - verbose = ($stdout.tty? || args.verbose?) && !args.quiet? fetch_head = args.fetch_HEAD? outdated_formulae = formulae.select { |f| f.outdated?(fetch_head: fetch_head) } .sort outdated_formulae.each do |f| - if verbose + if verbose? outdated_kegs = f.outdated_kegs(fetch_head: fetch_head) current_version = if f.alias_changed? @@ -87,7 +113,7 @@ def print_outdated(formulae) end end - def print_outdated_json(formulae) + def print_outdated_formulae_json(formulae) json = [] fetch_head = args.fetch_HEAD? outdated_formulae = formulae.select { |f| f.outdated?(fetch_head: fetch_head) } @@ -110,4 +136,20 @@ def print_outdated_json(formulae) outdated end + + def print_outdated_casks(casks) + outdated = casks.map { |cask| Cask::CaskLoader.load(cask) }.select do |cask| + odebug "Checking update info of Cask #{cask}" + cask.outdated?(args.greedy?) + end + + output = outdated.map { |cask| cask.outdated_info(args.greedy?, verbose?, args.json?) } + puts args.json ? JSON.generate(output) : output + + outdated + end + + def verbose? + ($stdout.tty? || args.verbose?) && !args.quiet? + end end From a3da9fa3506951bd425414a9c9e904554f723087 Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 6 Jul 2020 18:59:44 -0400 Subject: [PATCH 04/16] outdated: Actually print json information for casks --- Library/Homebrew/cmd/outdated.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index ec647d84c15b4..7d7dbabad6bfa 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -46,6 +46,10 @@ def outdated_args def outdated outdated_args.parse + if args.json + raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json + end + if args.formula_only? || !args.cask_only? && args.json formulae = args.resolved_formulae.blank? ? Formula.installed : args.resolved_formulae outdated = print_outdated_formulae(formulae) @@ -72,11 +76,7 @@ def outdated end def print_outdated_formulae(formulae) - if args.json - raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json - - return print_outdated_formulae_json(formulae) - end + return print_outdated_formulae_json(formulae) if args.json fetch_head = args.fetch_HEAD? @@ -143,7 +143,7 @@ def print_outdated_casks(casks) cask.outdated?(args.greedy?) end - output = outdated.map { |cask| cask.outdated_info(args.greedy?, verbose?, args.json?) } + output = outdated.map { |cask| cask.outdated_info(args.greedy?, verbose?, args.json) } puts args.json ? JSON.generate(output) : output outdated From 966131f7688576f52e3e3325bb07f63f11264e79 Mon Sep 17 00:00:00 2001 From: William Ma Date: Tue, 7 Jul 2020 09:37:36 -0400 Subject: [PATCH 05/16] outdated: Fail when user supplies formulae and some are outdated --- Library/Homebrew/cmd/outdated.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 7d7dbabad6bfa..2573434eff534 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -24,8 +24,8 @@ def outdated_args flag "--json", description: "Print output in JSON format. Currently the default and only accepted "\ "value for is `v1`. See the docs for examples of using the JSON "\ - "output: ."\ - "By default, this option treats all arguments as formulae."\ + "output: . "\ + "By default, this option treats all arguments as formulae. "\ "To treat arguments as casks, use the --cask-only option." switch "--fetch-HEAD", description: "Fetch the upstream repository to detect if the HEAD installation of the "\ @@ -53,13 +53,9 @@ def outdated if args.formula_only? || !args.cask_only? && args.json formulae = args.resolved_formulae.blank? ? Formula.installed : args.resolved_formulae outdated = print_outdated_formulae(formulae) - - Homebrew.failed = !formulae.blank? && !outdated.blank? elsif args.cask_only? casks = args.named.blank? ? Cask::Caskroom.casks : args.named outdated = print_outdated_casks(casks) - - Homebrew.failed = !casks.blank? && !outdated.blank? else formulae, casks = args.resolved_formulae_casks @@ -68,11 +64,10 @@ def outdated casks = Cask::Caskroom.casks end - outdated_formulae = print_outdated_formulae(formulae) - outdated_casks = print_outdated_casks(casks) - - Homebrew.failed = !formulae.blank? && !outdated_formulae.blank? || !casks.blank? && !outdated_casks.blank? + outdated = print_outdated_formulae(formulae) + print_outdated_casks(casks) end + + Homebrew.failed = args.named.present? && outdated.present? end def print_outdated_formulae(formulae) From ee0e9945e0f1a690471cca570ed005694d7ca6d8 Mon Sep 17 00:00:00 2001 From: William Ma Date: Wed, 8 Jul 2020 12:43:24 -0400 Subject: [PATCH 06/16] outdated: Update logic when handling --json to be more explicit --- Library/Homebrew/cmd/outdated.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 2573434eff534..3150f1d444834 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -48,9 +48,14 @@ def outdated if args.json raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json + + # When user asks for json, print json for formula only unless the user explicitly asks for casks + formula_only = !args.cask_only? + else + formula_only = args.formula_only? end - if args.formula_only? || !args.cask_only? && args.json + if formula_only formulae = args.resolved_formulae.blank? ? Formula.installed : args.resolved_formulae outdated = print_outdated_formulae(formulae) elsif args.cask_only? From d81296f8dcdbec3081a02f0bb668188adedeb7f0 Mon Sep 17 00:00:00 2001 From: William Ma Date: Wed, 8 Jul 2020 20:58:14 -0400 Subject: [PATCH 07/16] outdated: Rename --formula-only and --cask-only to --formula and --cask For consistency with brew --cache --- Library/Homebrew/cmd/outdated.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 3150f1d444834..795cbebc53e75 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -33,9 +33,9 @@ def outdated_args "updates when a new stable or development version has been released." switch "--greedy", description: "Print outdated casks with `auto_updates` or `version :latest`" - switch "--formula-only", + switch "--formula", description: "Treat all arguments as formulae" - switch "--cask-only", + switch "--cask", description: "Treat all arguments as casks" switch :debug conflicts "--quiet", "--verbose", "--json" @@ -46,19 +46,18 @@ def outdated_args def outdated outdated_args.parse + formula_only = args.formula? if args.json raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json # When user asks for json, print json for formula only unless the user explicitly asks for casks - formula_only = !args.cask_only? - else - formula_only = args.formula_only? + formula_only = !args.cask? end if formula_only formulae = args.resolved_formulae.blank? ? Formula.installed : args.resolved_formulae outdated = print_outdated_formulae(formulae) - elsif args.cask_only? + elsif args.cask? casks = args.named.blank? ? Cask::Caskroom.casks : args.named outdated = print_outdated_casks(casks) else From 299db98dbf8eda204bf552455aab1d4ef3061dd0 Mon Sep 17 00:00:00 2001 From: William Ma Date: Thu, 9 Jul 2020 08:55:42 -0400 Subject: [PATCH 08/16] Fix PR issues --- Library/Homebrew/cmd/outdated.rb | 18 ++++++++++-------- Library/Homebrew/cmd/upgrade.rb | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 795cbebc53e75..aab9b93913bad 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -26,7 +26,7 @@ def outdated_args "value for is `v1`. See the docs for examples of using the JSON "\ "output: . "\ "By default, this option treats all arguments as formulae. "\ - "To treat arguments as casks, use the --cask-only option." + "To treat arguments as casks, use the --cask option." switch "--fetch-HEAD", description: "Fetch the upstream repository to detect if the HEAD installation of the "\ "formula is outdated. Otherwise, the repository's HEAD will only be checked for "\ @@ -39,7 +39,7 @@ def outdated_args description: "Treat all arguments as casks" switch :debug conflicts "--quiet", "--verbose", "--json" - conflicts "--formula-only", "--cask-only" + conflicts "--formula", "--cask" end end @@ -54,12 +54,14 @@ def outdated formula_only = !args.cask? end - if formula_only - formulae = args.resolved_formulae.blank? ? Formula.installed : args.resolved_formulae - outdated = print_outdated_formulae(formulae) + outdated = if formula_only + formulae = args.resolved_formulae.presence || Formula.installed + + print_outdated_formulae(formulae) elsif args.cask? - casks = args.named.blank? ? Cask::Caskroom.casks : args.named - outdated = print_outdated_casks(casks) + casks = args.named.presence || Cask::Caskroom.casks.presence + + print_outdated_casks(casks) else formulae, casks = args.resolved_formulae_casks @@ -68,7 +70,7 @@ def outdated casks = Cask::Caskroom.casks end - outdated = print_outdated_formulae(formulae) + print_outdated_casks(casks) + print_outdated_formulae(formulae) + print_outdated_casks(casks) end Homebrew.failed = args.named.present? && outdated.present? diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index 6d4730759e6d0..5a2ae625bfb7b 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -94,7 +94,7 @@ def upgrade_outdated_formulae(formulae) end end - return if outdated.empty? + return if outdated.blank? pinned = outdated.select(&:pinned?) outdated -= pinned From 1efb55d953f9ff07026b7d960851a4a7ffc3df26 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Thu, 16 Jul 2020 17:34:46 +1000 Subject: [PATCH 09/16] docs: update manpages --- docs/Manpage.md | 10 +++++++++- manpages/brew.1 | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docs/Manpage.md b/docs/Manpage.md index fffec405be15b..5a2b8f7919136 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -356,9 +356,15 @@ otherwise. * `-v`, `--verbose`: Include detailed version information. * `--json`: - Print output in JSON format. Currently the default and only accepted value for *`version`* is `v1`. See the docs for examples of using the JSON output: + Print output in JSON format. Currently the default and only accepted value for *`version`* is `v1`. See the docs for examples of using the JSON output: . By default, this option treats all arguments as formulae. To treat arguments as casks, use the --cask option. * `--fetch-HEAD`: Fetch the upstream repository to detect if the HEAD installation of the formula is outdated. Otherwise, the repository's HEAD will only be checked for updates when a new stable or development version has been released. +* `--greedy`: + Print outdated casks with `auto_updates` or `version :latest` +* `--formula`: + Treat all arguments as formulae +* `--cask`: + Treat all arguments as casks ### `pin` *`formula`* @@ -556,6 +562,8 @@ the upgraded formulae or, every 30 days, for all formulae. Print install times for each formula at the end of the run. * `-n`, `--dry-run`: Show what would be upgraded, but do not actually upgrade anything. +* `--greedy`: + Upgrade casks with `auto_updates` or `version :latest` ### `uses` [*`options`*] *`formula`* diff --git a/manpages/brew.1 b/manpages/brew.1 index b2805d9c8468e..012d48c9961a3 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -477,12 +477,24 @@ Include detailed version information\. . .TP \fB\-\-json\fR -Print output in JSON format\. Currently the default and only accepted value for \fIversion\fR is \fBv1\fR\. See the docs for examples of using the JSON output: \fIhttps://docs\.brew\.sh/Querying\-Brew\fR +Print output in JSON format\. Currently the default and only accepted value for \fIversion\fR is \fBv1\fR\. See the docs for examples of using the JSON output: \fIhttps://docs\.brew\.sh/Querying\-Brew\fR\. By default, this option treats all arguments as formulae\. To treat arguments as casks, use the \-\-cask option\. . .TP \fB\-\-fetch\-HEAD\fR Fetch the upstream repository to detect if the HEAD installation of the formula is outdated\. Otherwise, the repository\'s HEAD will only be checked for updates when a new stable or development version has been released\. . +.TP +\fB\-\-greedy\fR +Print outdated casks with \fBauto_updates\fR or \fBversion :latest\fR +. +.TP +\fB\-\-formula\fR +Treat all arguments as formulae +. +.TP +\fB\-\-cask\fR +Treat all arguments as casks +. .SS "\fBpin\fR \fIformula\fR" Pin the specified \fIformula\fR, preventing them from being upgraded when issuing the \fBbrew upgrade\fR \fIformula\fR command\. See also \fBunpin\fR\. . @@ -722,6 +734,10 @@ Print install times for each formula at the end of the run\. \fB\-n\fR, \fB\-\-dry\-run\fR Show what would be upgraded, but do not actually upgrade anything\. . +.TP +\fB\-\-greedy\fR +Upgrade casks with \fBauto_updates\fR or \fBversion :latest\fR +. .SS "\fBuses\fR [\fIoptions\fR] \fIformula\fR" Show formulae that specify \fIformula\fR as a dependency\. When given multiple formula arguments, show the intersection of formulae that use \fIformula\fR\. By default, \fBuses\fR shows all formulae that specify \fIformula\fR as a required or recommended dependency for their stable builds\. . From 8c5140f6e6dddcd979ad7c388252199796e4917a Mon Sep 17 00:00:00 2001 From: William Ma Date: Fri, 17 Jul 2020 08:53:19 -0400 Subject: [PATCH 10/16] outdated: Implement json v2 --- Library/Homebrew/cmd/outdated.rb | 139 ++++++++++++++++++++----------- 1 file changed, 90 insertions(+), 49 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index aab9b93913bad..c64d81561b0c9 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -46,47 +46,55 @@ def outdated_args def outdated outdated_args.parse - formula_only = args.formula? - if args.json - raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json + case json_version + when :v1 + outdated = if args.formula? || !args.cask? + outdated_formulae + else + outdated_casks + end - # When user asks for json, print json for formula only unless the user explicitly asks for casks - formula_only = !args.cask? - end + puts JSON.generate(json_info(outdated)) + + when :v2 + formulae, casks = if args.formula? + [outdated_formulae, []] + elsif args.cask? + [[], outdated_casks] + else + outdated_formulae_casks + end - outdated = if formula_only - formulae = args.resolved_formulae.presence || Formula.installed + puts JSON.generate({ + "formulae" => json_info(formulae), + "casks" => json_info(casks), + }) - print_outdated_formulae(formulae) - elsif args.cask? - casks = args.named.presence || Cask::Caskroom.casks.presence + outdated = formulae + casks - print_outdated_casks(casks) else - formulae, casks = args.resolved_formulae_casks - - if formulae.blank? && casks.blank? - formulae = Formula.installed - casks = Cask::Caskroom.casks + outdated = if args.formula? + outdated_formulae + elsif args.cask? + outdated_casks + else + outdated_formulae_casks.flatten end - print_outdated_formulae(formulae) + print_outdated_casks(casks) + print_outdated(outdated) end Homebrew.failed = args.named.present? && outdated.present? end - def print_outdated_formulae(formulae) - return print_outdated_formulae_json(formulae) if args.json + def print_outdated(formula_or_cask) + return formula_or_cask.each { |f_or_c| print_outdated(f_or_c) } if formula_or_cask.is_a? Array - fetch_head = args.fetch_HEAD? + if formula_or_cask.is_a?(Formula) + f = formula_or_cask - outdated_formulae = formulae.select { |f| f.outdated?(fetch_head: fetch_head) } - .sort - - outdated_formulae.each do |f| if verbose? - outdated_kegs = f.outdated_kegs(fetch_head: fetch_head) + outdated_kegs = f.outdated_kegs(fetch_head: args.fetch_HEAD?) current_version = if f.alias_changed? latest = f.latest_formula @@ -111,46 +119,79 @@ def print_outdated_formulae(formulae) else puts f.full_installed_specified_name end + else + c = formula_or_cask + + puts c.outdated_info(args.greedy?, verbose?, false) end end - def print_outdated_formulae_json(formulae) - json = [] - fetch_head = args.fetch_HEAD? - outdated_formulae = formulae.select { |f| f.outdated?(fetch_head: fetch_head) } + def json_info(formula_or_cask) + return formula_or_cask.map { |f_or_c| json_info(f_or_c) } if formula_or_cask.is_a? Array + + if formula_or_cask.is_a?(Formula) + f = formula_or_cask - outdated = outdated_formulae.each do |f| - outdated_versions = f.outdated_kegs(fetch_head: fetch_head).map(&:version) + outdated_versions = f.outdated_kegs(fetch_head: args.fetch_HEAD?).map(&:version) current_version = if f.head? && outdated_versions.any? { |v| v.to_s == f.pkg_version.to_s } "HEAD" else f.pkg_version.to_s end - json << { name: f.full_name, - installed_versions: outdated_versions.map(&:to_s), - current_version: current_version, - pinned: f.pinned?, - pinned_version: f.pinned_version } + { name: f.full_name, + installed_versions: outdated_versions.map(&:to_s), + current_version: current_version, + pinned: f.pinned?, + pinned_version: f.pinned_version } + else + c = formula_or_cask + + c.outdated_info(args.greedy?, verbose?, true) end - puts JSON.generate(json) + end - outdated + def verbose? + ($stdout.tty? || args.verbose?) && !args.quiet? end - def print_outdated_casks(casks) - outdated = casks.map { |cask| Cask::CaskLoader.load(cask) }.select do |cask| - odebug "Checking update info of Cask #{cask}" - cask.outdated?(args.greedy?) - end + def json_version + version_hash = { + nil => nil, + true => :v1, + "v1" => :v1, + "v2" => :v2, + } - output = outdated.map { |cask| cask.outdated_info(args.greedy?, verbose?, args.json) } - puts args.json ? JSON.generate(output) : output + raise UsageError, "invalid JSON version: #{args.json}" unless version_hash.include? args.json - outdated + version_hash[args.json] end - def verbose? - ($stdout.tty? || args.verbose?) && !args.quiet? + def outdated_formulae + select_outdated((args.resolved_formulae.presence || Formula.installed)).sort + end + + def outdated_casks + select_outdated( + args.named.present? ? args.named.uniq.map { |ref| Cask::CaskLoader.load ref } : Cask::Caskroom.casks, + ) + end + + def outdated_formulae_casks + formulae, casks = args.resolved_formulae_casks + + if formulae.blank? && casks.blank? + formulae = Formula.installed + casks = Cask::Caskroom.casks + end + + [select_outdated(formulae), select_outdated(casks)] + end + + def select_outdated(formulae_or_casks) + formulae_or_casks.select do |fc| + fc.is_a?(Formula) ? fc.outdated?(fetch_head: args.fetch_HEAD?) : fc.outdated?(args.greedy?) + end end end From 03857319c97d596630c8b27a3b6868a9b1029d83 Mon Sep 17 00:00:00 2001 From: William Ma Date: Fri, 17 Jul 2020 10:28:09 -0400 Subject: [PATCH 11/16] outdated: Update documentation about json versions --- Library/Homebrew/cmd/outdated.rb | 10 +++++----- docs/Manpage.md | 2 +- manpages/brew.1 | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index c64d81561b0c9..3c6290d635c12 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -22,11 +22,9 @@ def outdated_args switch :verbose, description: "Include detailed version information." flag "--json", - description: "Print output in JSON format. Currently the default and only accepted "\ - "value for is `v1`. See the docs for examples of using the JSON "\ - "output: . "\ - "By default, this option treats all arguments as formulae. "\ - "To treat arguments as casks, use the --cask option." + description: "Print output in JSON format. There are two versions: v1 and v2. " \ + "v1 is deprecated and is currently the default if no version is specified. " \ + "v2 prints outdated formulae and casks. " switch "--fetch-HEAD", description: "Fetch the upstream repository to detect if the HEAD installation of the "\ "formula is outdated. Otherwise, the repository's HEAD will only be checked for "\ @@ -48,6 +46,8 @@ def outdated case json_version when :v1 + opoo "JSON v1 has been deprecated. Please use --json=v2" + outdated = if args.formula? || !args.cask? outdated_formulae else diff --git a/docs/Manpage.md b/docs/Manpage.md index 5a2b8f7919136..d2d2262a917a6 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -356,7 +356,7 @@ otherwise. * `-v`, `--verbose`: Include detailed version information. * `--json`: - Print output in JSON format. Currently the default and only accepted value for *`version`* is `v1`. See the docs for examples of using the JSON output: . By default, this option treats all arguments as formulae. To treat arguments as casks, use the --cask option. + Print output in JSON format. There are two versions: v1 and v2. v1 is deprecated and is currently the default if no version is specified. v2 prints outdated formulae and casks. * `--fetch-HEAD`: Fetch the upstream repository to detect if the HEAD installation of the formula is outdated. Otherwise, the repository's HEAD will only be checked for updates when a new stable or development version has been released. * `--greedy`: diff --git a/manpages/brew.1 b/manpages/brew.1 index 012d48c9961a3..5ad417cc91d95 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -477,7 +477,7 @@ Include detailed version information\. . .TP \fB\-\-json\fR -Print output in JSON format\. Currently the default and only accepted value for \fIversion\fR is \fBv1\fR\. See the docs for examples of using the JSON output: \fIhttps://docs\.brew\.sh/Querying\-Brew\fR\. By default, this option treats all arguments as formulae\. To treat arguments as casks, use the \-\-cask option\. +Print output in JSON format\. There are two versions: v1 and v2\. v1 is deprecated and is currently the default if no version is specified\. v2 prints outdated formulae and casks\. . .TP \fB\-\-fetch\-HEAD\fR From b4f8e23ee1050d3794974fbe93861c5fa4b421a0 Mon Sep 17 00:00:00 2001 From: William Ma <1882991+whoiswillma@users.noreply.github.com> Date: Mon, 20 Jul 2020 14:34:35 -0400 Subject: [PATCH 12/16] outdated: Fix documentation style Co-authored-by: Eric Knibbe --- Library/Homebrew/cmd/outdated.rb | 8 ++++---- docs/Manpage.md | 6 +++--- manpages/brew.1 | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 3c6290d635c12..3d499f7d69bb4 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -30,11 +30,11 @@ def outdated_args "formula is outdated. Otherwise, the repository's HEAD will only be checked for "\ "updates when a new stable or development version has been released." switch "--greedy", - description: "Print outdated casks with `auto_updates` or `version :latest`" + description: "Print outdated casks with `auto_updates` or `version :latest`." switch "--formula", - description: "Treat all arguments as formulae" + description: "Treat all arguments as formulae." switch "--cask", - description: "Treat all arguments as casks" + description: "Treat all arguments as casks." switch :debug conflicts "--quiet", "--verbose", "--json" conflicts "--formula", "--cask" @@ -46,7 +46,7 @@ def outdated case json_version when :v1 - opoo "JSON v1 has been deprecated. Please use --json=v2" + opoo "JSON v1 has been deprecated; please use --json=v2" outdated = if args.formula? || !args.cask? outdated_formulae diff --git a/docs/Manpage.md b/docs/Manpage.md index d2d2262a917a6..32e270a75ec50 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -360,11 +360,11 @@ otherwise. * `--fetch-HEAD`: Fetch the upstream repository to detect if the HEAD installation of the formula is outdated. Otherwise, the repository's HEAD will only be checked for updates when a new stable or development version has been released. * `--greedy`: - Print outdated casks with `auto_updates` or `version :latest` + Print outdated casks with `auto_updates` or `version :latest`. * `--formula`: - Treat all arguments as formulae + Treat all arguments as formulae. * `--cask`: - Treat all arguments as casks + Treat all arguments as casks. ### `pin` *`formula`* diff --git a/manpages/brew.1 b/manpages/brew.1 index 5ad417cc91d95..7d57c2c11b438 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -485,15 +485,15 @@ Fetch the upstream repository to detect if the HEAD installation of the formula . .TP \fB\-\-greedy\fR -Print outdated casks with \fBauto_updates\fR or \fBversion :latest\fR +Print outdated casks with \fBauto_updates\fR or \fBversion :latest\fR\. . .TP \fB\-\-formula\fR -Treat all arguments as formulae +Treat all arguments as formulae\. . .TP \fB\-\-cask\fR -Treat all arguments as casks +Treat all arguments as casks\. . .SS "\fBpin\fR \fIformula\fR" Pin the specified \fIformula\fR, preventing them from being upgraded when issuing the \fBbrew upgrade\fR \fIformula\fR command\. See also \fBunpin\fR\. From fa0f6f4d5b562bba433881cbfab6353d279c90f2 Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 20 Jul 2020 16:16:18 -0400 Subject: [PATCH 13/16] outdated: Update tests for deprecation warning --- Library/Homebrew/test/cmd/outdated_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Library/Homebrew/test/cmd/outdated_spec.rb b/Library/Homebrew/test/cmd/outdated_spec.rb index 54a65932851c5..bb594a1aae1a4 100644 --- a/Library/Homebrew/test/cmd/outdated_spec.rb +++ b/Library/Homebrew/test/cmd/outdated_spec.rb @@ -23,7 +23,6 @@ expect { brew "outdated", "--json=v1" } .to output(expected_json + "\n").to_stdout - .and not_to_output.to_stderr .and be_a_success end end From 8d4a443e4dcded9940ccfe2ee3d5a3c471e6c235 Mon Sep 17 00:00:00 2001 From: William Ma Date: Wed, 22 Jul 2020 22:06:48 -0400 Subject: [PATCH 14/16] outdated: Fix most code review comments --- Library/Homebrew/cmd/outdated.rb | 118 ++++++++++++++++--------------- 1 file changed, 62 insertions(+), 56 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 3d499f7d69bb4..668073d3f1e2b 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -46,7 +46,7 @@ def outdated case json_version when :v1 - opoo "JSON v1 has been deprecated; please use --json=v2" + odeprecated "brew outdated --json=v1", "brew outdated --json=v2" outdated = if args.formula? || !args.cask? outdated_formulae @@ -87,67 +87,67 @@ def outdated Homebrew.failed = args.named.present? && outdated.present? end - def print_outdated(formula_or_cask) - return formula_or_cask.each { |f_or_c| print_outdated(f_or_c) } if formula_or_cask.is_a? Array - - if formula_or_cask.is_a?(Formula) - f = formula_or_cask - - if verbose? - outdated_kegs = f.outdated_kegs(fetch_head: args.fetch_HEAD?) - - current_version = if f.alias_changed? - latest = f.latest_formula - "#{latest.name} (#{latest.pkg_version})" - elsif f.head? && outdated_kegs.any? { |k| k.version.to_s == f.pkg_version.to_s } - # There is a newer HEAD but the version number has not changed. - "latest HEAD" + def print_outdated(formulae_or_casks) + formulae_or_casks.each do |formula_or_cask| + if formula_or_cask.is_a?(Formula) + f = formula_or_cask + + if verbose? + outdated_kegs = f.outdated_kegs(fetch_head: args.fetch_HEAD?) + + current_version = if f.alias_changed? + latest = f.latest_formula + "#{latest.name} (#{latest.pkg_version})" + elsif f.head? && outdated_kegs.any? { |k| k.version.to_s == f.pkg_version.to_s } + # There is a newer HEAD but the version number has not changed. + "latest HEAD" + else + f.pkg_version.to_s + end + + outdated_versions = outdated_kegs + .group_by { |keg| Formulary.from_keg(keg).full_name } + .sort_by { |full_name, _kegs| full_name } + .map do |full_name, kegs| + "#{full_name} (#{kegs.map(&:version).join(", ")})" + end.join(", ") + + pinned_version = " [pinned at #{f.pinned_version}]" if f.pinned? + + puts "#{outdated_versions} < #{current_version}#{pinned_version}" else - f.pkg_version.to_s + puts f.full_installed_specified_name end - - outdated_versions = outdated_kegs - .group_by { |keg| Formulary.from_keg(keg).full_name } - .sort_by { |full_name, _kegs| full_name } - .map do |full_name, kegs| - "#{full_name} (#{kegs.map(&:version).join(", ")})" - end.join(", ") - - pinned_version = " [pinned at #{f.pinned_version}]" if f.pinned? - - puts "#{outdated_versions} < #{current_version}#{pinned_version}" else - puts f.full_installed_specified_name - end - else - c = formula_or_cask + c = formula_or_cask - puts c.outdated_info(args.greedy?, verbose?, false) + puts c.outdated_info(args.greedy?, verbose?, false) + end end end - def json_info(formula_or_cask) - return formula_or_cask.map { |f_or_c| json_info(f_or_c) } if formula_or_cask.is_a? Array + def json_info(formulae_or_casks) + formulae_or_casks.map do |formula_or_cask| + if formula_or_cask.is_a?(Formula) + f = formula_or_cask - if formula_or_cask.is_a?(Formula) - f = formula_or_cask + outdated_versions = f.outdated_kegs(fetch_head: args.fetch_HEAD?).map(&:version) + current_version = if f.head? && outdated_versions.any? { |v| v.to_s == f.pkg_version.to_s } + "HEAD" + else + f.pkg_version.to_s + end - outdated_versions = f.outdated_kegs(fetch_head: args.fetch_HEAD?).map(&:version) - current_version = if f.head? && outdated_versions.any? { |v| v.to_s == f.pkg_version.to_s } - "HEAD" + { name: f.full_name, + installed_versions: outdated_versions.map(&:to_s), + current_version: current_version, + pinned: f.pinned?, + pinned_version: f.pinned_version } else - f.pkg_version.to_s - end - - { name: f.full_name, - installed_versions: outdated_versions.map(&:to_s), - current_version: current_version, - pinned: f.pinned?, - pinned_version: f.pinned_version } - else - c = formula_or_cask + c = formula_or_cask - c.outdated_info(args.greedy?, verbose?, true) + c.outdated_info(args.greedy?, verbose?, true) + end end end @@ -173,9 +173,11 @@ def outdated_formulae end def outdated_casks - select_outdated( - args.named.present? ? args.named.uniq.map { |ref| Cask::CaskLoader.load ref } : Cask::Caskroom.casks, - ) + if args.named.present? + select_outdated(args.named.uniq.map(&Cask::CaskLoader.method(:load))) + else + select_outdated(Cask::Caskroom.casks) + end end def outdated_formulae_casks @@ -190,8 +192,12 @@ def outdated_formulae_casks end def select_outdated(formulae_or_casks) - formulae_or_casks.select do |fc| - fc.is_a?(Formula) ? fc.outdated?(fetch_head: args.fetch_HEAD?) : fc.outdated?(args.greedy?) + formulae_or_casks.select do |formula_or_cask| + if formula_or_cask.is_a?(Formula) + formula_or_cask.outdated?(fetch_head: args.fetch_HEAD?) + else + formula_or_cask.outdated?(args.greedy?) + end end end end From f8708ae80c38bff2b5c2ca76d509abe0cc6f3df6 Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 27 Jul 2020 09:12:30 -0400 Subject: [PATCH 15/16] Add todo for --json=v1 deprecation --- Library/Homebrew/cmd/outdated.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 668073d3f1e2b..52ea1a2ee3a98 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -46,7 +46,8 @@ def outdated case json_version when :v1 - odeprecated "brew outdated --json=v1", "brew outdated --json=v2" + # TODO: enable for next major/minor release + # odeprecated "brew outdated --json=v1", "brew outdated --json=v2" outdated = if args.formula? || !args.cask? outdated_formulae From fa60d99265a8be784f2f9d9c4ce4ea20df139b5c Mon Sep 17 00:00:00 2001 From: William Ma Date: Mon, 27 Jul 2020 09:34:52 -0400 Subject: [PATCH 16/16] outdated: vary deprecated message depending on flag --- Library/Homebrew/cmd/outdated.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 52ea1a2ee3a98..7c5f078716cf0 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -45,9 +45,9 @@ def outdated outdated_args.parse case json_version - when :v1 + when :v1, :default # TODO: enable for next major/minor release - # odeprecated "brew outdated --json=v1", "brew outdated --json=v2" + # odeprecated "brew outdated --json#{json_version == :v1 ? "=v1" : ""}", "brew outdated --json=v2" outdated = if args.formula? || !args.cask? outdated_formulae @@ -66,10 +66,11 @@ def outdated outdated_formulae_casks end - puts JSON.generate({ - "formulae" => json_info(formulae), - "casks" => json_info(casks), - }) + json = { + "formulae" => json_info(formulae), + "casks" => json_info(casks), + } + puts JSON.generate(json) outdated = formulae + casks @@ -106,10 +107,9 @@ def print_outdated(formulae_or_casks) f.pkg_version.to_s end - outdated_versions = outdated_kegs - .group_by { |keg| Formulary.from_keg(keg).full_name } - .sort_by { |full_name, _kegs| full_name } - .map do |full_name, kegs| + outdated_versions = outdated_kegs.group_by { |keg| Formulary.from_keg(keg).full_name } + .sort_by { |full_name, _kegs| full_name } + .map do |full_name, kegs| "#{full_name} (#{kegs.map(&:version).join(", ")})" end.join(", ") @@ -159,7 +159,7 @@ def verbose? def json_version version_hash = { nil => nil, - true => :v1, + true => :default, "v1" => :v1, "v2" => :v2, }