Remove global configuration and improve printing

This commit is contained in:
Christoph Hagen 2022-12-01 15:19:17 +01:00
parent 58eae51d40
commit 94375f3a81
5 changed files with 48 additions and 12 deletions

View File

@ -56,4 +56,13 @@ struct Configuration: Codable {
var outputDirectory: URL {
.init(fileURLWithPath: outputPath)
}
func printOverview() {
print(" Source folder: \(contentDirectory.path)")
print(" Output folder: \(outputDirectory.path)")
print(" Page width: \(pageImageWidth)")
print(" Minify JavaScript: \(minifyCSSandJS)")
print(" Minify CSS: \(minifyCSSandJS)")
print(" Create markdown files: \(createMdFilesIfMissing)")
}
}

View File

@ -14,6 +14,8 @@ final class FileSystem {
private let images: ImageGenerator
private let configuration: Configuration
private var tempFile: URL {
input.appendingPathComponent(FileSystem.tempFileName)
}
@ -68,11 +70,12 @@ final class FileSystem {
*/
private var generatedPages: Set<String> = []
init(in input: URL, to output: URL) {
init(in input: URL, to output: URL, configuration: Configuration) {
self.input = input
self.output = output
self.images = .init(input: input, output: output)
self.generatorInfoFolder = input.appendingPathComponent("run")
self.configuration = configuration
}
func urlInOutputFolder(_ path: String) -> URL {
@ -102,6 +105,10 @@ final class FileSystem {
}
}
func contentOfMdFile(atPath path: String, source: String) -> String? {
contentOfOptionalFile(atPath: path, source: source, createEmptyFileIfMissing: configuration.createMdFilesIfMissing)
}
func contentOfOptionalFile(atPath path: String, source: String, createEmptyFileIfMissing: Bool = false) -> String? {
let url = input.appendingPathComponent(path)
guard exists(url) else {
@ -148,6 +155,10 @@ final class FileSystem {
images.requireMultiVersionImage(source: source, destination: destination, requiredBy: path, width: width, desiredHeight: desiredHeight)
}
func requireFullSizeMultiVersionImage(source: String, destination: String, requiredBy path: String) -> NSSize {
images.requireMultiVersionImage(source: source, destination: destination, requiredBy: path, width: configuration.pageImageWidth, desiredHeight: nil)
}
func createImages() {
images.createImages()
}

View File

@ -120,11 +120,10 @@ struct PageContentGenerator {
private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String {
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
let size = files.requireMultiVersionImage(
let size = files.requireFullSizeMultiVersionImage(
source: imagePath,
destination: imagePath,
requiredBy: page.path,
width: configuration.pageImageWidth)
requiredBy: page.path)
let content: [PageImageTemplate.Key : String] = [
.image: file.dropAfterLast("."),

View File

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

View File

@ -1,7 +1,6 @@
import Foundation
import ArgumentParser
var configuration: Configuration!
let log = ValidationLog()
var files: FileSystem!
var siteRoot: Element!
@ -28,14 +27,34 @@ private func loadSiteData(in folder: URL) throws -> Element? {
return root
}
private func generate(configPath: String) throws {
private func loadConfiguration(at configPath: String) -> Configuration? {
print("--- CONFIGURATION ----------------------------------")
print("")
print(" Configuration file: \(configPath)")
let configUrl = URL(fileURLWithPath: configPath)
let data = try Data(contentsOf: configUrl)
configuration = try JSONDecoder().decode(from: data)
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)
to: configuration.outputDirectory,
configuration: configuration)
// 2. Scan site elements
siteRoot = try loadSiteData(in: configuration.contentDirectory)