CHGenerator/Sources/Generator/run.swift
2022-12-01 15:19:17 +01:00

76 lines
2.1 KiB
Swift

import Foundation
import ArgumentParser
let log = ValidationLog()
var files: FileSystem!
var siteRoot: Element!
@main
struct CHGenerator: ParsableCommand {
@Argument(help: "The path to the generator configuration file")
var configPath: String
mutating func run() throws {
try generate(configPath: configPath)
}
}
private func loadSiteData(in folder: URL) throws -> Element? {
let log = MetadataInfoLogger(input: folder)
print("--- SOURCE FILES -----------------------------------")
let root = Element(atRoot: folder, log: log)
print(" ")
log.printMetadataScanOverview()
print(" ")
try log.writeResultsToFile(in: files.generatorInfoFolder)
return root
}
private func loadConfiguration(at configPath: String) -> Configuration? {
print("--- CONFIGURATION ----------------------------------")
print("")
print(" Configuration file: \(configPath)")
let configUrl = URL(fileURLWithPath: configPath)
let config: Configuration
do {
let data = try Data(contentsOf: configUrl)
config = try JSONDecoder().decode(from: data)
} catch {
print(" Configuration error: \(error)")
return nil
}
config.printOverview()
print(" ")
return config
}
private func generate(configPath: String) throws {
guard let configuration = loadConfiguration(at: configPath) else {
return
}
files = .init(
in: configuration.contentDirectory,
to: configuration.outputDirectory,
configuration: configuration)
// 2. Scan site elements
siteRoot = try loadSiteData(in: configuration.contentDirectory)
guard siteRoot != nil else {
return
}
let siteGenerator = try SiteGenerator()
siteGenerator.generate(site: siteRoot)
files.printGeneratedPages()
files.printEmptyPages()
files.printDraftPages()
files.createImages()
files.copyRequiredFiles()
files.printExternalFiles()
files.writeDetectedFileChangesToDisk()
}