Read config path from command line

This commit is contained in:
Christoph Hagen 2022-09-09 11:00:12 +02:00
parent 9965e1a3ff
commit 64db75fb44
3 changed files with 16 additions and 2 deletions

View File

@ -50,6 +50,12 @@
ReferencedContainer = "container:WebsiteGenerator.xcodeproj"> ReferencedContainer = "container:WebsiteGenerator.xcodeproj">
</BuildableReference> </BuildableReference>
</BuildableProductRunnable> </BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "/Users/ch/Projects/MakerSpace/CHGenerator/config.json"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction> </LaunchAction>
<ProfileAction <ProfileAction
buildConfiguration = "Release" buildConfiguration = "Release"

View File

@ -59,7 +59,8 @@ struct PageGenerator {
} }
private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool, isEmpty: Bool) { private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool, isEmpty: Bool) {
if let raw = files.contentOfOptionalFile(atPath: path, source: page.path, createEmptyFileIfMissing: true)? let create = configuration.createMdFilesIfMissing
if let raw = files.contentOfOptionalFile(atPath: path, source: page.path, createEmptyFileIfMissing: create)?
.trimmed.nonEmpty { .trimmed.nonEmpty {
let (content, includesCode) = PageContentGenerator(factory: factory.factory) let (content, includesCode) = PageContentGenerator(factory: factory.factory)
.generate(page: page, language: language, content: raw) .generate(page: page, language: language, content: raw)

View File

@ -1,10 +1,17 @@
import Foundation import Foundation
let configUrl = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/CHGenerator/config.json") let args = CommandLine.arguments
guard args.count == 2 else {
print("Invalid argument list")
print("Usage: generator config-path")
exit(1)
}
let configuration: Configuration let configuration: Configuration
do { do {
let configUrl = URL(fileURLWithPath: args[1])
let data = try Data(contentsOf: configUrl) let data = try Data(contentsOf: configUrl)
configuration = try JSONDecoder().decode(from: data) configuration = try JSONDecoder().decode(from: data)
} catch { } catch {