Remove log files if empty

This commit is contained in:
Christoph Hagen 2023-01-08 21:13:40 +01:00
parent 67a0da13bd
commit 4ccb67d7ef
4 changed files with 37 additions and 4 deletions

View File

@ -259,6 +259,14 @@ final class FileGenerator {
} }
func writeResults(to file: URL) { func writeResults(to file: URL) {
guard !unreadableFiles.isEmpty || !unwritableFiles.isEmpty || !failedMinifications.isEmpty || !missingFiles.isEmpty || !copiedFiles.isEmpty || !minifiedFiles.isEmpty || !files.expected.isEmpty || !files.external.isEmpty else {
do {
try FileManager.default.removeItem(at: file)
} catch {
print(" Failed to delete file log: \(error)")
}
return
}
var lines: [String] = [] var lines: [String] = []
func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence { func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence {
let elements = property.map { " " + convert($0) }.sorted() let elements = property.map { " " + convert($0) }.sorted()
@ -275,12 +283,13 @@ final class FileGenerator {
add("Copied files", copiedFiles) { $0 } add("Copied files", copiedFiles) { $0 }
add("Minified files", minifiedFiles) { $0 } add("Minified files", minifiedFiles) { $0 }
add("Expected files", files.expected) { "\($0.key) (required by \($0.value))" } add("Expected files", files.expected) { "\($0.key) (required by \($0.value))" }
add("External files", files.external) { "\($0.key) (required by \($0.value))" }
let data = lines.joined(separator: "\n").data(using: .utf8)! let data = lines.joined(separator: "\n").data(using: .utf8)!
do { do {
try data.createFolderAndWrite(to: file) try data.createFolderAndWrite(to: file)
} catch { } catch {
print(" Failed to save log: \(error)") print(" Failed to save file log: \(error)")
} }
} }
} }

View File

@ -455,6 +455,14 @@ final class GenerationResultsHandler {
} }
func writeResults(to file: URL) { func writeResults(to file: URL) {
guard !missingFiles.isEmpty || !unreadableFiles.isEmpty || !unwritableFiles.isEmpty || !missingLinkedPages.isEmpty || !pageWarnings.isEmpty || !generatedFiles.isEmpty || !draftPages.isEmpty || !emptyPages.isEmpty else {
do {
try FileManager.default.removeItem(at: file)
} catch {
print(" Failed to delete generation log: \(error)")
}
return
}
var lines: [String] = [] var lines: [String] = []
func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence { func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence {
let elements = property.map { " " + convert($0) }.sorted() let elements = property.map { " " + convert($0) }.sorted()
@ -476,7 +484,7 @@ final class GenerationResultsHandler {
do { do {
try data.createFolderAndWrite(to: file) try data.createFolderAndWrite(to: file)
} catch { } catch {
print(" Failed to save log: \(error)") print(" Failed to save generation log: \(error)")
} }
} }
} }

View File

@ -312,6 +312,14 @@ final class ImageGenerator {
} }
func writeResults(to file: URL) { func writeResults(to file: URL) {
guard !missingImages.isEmpty || !unreadableImages.isEmpty || !failedImages.isEmpty || !unhandledImages.isEmpty || !imageWarnings.isEmpty || !generatedImages.isEmpty || !optimizedImages.isEmpty else {
do {
try FileManager.default.removeItem(at: file)
} catch {
print(" Failed to delete image log: \(error)")
}
return
}
var lines: [String] = [] var lines: [String] = []
func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence { func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence {
let elements = property.map { " " + convert($0) }.sorted() let elements = property.map { " " + convert($0) }.sorted()
@ -332,7 +340,7 @@ final class ImageGenerator {
do { do {
try data.createFolderAndWrite(to: file) try data.createFolderAndWrite(to: file)
} catch { } catch {
print(" Failed to save log: \(error)") print(" Failed to save image log: \(error)")
} }
} }
} }

View File

@ -151,6 +151,14 @@ final class MetadataInfoLogger {
} }
func writeResults(to file: URL) { func writeResults(to file: URL) {
guard !errors.isEmpty || !warnings.isEmpty || !unreadableMetadata.isEmpty || !unusedProperties.isEmpty || !invalidProperties.isEmpty || !unknownProperties.isEmpty || !missingProperties.isEmpty else {
do {
try FileManager.default.removeItem(at: file)
} catch {
print(" Failed to delete metadata log: \(error)")
}
return
}
var lines: [String] = [] var lines: [String] = []
func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence { func add<S>(_ name: String, _ property: S, convert: (S.Element) -> String) where S: Sequence {
let elements = property.map { " " + convert($0) }.sorted() let elements = property.map { " " + convert($0) }.sorted()
@ -172,7 +180,7 @@ final class MetadataInfoLogger {
do { do {
try data.createFolderAndWrite(to: file) try data.createFolderAndWrite(to: file)
} catch { } catch {
print(" Failed to save log: \(error)") print(" Failed to save metadata log: \(error)")
} }
} }
} }