Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Ask Homebrew for its prefix and Cellar instead of guessing them
Țugui Dragoșf5fe5f1ca567
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swiftindex da71a81d..9c643d11 100644--- a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift@@ -208,6 +208,12 @@ extension [HomebrewPackage] {public struct HomebrewInstallation: Sendable, Hashable {public let version: Stringpublic let prefix: URL+ /// Where Homebrew keeps its formulae, as it says: in its repository when it has one there, else under the prefix.+ public let cellar: URL++ public var caskroom: URL {+ prefix.appending(path: "Caskroom", directoryHint: .isDirectory)+ }private let numbers: [Int]@@ -221,9 +227,10 @@ public struct HomebrewInstallation: Sendable, Hashable {/// `brew bundle` is part of Homebrew since 4.4.25; before, it lived in a tap that asking for it would download.public var writesABrewfile: Bool { isAtLeast(4, 4, 25) }- public init(version: String, prefix: URL) {+ public init(version: String, prefix: URL, cellar: URL? = nil) {self.version = versionself.prefix = prefix+ self.cellar = cellar ?? prefix.appending(path: "Cellar", directoryHint: .isDirectory)numbers = version.prefix { $0.isNumber || $0 == "." }.split(separator: ".")@@ -699,19 +706,34 @@ public enum Homebrew {/// `bin` that `brew` was found in, never one the environment names.@concurrentpublic static func installation() async -> HomebrewInstallation? {- guard let executable = executableURL else { return nil }- guard let output = try? await answer(["--version"]) else { return nil }+ guard executableURL != nil, let version = try? await answer(["--version"]) else { return nil }+ return installation(+ versionAnswer: version,+ prefixAnswer: try? await answer(["--prefix"]),+ cellarAnswer: try? await answer(["--cellar"])+ )+ }++ /// The installation as Homebrew describes it. Its prefix and Cellar are asked, never worked out from where `brew`+ /// is: run from its repository, `bin/brew` moves the prefix to /usr/local when /usr/local/bin/brew leads there, and+ /// the Cellar is in the repository when there is one.+ static func installation(+ versionAnswer: String,+ prefixAnswer: String?,+ cellarAnswer: String?+ ) -> HomebrewInstallation? {// The first line is `Homebrew <version>`; anything under it names a tap.- guard let first = output.split(whereSeparator: \.isNewline).first else { return nil }+ guard let first = versionAnswer.split(whereSeparator: \.isNewline).first else { return nil }let line = first.trimmingCharacters(in: .whitespaces)let label = "Homebrew "let version = line.hasPrefix(label) ? String(line.dropFirst(label.count)) : line- guard !version.isEmpty else { return nil }- // Dropping `bin/brew` leaves a folder URL whose path ends in a slash; the prefix is rebuilt without it.- let folder = executable.deletingLastPathComponent().deletingLastPathComponent()+ guard !version.isEmpty, let prefix = prefixAnswer.flatMap(folder(answering:)),+ let cellar = cellarAnswer.flatMap(folder(answering:))+ else { return nil }return HomebrewInstallation(version: version,- prefix: URL(filePath: folder.path(percentEncoded: false), directoryHint: .notDirectory)+ prefix: URL(filePath: PathPattern.comparablePath(of: prefix), directoryHint: .notDirectory),+ cellar: cellar)}diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swiftindex 7eeb8fa0..438cef02 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift@@ -257,16 +257,19 @@ public struct Exclusions: Sendable, Codable, Hashable {}/// Whether `brew uninstall` would delete something excluded: an app the package installs (`apps` are the- /// installed apps known to be its), an app identifier it quits, or its own folder under `prefix`. Nil when one of- /// these can't be looked into for an excluded app. An unreadable list, or one not read yet, counts as excluding- /// every package, since `brew uninstall` cannot be undone.- public func excludes(_ package: HomebrewPackage, apps: [InstalledApp], prefix: URL?) -> Bool? {+ /// installed apps known to be its), an app identifier it quits, or its own folder in the installation's Caskroom+ /// or Cellar. Nil when one of these can't be looked into for an excluded app. An unreadable list, or one not read+ /// yet, counts as excluding every package, since `brew uninstall` cannot be undone.+ public func excludes(+ _ package: HomebrewPackage,+ apps: [InstalledApp],+ installation: HomebrewInstallation?+ ) -> Bool? {guard isKnown else { return true }if apps.contains(where: { $0.bundleIdentifier.map(excludes(bundleIdentifier:)) == true }) { return true }if package.quitIdentifiers.contains(where: { excludes(bundleIdentifier: $0) }) { return true }- let own = prefix.map {- $0.appending(path: package.kind == .cask ? "Caskroom" : "Cellar", directoryHint: .isDirectory)- .appending(path: package.name, directoryHint: .isDirectory)+ let own = installation.map {+ (package.kind == .cask ? $0.caskroom : $0.cellar).appending(path: package.name, directoryHint: .isDirectory)}let folders = package.appTargets.map { URL(filePath: $0, directoryHint: .isDirectory) } + apps.map(\.url)+ [own].compactMap(\.self)@@ -282,12 +285,11 @@ public struct Exclusions: Sendable, Codable, Hashable {return isKnownToHoldNothing ? false : nil}- /// The installed formulae whose own folder under `prefix` is excluded or holds something excluded, which- /// Homebrew is told to keep: `brew cleanup`, and the autoremove `brew uninstall` runs, delete formulae and their- /// old versions for good. An unreadable list, or one not read yet, keeps every formula. Nil when the Cellar, or a+ /// The installed formulae whose own folder in `cellar` is excluded or holds something excluded, which Homebrew+ /// is told to keep: `brew cleanup`, and the autoremove `brew uninstall` runs, delete formulae and their old+ /// versions for good. An unreadable list, or one not read yet, keeps every formula. Nil when the Cellar, or a/// formula's folder in it, can't be looked into.- public func keptFormulae(inCellarOf prefix: URL) -> [String]? {- let cellar = prefix.appending(path: "Cellar", directoryHint: .isDirectory)+ public func keptFormulae(inCellar cellar: URL) -> [String]? {guard let names = ProtectedData.names(in: cellar.path(percentEncoded: false), followingLinks: true) else {return nil}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swiftindex 5df38519..1f0045b8 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift@@ -87,24 +87,24 @@ struct ExclusionsTests {/// `brew uninstall` deletes for good, so the Homebrew page never runs it on a package that would take/// something the user excluded.@Test func keepsWhatBrewUninstallWouldDeleteForGood() {- let prefix = URL(filePath: "/opt/homebrew", directoryHint: .isDirectory)+ let installation = HomebrewInstallation(version: "7.0.9", prefix: URL(filePath: "/opt/homebrew"))let app = InstalledApp(url: URL(filePath: "/Applications/Example.app"), bundleIdentifier: "com.example.app", name: "Example")let cask = HomebrewPackage(name: "example", kind: .cask, appTargets: ["/Applications/Example.app"], quitIdentifiers: ["com.example.app"])let formula = HomebrewPackage(name: "wget", kind: .formula)- #expect(Exclusions(paths: [URL(filePath: "/Applications/Example.app")]).excludes(cask, apps: [], prefix: prefix) == true)- #expect(Exclusions(paths: [URL(filePath: "/Applications/Example.app/Contents/Resources/keep")]).excludes(cask, apps: [], prefix: prefix) == true)- #expect(Exclusions(bundleIdentifiers: ["com.example.app"]).excludes(cask, apps: [app], prefix: prefix) == true)- #expect(Exclusions(bundleIdentifiers: ["com.example.app"]).excludes(cask, apps: [], prefix: prefix) == true, "the identifier it quits names it too")- #expect(Exclusions(paths: [URL(filePath: "/opt/homebrew/Caskroom/example")]).excludes(cask, apps: [], prefix: prefix) == true)- #expect(Exclusions(paths: [URL(filePath: "/opt/homebrew")]).excludes(formula, apps: [], prefix: prefix) == true)- #expect(Exclusions.unreadable.excludes(formula, apps: [], prefix: prefix) == true, "a list that cannot be read keeps everything")- #expect(Exclusions.notYetRead.excludes(formula, apps: [], prefix: prefix) == true, "a list not read yet keeps everything")+ #expect(Exclusions(paths: [URL(filePath: "/Applications/Example.app")]).excludes(cask, apps: [], installation: installation) == true)+ #expect(Exclusions(paths: [URL(filePath: "/Applications/Example.app/Contents/Resources/keep")]).excludes(cask, apps: [], installation: installation) == true)+ #expect(Exclusions(bundleIdentifiers: ["com.example.app"]).excludes(cask, apps: [app], installation: installation) == true)+ #expect(Exclusions(bundleIdentifiers: ["com.example.app"]).excludes(cask, apps: [], installation: installation) == true, "the identifier it quits names it too")+ #expect(Exclusions(paths: [URL(filePath: "/opt/homebrew/Caskroom/example")]).excludes(cask, apps: [], installation: installation) == true)+ #expect(Exclusions(paths: [URL(filePath: "/opt/homebrew")]).excludes(formula, apps: [], installation: installation) == true)+ #expect(Exclusions.unreadable.excludes(formula, apps: [], installation: installation) == true, "a list that cannot be read keeps everything")+ #expect(Exclusions.notYetRead.excludes(formula, apps: [], installation: installation) == true, "a list not read yet keeps everything")let unrelated = Exclusions(paths: [URL(filePath: "/Applications/Other.app")], bundleIdentifiers: ["com.other.app"])- #expect(unrelated.excludes(cask, apps: [app], prefix: prefix) == false)- #expect(unrelated.excludes(formula, apps: [], prefix: prefix) == false)- #expect(Exclusions(paths: [URL(filePath: "/opt/homebrew/Cellar/curl")]).excludes(formula, apps: [], prefix: prefix) == false)+ #expect(unrelated.excludes(cask, apps: [app], installation: installation) == false)+ #expect(unrelated.excludes(formula, apps: [], installation: installation) == false)+ #expect(Exclusions(paths: [URL(filePath: "/opt/homebrew/Cellar/curl")]).excludes(formula, apps: [], installation: installation) == false)}@Test(.permissionsHold) func keepsAPackageItCannotSearchForAnExclusion() throws {@@ -112,10 +112,10 @@ struct ExclusionsTests {try directory.file("prefix/Caskroom/example/1.0/kept/org.example.excluded/state.db")try directory.setPermissions(0, of: "prefix/Caskroom/example/1.0")defer { try? directory.setPermissions(0o755, of: "prefix/Caskroom/example/1.0") }- let prefix = directory.url.appending(path: "prefix", directoryHint: .isDirectory)+ let installation = HomebrewInstallation(version: "7.0.9", prefix: directory.url.appending(path: "prefix"))let cask = HomebrewPackage(name: "example", kind: .cask)- #expect(Exclusions(bundleIdentifiers: ["org.example.excluded"]).excludes(cask, apps: [], prefix: prefix) == nil)+ #expect(Exclusions(bundleIdentifiers: ["org.example.excluded"]).excludes(cask, apps: [], installation: installation) == nil)}@Test func matchesAppsByBundleIdentifierOrPlace() {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swiftindex bf5e246e..dd906831 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift@@ -4,6 +4,18 @@ import Synchronizationimport Testingstruct HomebrewTests {+ @Test func takesThePrefixAndTheCellarHomebrewAnswers() {+ let installation = Homebrew.installation(+ versionAnswer: "Homebrew 7.0.9\n", prefixAnswer: "/usr/local\n", cellarAnswer: "/usr/local/Homebrew/Cellar\n"+ )+ #expect(installation?.version == "7.0.9")+ #expect(installation?.prefix.path(percentEncoded: false) == "/usr/local")+ #expect(installation?.cellar.path(percentEncoded: false) == "/usr/local/Homebrew/Cellar/")+ #expect(installation?.caskroom.path(percentEncoded: false) == "/usr/local/Caskroom/")+ #expect(Homebrew.installation(versionAnswer: "Homebrew 7.0.9\n", prefixAnswer: nil, cellarAnswer: "/c\n") == nil)+ #expect(Homebrew.installation(versionAnswer: "Homebrew 7.0.9\n", prefixAnswer: "/p\n", cellarAnswer: "c\n") == nil)+ }+/// `man brew`: unless `HOMEBREW_NO_INSTALL_CLEANUP` is set, `brew upgrade` runs `brew cleanup` by itself, for/// the upgraded packages and every 30 days for all of them, which deletes old versions and downloads for good./// Peel deletes those only through Clean Up, which asks first, so no call it makes may clean up on its own.@@ -110,11 +122,11 @@ struct HomebrewTests {}try directory.setPermissions(0, of: "Cellar")- #expect(excludingWget.keptFormulae(inCellarOf: directory.url) == nil)+ #expect(excludingWget.keptFormulae(inCellar: directory.url.appending(path: "Cellar")) == nil)try directory.setPermissions(0o755, of: "Cellar")try directory.setPermissions(0, of: "Cellar/jq/1.8.1")- #expect(excludingAnApp.keptFormulae(inCellarOf: directory.url) == nil)- #expect(excludingWget.keptFormulae(inCellarOf: directory.url.appending(path: "Missing")) == [])+ #expect(excludingAnApp.keptFormulae(inCellar: directory.url.appending(path: "Cellar")) == nil)+ #expect(excludingWget.keptFormulae(inCellar: directory.url.appending(path: "Missing/Cellar")) == [])}/// `brew cleanup` and the autoremove that `brew uninstall` runs delete formulae and their old versions for good,@@ -129,9 +141,9 @@ struct HomebrewTests {directory.url.appending(path: "Cellar/jq"), directory.url.appending(path: "Cellar/[email protected]/3.13.7"),])- let kept = try #require(exclusions.keptFormulae(inCellarOf: directory.url))+ let kept = try #require(exclusions.keptFormulae(inCellar: directory.url.appending(path: "Cellar")))#expect(kept == ["jq", "[email protected]"])- #expect(Exclusions.unreadable.keptFormulae(inCellarOf: directory.url) == ["jq", "[email protected]", "wget"])+ #expect(Exclusions.unreadable.keptFormulae(inCellar: directory.url.appending(path: "Cellar")) == ["jq", "[email protected]", "wget"])let attempt = try await Homebrew.execute(["-c", "printf %s \"$HOMEBREW_NO_CLEANUP_FORMULAE\""], autoUpdate: false, keeping: kept, timeout: 10,diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swiftindex 4b44ca1b..dd13ec4f 100644--- a/Peel/Model/HomebrewLibrary.swift+++ b/Peel/Model/HomebrewLibrary.swift@@ -268,7 +268,7 @@ final class HomebrewLibrary {// The installation is read first, since it says how to read the figures that follow. The other questions// take about as long as each other, so they run at the same time. Only the page shows the last two.let found = await Homebrew.installation()- let kept = found.flatMap { ExclusionsStore.shared.exclusions.keptFormulae(inCellarOf: $0.prefix) } ?? []+ let kept = found.flatMap { ExclusionsStore.shared.exclusions.keptFormulae(inCellar: $0.cellar) } ?? []async let preview = includingReclaimable ? Homebrew.cleanupPreview(asWrittenBy: found, keeping: kept) : nilasync let overridden = includingReclaimable ? Homebrew.overrides() : nillet installed: Result<[HomebrewPackage], Homebrew.CommandFailure>@@ -422,8 +422,8 @@ final class HomebrewLibrary {/// The formulae the exclusions cover, which Homebrew is told to keep, asked again right before a command that/// deletes for good. Nothing runs when Peel can't tell which they are or Homebrew's own settings replace the list.private func keptFormulae() async -> Keeping {- guard let prefix = installation?.prefix else { return .nothingRuns(keepsNothing) }- guard let kept = ExclusionsStore.shared.exclusions.keptFormulae(inCellarOf: prefix) else {+ guard let cellar = installation?.cellar else { return .nothingRuns(keepsNothing) }+ guard let kept = ExclusionsStore.shared.exclusions.keptFormulae(inCellar: cellar) else {return .nothingRuns(CommandResult(succeeded: false, output: String(localized: "Peel couldn’t read Homebrew’s Cellar to tell which formulae your exclusions cover, so it ran nothing.")))}return await Homebrew.keepsEvery(kept) ? .formulae(kept) : .nothingRuns(keepsNothing)diff --git a/Peel/Views/Homebrew/HomebrewDetailView.swift b/Peel/Views/Homebrew/HomebrewDetailView.swiftindex 66aea3b7..7b11a250 100644--- a/Peel/Views/Homebrew/HomebrewDetailView.swift+++ b/Peel/Views/Homebrew/HomebrewDetailView.swift@@ -269,7 +269,7 @@ struct HomebrewDetailView: View {guard let extensionsAnswer, extensionsAnswer.apps == carrying else { return "Checking…" }if let removedElsewhere = extensionsAnswer.removedElsewhere { return removedElsewhere.explanation }}- switch exclusions.exclusions.excludes(package, apps: apps, prefix: homebrew.installation?.prefix) {+ switch exclusions.exclusions.excludes(package, apps: apps, installation: homebrew.installation) {case true?: return "Excluded in Settings"case nil: return "Can’t be checked against your exclusions"case false?: return nildiff --git a/SAFETY.md b/SAFETY.mdindex a95ac2b0..8dcdd6f1 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -536,11 +536,12 @@ there: that holds even for what only the helper could move, as the helper knowsfiles, such as plug-ins, receipts, or a folder named after the app, are excluded only by their own paths, so to keepthem, exclude them too.-Exclusions reach the Homebrew page too, since Homebrew deletes for good: Peel never runs `brew uninstall` for a-package whose app, or whose own folder, you excluded, nor for one with a folder Peel can't look into for an app you-excluded, nor for any package while your list isn't read or can't be, and Homebrew's cleanup keeps every formula whose-folder you excluded. While Peel can't read Homebrew's Cellar, or can't look into a formula's folder in it for an app-you excluded, nothing that deletes formulae runs at all.+Exclusions reach the Homebrew page too, since Homebrew deletes for good: Peel never runs `brew uninstall` for a package+whose app, or whose own folder, you excluded, nor for one with a folder Peel can't look into for an app you excluded,+nor for any package while your list isn't read or can't be, and Homebrew's cleanup keeps every formula whose folder you+excluded. While Peel can't read Homebrew's Cellar, or can't look into a formula's folder in it for an app you excluded,+nothing that deletes formulae runs at all. The Cellar and the prefix are the ones Homebrew names (`brew --cellar`, `brew+--prefix`), never worked out from where `brew` sits.If the saved list is there and cannot be read, Peel does not treat it as empty. It moves nothing until youstart over in Settings, `peel` refuses, and the file it could not read is kept beside the new one.