Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Homebrew's local definitions are the ones it reads now

Țugui Dragoș498c36b122d4
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
index bd9139f1..a01adc3c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
@@ -342,26 +342,34 @@ public enum Homebrew {
/// Returns whether Homebrew's local copy of its definitions is on disk. Without it, asking about a cask
/// makes Homebrew download the copy, even with `HOMEBREW_NO_AUTO_UPDATE` set. So Peel looks for the copy
- /// itself, and asks nothing about casks when it is missing.
+ /// itself, and asks nothing about casks when it is missing. Where Homebrew keeps it, the platform it is named
+ /// for, and whether Homebrew reads its taps instead are asked of Homebrew itself, which reads its own
+ /// settings, `brew.env` included (`api/internal.rb`, `env_config.rb` in 7.0).
@concurrent
public static func hasLocalDefinitions() async -> Bool {
guard executableURL != nil else { return false }
- guard let cache = try? await answer(["--cache"]).trimmingCharacters(in: .whitespacesAndNewlines),
- !cache.isEmpty
- else { return false }
-
- return hasLocalDefinitions(inCache: URL(filePath: cache, directoryHint: .isDirectory))
+ let question = "puts HOMEBREW_CACHE; puts Utils::Bottles.tag; puts(Homebrew::EnvConfig.no_install_from_api? "
+ + "&& CoreTap.instance.installed? && CoreCaskTap.instance.installed?)"
+ guard let answer = try? await answer(["ruby", "-e", question]) else { return false }
+ let lines = answer.split(whereSeparator: \.isNewline).map(String.init)
+ guard lines.count == 3, !lines[0].isEmpty, !lines[1].isEmpty else { return false }
+ let cache = URL(filePath: lines[0], directoryHint: .isDirectory)
+ return hasLocalDefinitions(inCache: cache, tag: lines[1], readsTheTaps: lines[2] == "true")
}
/// Returns whether `cache` holds that copy. In Homebrew 6.0.0 and later, the copy is one file named for the
- /// platform it was built for, like `api/internal/packages.arm64_tahoe.jws.json`. Earlier versions keep one
- /// file for formulae and one for casks (`cmd/update.sh` in 5.1.9), and both must be there: a query of
- /// either kind downloads its file when it is missing.
- static func hasLocalDefinitions(inCache cache: URL) -> Bool {
+ /// platform Homebrew reads it for (`tag`), like `api/internal/packages.arm64_tahoe.jws.json`: a file left for
+ /// an older macOS is no copy, since Homebrew downloads the one it wants. Earlier versions keep one file for
+ /// formulae and one for casks (`cmd/update.sh` in 5.1.9), and both must be there: a query of either kind
+ /// downloads its file when it is missing. A Homebrew that reads its taps needs neither.
+ static func hasLocalDefinitions(inCache cache: URL, tag: String, readsTheTaps: Bool) -> Bool {
+ guard !readsTheTaps else { return true }
let api = cache.appending(path: "api", directoryHint: .isDirectory)
- let packages = (try? FileManager.default.contentsOfDirectory(atPath: api.appending(path: "internal").path(percentEncoded: false))) ?? []
- if packages.contains(where: { $0.hasPrefix("packages.") && $0.hasSuffix(".jws.json") }) { return true }
- return ["formula.jws.json", "cask.jws.json"].allSatisfy { FileManager.default.fileExists(atPath: api.appending(path: $0).path(percentEncoded: false)) }
+ let packages = api.appending(path: "internal/packages.\(tag).jws.json")
+ if FileManager.default.fileExists(atPath: packages.path(percentEncoded: false)) { return true }
+ return ["formula.jws.json", "cask.jws.json"].allSatisfy {
+ FileManager.default.fileExists(atPath: api.appending(path: $0).path(percentEncoded: false))
+ }
}
@concurrent
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
index 9319bd74..78b2941b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
@@ -489,21 +489,38 @@ struct HomebrewTests {
#expect(report.skipped.isEmpty)
}
- /// From 6.0.0 Homebrew keeps its copy of the definitions in one file. Earlier versions keep one file for
- /// formulae and one for casks. Either layout counts as a local copy.
+ /// From 6.0.0 Homebrew keeps its copy of the definitions in one file, named for the platform it reads it for.
+ /// Earlier versions keep one file for formulae and one for casks. Either layout counts as a local copy.
@Test func findsHomebrewsCopyOfItsDefinitionsInEitherLayout() throws {
let newer = try TemporaryDirectory()
try newer.file("api/internal/packages.arm64_tahoe.jws.json")
- #expect(Homebrew.hasLocalDefinitions(inCache: newer.url))
+ #expect(Homebrew.hasLocalDefinitions(inCache: newer.url, tag: "arm64_tahoe", readsTheTaps: false))
let older = try TemporaryDirectory()
try older.file("api/formula.jws.json")
- #expect(!Homebrew.hasLocalDefinitions(inCache: older.url), "a cask query would still fetch the cask file")
+ #expect(!Homebrew.hasLocalDefinitions(inCache: older.url, tag: "arm64_tahoe", readsTheTaps: false))
try older.file("api/cask.jws.json")
- #expect(Homebrew.hasLocalDefinitions(inCache: older.url))
+ #expect(Homebrew.hasLocalDefinitions(inCache: older.url, tag: "arm64_tahoe", readsTheTaps: false))
let empty = try TemporaryDirectory()
try empty.directory("api/internal")
- #expect(!Homebrew.hasLocalDefinitions(inCache: empty.url))
+ #expect(!Homebrew.hasLocalDefinitions(inCache: empty.url, tag: "arm64_tahoe", readsTheTaps: false))
+ }
+
+ /// After a move to a newer macOS, only the file for the old platform is left. Homebrew reads the new one and
+ /// downloads it when missing, so the old one is no local copy.
+ @Test func aCopyForAnotherPlatformIsNoLocalCopy() throws {
+ let cache = try TemporaryDirectory()
+ try cache.file("api/internal/packages.arm64_tahoe.jws.json")
+
+ #expect(!Homebrew.hasLocalDefinitions(inCache: cache.url, tag: "arm64_27", readsTheTaps: false))
+ }
+
+ /// With `HOMEBREW_NO_INSTALL_FROM_API`, Homebrew reads its taps and never the API files, so its taps are the
+ /// local copy.
+ @Test func readingTheTapsNeedsNoAPICopy() throws {
+ let cache = try TemporaryDirectory()
+
+ #expect(Homebrew.hasLocalDefinitions(inCache: cache.url, tag: "arm64_tahoe", readsTheTaps: true))
}
}