Files
steipete--codexbar/Sources/CodexBarCore/CodexHomeScope.swift
T
2026-07-13 12:22:33 +08:00

43 lines
1.5 KiB
Swift

import Foundation
public enum CodexHomeScope {
public static func normalizedHomePath(
_ rawPath: String?,
fileManager: FileManager = .default)
-> String?
{
guard var path = rawPath?.trimmingCharacters(in: .whitespacesAndNewlines), !path.isEmpty else {
return nil
}
if path == "~" {
path = fileManager.homeDirectoryForCurrentUser.path
} else if path.hasPrefix("~/") {
path = fileManager.homeDirectoryForCurrentUser
.appendingPathComponent(String(path.dropFirst(2)), isDirectory: true)
.path
} else if path.hasPrefix("~") {
return nil
}
guard (path as NSString).isAbsolutePath else { return nil }
return URL(fileURLWithPath: path, isDirectory: true).standardizedFileURL.path
}
public static func ambientHomeURL(
env: [String: String],
fileManager: FileManager = .default)
-> URL
{
if let raw = env["CODEX_HOME"]?.trimmingCharacters(in: .whitespacesAndNewlines), !raw.isEmpty {
return URL(fileURLWithPath: raw, isDirectory: true)
}
return fileManager.homeDirectoryForCurrentUser.appendingPathComponent(".codex", isDirectory: true)
}
public static func scopedEnvironment(base: [String: String], codexHome: String?) -> [String: String] {
guard let codexHome, !codexHome.isEmpty else { return base }
var env = base
env["CODEX_HOME"] = codexHome
return env
}
}