158 lines
6.5 KiB
Swift
158 lines
6.5 KiB
Swift
// LyrebirdPaths.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// Filesystem-location conveniences for Lyrebird projects.
|
|
///
|
|
/// A small namespace of URLs you commonly need when writing render output,
|
|
/// presets, recordings, or caches — without hand-rolling `FileManager`
|
|
/// boilerplate at every call site.
|
|
///
|
|
/// Two groups:
|
|
///
|
|
/// 1. **Source-anchored** — ``projectRoot(from:marker:)``. Resolves the package
|
|
/// directory by walking up from the *calling* source file at compile time.
|
|
/// Dev-time only: the returned path is the location of your source on the
|
|
/// machine that **built** the binary. It will not exist on another user's
|
|
/// machine, so do not use it for shipped apps writing user data — use
|
|
/// ``documents`` (or ``applicationSupport``) there.
|
|
///
|
|
/// 2. **Sandbox-standard** — ``documents`` / ``applicationSupport`` / ``caches``
|
|
/// / ``temporary`` / ``home`` / ``currentWorkingDirectory`` (and macOS-only
|
|
/// ``desktop``). These resolve at runtime via `FileManager` and are correct
|
|
/// on both macOS and iOS, including sandboxed apps.
|
|
///
|
|
/// All members are non-RT — never call from a render callback.
|
|
public enum LyrebirdPaths {
|
|
|
|
// MARK: Source-anchored
|
|
|
|
/// The project root, found by walking up from the **calling** source file
|
|
/// until a directory containing `marker` is reached.
|
|
///
|
|
/// `#filePath` is a magic literal: as a *default argument* it expands at the
|
|
/// call site, so `LyrebirdPaths.projectRoot()` captures the caller's source
|
|
/// path, not this file's. Walking up to a marker (default `Package.swift`)
|
|
/// survives moving the source file to any depth under the project.
|
|
///
|
|
/// - Important: Dev-time only. The path reflects where the binary was built;
|
|
/// it will not exist on another machine. For shipped apps writing user
|
|
/// data, use ``documents`` instead.
|
|
///
|
|
/// - Parameters:
|
|
/// - file: The calling source file. Leave at the `#filePath` default.
|
|
/// - marker: What marks the project root. Either an exact filename
|
|
/// (default `"Package.swift"`) or — when it begins with a dot — a path
|
|
/// extension matched against directory contents, so `".xcodeproj"`
|
|
/// matches `MyApp.xcodeproj` (use for an Xcode-project layout with no
|
|
/// `Package.swift`).
|
|
/// - Returns: The project-root URL, or `nil` if no `marker` is found up to
|
|
/// the filesystem root.
|
|
public static func projectRoot(
|
|
from file: String = #filePath,
|
|
marker: String = "Package.swift"
|
|
) -> URL? {
|
|
var dir: URL = URL(fileURLWithPath: file).deletingLastPathComponent()
|
|
while !directory(dir, contains: marker) {
|
|
let parent: URL = dir.deletingLastPathComponent()
|
|
if parent.path == dir.path { return nil } // reached "/" — not found
|
|
dir = parent
|
|
}
|
|
return dir
|
|
}
|
|
|
|
/// Whether `dir` directly contains `marker` — by exact filename, or, when
|
|
/// `marker` begins with a dot, by any entry with that path extension
|
|
/// (so `".xcodeproj"` matches `MyApp.xcodeproj`).
|
|
private static func directory(_ dir: URL, contains marker: String) -> Bool {
|
|
let fm: FileManager = FileManager.default
|
|
if fm.fileExists(atPath: dir.appendingPathComponent(marker).path) {
|
|
return true
|
|
}
|
|
guard marker.hasPrefix(".") else { return false }
|
|
let ext: String = String(marker.dropFirst())
|
|
let entries: [String] = (try? fm.contentsOfDirectory(atPath: dir.path)) ?? []
|
|
return entries.contains { (entry: String) in
|
|
(entry as NSString).pathExtension == ext
|
|
}
|
|
}
|
|
|
|
// MARK: Sandbox-standard
|
|
|
|
/// The user Documents directory (`.documentDirectory`).
|
|
///
|
|
/// The canonical writable, user-visible location on iOS; `~/Documents` on
|
|
/// macOS. Falls back to ``temporary`` in the unlikely event the lookup fails.
|
|
public static var documents: URL {
|
|
searchPath(.documentDirectory)
|
|
}
|
|
|
|
/// The Application Support directory (`.applicationSupportDirectory`).
|
|
///
|
|
/// For app-managed data not meant to be user-visible. Falls back to
|
|
/// ``temporary`` if the lookup fails.
|
|
public static var applicationSupport: URL {
|
|
searchPath(.applicationSupportDirectory)
|
|
}
|
|
|
|
/// The Caches directory (`.cachesDirectory`).
|
|
///
|
|
/// For regenerable data the system may purge. Falls back to ``temporary``
|
|
/// if the lookup fails.
|
|
public static var caches: URL {
|
|
searchPath(.cachesDirectory)
|
|
}
|
|
|
|
/// The per-process temporary directory.
|
|
public static var temporary: URL {
|
|
FileManager.default.temporaryDirectory
|
|
}
|
|
|
|
/// The user's home directory.
|
|
///
|
|
/// On macOS this is the real login home (`/Users/<name>`). On iOS (and other
|
|
/// sandboxed platforms) there is no user-writable home — `homeDirectoryForCurrentUser`
|
|
/// returns the app's sandbox container root, which is not a usable write target — so
|
|
/// this returns the app's Documents directory (``documents``) instead, the canonical
|
|
/// writable, user-visible location there.
|
|
public static var home: URL {
|
|
#if os(macOS)
|
|
FileManager.default.homeDirectoryForCurrentUser
|
|
#else
|
|
searchPath(.documentDirectory)
|
|
#endif
|
|
}
|
|
|
|
/// The process current working directory.
|
|
///
|
|
/// Equals the project root when launched via `lyrebird run` / `swift run`,
|
|
/// but is `DerivedData` under Xcode and arbitrary for a bare binary — prefer
|
|
/// ``projectRoot(from:marker:)`` for dev output.
|
|
public static var currentWorkingDirectory: URL {
|
|
URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
|
|
}
|
|
|
|
#if os(macOS)
|
|
/// The user Desktop directory (`.desktopDirectory`). macOS only.
|
|
///
|
|
/// - Returns: The Desktop URL, or `nil` if it cannot be resolved (e.g. a
|
|
/// sandboxed app without the corresponding entitlement).
|
|
public static var desktop: URL? {
|
|
FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first
|
|
}
|
|
#endif
|
|
|
|
// MARK: Private
|
|
|
|
/// First URL for `directory` in the user domain, or ``temporary`` as a
|
|
/// last-resort fallback (the standard sandbox dirs always resolve).
|
|
private static func searchPath(_ directory: FileManager.SearchPathDirectory) -> URL {
|
|
let urls: [URL] = FileManager.default.urls(for: directory, in: .userDomainMask)
|
|
return urls.first ?? FileManager.default.temporaryDirectory
|
|
}
|
|
}
|