|
1 |
| -import Foundation |
2 | 1 | import FigmaExportCore
|
| 2 | +import Foundation |
| 3 | +import Logging |
3 | 4 | #if os(Linux)
|
4 | 5 | import FoundationXML
|
5 | 6 | #endif
|
6 | 7 |
|
7 | 8 | final class FileWriter {
|
8 |
| - |
9 | 9 | private let fileManager: FileManager
|
10 |
| - |
| 10 | + private let logger = Logger(label: "com.redmadrobot.figma-export.file-writer") |
| 11 | + |
11 | 12 | init(fileManager: FileManager = .default) {
|
12 | 13 | self.fileManager = fileManager
|
13 | 14 | }
|
14 |
| - |
| 15 | + |
15 | 16 | func write(files: [FileContents]) throws {
|
16 | 17 | try files.forEach { file in
|
17 | 18 | let directoryURL = URL(fileURLWithPath: file.destination.directory.path)
|
18 | 19 | try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
|
19 |
| - |
| 20 | + |
20 | 21 | let fileURL = URL(fileURLWithPath: file.destination.url.path)
|
21 |
| - if let data = file.data { |
22 |
| - try data.write(to: fileURL, options: .atomic) |
23 |
| - } else if let localFileURL = file.dataFile { |
24 |
| - _ = try fileManager.replaceItemAt(fileURL, withItemAt: localFileURL) |
25 |
| - } else { |
26 |
| - fatalError("FileContents.data is nil. Use FileDownloader to download contents of the file.") |
| 22 | + do { |
| 23 | + if let data = file.data { |
| 24 | + try data.write(to: fileURL, options: .atomic) |
| 25 | + } else if let localFileURL = file.dataFile { |
| 26 | + _ = try fileManager.replaceItemAt(fileURL, withItemAt: localFileURL) |
| 27 | + } else { |
| 28 | + fatalError("FileContents.data is nil. Use FileDownloader to download contents of the file.") |
| 29 | + } |
| 30 | + } catch let e { |
| 31 | + logger.error("\(e.localizedDescription)") |
27 | 32 | }
|
28 | 33 | }
|
29 | 34 | }
|
30 |
| - |
| 35 | + |
31 | 36 | func write(xmlFile: XMLDocument, directory: URL) throws {
|
32 | 37 | let fileURL = URL(fileURLWithPath: directory.path)
|
33 | 38 | let options: XMLNode.Options = [.nodePrettyPrint, .nodeCompactEmptyElement]
|
|
0 commit comments