|
| 1 | +// |
| 2 | +// ClaudePaths.swift |
| 3 | +// ClaudeIsland |
| 4 | +// |
| 5 | +// Single source of truth for all Claude config directory paths. |
| 6 | +// Resolves automatically via CLAUDE_CONFIG_DIR env var or filesystem detection, |
| 7 | +// with an optional user override via AppSettings.claudeDirectoryName. |
| 8 | +// |
| 9 | + |
| 10 | +import Foundation |
| 11 | + |
| 12 | +enum ClaudePaths { |
| 13 | + |
| 14 | + /// Cached resolved directory to avoid filesystem checks on every access |
| 15 | + private static var _cachedDir: URL? |
| 16 | + |
| 17 | + /// Guards reads/writes to _cachedDir — accessed from the main actor |
| 18 | + /// (UI settings), the ConversationParser actor, and background watcher |
| 19 | + /// queues, so cross-thread access needs synchronization. |
| 20 | + private static let cacheLock = NSLock() |
| 21 | + |
| 22 | + /// Root Claude config directory, resolved once and cached. |
| 23 | + /// |
| 24 | + /// Resolution order: |
| 25 | + /// 1. CLAUDE_CONFIG_DIR environment variable (if set and exists) |
| 26 | + /// 2. AppSettings.claudeDirectoryName override (if changed from default) |
| 27 | + /// 3. ~/.config/claude/ (new default since Claude Code v2.1.30+, if projects/ exists) |
| 28 | + /// 4. ~/.claude/ (legacy fallback) |
| 29 | + static var claudeDir: URL { |
| 30 | + cacheLock.lock() |
| 31 | + if let cached = _cachedDir { |
| 32 | + cacheLock.unlock() |
| 33 | + return cached |
| 34 | + } |
| 35 | + cacheLock.unlock() |
| 36 | + |
| 37 | + // Resolve outside the lock — involves filesystem and settings reads |
| 38 | + // that shouldn't block other threads. |
| 39 | + let resolved = resolveClaudeDir() |
| 40 | + |
| 41 | + cacheLock.lock() |
| 42 | + // Another thread may have populated the cache while we were resolving; |
| 43 | + // prefer theirs for consistency, but either value is correct. |
| 44 | + if let existing = _cachedDir { |
| 45 | + cacheLock.unlock() |
| 46 | + return existing |
| 47 | + } |
| 48 | + _cachedDir = resolved |
| 49 | + cacheLock.unlock() |
| 50 | + return resolved |
| 51 | + } |
| 52 | + |
| 53 | + static var hooksDir: URL { |
| 54 | + claudeDir.appendingPathComponent("hooks") |
| 55 | + } |
| 56 | + |
| 57 | + static var settingsFile: URL { |
| 58 | + claudeDir.appendingPathComponent("settings.json") |
| 59 | + } |
| 60 | + |
| 61 | + static var projectsDir: URL { |
| 62 | + claudeDir.appendingPathComponent("projects") |
| 63 | + } |
| 64 | + |
| 65 | + /// Shell-safe absolute path for hook commands in settings.json. |
| 66 | + /// Absolute paths keep custom directories and ~/.config/claude working; |
| 67 | + /// quoting keeps paths with spaces from being split by the shell. |
| 68 | + static var hookScriptShellPath: String { |
| 69 | + shellQuote(claudeDir.appendingPathComponent("hooks/claude-island-state.py").path) |
| 70 | + } |
| 71 | + |
| 72 | + /// Invalidate the cached directory so the next access re-resolves. |
| 73 | + /// Call this when the user changes AppSettings.claudeDirectoryName. |
| 74 | + static func invalidateCache() { |
| 75 | + cacheLock.lock() |
| 76 | + _cachedDir = nil |
| 77 | + cacheLock.unlock() |
| 78 | + } |
| 79 | + |
| 80 | + private static func resolveClaudeDir() -> URL { |
| 81 | + let fm = FileManager.default |
| 82 | + let home = fm.homeDirectoryForCurrentUser |
| 83 | + |
| 84 | + // 1. CLAUDE_CONFIG_DIR env var takes highest priority |
| 85 | + if let envDir = Foundation.ProcessInfo.processInfo.environment["CLAUDE_CONFIG_DIR"] { |
| 86 | + let expanded = (envDir as NSString).expandingTildeInPath |
| 87 | + let url = URL(fileURLWithPath: expanded) |
| 88 | + if fm.fileExists(atPath: url.path) { |
| 89 | + return url |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // 2. User override via settings — accepts either an absolute path (chosen |
| 94 | + // via the folder picker) or a legacy directory name under ~/ |
| 95 | + let settingsValue = AppSettings.claudeDirectoryName |
| 96 | + if !settingsValue.isEmpty && settingsValue != ".claude" { |
| 97 | + if settingsValue.hasPrefix("/") { |
| 98 | + return URL(fileURLWithPath: settingsValue) |
| 99 | + } else { |
| 100 | + return home.appendingPathComponent(settingsValue) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // 3. New default ~/.config/claude/ (if projects/ exists there) |
| 105 | + let newDefault = home.appendingPathComponent(".config/claude") |
| 106 | + if fm.fileExists(atPath: newDefault.appendingPathComponent("projects").path) { |
| 107 | + return newDefault |
| 108 | + } |
| 109 | + |
| 110 | + // 4. Legacy fallback |
| 111 | + return home.appendingPathComponent(".claude") |
| 112 | + } |
| 113 | + |
| 114 | + private static func shellQuote(_ path: String) -> String { |
| 115 | + "'" + path.replacingOccurrences(of: "'", with: "'\\''") + "'" |
| 116 | + } |
| 117 | +} |
0 commit comments