Factor out a method

This commit is contained in:
Sami Samhuri 2019-12-01 21:28:52 -08:00
parent 0091566f00
commit cc1c97b4a1

View file

@ -53,25 +53,27 @@ public final class Generator {
// Make sure this path exists so we can write to it.
try fileManager.createDirectory(at: targetURL, withIntermediateDirectories: true, attributes: nil)
// Handle the file, transforming it if necessary.
// Processes the file, transforming it if necessary.
try renderOrCopyFile(url: fileURL, targetDir: targetURL)
}
}
func renderOrCopyFile(url fileURL: URL, targetDir: URL) throws {
let filename = fileURL.lastPathComponent
let ext = filename.split(separator: ".").last!
switch ext {
case "less":
let cssURL = targetURL.appendingPathComponent(filename.replacingOccurrences(of: ".less", with: ".css"))
let cssURL = targetDir.appendingPathComponent(filename.replacingOccurrences(of: ".less", with: ".css"))
try renderLess(from: fileURL, to: cssURL)
case "md":
let htmlURL = targetURL.appendingPathComponent(filename.replacingOccurrences(of: ".md", with: ".html"))
let htmlURL = targetDir.appendingPathComponent(filename.replacingOccurrences(of: ".md", with: ".html"))
try renderMarkdown(from: fileURL, to: htmlURL)
default:
// Who knows. Copy the file unchanged.
let src = URL(fileURLWithPath: path).appendingPathComponent(filename)
let dest = targetURL.appendingPathComponent(filename)
try fileManager.copyItem(at: src, to: dest)
}
let dest = targetDir.appendingPathComponent(filename)
try fileManager.copyItem(at: fileURL, to: dest)
}
}