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.

Ask zsh which .zshrc it reads

Țugui Dragoș5f82e4be532d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 12388440..73989f70 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -142,6 +142,7 @@ along a removal is, and says within seconds when its helper isn't answering.
- **Terminal:** The Git tab leaves a setting to a file your settings include, so Peel's value never wins over yours.
Keep key passphrases in your keychain no longer stops an ssh installed from elsewhere, such as Homebrew's, which
doesn't know `UseKeychain`, and delta's lines on the Tools tab turn on the line numbers its description promises.
+ The Shell tab finds the `.zshrc` zsh reads when your `~/.zshenv` moves zsh's files with `ZDOTDIR`.
- **History:** Put Back brings a file a Homebrew cask or an installer receipt named back under the name the disk
gives it, registers an app it brings back with macOS, and judges with your exclusions as `peel restore` does. Undo
now stays off, as Put Back already did, while History or the exclusions can't be read.
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
index c1ae56f4..3402b4d6 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
@@ -1,4 +1,5 @@
public import Foundation
+import PeelPrivileged
public enum ShellSessions: Sendable, Equatable {
case zsh(startupFile: URL)
@@ -26,10 +27,26 @@ public enum ShellSessions: Sendable, Equatable {
}
}
- /// The `.zshrc` beside the `.zshenv` zsh reads, where the line that sources Peel's file goes.
- public var zshrc: URL? {
+ /// The `.zshrc` zsh reads, where the line that sources Peel's file goes: in the folder `ZDOTDIR` names once
+ /// `.zshenv` has run, since `.zshenv` may set it (`man zsh`, STARTUP/SHUTDOWN FILES). zsh is asked with `-c`, which
+ /// reads `.zshenv` and no other startup file, and the folder of `.zshenv` stands when it doesn't answer.
+ public func zshrc(home: URL = .homeDirectory) async -> URL? {
guard case .zsh(let zshenv) = self else { return nil }
- return zshenv.deletingLastPathComponent().appending(path: ".zshrc", directoryHint: .notDirectory)
+ let folder = zshenv.deletingLastPathComponent()
+ // As Terminal sets them, with no slash at the end, which `"$HOME/.config/zsh"` would double.
+ let environment = [
+ "HOME": (home.path(percentEncoded: false) as NSString).standardizingPath,
+ "ZDOTDIR": (folder.path(percentEncoded: false) as NSString).standardizingPath,
+ "PATH": "/usr/bin:/bin:/usr/sbin:/sbin",
+ ]
+ let answer = await Subprocess.run(
+ "/bin/zsh", ["-c", #"print -r -- "${ZDOTDIR:-$HOME}""#], environment: environment, timeout: 10
+ )
+ // What `.zshenv` itself prints comes first, so the answer is the last line.
+ let named: String? = if case .success(let output) = answer, output.status == 0,
+ let line = output.text.split(separator: "\n").last, line.hasPrefix("/") { String(line) } else { nil }
+ return (named.map { URL(filePath: $0, directoryHint: .isDirectory) } ?? folder)
+ .appending(path: ".zshrc", directoryHint: .notDirectory)
}
public var name: String {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift
index ec019eb9..a6deee53 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift
@@ -209,7 +209,5 @@ struct PeelLinesCommandTests {
#expect(home.appending(path: ".zshrc").pathForTheShell(home: home) == "~/.zshrc")
#expect(home.appending(path: "My Files/.zshrc").pathForTheShell(home: home) == "'/Users/example/My Files/.zshrc'")
#expect(URL(filePath: "/etc/it's").pathForTheShell(home: home) == "'/etc/it'\\''s'")
- #expect(ShellSessions.zsh(startupFile: home.appending(path: ".zshenv")).zshrc == home.appending(path: ".zshrc"))
- #expect(ShellSessions.bash.zshrc == nil)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
index 9c2d6416..5fbf07c0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
@@ -29,6 +29,19 @@ struct ShellSessionsTests {
== .zsh(startupFile: home.appending(path: ".zshenv", directoryHint: .notDirectory)))
}
+ @Test func zshReadsItsZshrcFromTheFolderItsZshenvNames() async throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url
+ let zsh = ShellSessions.zsh(startupFile: home.appending(path: ".zshenv", directoryHint: .notDirectory))
+ #expect(await zsh.zshrc(home: home)?.path(percentEncoded: false)
+ == home.appending(path: ".zshrc").path(percentEncoded: false))
+
+ try directory.file(".zshenv", contents: Data("export ZDOTDIR=\"$HOME/.config/zsh\"\n".utf8))
+ #expect(await zsh.zshrc(home: home)?.path(percentEncoded: false)
+ == home.appending(path: ".config/zsh/.zshrc").path(percentEncoded: false))
+ #expect(await ShellSessions.bash.zshrc(home: home) == nil)
+ }
+
@Test func turnsSessionsOffTheWayTheScriptsOfThisMacRead() throws {
let zshrc = try String(contentsOfFile: "/etc/zshrc", encoding: .utf8)
#expect(zshrc.contains(#". "/etc/zshrc_$TERM_PROGRAM""#))
diff --git a/Peel/Model/ShellLibrary.swift b/Peel/Model/ShellLibrary.swift
index 7b61c806..1d0172bb 100644
--- a/Peel/Model/ShellLibrary.swift
+++ b/Peel/Model/ShellLibrary.swift
@@ -6,14 +6,11 @@ import PeelCore
final class ShellLibrary {
struct State: Equatable {
var shell: ShellSessions?
+ var startupFile: URL?
var choices: ShellFile.Choices?
var isSourced: Bool
var setsItsOwnPrompt: Bool
var known: Set<String>?
-
- var startupFile: URL? {
- shell?.zshrc
- }
}
let file = ShellFile.url(in: PeelFolder.url)
@@ -35,12 +32,14 @@ final class ShellLibrary {
known = await ShellFile.knownToZsh()
}
let shell = ShellSessions.inTerminal(TerminalSettings())
+ let startupFile = await shell?.zshrc()
let choices = ShellFile.read(file)
state = State(
shell: shell,
+ startupFile: startupFile,
choices: choices,
- isSourced: shell?.zshrc.map { ShellFile.isSourced(file, from: $0, home: .homeDirectory) } ?? false,
- setsItsOwnPrompt: shell?.zshrc.map {
+ isSourced: startupFile.map { ShellFile.isSourced(file, from: $0, home: .homeDirectory) } ?? false,
+ setsItsOwnPrompt: startupFile.map {
ShellFile.setsItsOwnPrompt(after: file, in: $0, home: .homeDirectory)
} ?? false,
known: known