CHGenerator/WebsiteGenerator/Content/Metadata.swift

30 lines
656 B
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
protocol Metadata: Codable {
static var fileName: String { get }
static var initial: Self { get }
}
extension Metadata {
static func url(in folder: URL) -> URL {
folder.appendingPathComponent(fileName)
}
static func exists(in folder: URL) -> Bool {
url(in: folder).exists
}
init?(in folder: URL) throws {
let metadataUrl = Self.url(in: folder)
guard metadataUrl.exists else {
try Self.initial.writeJSON(to: metadataUrl)
print("Created metadata in \(folder)")
return nil
}
try self.init(decodeFrom: metadataUrl)
}
}