Use argument parser

This commit is contained in:
Christoph Hagen 2022-09-09 13:29:31 +02:00
parent 2a9061c1d6
commit 9e6ee2c499
3 changed files with 45 additions and 50 deletions

View File

@ -12,6 +12,7 @@ let package = Package(
dependencies: [ dependencies: [
.package(url: "https://github.com/johnsundell/ink.git", from: "0.5.0"), .package(url: "https://github.com/johnsundell/ink.git", from: "0.5.0"),
.package(url: "https://github.com/JohnSundell/Splash", from: "0.16.0"), .package(url: "https://github.com/JohnSundell/Splash", from: "0.16.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
], ],
targets: [ targets: [
.executableTarget( .executableTarget(
@ -19,6 +20,7 @@ let package = Package(
dependencies: [ dependencies: [
.product(name: "Ink", package: "ink"), .product(name: "Ink", package: "ink"),
.product(name: "Splash", package: "Splash"), .product(name: "Splash", package: "Splash"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]), ]),
] ]
) )

View File

@ -1,50 +0,0 @@
import Foundation
#warning("TODO: Add markdown box command: ![box](title;body)")
#warning("TODO: Add pretty link to other page in page content: ![page](page_id)")
#warning("TODO: Improve display of processed image list and warnings")
let args = CommandLine.arguments
guard args.count == 2 else {
print("Invalid argument list")
print("Usage: generator config-path")
exit(1)
}
let configuration: Configuration
do {
let configUrl = URL(fileURLWithPath: args[1])
let data = try Data(contentsOf: configUrl)
configuration = try JSONDecoder().decode(from: data)
} catch {
print("Failed to read configuration: \(error)")
exit(1)
}
let log = ValidationLog()
let files = FileSystem(
in: configuration.contentDirectory,
to: configuration.outputDirectory)
guard let siteData = Element(atRoot: configuration.contentDirectory) else {
exit(0)
}
do {
let siteGenerator = try SiteGenerator()
siteGenerator.generate(site: siteData)
} catch {
print("Failed to generate website: \(error)")
exit(2)
}
files.printGeneratedPages()
files.printEmptyPages()
files.printDraftPages()
files.createImages()
print("Images generated")
files.copyRequiredFiles()
files.printExternalFiles()
files.writeHashes()

View File

@ -0,0 +1,43 @@
import Foundation
import ArgumentParser
var configuration: Configuration!
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 generate(configPath: String) throws {
let configUrl = URL(fileURLWithPath: configPath)
let data = try Data(contentsOf: configUrl)
configuration = try JSONDecoder().decode(from: data)
files = .init(
in: configuration.contentDirectory,
to: configuration.outputDirectory)
guard let siteData = Element(atRoot: configuration.contentDirectory) else {
return
}
let siteGenerator = try SiteGenerator()
siteGenerator.generate(site: siteData)
files.printGeneratedPages()
files.printEmptyPages()
files.printDraftPages()
files.createImages()
print("Images generated")
files.copyRequiredFiles()
files.printExternalFiles()
files.writeHashes()
}