From ea0776c42532026071de0fb4087c0c37b6496e8b Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Mon, 26 Aug 2024 16:59:37 -0400 Subject: [PATCH] cmd/info: show size information Co-authored-by: Mike McQuaid Co-authored-by: Markus Reiter --- Library/Homebrew/cmd/info.rb | 15 ++++++++ Library/Homebrew/resource.rb | 35 ++++++++++++++----- Library/Homebrew/software_spec.rb | 16 +++++++++ .../sorbet/rbi/dsl/homebrew/cmd/info.rbi | 3 ++ completions/bash/brew | 2 ++ completions/fish/brew.fish | 2 ++ completions/zsh/_brew | 10 +++--- docs/Manpage.md | 5 +++ manpages/brew.1 | 3 ++ 9 files changed, 79 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index d213dd9c8d4ef..9b0424e65e40f 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -45,6 +45,8 @@ class Info < AbstractCommand switch "--github", description: "Open the GitHub source page for and in a browser. " \ "To view the history locally: `brew log -p` or " + switch "--fetch-manifest", + description: "Fetch GitHub Packages manifest for extra information when is not installed." flag "--json", description: "Print a JSON representation. Currently the default value for is `v1` for " \ ". For and use `v2`. See the docs for examples of using the " \ @@ -69,6 +71,8 @@ class Info < AbstractCommand conflicts "--installed", "--eval-all" conflicts "--installed", "--all" conflicts "--formula", "--cask" + conflicts "--fetch-manifest", "--cask" + conflicts "--fetch-manifest", "--json" named_args [:formula, :cask] end @@ -303,6 +307,17 @@ def info_formula(formula) ] if kegs.empty? puts "Not installed" + if (bottle = formula.bottle) + begin + bottle.fetch_tab(quiet: !args.debug?) if args.fetch_manifest? + bottle_size = bottle.bottle_size + installed_size = bottle.installed_size + puts "Bottle Size: #{disk_usage_readable(bottle_size)}" if bottle_size + puts "Installed Size: #{disk_usage_readable(installed_size)}" if installed_size + rescue RuntimeError => e + odebug e + end + end else puts "Installed" kegs.each do |keg| diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 51cc555231f76..c8c3058cc0d11 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -306,6 +306,7 @@ class Error < RuntimeError; end def initialize(bottle) super("#{bottle.name}_bottle_manifest") @bottle = bottle + @manifest_annotations = nil end def verify_download_integrity(_filename) @@ -314,6 +315,31 @@ def verify_download_integrity(_filename) end def tab + tab = manifest_annotations["sh.brew.tab"] + raise Error, "Couldn't find tab from manifest." if tab.blank? + + begin + JSON.parse(tab) + rescue JSON::ParserError + raise Error, "Couldn't parse tab JSON." + end + end + + sig { returns(T.nilable(Integer)) } + def bottle_size + manifest_annotations["sh.brew.bottle.size"]&.to_i + end + + sig { returns(T.nilable(Integer)) } + def installed_size + manifest_annotations["sh.brew.bottle.installed_size"]&.to_i + end + + private + + def manifest_annotations + return @manifest_annotations unless @manifest_annotations.nil? + json = begin JSON.parse(cached_download.read) rescue JSON::ParserError @@ -336,14 +362,7 @@ def tab end raise Error, "Couldn't find manifest matching bottle checksum." if manifest_annotations.blank? - tab = manifest_annotations["sh.brew.tab"] - raise Error, "Couldn't find tab from manifest." if tab.blank? - - begin - JSON.parse(tab) - rescue JSON::ParserError - raise Error, "Couldn't parse tab JSON." - end + @manifest_annotations = manifest_annotations end end diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 5c4bd6f911c49..91af58f3a5d30 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -436,6 +436,22 @@ def tab_attributes {} end + sig { returns(T.nilable(Integer)) } + def bottle_size + resource = github_packages_manifest_resource + return unless resource&.downloaded? + + resource.bottle_size + end + + sig { returns(T.nilable(Integer)) } + def installed_size + resource = github_packages_manifest_resource + return unless resource&.downloaded? + + resource.installed_size + end + sig { returns(Filename) } def filename Filename.create(resource.owner, @tag, @spec.rebuild) diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi index 21c5bdeeded03..db897357c803c 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi @@ -29,6 +29,9 @@ class Homebrew::Cmd::Info::Args < Homebrew::CLI::Args sig { returns(T::Boolean) } def eval_all?; end + sig { returns(T::Boolean) } + def fetch_manifest?; end + sig { returns(T::Boolean) } def formula?; end diff --git a/completions/bash/brew b/completions/bash/brew index 4433c10b7cebc..1a916500f3468 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -341,6 +341,7 @@ _brew_abv() { --days --debug --eval-all + --fetch-manifest --formula --github --help @@ -1229,6 +1230,7 @@ _brew_info() { --days --debug --eval-all + --fetch-manifest --formula --github --help diff --git a/completions/fish/brew.fish b/completions/fish/brew.fish index b770dd0c80741..7ecabc2e8baa9 100644 --- a/completions/fish/brew.fish +++ b/completions/fish/brew.fish @@ -306,6 +306,7 @@ __fish_brew_complete_arg 'abv' -l category -d 'Which type of analytics data to r __fish_brew_complete_arg 'abv' -l days -d 'How many days of analytics data to retrieve. The value for days must be `30`, `90` or `365`. The default is `30`' __fish_brew_complete_arg 'abv' -l debug -d 'Display any debugging information' __fish_brew_complete_arg 'abv' -l eval-all -d 'Evaluate all available formulae and casks, whether installed or not, to print their JSON. Implied if `HOMEBREW_EVAL_ALL` is set' +__fish_brew_complete_arg 'abv' -l fetch-manifest -d 'Fetch GitHub Packages manifest for extra information when formula is not installed' __fish_brew_complete_arg 'abv' -l formula -d 'Treat all named arguments as formulae' __fish_brew_complete_arg 'abv' -l github -d 'Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask' __fish_brew_complete_arg 'abv' -l help -d 'Show this message' @@ -840,6 +841,7 @@ __fish_brew_complete_arg 'info' -l category -d 'Which type of analytics data to __fish_brew_complete_arg 'info' -l days -d 'How many days of analytics data to retrieve. The value for days must be `30`, `90` or `365`. The default is `30`' __fish_brew_complete_arg 'info' -l debug -d 'Display any debugging information' __fish_brew_complete_arg 'info' -l eval-all -d 'Evaluate all available formulae and casks, whether installed or not, to print their JSON. Implied if `HOMEBREW_EVAL_ALL` is set' +__fish_brew_complete_arg 'info' -l fetch-manifest -d 'Fetch GitHub Packages manifest for extra information when formula is not installed' __fish_brew_complete_arg 'info' -l formula -d 'Treat all named arguments as formulae' __fish_brew_complete_arg 'info' -l github -d 'Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask' __fish_brew_complete_arg 'info' -l help -d 'Show this message' diff --git a/completions/zsh/_brew b/completions/zsh/_brew index 48b262c36e620..00dd2ff84616b 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -414,10 +414,11 @@ _brew_abv() { '--days[How many days of analytics data to retrieve. The value for days must be `30`, `90` or `365`. The default is `30`]' \ '--debug[Display any debugging information]' \ '(--installed)--eval-all[Evaluate all available formulae and casks, whether installed or not, to print their JSON. Implied if `HOMEBREW_EVAL_ALL` is set]' \ + '(--cask --json)--fetch-manifest[Fetch GitHub Packages manifest for extra information when formula is not installed]' \ '--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \ '--help[Show this message]' \ '(--eval-all --all)--installed[Print JSON of formulae that are currently installed]' \ - '--json[Print a JSON representation. Currently the default value for version is `v1` for formula. For formula and cask use `v2`. See the docs for examples of using the JSON output: https://docs.brew.sh/Querying-Brew]' \ + '(--fetch-manifest)--json[Print a JSON representation. Currently the default value for version is `v1` for formula. For formula and cask use `v2`. See the docs for examples of using the JSON output: https://docs.brew.sh/Querying-Brew]' \ '--quiet[Make some output more quiet]' \ '--variations[Include the variations hash in each formula'\''s JSON output]' \ '--verbose[Show more verbose analytics data for formula]' \ @@ -425,7 +426,7 @@ _brew_abv() { '(--cask)--formula[Treat all named arguments as formulae]' \ '*::formula:__brew_formulae' \ - cask \ - '(--formula)--cask[Treat all named arguments as casks]' \ + '(--formula --fetch-manifest)--cask[Treat all named arguments as casks]' \ '*::cask:__brew_casks' } @@ -1065,10 +1066,11 @@ _brew_info() { '--days[How many days of analytics data to retrieve. The value for days must be `30`, `90` or `365`. The default is `30`]' \ '--debug[Display any debugging information]' \ '(--installed)--eval-all[Evaluate all available formulae and casks, whether installed or not, to print their JSON. Implied if `HOMEBREW_EVAL_ALL` is set]' \ + '(--cask --json)--fetch-manifest[Fetch GitHub Packages manifest for extra information when formula is not installed]' \ '--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \ '--help[Show this message]' \ '(--eval-all --all)--installed[Print JSON of formulae that are currently installed]' \ - '--json[Print a JSON representation. Currently the default value for version is `v1` for formula. For formula and cask use `v2`. See the docs for examples of using the JSON output: https://docs.brew.sh/Querying-Brew]' \ + '(--fetch-manifest)--json[Print a JSON representation. Currently the default value for version is `v1` for formula. For formula and cask use `v2`. See the docs for examples of using the JSON output: https://docs.brew.sh/Querying-Brew]' \ '--quiet[Make some output more quiet]' \ '--variations[Include the variations hash in each formula'\''s JSON output]' \ '--verbose[Show more verbose analytics data for formula]' \ @@ -1076,7 +1078,7 @@ _brew_info() { '(--cask)--formula[Treat all named arguments as formulae]' \ '*::formula:__brew_formulae' \ - cask \ - '(--formula)--cask[Treat all named arguments as casks]' \ + '(--formula --fetch-manifest)--cask[Treat all named arguments as casks]' \ '*::cask:__brew_casks' } diff --git a/docs/Manpage.md b/docs/Manpage.md index 5f5d9ef7aa839..f510f84d8545f 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -499,6 +499,11 @@ Display brief statistics for your Homebrew installation. If a *`formula`* or : Open the GitHub source page for *`formula`* and *`cask`* in a browser. To view the history locally: `brew log -p` *`formula`* or *`cask`* +`--fetch-manifest` + +: Fetch GitHub Packages manifest for extra information when *`formula`* is not + installed. + `--json` : Print a JSON representation. Currently the default value for *`version`* is diff --git a/manpages/brew.1 b/manpages/brew.1 index 713e487522ad4..99f75ff08d3cc 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -312,6 +312,9 @@ Which type of analytics data to retrieve\. The value for \fIcategory\fP must be \fB\-\-github\fP Open the GitHub source page for \fIformula\fP and \fIcask\fP in a browser\. To view the history locally: \fBbrew log \-p\fP \fIformula\fP or \fIcask\fP .TP +\fB\-\-fetch\-manifest\fP +Fetch GitHub Packages manifest for extra information when \fIformula\fP is not installed\. +.TP \fB\-\-json\fP Print a JSON representation\. Currently the default value for \fIversion\fP is \fBv1\fP for \fIformula\fP\&\. For \fIformula\fP and \fIcask\fP use \fBv2\fP\&\. See the docs for examples of using the JSON output: .UR https://docs\.brew\.sh/Querying\-Brew