Improve printing and image creation
This commit is contained in:
@ -1,443 +0,0 @@
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
import AppKit
|
||||
|
||||
final class FileSystem {
|
||||
|
||||
private static let tempFileName = "temp.bin"
|
||||
|
||||
private let input: URL
|
||||
|
||||
private let output: URL
|
||||
|
||||
private let source = "FileSystem"
|
||||
|
||||
private let images: ImageGenerator
|
||||
|
||||
private let configuration: Configuration
|
||||
|
||||
private var tempFile: URL {
|
||||
input.appendingPathComponent(FileSystem.tempFileName)
|
||||
}
|
||||
|
||||
let generatorInfoFolder: URL
|
||||
|
||||
/**
|
||||
All files which should be copied to the output folder
|
||||
*/
|
||||
private var requiredFiles: Set<String> = []
|
||||
|
||||
/**
|
||||
The files marked as external in element metadata.
|
||||
|
||||
Files included here are not generated, since they are assumed to be added separately.
|
||||
*/
|
||||
private var externalFiles: Set<String> = []
|
||||
|
||||
/**
|
||||
The files marked as expected, i.e. they exist after the generation is completed.
|
||||
|
||||
The key of the dictionary is the file path, the value is the file providing the link
|
||||
*/
|
||||
private var expectedFiles: [String : String] = [:]
|
||||
|
||||
/**
|
||||
All pages without content which have been created
|
||||
*/
|
||||
private var emptyPages: Set<String> = []
|
||||
|
||||
/**
|
||||
All pages which have `status` set to ``PageState.draft``
|
||||
*/
|
||||
private var draftPages: Set<String> = []
|
||||
|
||||
/**
|
||||
All paths to page element folders, indexed by their unique id.
|
||||
|
||||
This relation is used to generate relative links to pages using the ``Element.id`
|
||||
*/
|
||||
private var pagePaths: [String: String] = [:]
|
||||
|
||||
/**
|
||||
The image creation tasks.
|
||||
|
||||
The key is the destination path.
|
||||
*/
|
||||
private var imageTasks: [String : ImageOutput] = [:]
|
||||
|
||||
/**
|
||||
The paths to all pages which were changed
|
||||
*/
|
||||
private var generatedPages: Set<String> = []
|
||||
|
||||
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 {
|
||||
output.appendingPathComponent(path)
|
||||
}
|
||||
|
||||
func urlInContentFolder(_ path: String) -> URL {
|
||||
input.appendingPathComponent(path)
|
||||
}
|
||||
|
||||
private func exists(_ url: URL) -> Bool {
|
||||
FileManager.default.fileExists(atPath: url.path)
|
||||
}
|
||||
|
||||
func dataOfRequiredFile(atPath path: String, source: String) -> Data? {
|
||||
let url = input.appendingPathComponent(path)
|
||||
guard exists(url) else {
|
||||
log.failedToOpen(path, requiredBy: source, error: nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
return try Data(contentsOf: url)
|
||||
} catch {
|
||||
log.failedToOpen(path, requiredBy: source, error: error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if createEmptyFileIfMissing {
|
||||
try? Data().write(to: url)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
return try String(contentsOf: url)
|
||||
} catch {
|
||||
log.failedToOpen(path, requiredBy: source, error: error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func writeDetectedFileChangesToDisk() {
|
||||
images.writeDetectedFileChangesToDisk()
|
||||
}
|
||||
|
||||
// MARK: Images
|
||||
|
||||
@discardableResult
|
||||
func requireSingleImage(source: String, destination: String, requiredBy path: String, width: Int, desiredHeight: Int? = nil) -> NSSize {
|
||||
images.requireImage(
|
||||
at: destination,
|
||||
generatedFrom: source,
|
||||
requiredBy: path,
|
||||
quality: 0.7,
|
||||
width: width,
|
||||
height: desiredHeight,
|
||||
alwaysGenerate: false)
|
||||
}
|
||||
|
||||
/**
|
||||
Create images of different types.
|
||||
|
||||
This function generates versions for the given image, including png/jpg, avif, and webp. Different pixel density versions (1x and 2x) are also generated.
|
||||
- Parameter destination: The path to the destination file
|
||||
*/
|
||||
@discardableResult
|
||||
func requireMultiVersionImage(source: String, destination: String, requiredBy path: String, width: Int, desiredHeight: Int? = nil) -> NSSize {
|
||||
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()
|
||||
}
|
||||
|
||||
// MARK: File copying
|
||||
|
||||
/**
|
||||
Add a file as required, so that it will be copied to the output directory.
|
||||
*/
|
||||
func require(file: String) {
|
||||
let url = input.appendingPathComponent(file)
|
||||
guard url.exists, url.isDirectory else {
|
||||
requiredFiles.insert(file)
|
||||
return
|
||||
}
|
||||
do {
|
||||
try FileManager.default
|
||||
.contentsOfDirectory(atPath: url.path)
|
||||
.forEach {
|
||||
// Recurse into subfolders
|
||||
require(file: file + "/" + $0)
|
||||
}
|
||||
} catch {
|
||||
log.add(error: "Failed to read folder \(file): \(error)", source: source)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Mark a file as explicitly missing.
|
||||
|
||||
This is done for the `externalFiles` entries in metadata,
|
||||
to indicate that these files will be copied to the output folder manually.
|
||||
*/
|
||||
func exclude(file: String) {
|
||||
externalFiles.insert(file)
|
||||
}
|
||||
|
||||
/**
|
||||
Mark a file as expected to be present in the output folder after generation.
|
||||
|
||||
This is done for all links between pages, which only exist after the pages have been generated.
|
||||
*/
|
||||
func expect(file: String, source: String) {
|
||||
expectedFiles[file] = source
|
||||
}
|
||||
|
||||
func copyRequiredFiles() {
|
||||
var copiedFiles = Set<String>()
|
||||
for file in requiredFiles {
|
||||
let cleanPath = cleanRelativeURL(file)
|
||||
let sourceUrl = input.appendingPathComponent(cleanPath)
|
||||
let destinationUrl = output.appendingPathComponent(cleanPath)
|
||||
guard sourceUrl.exists else {
|
||||
if !isExternal(file: file) {
|
||||
log.add(error: "Missing required file", source: cleanPath)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if copyFileIfChanged(from: sourceUrl, to: destinationUrl) {
|
||||
copiedFiles.insert(file)
|
||||
}
|
||||
}
|
||||
try? tempFile.delete()
|
||||
for (file, source) in expectedFiles {
|
||||
guard !isExternal(file: file) else {
|
||||
continue
|
||||
}
|
||||
let cleanPath = cleanRelativeURL(file)
|
||||
let destinationUrl = output.appendingPathComponent(cleanPath)
|
||||
if !destinationUrl.exists {
|
||||
log.add(error: "Missing \(cleanPath)", source: source)
|
||||
}
|
||||
}
|
||||
guard !copiedFiles.isEmpty else {
|
||||
print("No required files copied")
|
||||
return
|
||||
}
|
||||
print("\(copiedFiles.count) required files copied:")
|
||||
for file in copiedFiles.sorted() {
|
||||
print(" " + file)
|
||||
}
|
||||
}
|
||||
|
||||
private func copyFileIfChanged(from sourceUrl: URL, to destinationUrl: URL) -> Bool {
|
||||
guard configuration.minifyCSSandJS else {
|
||||
return copyBinaryFileIfChanged(from: sourceUrl, to: destinationUrl)
|
||||
}
|
||||
switch sourceUrl.pathExtension.lowercased() {
|
||||
case "js":
|
||||
return minifyJS(at: sourceUrl, andWriteTo: destinationUrl)
|
||||
case "css":
|
||||
return minifyCSS(at: sourceUrl, andWriteTo: destinationUrl)
|
||||
default:
|
||||
return copyBinaryFileIfChanged(from: sourceUrl, to: destinationUrl)
|
||||
}
|
||||
}
|
||||
|
||||
private func copyBinaryFileIfChanged(from sourceUrl: URL, to destinationUrl: URL) -> Bool {
|
||||
do {
|
||||
let data = try Data(contentsOf: sourceUrl)
|
||||
return writeIfChanged(data, to: destinationUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to read data at \(sourceUrl.path)", source: source, error: error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func minifyJS(at sourceUrl: URL, andWriteTo destinationUrl: URL) -> Bool {
|
||||
let command = "uglifyjs \(sourceUrl.path) > \(tempFile.path)"
|
||||
do {
|
||||
_ = try FileSystem.safeShell(command)
|
||||
return copyBinaryFileIfChanged(from: tempFile, to: destinationUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to minify \(sourceUrl.path): \(error)", source: source)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func minifyCSS(at sourceUrl: URL, andWriteTo destinationUrl: URL) -> Bool {
|
||||
let command = "cleancss \(sourceUrl.path) -o \(tempFile.path)"
|
||||
do {
|
||||
_ = try FileSystem.safeShell(command)
|
||||
return copyBinaryFileIfChanged(from: tempFile, to: destinationUrl)
|
||||
} catch {
|
||||
log.add(error: "Failed to minify \(sourceUrl.path): \(error)", source: source)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func cleanRelativeURL(_ raw: String) -> String {
|
||||
let raw = raw.dropAfterLast("#") // Clean links to page content
|
||||
guard raw.contains("..") else {
|
||||
return raw
|
||||
}
|
||||
var result: [String] = []
|
||||
for component in raw.components(separatedBy: "/") {
|
||||
if component == ".." {
|
||||
_ = result.popLast()
|
||||
} else {
|
||||
result.append(component)
|
||||
}
|
||||
}
|
||||
return result.joined(separator: "/")
|
||||
}
|
||||
|
||||
/**
|
||||
Check if a file is marked as external.
|
||||
|
||||
Also checks for sub-paths of the file, e.g if the folder `docs` is marked as external,
|
||||
then files like `docs/index.html` are also found to be external.
|
||||
- Note: All paths are either relative to root (no leading slash) or absolute paths of the domain (leading slash)
|
||||
*/
|
||||
func isExternal(file: String) -> Bool {
|
||||
// Deconstruct file path
|
||||
var path = ""
|
||||
for part in file.components(separatedBy: "/") {
|
||||
guard part != "" else {
|
||||
continue
|
||||
}
|
||||
if path == "" {
|
||||
path = part
|
||||
} else {
|
||||
path += "/" + part
|
||||
}
|
||||
if externalFiles.contains(path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func printExternalFiles() {
|
||||
guard !externalFiles.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(externalFiles.count) external resources needed:")
|
||||
for file in externalFiles.sorted() {
|
||||
print(" " + file)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Pages
|
||||
|
||||
func isEmpty(page: String) {
|
||||
emptyPages.insert(page)
|
||||
}
|
||||
|
||||
func printEmptyPages() {
|
||||
guard !emptyPages.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(emptyPages.count) empty pages:")
|
||||
for page in emptyPages.sorted() {
|
||||
print(" " + page)
|
||||
}
|
||||
}
|
||||
|
||||
func isDraft(path: String) {
|
||||
draftPages.insert(path)
|
||||
}
|
||||
|
||||
func printDraftPages() {
|
||||
guard !draftPages.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(draftPages.count) drafts:")
|
||||
for page in draftPages.sorted() {
|
||||
print(" " + page)
|
||||
}
|
||||
}
|
||||
|
||||
func add(page: String, id: String) {
|
||||
if let existing = pagePaths[id] {
|
||||
log.add(error: "Conflicting id with \(existing)", source: page)
|
||||
}
|
||||
pagePaths[id] = page
|
||||
}
|
||||
|
||||
func getPage(for id: String) -> String? {
|
||||
pagePaths[id]
|
||||
}
|
||||
|
||||
func generated(page: String) {
|
||||
generatedPages.insert(page)
|
||||
}
|
||||
|
||||
func printGeneratedPages() {
|
||||
guard !generatedPages.isEmpty else {
|
||||
print("No pages modified")
|
||||
return
|
||||
}
|
||||
print("\(generatedPages.count) pages modified")
|
||||
for page in generatedPages.sorted() {
|
||||
print(" " + page)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Writing files
|
||||
|
||||
@discardableResult
|
||||
func writeIfChanged(_ data: Data, to url: URL) -> Bool {
|
||||
// Only write changed files
|
||||
if url.exists, let oldContent = try? Data(contentsOf: url), data == oldContent {
|
||||
return false
|
||||
}
|
||||
do {
|
||||
try data.createFolderAndWrite(to: url)
|
||||
return true
|
||||
} catch {
|
||||
log.add(error: "Failed to write file", source: url.path, error: error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func write(_ string: String, to url: URL) -> Bool {
|
||||
let data = string.data(using: .utf8)!
|
||||
return writeIfChanged(data, to: url)
|
||||
}
|
||||
|
||||
// MARK: Running other tasks
|
||||
|
||||
@discardableResult
|
||||
static func safeShell(_ command: String) throws -> String {
|
||||
let task = Process()
|
||||
let pipe = Pipe()
|
||||
|
||||
task.standardOutput = pipe
|
||||
task.standardError = pipe
|
||||
task.arguments = ["-cl", command]
|
||||
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
|
||||
task.standardInput = nil
|
||||
|
||||
try task.run()
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output = String(data: data, encoding: .utf8)!
|
||||
|
||||
return output
|
||||
}
|
||||
}
|
@ -3,13 +3,9 @@ import CryptoKit
|
||||
|
||||
final class FileUpdateChecker {
|
||||
|
||||
private static let hashesFileName = "hashes.json"
|
||||
private let hashesFileName = "hashes.json"
|
||||
|
||||
private let input: URL
|
||||
|
||||
private var hashesFile: URL {
|
||||
input.appendingPathComponent(FileUpdateChecker.hashesFileName)
|
||||
}
|
||||
|
||||
/**
|
||||
The hashes of all accessed files from the previous run
|
||||
@ -25,35 +21,41 @@ final class FileUpdateChecker {
|
||||
*/
|
||||
private var accessedFiles: [String : Data] = [:]
|
||||
|
||||
private var source: String {
|
||||
"FileUpdateChecker"
|
||||
var numberOfFilesLoaded: Int {
|
||||
previousFiles.count
|
||||
}
|
||||
|
||||
var numberOfFilesAccessed: Int {
|
||||
accessedFiles.count
|
||||
}
|
||||
|
||||
init(input: URL) {
|
||||
self.input = input
|
||||
guard hashesFile.exists else {
|
||||
log.add(info: "No file hashes loaded, regarding all content as new", source: source)
|
||||
return
|
||||
}
|
||||
|
||||
enum LoadResult {
|
||||
case notLoaded
|
||||
case loaded
|
||||
case failed(String)
|
||||
}
|
||||
|
||||
func loadPreviousRun(from folder: URL) -> LoadResult {
|
||||
let url = folder.appendingPathComponent(hashesFileName)
|
||||
guard url.exists else {
|
||||
return .notLoaded
|
||||
}
|
||||
let data: Data
|
||||
do {
|
||||
data = try Data(contentsOf: hashesFile)
|
||||
data = try Data(contentsOf: url)
|
||||
} catch {
|
||||
log.add(
|
||||
warning: "File hashes could not be read, regarding all content as new",
|
||||
source: source,
|
||||
error: error)
|
||||
return
|
||||
return .failed("Failed to read hashes from last run: \(error)")
|
||||
}
|
||||
do {
|
||||
self.previousFiles = try JSONDecoder().decode(from: data)
|
||||
} catch {
|
||||
log.add(
|
||||
warning: "File hashes could not be decoded, regarding all content as new",
|
||||
source: source,
|
||||
error: error)
|
||||
return
|
||||
return .failed("Failed to decode hashes from last run: \(error)")
|
||||
}
|
||||
return .loaded
|
||||
}
|
||||
|
||||
func fileHasChanged(at path: String) -> Bool {
|
||||
@ -73,16 +75,18 @@ final class FileUpdateChecker {
|
||||
accessedFiles[path] = SHA256.hash(data: data).data
|
||||
}
|
||||
|
||||
|
||||
func writeDetectedFileChangesToDisk() {
|
||||
func writeDetectedFileChanges(to folder: URL) -> String? {
|
||||
let url = folder.appendingPathComponent(hashesFileName)
|
||||
do {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
let data = try encoder.encode(accessedFiles)
|
||||
try data.write(to: hashesFile)
|
||||
try data.write(to: url)
|
||||
return nil
|
||||
} catch {
|
||||
log.add(warning: "Failed to save file hashes", source: source, error: error)
|
||||
return "Failed to save file hashes: \(error)"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var notFound = 0
|
||||
|
@ -1,456 +0,0 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
import CryptoKit
|
||||
import Darwin.C
|
||||
|
||||
private struct ImageJob {
|
||||
|
||||
let destination: String
|
||||
|
||||
let width: Int
|
||||
|
||||
let path: String
|
||||
|
||||
let quality: Float
|
||||
|
||||
let alwaysGenerate: Bool
|
||||
}
|
||||
|
||||
final class ImageGenerator {
|
||||
|
||||
private let imageOptimSupportedFileExtensions: Set<String> = ["jpg", "png", "svg"]
|
||||
|
||||
private let imageOptimizationBatchSize = 50
|
||||
|
||||
/**
|
||||
The path to the input folder.
|
||||
*/
|
||||
private let input: URL
|
||||
|
||||
/**
|
||||
The path to the output folder
|
||||
*/
|
||||
private let output: URL
|
||||
|
||||
/**
|
||||
The images to generate.
|
||||
|
||||
The key is the image source path relative to the input folder, and the values are the destination path (relative to the output folder) and the required image width.
|
||||
*/
|
||||
private var imageJobs: [String : [ImageJob]] = [:]
|
||||
|
||||
/**
|
||||
The images for which to generate multiple versions
|
||||
|
||||
The key is the source file, the value is the path of the requiring page.
|
||||
*/
|
||||
private var multiImageJobs: [String : String] = [:]
|
||||
|
||||
/**
|
||||
The images which could not be found, but are required for the site.
|
||||
|
||||
The key is the image path, and the value is the page that requires it.
|
||||
*/
|
||||
private var missingImages: [String : String] = [:]
|
||||
|
||||
/**
|
||||
All warnings produced for images during generation
|
||||
*/
|
||||
private var imageWarnings: Set<String> = []
|
||||
|
||||
/**
|
||||
All images required by the site.
|
||||
|
||||
The values are the destination paths of the images, relative to the output folder
|
||||
*/
|
||||
private var requiredImages: Set<String> = []
|
||||
|
||||
/**
|
||||
All images modified or created during this generator run.
|
||||
*/
|
||||
private var generatedImages: Set<String> = []
|
||||
|
||||
/**
|
||||
The images optimized by ImageOptim
|
||||
*/
|
||||
private var optimizedImages: Set<String> = []
|
||||
|
||||
/**
|
||||
A cache to get the size of source images, so that files don't have to be loaded multiple times.
|
||||
|
||||
The key is the absolute source path, and the value is the image size
|
||||
*/
|
||||
private var imageSizeCache: [String : NSSize] = [:]
|
||||
|
||||
private var fileUpdates: FileUpdateChecker
|
||||
|
||||
init(input: URL, output: URL) {
|
||||
self.fileUpdates = FileUpdateChecker(input: input)
|
||||
self.input = input
|
||||
self.output = output
|
||||
}
|
||||
|
||||
func writeDetectedFileChangesToDisk() {
|
||||
fileUpdates.writeDetectedFileChangesToDisk()
|
||||
}
|
||||
|
||||
private func getImageSize(atPath path: String) -> NSSize? {
|
||||
if let size = imageSizeCache[path] {
|
||||
return size
|
||||
}
|
||||
guard let image = getImage(atPath: path) else {
|
||||
return nil
|
||||
}
|
||||
let size = image.size
|
||||
imageSizeCache[path] = size
|
||||
return size
|
||||
}
|
||||
|
||||
private func getImage(atPath path: String) -> NSImage? {
|
||||
guard let data = getData(atPath: path) else {
|
||||
log.add(error: "Failed to load file", source: path)
|
||||
return nil
|
||||
}
|
||||
guard let image = NSImage(data: data) else {
|
||||
log.add(error: "Failed to read image", source: path)
|
||||
return nil
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
private func getData(atPath path: String) -> Data? {
|
||||
let url = input.appendingPathComponent(path)
|
||||
guard url.exists else {
|
||||
return nil
|
||||
}
|
||||
do {
|
||||
let data = try Data(contentsOf: url)
|
||||
fileUpdates.didLoad(data, at: path)
|
||||
return data
|
||||
} catch {
|
||||
log.add(error: "Failed to read data", source: path, error: error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func requireImage(at destination: String, generatedFrom source: String, requiredBy path: String, quality: Float, width: Int, height: Int?, alwaysGenerate: Bool) -> NSSize {
|
||||
requiredImages.insert(destination)
|
||||
let height = height.unwrapped(CGFloat.init)
|
||||
let sourceUrl = input.appendingPathComponent(source)
|
||||
guard sourceUrl.exists else {
|
||||
missingImages[source] = path
|
||||
return .zero
|
||||
}
|
||||
|
||||
guard let imageSize = getImageSize(atPath: source) else {
|
||||
missingImages[source] = path
|
||||
return .zero
|
||||
}
|
||||
let scaledSize = imageSize.scaledDown(to: CGFloat(width))
|
||||
|
||||
// Check desired height, then we can forget about it
|
||||
if let height = height {
|
||||
let expectedHeight = scaledSize.width / CGFloat(width) * height
|
||||
if abs(expectedHeight - scaledSize.height) > 2 {
|
||||
addWarning("Invalid height (\(scaledSize.height) instead of \(expectedHeight))", destination: destination, path: path)
|
||||
}
|
||||
}
|
||||
|
||||
let job = ImageJob(
|
||||
destination: destination,
|
||||
width: width,
|
||||
path: path,
|
||||
quality: quality,
|
||||
alwaysGenerate: alwaysGenerate)
|
||||
insert(job: job, source: source)
|
||||
|
||||
return scaledSize
|
||||
}
|
||||
|
||||
private func insert(job: ImageJob, source: String) {
|
||||
guard let existingSource = imageJobs[source] else {
|
||||
imageJobs[source] = [job]
|
||||
return
|
||||
}
|
||||
|
||||
guard let existingJob = existingSource.first(where: { $0.destination == job.destination }) else {
|
||||
imageJobs[source] = existingSource + [job]
|
||||
return
|
||||
}
|
||||
|
||||
if existingJob.width != job.width {
|
||||
addWarning("Multiple image widths (\(existingJob.width) and \(job.width))", destination: job.destination, path: "\(existingJob.path) and \(job.path)")
|
||||
}
|
||||
}
|
||||
|
||||
func createImages() {
|
||||
var count = 0
|
||||
for (source, jobs) in imageJobs.sorted(by: { $0.key < $1.key }) {
|
||||
print(String(format: "Creating images: %4d / %d\r", count, imageJobs.count), terminator: "")
|
||||
fflush(stdout)
|
||||
create(images: jobs, from: source)
|
||||
count += 1
|
||||
}
|
||||
print(" \r", terminator: "")
|
||||
createMultiImages()
|
||||
optimizeImages()
|
||||
printMissingImages()
|
||||
printImageWarnings()
|
||||
printGeneratedImages()
|
||||
printTotalImageCount()
|
||||
}
|
||||
|
||||
private func printMissingImages() {
|
||||
guard !missingImages.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(missingImages.count) missing images:")
|
||||
let sort = missingImages.sorted { (a, b) in
|
||||
a.value < b.value && a.key < b.key
|
||||
}
|
||||
for (source, path) in sort {
|
||||
print(" \(source) (required by \(path))")
|
||||
}
|
||||
}
|
||||
|
||||
private func printImageWarnings() {
|
||||
guard !imageWarnings.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(imageWarnings.count) image warnings:")
|
||||
for imageWarning in imageWarnings {
|
||||
print(imageWarning)
|
||||
}
|
||||
}
|
||||
|
||||
private func printGeneratedImages() {
|
||||
guard !generatedImages.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(generatedImages.count) images generated:")
|
||||
for image in generatedImages {
|
||||
print(" " + image)
|
||||
}
|
||||
}
|
||||
|
||||
private func printTotalImageCount() {
|
||||
print("\(requiredImages.count) images")
|
||||
}
|
||||
|
||||
private func addWarning(_ message: String, destination: String, path: String) {
|
||||
let warning = " \(destination): \(message) required by \(path)"
|
||||
imageWarnings.insert(warning)
|
||||
}
|
||||
|
||||
private func addWarning(_ message: String, job: ImageJob) {
|
||||
addWarning(message, destination: job.destination, path: job.path)
|
||||
}
|
||||
|
||||
private func isMissing(_ job: ImageJob) -> Bool {
|
||||
job.alwaysGenerate || !output.appendingPathComponent(job.destination).exists
|
||||
}
|
||||
|
||||
private func create(images: [ImageJob], from source: String) {
|
||||
// Only load image if required
|
||||
let imageHasChanged = fileUpdates.fileHasChanged(at: source)
|
||||
guard imageHasChanged || images.contains(where: isMissing) else {
|
||||
return
|
||||
}
|
||||
|
||||
guard let image = getImage(atPath: source) else {
|
||||
missingImages[source] = images.first?.path
|
||||
return
|
||||
}
|
||||
let jobs = imageHasChanged ? images : images.filter(isMissing)
|
||||
// Update all images
|
||||
jobs.forEach { job in
|
||||
// Prevent memory overflow due to repeated NSImage operations
|
||||
autoreleasepool {
|
||||
create(job: job, from: image, source: source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func create(job: ImageJob, from image: NSImage, source: String) {
|
||||
let destinationUrl = output.appendingPathComponent(job.destination)
|
||||
create(job: job, from: image, source: source, at: destinationUrl)
|
||||
}
|
||||
|
||||
private func create(job: ImageJob, from image: NSImage, source: String, at destinationUrl: URL) {
|
||||
|
||||
// Ensure that image file is supported
|
||||
let ext = destinationUrl.pathExtension.lowercased()
|
||||
guard ImageType(fileExtension: ext) != nil else {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
let destinationExtension = destinationUrl.pathExtension.lowercased()
|
||||
guard let type = ImageType(fileExtension: destinationExtension)?.fileType else {
|
||||
addWarning("Invalid image extension \(destinationExtension)", job: job)
|
||||
return
|
||||
}
|
||||
|
||||
let desiredWidth = CGFloat(job.width)
|
||||
|
||||
let sourceRep = image.representations[0]
|
||||
let destinationSize = NSSize(width: sourceRep.pixelsWide, height: sourceRep.pixelsHigh)
|
||||
.scaledDown(to: desiredWidth)
|
||||
|
||||
// create NSBitmapRep manually, if using cgImage, the resulting size is wrong
|
||||
let rep = NSBitmapImageRep(bitmapDataPlanes: nil,
|
||||
pixelsWide: Int(destinationSize.width),
|
||||
pixelsHigh: Int(destinationSize.height),
|
||||
bitsPerSample: 8,
|
||||
samplesPerPixel: 4,
|
||||
hasAlpha: true,
|
||||
isPlanar: false,
|
||||
colorSpaceName: NSColorSpaceName.deviceRGB,
|
||||
bytesPerRow: Int(destinationSize.width) * 4,
|
||||
bitsPerPixel: 32)!
|
||||
|
||||
let ctx = NSGraphicsContext(bitmapImageRep: rep)
|
||||
NSGraphicsContext.saveGraphicsState()
|
||||
NSGraphicsContext.current = ctx
|
||||
image.draw(in: NSMakeRect(0, 0, destinationSize.width, destinationSize.height))
|
||||
ctx?.flushGraphics()
|
||||
NSGraphicsContext.restoreGraphicsState()
|
||||
|
||||
// Get NSData, and save it
|
||||
guard let data = rep.representation(using: type, properties: [.compressionFactor: NSNumber(value: job.quality)]) else {
|
||||
addWarning("Failed to get data", job: job)
|
||||
return
|
||||
}
|
||||
do {
|
||||
try data.createFolderAndWrite(to: destinationUrl)
|
||||
} catch {
|
||||
addWarning("Failed to write image (\(error))", job: job)
|
||||
return
|
||||
}
|
||||
generatedImages.insert(job.destination)
|
||||
}
|
||||
|
||||
/**
|
||||
Create images of different types.
|
||||
|
||||
This function generates versions for the given image, including png/jpg, avif, and webp. Different pixel density versions (1x and 2x) are also generated.
|
||||
- Parameter destination: The path to the destination file
|
||||
*/
|
||||
@discardableResult
|
||||
func requireMultiVersionImage(source: String, destination: String, requiredBy path: String, width: Int, desiredHeight: Int?) -> NSSize {
|
||||
// Add @1x version
|
||||
_ = requireScaledMultiImage(source: source, destination: destination, requiredBy: path, width: width, desiredHeight: desiredHeight)
|
||||
|
||||
// Add @2x version
|
||||
return requireScaledMultiImage(
|
||||
source: source,
|
||||
destination: destination.insert("@2x", beforeLast: "."),
|
||||
requiredBy: path,
|
||||
width: width * 2,
|
||||
desiredHeight: desiredHeight.unwrapped { $0 * 2 })
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
private func requireScaledMultiImage(source: String, destination: String, requiredBy path: String, width: Int, desiredHeight: Int?) -> NSSize {
|
||||
let rawDestinationPath = destination.dropAfterLast(".")
|
||||
let avifPath = rawDestinationPath + ".avif"
|
||||
let webpPath = rawDestinationPath + ".webp"
|
||||
let needsGeneration = !output.appendingPathComponent(avifPath).exists || !output.appendingPathComponent(webpPath).exists
|
||||
|
||||
let size = requireImage(at: destination, generatedFrom: source, requiredBy: path, quality: 1.0, width: width, height: desiredHeight, alwaysGenerate: needsGeneration)
|
||||
multiImageJobs[destination] = path
|
||||
return size
|
||||
}
|
||||
|
||||
private func createMultiImages() {
|
||||
let sort = multiImageJobs.sorted { $0.value < $1.value && $0.key < $1.key }
|
||||
var count = 1
|
||||
for (baseImage, path) in sort {
|
||||
print(String(format: "Creating image versions: %4d / %d\r", count, sort.count), terminator: "")
|
||||
fflush(stdout)
|
||||
createMultiImages(from: baseImage, path: path)
|
||||
count += 1
|
||||
}
|
||||
print(" \r", terminator: "")
|
||||
}
|
||||
|
||||
private func createMultiImages(from source: String, path: String) {
|
||||
guard generatedImages.contains(source) else {
|
||||
return
|
||||
}
|
||||
|
||||
let sourceUrl = output.appendingPathComponent(source)
|
||||
let sourcePath = sourceUrl.path
|
||||
guard sourceUrl.exists else {
|
||||
addWarning("No image at path \(sourcePath)", destination: source, path: path)
|
||||
missingImages[source] = path
|
||||
return
|
||||
}
|
||||
|
||||
let avifPath = source.dropAfterLast(".") + ".avif"
|
||||
createAVIF(at: output.appendingPathComponent(avifPath).path, from: sourcePath)
|
||||
generatedImages.insert(avifPath)
|
||||
|
||||
let webpPath = source.dropAfterLast(".") + ".webp"
|
||||
createWEBP(at: output.appendingPathComponent(webpPath).path, from: sourcePath)
|
||||
generatedImages.insert(webpPath)
|
||||
|
||||
compress(at: source)
|
||||
}
|
||||
|
||||
private func createAVIF(at destination: String, from source: String, quality: Int = 55, effort: Int = 5) {
|
||||
let folder = destination.dropAfterLast("/")
|
||||
let command = "npx avif --input=\(source) --quality=\(quality) --effort=\(effort) --output=\(folder) --overwrite"
|
||||
do {
|
||||
_ = try FileSystem.safeShell(command)
|
||||
} catch {
|
||||
addWarning("Failed to create AVIF image", destination: destination, path: destination)
|
||||
}
|
||||
}
|
||||
|
||||
private func createWEBP(at destination: String, from source: String, quality: Int = 75) {
|
||||
let command = "cwebp \(source) -q \(quality) -o \(destination)"
|
||||
do {
|
||||
_ = try FileSystem.safeShell(command)
|
||||
} catch {
|
||||
addWarning("Failed to create WEBP image", destination: destination, path: destination)
|
||||
}
|
||||
}
|
||||
|
||||
private func compress(at destination: String, quality: Int = 70) {
|
||||
let command = "magick convert \(destination) -quality \(quality)% \(destination)"
|
||||
do {
|
||||
_ = try FileSystem.safeShell(command)
|
||||
} catch {
|
||||
addWarning("Failed to compress image", destination: destination, path: destination)
|
||||
}
|
||||
}
|
||||
|
||||
private func optimizeImages() {
|
||||
let all = generatedImages
|
||||
.filter { imageOptimSupportedFileExtensions.contains($0.lastComponentAfter(".")) }
|
||||
.map { output.appendingPathComponent($0).path }
|
||||
for i in stride(from: 0, to: all.count, by: imageOptimizationBatchSize) {
|
||||
let endIndex = min(i+imageOptimizationBatchSize, all.count)
|
||||
let batch = all[i..<endIndex]
|
||||
print(String(format: "Optimizing images: %4d / %d\r", endIndex, all.count), terminator: "")
|
||||
fflush(stdout)
|
||||
if optimizeImageBatch(batch) {
|
||||
optimizedImages.formUnion(batch)
|
||||
}
|
||||
}
|
||||
print(" \r", terminator: "")
|
||||
fflush(stdout)
|
||||
print("\(optimizedImages.count) images optimized")
|
||||
}
|
||||
|
||||
private func optimizeImageBatch(_ batch: ArraySlice<String>) -> Bool {
|
||||
let command = "imageoptim " + batch.joined(separator: " ")
|
||||
do {
|
||||
_ = try FileSystem.safeShell(command)
|
||||
return true
|
||||
} catch {
|
||||
addWarning("Failed to optimize images", destination: "", path: "")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
14
Sources/Generator/Files/ImageJob.swift
Normal file
14
Sources/Generator/Files/ImageJob.swift
Normal file
@ -0,0 +1,14 @@
|
||||
import Foundation
|
||||
|
||||
struct ImageJob {
|
||||
|
||||
let destination: String
|
||||
|
||||
let width: Int
|
||||
|
||||
let path: String
|
||||
|
||||
let quality: Float
|
||||
|
||||
let alwaysGenerate: Bool
|
||||
}
|
66
Sources/Generator/Files/ImageReader.swift
Normal file
66
Sources/Generator/Files/ImageReader.swift
Normal file
@ -0,0 +1,66 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
final class ImageReader {
|
||||
|
||||
/// The content folder where the input data is stored
|
||||
let contentFolder: URL
|
||||
|
||||
private let fileUpdates: FileUpdateChecker
|
||||
|
||||
let runDataFolder: URL
|
||||
|
||||
init(in input: URL, runFolder: URL, fileUpdates: FileUpdateChecker) {
|
||||
self.contentFolder = input
|
||||
self.runDataFolder = runFolder
|
||||
self.fileUpdates = fileUpdates
|
||||
}
|
||||
|
||||
var numberOfFilesLoaded: Int {
|
||||
fileUpdates.numberOfFilesLoaded
|
||||
}
|
||||
|
||||
var numberOfFilesAccessed: Int {
|
||||
fileUpdates.numberOfFilesAccessed
|
||||
}
|
||||
|
||||
func loadData() -> FileUpdateChecker.LoadResult {
|
||||
fileUpdates.loadPreviousRun(from: runDataFolder)
|
||||
}
|
||||
|
||||
func writeDetectedFileChangesToDisk() -> String? {
|
||||
fileUpdates.writeDetectedFileChanges(to: runDataFolder)
|
||||
}
|
||||
|
||||
func imageHasChanged(at path: String) -> Bool {
|
||||
fileUpdates.fileHasChanged(at: path)
|
||||
}
|
||||
|
||||
func getImage(atPath path: String) -> NSImage? {
|
||||
guard let data = getData(atPath: path) else {
|
||||
// TODO: log.error("Failed to load file", source: path)
|
||||
return nil
|
||||
}
|
||||
guard let image = NSImage(data: data) else {
|
||||
// TODO: log.error("Failed to read image", source: path)
|
||||
return nil
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
private func getData(atPath path: String) -> Data? {
|
||||
let url = contentFolder.appendingPathComponent(path)
|
||||
guard url.exists else {
|
||||
return nil
|
||||
}
|
||||
do {
|
||||
let data = try Data(contentsOf: url)
|
||||
fileUpdates.didLoad(data, at: path)
|
||||
return data
|
||||
} catch {
|
||||
// TODO: log.error("Failed to read data: \(error)", source: path)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user