Files
swiftpm-mustache/Sources/Mustache/Library+FileSystem.swift
T. R. Bernstein 6061715025
Some checks failed
CI / macOS (push) Has been cancelled
CI / linux (swift:6.0) (push) Has been cancelled
CI / linux (swift:6.1) (push) Has been cancelled
CI / linux (swift:6.2) (push) Has been cancelled
CI / windows (6.1) (push) Has been cancelled
Adapt project for Astzweig
2025-09-29 15:03:48 +02:00

32 lines
1.3 KiB
Swift

import Foundation
extension MustacheLibrary {
/// Load templates from a folder
static func loadTemplates(from directory: String, withExtension extension: String = "mustache") throws -> [String: MustacheTemplate] {
var directory = directory
if !directory.hasSuffix("/") {
directory += "/"
}
let extWithDot = ".\(`extension`)"
let fs = FileManager()
guard let enumerator = fs.enumerator(atPath: directory) else { return [:] }
var templates: [String: MustacheTemplate] = [:]
for case let path as String in enumerator {
guard path.hasSuffix(extWithDot) else { continue }
do {
guard let template = try MustacheTemplate(filename: directory + path) else { continue }
// drop ".mustache" from path to get name
let name = String(path.dropLast(extWithDot.count))
#if os(Windows)
templates[name.replacingOccurrences(of: "\\", with: "/")] = template
#else
templates[name] = template
#endif
} catch let error as MustacheTemplate.ParserError {
throw ParserError(filename: path, context: error.context, error: error.error)
}
}
return templates
}
}