restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
public protocol ConfigServicing: Sendable {
|
||||
/// Rewrites `config.default.json`, re-reads `config.json`, and returns the effective config.
|
||||
@discardableResult
|
||||
func resolve() async -> HelderConfig
|
||||
/// The last resolved config; the defaults before the first `resolve()`.
|
||||
func current() async -> HelderConfig
|
||||
}
|
||||
|
||||
public actor ConfigService: ConfigServicing {
|
||||
public nonisolated let root: URL
|
||||
private var config = HelderConfig.defaults
|
||||
|
||||
public init(root: URL) {
|
||||
self.root = root
|
||||
}
|
||||
|
||||
public nonisolated var folder: URL { root.appendingPathComponent(HelderConfig.folderName) }
|
||||
public nonisolated var overridesURL: URL { folder.appendingPathComponent(HelderConfig.overridesFileName) }
|
||||
|
||||
/// A read-only or missing project folder yields the defaults instead of an error.
|
||||
@discardableResult
|
||||
public func resolve() async -> HelderConfig {
|
||||
let fm = FileManager.default
|
||||
do {
|
||||
try fm.createDirectory(at: folder, withIntermediateDirectories: true)
|
||||
try HelderConfig.defaultsJSON().write(to: folder.appendingPathComponent(HelderConfig.defaultsFileName))
|
||||
let gitignore = folder.appendingPathComponent(".gitignore")
|
||||
if (try? String(contentsOf: gitignore, encoding: .utf8)) != HelderConfig.gitignoreBody {
|
||||
try Data(HelderConfig.gitignoreBody.utf8).write(to: gitignore)
|
||||
}
|
||||
} catch {
|
||||
Log.warn("config", "cannot write \(folder.path): \(error.localizedDescription)")
|
||||
}
|
||||
config = HelderConfig.resolved(overrides: try? Data(contentsOf: overridesURL))
|
||||
return config
|
||||
}
|
||||
|
||||
public func current() async -> HelderConfig {
|
||||
config
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import Foundation
|
||||
|
||||
public protocol NotesServicing: Sendable {
|
||||
/// The note's text; "" when the project has none yet.
|
||||
func read() async -> String
|
||||
func write(_ text: String) async throws
|
||||
}
|
||||
|
||||
/// The note is a plain `.notes.txt` in the project root, not JSON and not in `.helder/`,
|
||||
/// so it stays readable and editable outside Helder.
|
||||
public struct NotesService: NotesServicing {
|
||||
public static let fileName = ".notes.txt"
|
||||
public let root: URL
|
||||
|
||||
public init(root: URL) {
|
||||
self.root = root
|
||||
}
|
||||
|
||||
private var url: URL { root.appendingPathComponent(Self.fileName) }
|
||||
|
||||
public func read() async -> String {
|
||||
(try? String(contentsOf: url, encoding: .utf8)) ?? ""
|
||||
}
|
||||
|
||||
public func write(_ text: String) async throws {
|
||||
try Data(text.utf8).write(to: url, options: .atomic)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
public enum ProjectLocator {
|
||||
/// The folder a fresh window opens: `$HELDER_PROJECT`, else the first directory argument; nil shows the launcher.
|
||||
/// A bare "." is ignored, so a development launch lands on the launcher too.
|
||||
public static func initialRoot(environment: [String: String] = ProcessInfo.processInfo.environment,
|
||||
arguments: [String] = CommandLine.arguments) -> URL? {
|
||||
if let env = environment["HELDER_PROJECT"], isDirectory(env) {
|
||||
return URL(fileURLWithPath: env).standardizedFileURL
|
||||
}
|
||||
let candidate = arguments.dropFirst().first { $0 != "." && !$0.hasPrefix("-") && isDirectory($0) }
|
||||
return candidate.map { URL(fileURLWithPath: $0).standardizedFileURL }
|
||||
}
|
||||
|
||||
/// The folder name, used for recents and the Explorer header.
|
||||
public static func name(of root: URL) -> String {
|
||||
root.lastPathComponent.isEmpty ? root.path : root.lastPathComponent
|
||||
}
|
||||
|
||||
/// The window title: `APP_NAME` from the project's `.env`, else the folder name.
|
||||
public static func title(of root: URL) -> String {
|
||||
let env = try? String(contentsOf: root.appendingPathComponent(".env"), encoding: .utf8)
|
||||
return ProjectTitle.title(folderName: name(of: root), envContents: env)
|
||||
}
|
||||
|
||||
private static func isDirectory(_ path: String) -> Bool {
|
||||
var isDir: ObjCBool = false
|
||||
return FileManager.default.fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
public protocol RecentProjectsStoring: Sendable {
|
||||
/// Newest first; projects whose folder no longer exists are left out.
|
||||
func list() async -> [RecentProject]
|
||||
/// Moves a project folder to the front of the list.
|
||||
func add(_ path: String) async
|
||||
}
|
||||
|
||||
/// Machine-wide list in `~/Library/Application Support/Helder/recent-projects.json`.
|
||||
public struct RecentProjectsStore: RecentProjectsStoring {
|
||||
public let fileURL: URL
|
||||
|
||||
public init(fileURL: URL = RecentProjectsStore.defaultURL) {
|
||||
self.fileURL = fileURL
|
||||
}
|
||||
|
||||
public static var defaultURL: URL {
|
||||
let support = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
|
||||
?? URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library/Application Support")
|
||||
return support.appendingPathComponent("Helder/recent-projects.json")
|
||||
}
|
||||
|
||||
public func list() async -> [RecentProject] {
|
||||
guard let data = try? Data(contentsOf: fileURL),
|
||||
let list = try? JSONDecoder().decode([RecentProject].self, from: data) else { return [] }
|
||||
return Array(list.filter { FileManager.default.fileExists(atPath: $0.path) }.prefix(RecentLists.maxProjects))
|
||||
}
|
||||
|
||||
public func add(_ path: String) async {
|
||||
let abs = URL(fileURLWithPath: path).standardizedFileURL.path
|
||||
let entry = RecentProject(path: abs, name: PathUtils.basename(abs).isEmpty ? abs : PathUtils.basename(abs))
|
||||
let next = RecentLists.pushing(entry, onto: await list())
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
||||
try encoder.encode(next).write(to: fileURL, options: .atomic)
|
||||
} catch {
|
||||
Log.warn("recent", "cannot save recent projects: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public protocol RecentFilesStoring: Sendable {
|
||||
/// Recently opened files of this project, newest first.
|
||||
func load() async -> [String]
|
||||
func save(_ paths: [String]) async
|
||||
}
|
||||
|
||||
/// Per-project list in `.helder/recent.json`; drives the ⌘↑/⌘↓ recent-files navigator.
|
||||
public struct RecentFilesStore: RecentFilesStoring {
|
||||
public let fileURL: URL
|
||||
|
||||
public init(root: URL) {
|
||||
fileURL = root.appendingPathComponent(HelderConfig.folderName).appendingPathComponent(RecentLists.recentFilesName)
|
||||
}
|
||||
|
||||
public func load() async -> [String] {
|
||||
guard let data = try? Data(contentsOf: fileURL),
|
||||
let list = try? JSONDecoder().decode([String].self, from: data) else { return [] }
|
||||
return Array(list.prefix(RecentLists.maxFiles))
|
||||
}
|
||||
|
||||
public func save(_ paths: [String]) async {
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
||||
try encoder.encode(Array(paths.prefix(RecentLists.maxFiles))).write(to: fileURL, options: .atomic)
|
||||
} catch {
|
||||
Log.warn("recent", "cannot save recent files: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
public protocol SessionStoring: Sendable {
|
||||
func loadSession(root: String) -> SessionState?
|
||||
func saveSession(_ session: SessionState, root: String)
|
||||
func loadLayout() -> LayoutState
|
||||
func saveLayout(_ layout: LayoutState)
|
||||
}
|
||||
|
||||
/// Never throws: storage that cannot be read gives the defaults, one that cannot be written is skipped.
|
||||
public struct SessionStore: SessionStoring, @unchecked Sendable {
|
||||
private let defaults: UserDefaults
|
||||
|
||||
public init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
}
|
||||
|
||||
public func loadSession(root: String) -> SessionState? {
|
||||
defaults.data(forKey: SessionState.key(root: root)).flatMap { try? JSONDecoder().decode(SessionState.self, from: $0) }
|
||||
}
|
||||
|
||||
public func saveSession(_ session: SessionState, root: String) {
|
||||
if let data = try? JSONEncoder().encode(session) { defaults.set(data, forKey: SessionState.key(root: root)) }
|
||||
}
|
||||
|
||||
public func loadLayout() -> LayoutState {
|
||||
guard defaults.object(forKey: LayoutState.key) != nil else { return LayoutState() }
|
||||
return LayoutState(agentFraction: defaults.double(forKey: LayoutState.key))
|
||||
}
|
||||
|
||||
public func saveLayout(_ layout: LayoutState) {
|
||||
defaults.set(layout.agentFraction, forKey: LayoutState.key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user