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.

Hold a cask back for anything excluded that its uninstall removes

Țugui Dragoș717611460121
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a6a7833c..87d71622 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -561,6 +561,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
#### Homebrew
+- Uninstall and Upgrade stay off for a cask when you excluded anything it placed, such as a font or a manual page, or
+ anything its `uninstall` stanza removes: before, only its app and its own folder counted, and Homebrew took the rest.
- With several apps to quit before an upgrade, the button says Quit Apps, since some languages say "the app" there.
- Peel asks Homebrew for its prefix and its Cellar, so the formulae you excluded are always kept.
- Uninstall and Upgrade wait until Homebrew's apps are known, and the copied commands keep the excluded formulae.
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
index 72173af3..cdb8a004 100644
--- a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
@@ -28,6 +28,12 @@ public struct HomebrewPackage: Sendable, Hashable, Identifiable {
/// Where Homebrew linked the commands an installed cask puts on the path: "/opt/homebrew/bin/example".
/// `brew uninstall` removes these links.
public let commandLinks: [String]
+ /// Where Homebrew put what else an installed cask places, such as a font or a manual page. `brew uninstall` removes
+ /// these too.
+ public let otherTargets: [String]
+ /// Paths the cask's `uninstall` stanza removes, as written, which `brew uninstall` and an upgrade run. `zap` is
+ /// left out: Peel never runs it.
+ public let uninstallPatterns: [String]
/// Bundle identifiers the cask tells macOS to quit, which prove a cask really is a given app.
public let quitIdentifiers: [String]
/// Installer receipts the cask forgets, which can prove the cask for an app installed from a `.pkg`.
@@ -199,6 +205,8 @@ public struct HomebrewPackage: Sendable, Hashable, Identifiable {
appNames: [String] = [],
appTargets: [String] = [],
commandLinks: [String] = [],
+ otherTargets: [String] = [],
+ uninstallPatterns: [String] = [],
leftoverPatterns: [String] = [],
emptyFolderPatterns: [String] = [],
quitIdentifiers: [String] = [],
@@ -222,6 +230,8 @@ public struct HomebrewPackage: Sendable, Hashable, Identifiable {
self.appNames = appNames
self.appTargets = appTargets
self.commandLinks = commandLinks
+ self.otherTargets = otherTargets
+ self.uninstallPatterns = uninstallPatterns
self.leftoverPatterns = leftoverPatterns
self.emptyFolderPatterns = emptyFolderPatterns
self.quitIdentifiers = quitIdentifiers
@@ -447,6 +457,8 @@ struct CaskArtifact: Decodable, Sendable {
let appNames: [String]
let appTargets: [String]
let commandLinks: [String]
+ let otherTargets: [String]
+ let uninstallPatterns: [String]
let leftoverPatterns: [String]
let emptyFolderPatterns: [String]
/// Bundle identifiers the cask tells macOS to quit. The surest sign a cask really is a given app.
@@ -476,7 +488,12 @@ struct CaskArtifact: Decodable, Sendable {
// A command's link is written the same way: `{"binary": [...], "target": "/opt/homebrew/bin/studio"}`, and
// so is the link to a script Homebrew writes to run the app's program (`command_wrapper`).
let linksACommand = raw["binary"] != nil || raw["command_wrapper"] != nil
- commandLinks = linksACommand ? (raw["target"]?.strings ?? []).filter { $0.hasPrefix("/") } : []
+ let targets = (raw["target"]?.strings ?? []).filter { $0.hasPrefix("/") }
+ commandLinks = linksACommand ? targets : []
+ otherTargets = app == nil && !linksACommand ? targets : []
+ uninstallPatterns = (raw["uninstall"]?.dictionaries ?? []).flatMap { stanza in
+ (Self.removalKeys + ["rmdir"]).flatMap { stanza[$0]?.strings ?? [] }
+ }
var patterns: [String] = []
var emptyFolders: [String] = []
@@ -1294,6 +1311,8 @@ public enum Homebrew {
appNames: cask.artifacts?.flatMap(\.appNames) ?? [],
appTargets: cask.artifacts?.flatMap(\.appTargets) ?? [],
commandLinks: cask.artifacts?.flatMap(\.commandLinks) ?? [],
+ otherTargets: cask.artifacts?.flatMap(\.otherTargets) ?? [],
+ uninstallPatterns: cask.artifacts?.flatMap(\.uninstallPatterns) ?? [],
leftoverPatterns: cask.artifacts?.flatMap(\.leftoverPatterns) ?? [],
emptyFolderPatterns: cask.artifacts?.flatMap(\.emptyFolderPatterns) ?? [],
quitIdentifiers: cask.artifacts?.flatMap(\.quitIdentifiers) ?? [],
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
index 71954b6b..3e776b3b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
@@ -261,13 +261,15 @@ public struct Exclusions: Sendable, Codable, Hashable {
}
/// Whether `brew uninstall`, or an upgrade, which deletes the version it replaces, 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 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 neither can be undone.
+ /// an app the package installs (`apps` are the installed apps known to be its), an app identifier it quits,
+ /// whatever else it placed, what its `uninstall` stanza names, 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 neither can be undone.
public func excludes(
_ package: HomebrewPackage,
apps: [InstalledApp],
- installation: HomebrewInstallation?
+ installation: HomebrewInstallation?,
+ home: URL = .homeDirectory
) -> Bool? {
guard isKnown else { return true }
if apps.contains(where: { $0.bundleIdentifier.map(excludes(bundleIdentifier:)) == true }) { return true }
@@ -275,7 +277,9 @@ public struct Exclusions: Sendable, Codable, Hashable {
let own = installation.map {
(package.kind == .cask ? $0.caskroom : $0.cellar).appending(path: package.name, directoryHint: .isDirectory)
}
+ let named = isEmpty ? [] : package.uninstallPatterns.flatMap { CaskEvidence.expand($0, home: home) }
let folders = package.appTargets.map { URL(filePath: $0, directoryHint: .isDirectory) } + apps.map(\.url)
+ + (package.commandLinks + package.otherTargets).map { URL(filePath: $0) } + named
+ [own].compactMap(\.self)
var isKnownToHoldNothing = true
for folder in folders {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
index 7e9df290..b232a6da 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ExclusionsTests.swift
@@ -107,6 +107,34 @@ struct ExclusionsTests {
#expect(Exclusions(paths: [URL(filePath: "/opt/homebrew/Cellar/curl")]).excludes(formula, apps: [], installation: installation) == false)
}
+ @Test func keepsWhatElseBrewUninstallRemoves() throws {
+ let directory = try TemporaryDirectory()
+ let installation = HomebrewInstallation(version: "7.0.9", prefix: directory.url.appending(path: "prefix"))
+ let kept = try directory.file("home/Library/Application Support/Kept/state.db")
+ let folder = kept.deletingLastPathComponent()
+ let font = try directory.file("home/Library/Fonts/Example.ttf")
+ let link = try directory.file("prefix/bin/example")
+ let excludes = { (exclusions: Exclusions, cask: HomebrewPackage) in
+ exclusions.excludes(cask, apps: [], installation: installation, home: directory.url.appending(path: "home"))
+ }
+ let named = HomebrewPackage(name: "example", kind: .cask, uninstallPatterns: ["~/Library/Application Support/Kept"])
+ let globbed = HomebrewPackage(name: "example", kind: .cask, uninstallPatterns: ["~/Library/Application Support/Ke*"])
+ let zapped = HomebrewPackage(name: "example", kind: .cask, leftoverPatterns: ["~/Library/Application Support/Kept"])
+ let placed = HomebrewPackage(
+ name: "example", kind: .cask, commandLinks: [link.path(percentEncoded: false)],
+ otherTargets: [font.path(percentEncoded: false)]
+ )
+
+ #expect(excludes(Exclusions(paths: [folder]), named) == true)
+ #expect(excludes(Exclusions(paths: [kept]), named) == true, "the folder it removes holds what is excluded")
+ #expect(excludes(Exclusions(paths: [folder]), globbed) == true)
+ #expect(excludes(Exclusions(paths: [folder]), zapped) == false, "Peel never runs zap")
+ #expect(excludes(Exclusions(paths: [font]), placed) == true)
+ #expect(excludes(Exclusions(paths: [link]), placed) == true)
+ #expect(excludes(Exclusions(paths: [folder]), placed) == false)
+ #expect(excludes(Exclusions(bundleIdentifiers: ["org.example.other"]), placed) == false, "a file holds no app")
+ }
+
@Test(.permissionsHold) func keepsAPackageItCannotSearchForAnExclusion() throws {
let directory = try TemporaryDirectory()
try directory.file("prefix/Caskroom/example/1.0/kept/org.example.excluded/state.db")
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
index 3af00dce..0cd028d8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
@@ -513,6 +513,29 @@ struct HomebrewTests {
}
}
+ @Test func readsWhatACaskPlacesAndWhatItsUninstallStanzaRemoves() throws {
+ let json = """
+ {"formulae": [], "casks": [{"token": "sample", "version": "1", "outdated": false, "artifacts": [
+ {"uninstall": [{"quit": "org.example.sample", "trash": "~/Library/Application Support/Sample",
+ "delete": ["/Library/Sample"], "rmdir": "/Library/Empty"}]},
+ {"app": ["Sample.app"], "target": "/Applications/Sample.app"},
+ {"binary": ["/Applications/Sample.app/Contents/MacOS/sample", {"target": "sample"}],
+ "target": "/opt/homebrew/bin/sample"},
+ {"font": ["Sample.ttf"], "target": "/Users/org.example/Library/Fonts/Sample.ttf"},
+ {"manpage": ["sample.1"], "target": "/opt/homebrew/share/man/man1/sample.1"},
+ {"zap": [{"trash": "~/Library/Application Support/SampleZap"}]}
+ ]}]}
+ """
+ let cask = try #require(Homebrew.parseInstalled(Data(json.utf8))?.first)
+
+ #expect(cask.appTargets == ["/Applications/Sample.app"])
+ #expect(cask.commandLinks == ["/opt/homebrew/bin/sample"])
+ #expect(cask.otherTargets == [
+ "/Users/org.example/Library/Fonts/Sample.ttf", "/opt/homebrew/share/man/man1/sample.1",
+ ])
+ #expect(cask.uninstallPatterns == ["~/Library/Application Support/Sample", "/Library/Sample", "/Library/Empty"])
+ }
+
@Test func parsesInstalledFormulaeAndCasks() throws {
let json = """
{
diff --git a/SAFETY.md b/SAFETY.md
index 1fbf785a..f707b85e 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -573,8 +573,9 @@ receipt stays too, on its page and on Package Receipts. Its other files, such as
app, are excluded only by their own paths, so to keep them, exclude them too.
Exclusions reach the Homebrew page too, since Homebrew deletes for good: Peel never runs `brew uninstall` or `brew
-upgrade`, which deletes the version it replaces, 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,
+upgrade`, which deletes the version it replaces, for a package whose app or own folder you excluded, or anything else it
+placed or its `uninstall` stanza names, 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