Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import Foundation
  2. import LoggerAPI
  3.  
  4. struct LicensePlistHolder {
  5. let root: Data
  6. let items: [(LicenseInfo, Data)]
  7. static func load(licenses: [LicenseInfo], options: Options) -> LicensePlistHolder {
  8. var rootItems: [[String: String]] = licenses.map { license in
  9. return ["Type": "PSChildPaneSpecifier",
  10. "Title": license.name(withVersion: options.config.addVersionNumbers),
  11. "File": "\(options.prefix)/\(license.name)"]
  12. }
  13. rootItems.insert(["Type": "PSGroupSpecifier", "Title": "Licenses"], at: 0)
  14. let root = try! PropertyListSerialization.data(fromPropertyList: ["PreferenceSpecifiers": rootItems],
  15. format: .xml,
  16. options: 0)
  17. let items: [(LicenseInfo, Data)] = licenses.map { license in
  18. let item = ["PreferenceSpecifiers": [["Type": "PSGroupSpecifier", "FooterText": license.body]]]
  19. let value = try! PropertyListSerialization.data(fromPropertyList: item, format: .xml, options: 0)
  20. return (license, value)
  21. }
  22. return LicensePlistHolder(root: root, items: items)
  23. }
  24.  
  25. func deserialized() -> (root: [String: [[String: String]]], items: [(LicenseInfo, [String: [[String: String]]])]) {
  26. let root = try! PropertyListSerialization.propertyList(from: self.root, options: [], format: nil) as! [String: [[String: String]]]
  27. let items: [(LicenseInfo, [String: [[String: String]]])] = self.items.map { license, data in
  28. let value = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String: [[String: String]]]
  29. return (license, value)
  30. }
  31. return (root: root, items: items)
  32. }
  33. func write(to rootPath: URL, itemsPath: URL) {
  34. do {
  35. try root.write(to: rootPath)
  36. try items.forEach {
  37. try $0.1.write(to: itemsPath.appendingPathComponent("\($0.0.name).plist"))
  38. }
  39. } catch let e {
  40. Log.error("Failed to write to (rootPath: \(rootPath), itemsPath: \(itemsPath)).\nerror: \(e)")
  41. }
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement