Skip to content

Commit

Permalink
cmd/info: show size information
Browse files Browse the repository at this point in the history
Co-authored-by: Mike McQuaid <[email protected]>
Co-authored-by: Markus Reiter <[email protected]>
  • Loading branch information
3 people committed Sep 24, 2024
1 parent bd3c7f8 commit ad216b6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 10 deletions.
14 changes: 14 additions & 0 deletions Library/Homebrew/cmd/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Info < AbstractCommand
switch "--github",
description: "Open the GitHub source page for <formula> and <cask> in a browser. " \
"To view the history locally: `brew log -p` <formula> or <cask>"
switch "--github-manifest",
description: "Fetch GitHub package manifest for extra information when <formula> is not installed."
flag "--json",
description: "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 " \
Expand All @@ -69,6 +71,7 @@ class Info < AbstractCommand
conflicts "--installed", "--eval-all"
conflicts "--installed", "--all"
conflicts "--formula", "--cask"
conflicts "--github-manifest", "--json"

named_args [:formula, :cask]
end
Expand Down Expand Up @@ -303,6 +306,17 @@ def info_formula(formula)
]
if kegs.empty?
puts "Not installed"
if args.github_manifest? && !(bottle = formula.bottle).nil?
begin
bottle.fetch_tab(quiet: !args.debug?)
bottle_size = bottle.bottle_size
installed_size = bottle.installed_size

Check warning on line 313 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L312-L313

Added lines #L312 - L313 were not covered by tests
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

Check warning on line 317 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L317

Added line #L317 was not covered by tests
end
end
else
puts "Installed"
kegs.each do |keg|
Expand Down
33 changes: 25 additions & 8 deletions Library/Homebrew/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class Error < RuntimeError; end
def initialize(bottle)
super("#{bottle.name}_bottle_manifest")
@bottle = bottle
@manifest_annotations = nil

Check warning on line 309 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L309

Added line #L309 was not covered by tests
end

def verify_download_integrity(_filename)
Expand All @@ -314,6 +315,29 @@ def verify_download_integrity(_filename)
end

def tab
tab = manifest_annotations["sh.brew.tab"]

Check warning on line 318 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L318

Added line #L318 was not covered by tests
raise Error, "Couldn't find tab from manifest." if tab.blank?

begin
JSON.parse(tab)

Check warning on line 322 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L322

Added line #L322 was not covered by tests
rescue JSON::ParserError
raise Error, "Couldn't parse tab JSON."

Check warning on line 324 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L324

Added line #L324 was not covered by tests
end
end

def bottle_size
manifest_annotations["sh.brew.bottle.size"]&.to_i
end

def installed_size
manifest_annotations["sh.brew.bottle.installed_size"]&.to_i
end

private

def manifest_annotations
return @manifest_annotations if @manifest_annotations.present?

json = begin
JSON.parse(cached_download.read)
rescue JSON::ParserError
Expand All @@ -336,14 +360,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

Check warning on line 363 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L363

Added line #L363 was not covered by tests
end
end

Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,20 @@ def tab_attributes
{}
end

sig { returns(T.nilable(Integer)) }
def bottle_size
return unless (resource = github_packages_manifest_resource)&.downloaded?

resource.bottle_size

Check warning on line 443 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L443

Added line #L443 was not covered by tests
end

sig { returns(T.nilable(Integer)) }
def installed_size
return unless (resource = github_packages_manifest_resource)&.downloaded?

resource.installed_size

Check warning on line 450 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L450

Added line #L450 was not covered by tests
end

sig { returns(Filename) }
def filename
Filename.create(resource.owner, @tag, @spec.rebuild)
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions completions/bash/brew
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ _brew_abv() {
--eval-all
--formula
--github
--github-manifest
--help
--installed
--json
Expand Down Expand Up @@ -1231,6 +1232,7 @@ _brew_info() {
--eval-all
--formula
--github
--github-manifest
--help
--installed
--json
Expand Down
2 changes: 2 additions & 0 deletions completions/fish/brew.fish
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ __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 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 github-manifest -d 'Fetch GitHub package manifest for extra information when formula is not installed'
__fish_brew_complete_arg 'abv' -l help -d 'Show this message'
__fish_brew_complete_arg 'abv' -l installed -d 'Print JSON of formulae that are currently installed'
__fish_brew_complete_arg 'abv' -l json -d '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'
Expand Down Expand Up @@ -842,6 +843,7 @@ __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 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 github-manifest -d 'Fetch GitHub package manifest for extra information when formula is not installed'
__fish_brew_complete_arg 'info' -l help -d 'Show this message'
__fish_brew_complete_arg 'info' -l installed -d 'Print JSON of formulae that are currently installed'
__fish_brew_complete_arg 'info' -l json -d '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'
Expand Down
6 changes: 4 additions & 2 deletions completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,10 @@ _brew_abv() {
'--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]' \
'--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \
'(--json)--github-manifest[Fetch GitHub package manifest for extra information when formula is not installed]' \
'--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]' \
'(--github-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]' \
Expand Down Expand Up @@ -1066,9 +1067,10 @@ _brew_info() {
'--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]' \
'--github[Open the GitHub source page for formula and cask in a browser. To view the history locally: `brew log -p` formula or cask]' \
'(--json)--github-manifest[Fetch GitHub package manifest for extra information when formula is not installed]' \
'--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]' \
'(--github-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]' \
Expand Down
5 changes: 5 additions & 0 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`*

`--github-manifest`

: Fetch GitHub package manifest for extra information when *`formula`* is not
installed.

`--json`

: Print a JSON representation. Currently the default value for *`version`* is
Expand Down
3 changes: 3 additions & 0 deletions manpages/brew.1
Original file line number Diff line number Diff line change
Expand Up @@ -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\-\-github\-manifest\fP
Fetch GitHub package 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
Expand Down

0 comments on commit ad216b6

Please sign in to comment.