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.

Name a shell startup line only where the moved path stands whole

Țugui Dragoș228db6ea5d69
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/ShellReferences.swift b/Packages/PeelCore/Sources/PeelCore/Removal/ShellReferences.swift
index 7d66122d..f458a101 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/ShellReferences.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/ShellReferences.swift
@@ -18,14 +18,15 @@ public enum ShellReferences {
@concurrent
public static func find(naming moved: [URL], home: URL = .homeDirectory) async -> [Reference] {
- let spellings = moved.flatMap { spellings(of: $0, home: home) }
+ let spellings = moved.flatMap { spellings(of: $0, home: home) }.map { Array($0.lowercased().utf8) }
guard !spellings.isEmpty else { return [] }
var references: [Reference] = []
for file in await startupFiles(home: home) {
guard let data = BoundedRead.data(at: file, maximum: maximumBytes) else { continue }
let lines = String(decoding: data, as: UTF8.self).split(separator: "\n", omittingEmptySubsequences: false)
for (index, line) in lines.enumerated() where !line.drop(while: \.isWhitespace).hasPrefix("#") {
- if spellings.contains(where: { line.range(of: $0, options: .caseInsensitive) != nil }) {
+ let line = Array(line.lowercased().utf8)
+ if spellings.contains(where: { holdsWhole($0, in: line) }) {
references.append(Reference(file: file, line: index + 1))
}
}
@@ -33,6 +34,34 @@ public enum ShellReferences {
return references
}
+ /// Whether `line` holds `path` whole, or followed by a slash into it, both in UTF-8: `~/.config/pad` is not in
+ /// `~/.config/padlock`, nor `/Applications/Pad.app` in `~/Applications/Pad.app`.
+ static func holdsWhole(_ path: [UInt8], in line: [UInt8]) -> Bool {
+ line.withUnsafeBytes { haystack in
+ path.withUnsafeBytes { needle in
+ guard let start = haystack.baseAddress, let target = needle.baseAddress, !path.isEmpty else {
+ return false
+ }
+ var offset = 0
+ while let found = memmem(start + offset, line.count - offset, target, path.count) {
+ let begin = UnsafeRawPointer(found) - start
+ let end = begin + path.count
+ if begin == 0 || startsAWord.contains(line[begin - 1]),
+ end == line.count || endsAPath.contains(line[end]) {
+ return true
+ }
+ offset = begin + 1
+ }
+ return false
+ }
+ }
+ }
+
+ private static let startsAWord = Set(" \t\r\u{0B}\u{0C}\"'`=:;,|&({[<>".utf8)
+
+ /// A backslash or a dollar sign goes on with the name, as an escaped space or a variable does.
+ private static let endsAPath = Set(" \t\r\u{0B}\u{0C}/\"'`:;,|&()}]<>".utf8)
+
/// The files zsh, bash and fish read when a shell starts: zsh's `.zshenv` where the session's ZDOTDIR or the
/// home folder puts it, the other three in the folder zsh names once `.zshenv` has run, and the rest in the home
/// folder.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellReferencesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellReferencesTests.swift
index c8a57e74..83cbd042 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellReferencesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellReferencesTests.swift
@@ -28,6 +28,29 @@ struct ShellReferencesTests {
#expect(await ShellReferences.find(naming: [], home: home).isEmpty)
}
+ @Test func namesALineOnlyWhereThePathStandsWhole() async throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ try directory.file("home/.zshrc", contents: Data("""
+ export PATH="$HOME/Library/Application Support/CodeWhisperer/bin:$PATH"
+ source ~/Applications/Foo.app/Contents/env.sh
+ PATH=/Applications/Foo.appendix/bin:$PATH
+ cp -R /Applications/Foo.app.bak /tmp
+ alias foo=/applications/FOO.app/Contents/MacOS/foo
+ export PATH="$PATH:$HOME/Library/Application Support/Code/bin"
+ [ -d "/Applications/Foo.app" ] && echo yes
+ fpath=(~/Library/Application\\ Support/Code $fpath)
+ """.utf8))
+ let moved = [
+ URL(filePath: "/Applications/Foo.app", directoryHint: .isDirectory),
+ home.appending(path: "Library/Application Support/Code", directoryHint: .isDirectory),
+ ]
+
+ let found = await ShellReferences.find(naming: moved, home: home)
+
+ #expect(found.map(\.line) == [5, 6, 7, 8])
+ }
+
@Test func readsTheStartupFilesOfEachShellOnce() async throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)