如何让 Brew 在安装之前显示公式的大小?

How to make Brew show the size of the formula before installing it?

我正在寻找一种无需安装即可使 brew 显示公式大小的方法。我尝试了 brew infobrew install 的各种选项,但都没有让我查看公式的大小。

但是,在安装公式时,其大小显示为输出的一部分。

有什么想法吗?

nlykkei-mbp:Projects nlykkei$ brew info llvm
llvm: stable 8.0.1 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/llvm.rb
==> Dependencies
Build: cmake ✔
Required: libffi ✔, swig ✔
==> Requirements
Build: xcode ✔
==> Options
--HEAD
        Install HEAD version
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
  LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"

llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

==> Analytics
install: 15,199 (30 days), 39,871 (90 days), 183,880 (365 days)
install_on_request: 11,125 (30 days), 30,095 (90 days), 140,885 (365 days)
build_error: 0 (30 days)

这里有两个问题:

  1. 如果配方没有瓶子(= pre-built 存档),你无法在不安装的情况下知道它的大小。
  2. 如果一个配方有一个瓶子,你需要下载它才能知道它的压缩大小

我跳过第一个问题,因为它没有解决你的问题。关于第二个,我们可以使用 Homebrew 的 JSON API 和 jq:

得到瓶子的 URL
$ brew info --json=v1 llvm | jq --raw-output '.[0].bottle.stable.files.big_sur.url'
https://ghcr.io/v2/homebrew/core/llvm/blobs/sha256:ff9a71b7b35ecb6c1dfcfe40152b00f4777a3f4a10dcf5cc41044458b02c99cd

注意:因为这个 URL 很长,我在下面的命令中将其截断以便于阅读。

我们可以curl这个URL知道压缩后的大小。请注意,自 2021 年 4 月起,ghcr.io 需要身份验证,但 using a dummy token works。我们使用 -I 执行 HEAD 请求并获得响应 headers 而没有其 body (-L = 跟随重定向;-s 不显示进度)。

$ curl -Ls -I -H 'Authorization: Bearer QQ==' https://ghcr.io/v2/homebrew/... | grep -i content-length
content-length: 474762229

此公式的 gzip-compressed 大小为 474,762,229 字节,即 ~474MiB。

gzip 压缩包不包含它们的最终大小;我们必须解压缩它们才能知道。

我们可以通过 gunzip 流式传输存档并获取其大小,而无需在磁盘上存储任何内容:

$ curl -Ls -H 'Authorization: Bearer QQ==' https://ghcr.io/v2/homebrew/... | gunzip - | wc -c
1695293440

未压缩的 llvm 公式权重为 1,695,293,440 字节,即 ~1.7GiB。

不幸的是,这可能需要一些时间,具体取决于您的连接和瓶子的大小。

One-liner:

brew info --json=v1 llvm | jq -r '.[0].bottle.stable.files.big_sur.url' | xargs curl -Ls -H 'Authorization: Bearer QQ==' | gunzip - | wc -c