diff --git a/Sources/Generator/Files/ImageGenerator.swift b/Sources/Generator/Files/ImageGenerator.swift index 0af272b..3b4beeb 100644 --- a/Sources/Generator/Files/ImageGenerator.swift +++ b/Sources/Generator/Files/ImageGenerator.swift @@ -18,6 +18,10 @@ private struct ImageJob { final class ImageGenerator { + private let imageOptimSupportedFileExtensions: Set = ["jpg", "png", "svg"] + + private let imageOptimizationBatchSize = 50 + /** The path to the input folder. */ @@ -66,6 +70,11 @@ final class ImageGenerator { */ private var generatedImages: Set = [] + /** + The images optimized by ImageOptim + */ + private var optimizedImages: Set = [] + /** A cache to get the size of source images, so that files don't have to be loaded multiple times. @@ -182,8 +191,9 @@ final class ImageGenerator { create(images: jobs, from: source) count += 1 } - print(String(format: " \r", count, imageJobs.count), terminator: "") + print(" \r", terminator: "") createMultiImages() + optimizeImages() printMissingImages() printImageWarnings() printGeneratedImages() @@ -360,7 +370,7 @@ final class ImageGenerator { createMultiImages(from: baseImage, path: path) count += 1 } - print(String(format: " \r", count, sort.count), terminator: "") + print(" \r", terminator: "") } private func createMultiImages(from source: String, path: String) { @@ -414,4 +424,33 @@ final class ImageGenerator { 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..) -> Bool { + let command = "imageoptim " + batch.joined(separator: " ") + do { + _ = try FileSystem.safeShell(command) + return true + } catch { + addWarning("Failed to optimize images", destination: "", path: "") + return false + } + } }