Skip to content

Commit

Permalink
cmd/info: show size information
Browse files Browse the repository at this point in the history
  • Loading branch information
cho-m committed Aug 26, 2024
1 parent abc0584 commit 9998f52
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 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.positive?
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.positive?
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
11 changes: 9 additions & 2 deletions Library/Homebrew/downloadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ def downloader
end
end

sig { params(verify_download_integrity: T::Boolean, timeout: T.nilable(T.any(Integer, Float))).returns(Pathname) }
def fetch(verify_download_integrity: true, timeout: nil)
sig {
params(
verify_download_integrity: T::Boolean,
timeout: T.nilable(T.any(Integer, Float)),
quiet: T::Boolean,
).returns(Pathname)
}
def fetch(verify_download_integrity: true, timeout: nil, quiet: false)
cache.mkpath

begin
downloader.quiet! if quiet
downloader.fetch(timeout:)
rescue ErrorDuringExecution, CurlDownloadStrategyError => e
raise DownloadError.new(self, e)
Expand Down
32 changes: 23 additions & 9 deletions Library/Homebrew/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def files(*files)
Partial.new(self, files)
end

def fetch(verify_download_integrity: true)
def fetch(verify_download_integrity: true, quiet: false)
fetch_patches

super
Expand Down Expand Up @@ -286,6 +286,27 @@ def verify_download_integrity(_filename)
end

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L289

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

begin
JSON.parse(tab)

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L293

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L295

Added line #L295 was not covered by tests
end
end

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L300

Added line #L300 was not covered by tests
end

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L304

Added line #L304 was not covered by tests
end

private

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L332

Added line #L332 was not covered by tests
end
end

Expand Down
18 changes: 16 additions & 2 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ def stage
resource.downloader.stage
end

def fetch_tab
def fetch_tab(quiet: false)
return if github_packages_manifest_resource.blank?

github_packages_manifest_resource.fetch
github_packages_manifest_resource.fetch(quiet:)

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L394

Added line #L394 was not covered by tests
rescue DownloadError
raise unless fallback_on_error

Expand All @@ -410,6 +410,20 @@ def tab_attributes
github_packages_manifest_resource.tab
end

sig { returns(Integer) }
def bottle_size
return 0 unless github_packages_manifest_resource&.downloaded?

github_packages_manifest_resource.bottle_size

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L417

Added line #L417 was not covered by tests
end

sig { returns(Integer) }
def installed_size
return 0 unless github_packages_manifest_resource&.downloaded?

github_packages_manifest_resource.installed_size

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L424

Added line #L424 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 9998f52

Please sign in to comment.