import Foundation import ArgumentParser let log = ValidationLog() var files: FileSystem! @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 guard let siteRoot = try loadSiteData(in: configuration.contentDirectory) else { return } let siteGenerator = try SiteGenerator() siteGenerator.generate(site: siteRoot) files.printGeneratedPages() files.printEmptyPages() files.printDraftPages() files.createImages() files.copyRequiredFiles() files.printExternalFiles() files.writeDetectedFileChangesToDisk() }