// TemplateInstaller.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation /// Installs the bundled Lyrebird Xcode file templates into the user template /// directory so Xcode's New File dialog (⌘N) discovers them. /// /// The templates ship as a copied SPM resource on the `LyrebirdCLI` target — /// `Bundle.module.resourceURL.appendingPathComponent("XcodeTemplates")` resolves /// to the directory holding every `.xctemplate` bundle. /// /// User template destination: /// `~/Library/Developer/Xcode/Templates/File Templates/Lyrebird/` /// /// Apple guarantees this path is the user-scoped template scan root used by /// every recent Xcode version. No Xcode restart is required — the dialog /// rescans on next open. public enum TemplateInstaller { /// Errors thrown by `install` / `uninstall`. public enum Error: Swift.Error, Equatable { /// Resource bundle did not contain the expected `XcodeTemplates` directory. /// Indicates a build configuration problem, not a user error. case bundleMissingTemplates(expectedPath: String) /// Destination exists already and `force` was `false`. case destinationExists(path: String) /// Underlying `FileManager` error wrapped with the path that failed. case fileSystemError(path: String, reason: String) } /// Subdirectory name inside the LyrebirdCLI resource bundle. static let resourceSubdir: String = "XcodeTemplates" /// Last path component under the user's Xcode template dir. The "File Templates" /// parent is fixed by Xcode; only this trailing segment is owned by Lyrebird and /// can therefore be safely removed by `uninstall`. public static let installDirName: String = "Lyrebird" /// `~/Library/Developer/Xcode/Templates/File Templates/Lyrebird/` public static var destinationURL: URL { FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent("Library/Developer/Xcode/Templates/File Templates/\(installDirName)", isDirectory: true) } /// Resolves `Bundle.module`'s `XcodeTemplates` resource subdir, or throws if missing. static func sourceURL() throws -> URL { guard let resourceURL: URL = Bundle.module.resourceURL else { throw Error.bundleMissingTemplates(expectedPath: "") } let candidate: URL = resourceURL.appendingPathComponent(resourceSubdir, isDirectory: true) guard FileManager.default.fileExists(atPath: candidate.path) else { throw Error.bundleMissingTemplates(expectedPath: candidate.path) } return candidate } /// Lists the `.xctemplate` bundle names inside the source resource directory. public static func availableTemplates() throws -> [String] { let src: URL = try sourceURL() let contents: [String] = try FileManager.default.contentsOfDirectory(atPath: src.path) return contents.filter { $0.hasSuffix(".xctemplate") }.sorted() } /// Copies every `.xctemplate` from the bundle to `destinationURL`. /// /// - Parameter force: When `true`, overwrites an existing destination directory. /// When `false`, throws `Error.destinationExists` if the destination already /// exists. Per-template overwrite is unconditional once we've decided to write /// — `force` is the top-level guard. /// - Returns: Number of templates copied. @discardableResult public static func install(force: Bool = false) throws -> Int { let src: URL = try sourceURL() let dest: URL = destinationURL let fm: FileManager = FileManager.default if fm.fileExists(atPath: dest.path) { if !force { throw Error.destinationExists(path: dest.path) } do { try fm.removeItem(at: dest) } catch { throw Error.fileSystemError(path: dest.path, reason: error.localizedDescription) } } // Recreate the destination dir. do { try fm.createDirectory(at: dest, withIntermediateDirectories: true) } catch { throw Error.fileSystemError(path: dest.path, reason: error.localizedDescription) } // Copy each .xctemplate bundle. let templates: [String] = try availableTemplates() for name: String in templates { let from: URL = src.appendingPathComponent(name, isDirectory: true) let to: URL = dest.appendingPathComponent(name, isDirectory: true) do { try fm.copyItem(at: from, to: to) } catch { throw Error.fileSystemError(path: to.path, reason: error.localizedDescription) } } return templates.count } /// Removes the entire `~/Library/.../File Templates/Lyrebird/` directory. /// /// - Returns: `true` if the directory existed and was removed; `false` if it /// was not installed. @discardableResult public static func uninstall() throws -> Bool { let dest: URL = destinationURL let fm: FileManager = FileManager.default guard fm.fileExists(atPath: dest.path) else { return false } do { try fm.removeItem(at: dest) } catch { throw Error.fileSystemError(path: dest.path, reason: error.localizedDescription) } return true } }