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 <mike@mikemcquaid.com>
Co-authored-by: Markus Reiter <me@reitermark.us>
  • Loading branch information
3 people committed Sep 24, 2024
1 parent bd3c7f8 commit 2feba60
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
13 changes: 13 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 Down Expand Up @@ -303,6 +305,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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L311

Added line #L311 was not covered by tests
puts "Bottle size: #{disk_usage_readable(bottle_size)}" if 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#L313

Added line #L313 was not covered by tests
puts "Installed size: #{disk_usage_readable(installed_size)}" if installed_size
rescue RuntimeError => e
odebug e

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L316

Added line #L316 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.

0 comments on commit 2feba60

Please sign in to comment.