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 for its .zshrc with the ZDOTDIR the session gives

Țugui Dragoșc34d4f7ee29e
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
index 3402b4d6..6191e52e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
@@ -6,12 +6,12 @@ public enum ShellSessions: Sendable, Equatable {
case bash
public static func inTerminal(_ terminal: TerminalSettings, home: URL = .homeDirectory) -> ShellSessions? {
- of(
- terminalSetting: terminal.shell(),
- loginShell: loginShell(),
- zdotdir: ProcessInfo.processInfo.environment["ZDOTDIR"],
- home: home
- )
+ of(terminalSetting: terminal.shell(), loginShell: loginShell(), zdotdir: sessionZDOTDIR, home: home)
+ }
+
+ /// The ZDOTDIR of the session Peel runs in, which Terminal's zsh starts with too, since launchd starts both.
+ static var sessionZDOTDIR: String? {
+ ProcessInfo.processInfo.environment["ZDOTDIR"]
}
static func of(terminalSetting: String?, loginShell: String?, zdotdir: String?, home: URL) -> ShellSessions? {
@@ -31,14 +31,22 @@ public enum ShellSessions: Sendable, Equatable {
/// `.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? {
+ await zshrc(home: home, zdotdir: Self.sessionZDOTDIR)
+ }
+
+ /// `zdotdir` is set for zsh only when the session sets it, since `.zshenv` may keep a ZDOTDIR it finds
+ /// (`${ZDOTDIR:-$HOME/.config/zsh}`).
+ func zshrc(home: URL, zdotdir: String?) async -> URL? {
guard case .zsh(let zshenv) = self else { return nil }
let folder = zshenv.deletingLastPathComponent()
- // As Terminal sets them, with no slash at the end, which `"$HOME/.config/zsh"` would double.
- let environment = [
+ // With no slash at the end, which `"$HOME/.config/zsh"` would double.
+ var environment = [
"HOME": (home.path(percentEncoded: false) as NSString).standardizingPath,
- "ZDOTDIR": (folder.path(percentEncoded: false) as NSString).standardizingPath,
"PATH": "/usr/bin:/bin:/usr/sbin:/sbin",
]
+ if let zdotdir, !zdotdir.isEmpty {
+ environment["ZDOTDIR"] = (zdotdir as NSString).standardizingPath
+ }
let answer = await Subprocess.run(
"/bin/zsh", ["-c", #"print -r -- "${ZDOTDIR:-$HOME}""#], environment: environment, timeout: 10
)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
index 5fbf07c0..c3e2a017 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
@@ -33,13 +33,25 @@ struct ShellSessionsTests {
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)
+ #expect(await zsh.zshrc(home: home, zdotdir: nil)?.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)
+ #expect(await zsh.zshrc(home: home, zdotdir: nil)?.path(percentEncoded: false)
== home.appending(path: ".config/zsh/.zshrc").path(percentEncoded: false))
- #expect(await ShellSessions.bash.zshrc(home: home) == nil)
+ #expect(await ShellSessions.bash.zshrc(home: home, zdotdir: nil) == nil)
+ }
+
+ @Test func zshStartsWithZdotdirOnlyWhenTheSessionSetsIt() async throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url
+ let zsh = ShellSessions.zsh(startupFile: home.appending(path: ".zshenv", directoryHint: .notDirectory))
+ try directory.file(".zshenv", contents: Data("export ZDOTDIR=${ZDOTDIR:-$HOME/.config/zsh}\n".utf8))
+
+ #expect(await zsh.zshrc(home: home, zdotdir: nil)?.path(percentEncoded: false)
+ == home.appending(path: ".config/zsh/.zshrc").path(percentEncoded: false))
+ #expect(await zsh.zshrc(home: home, zdotdir: home.path(percentEncoded: false))?.path(percentEncoded: false)
+ == home.appending(path: ".zshrc").path(percentEncoded: false))
}
@Test func turnsSessionsOffTheWayTheScriptsOfThisMacRead() throws {