-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from mono0926/use-property-list-api
Use property list api
- Loading branch information
Showing
10 changed files
with
82 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Foundation | ||
import LoggerAPI | ||
|
||
struct LicensePlistHolder { | ||
let root: Data | ||
let items: [(LicenseInfo, Data)] | ||
static func load(licenses: [LicenseInfo], config: Config) -> LicensePlistHolder { | ||
let rootItems: [[String: String]] = licenses.map { license in | ||
return ["Type": "PSChildPaneSpecifier", | ||
"Title": license.name(withVersion: config.addVersionNumbers), | ||
"File": "\(Consts.prefix)/\(license.name)"] | ||
} | ||
let root = try! PropertyListSerialization.data(fromPropertyList: ["PreferenceSpecifiers": rootItems], | ||
format: .xml, | ||
options: 0) | ||
let items: [(LicenseInfo, Data)] = licenses.map { license in | ||
let item = ["PreferenceSpecifiers": [["Type": "PSGroupSpecifier", "FooterText": license.body]]] | ||
let value = try! PropertyListSerialization.data(fromPropertyList: item, format: .xml, options: 0) | ||
return (license, value) | ||
} | ||
return LicensePlistHolder(root: root, items: items) | ||
} | ||
|
||
func deserialized() -> (root: [String: [[String: String]]], items: [(LicenseInfo, [String: [[String: String]]])]) { | ||
let root = try! PropertyListSerialization.propertyList(from: self.root, options: [], format: nil) as! [String: [[String: String]]] | ||
let items: [(LicenseInfo, [String: [[String: String]]])] = self.items.map { license, data in | ||
let value = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String: [[String: String]]] | ||
return (license, value) | ||
} | ||
return (root: root, items: items) | ||
} | ||
func write(to rootPath: URL, itemsPath: URL) { | ||
do { | ||
try root.write(to: rootPath) | ||
try items.forEach { | ||
try $0.1.write(to: itemsPath.appendingPathComponent("\($0.0.name).plist")) | ||
} | ||
} catch let e { | ||
Log.error("Failed to write to (rootPath: \(rootPath), itemsPath: \(itemsPath)).\nerror: \(e)") | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import LicensePlistCore | ||
|
||
class LicensePlistHolderTests: XCTestCase { | ||
func testLoad_empty() { | ||
let result = LicensePlistHolder.load(licenses: [], config: Config.empty) | ||
let (root, items) = result.deserialized() | ||
let rootItems = root["PreferenceSpecifiers"]! | ||
XCTAssertTrue(rootItems.isEmpty) | ||
XCTAssertTrue(items.isEmpty) | ||
} | ||
func testLoad_one() { | ||
let pods = CocoaPods(name: "name", nameSpecified: nil, version: nil) | ||
let podsLicense = CocoaPodsLicense(library: pods, body: "'<body>") | ||
let result = LicensePlistHolder.load(licenses: [podsLicense], config: Config.empty) | ||
let (root, items) = result.deserialized() | ||
let rootItems = root["PreferenceSpecifiers"]! | ||
XCTAssertEqual(rootItems.count, 1) | ||
XCTAssertEqual(items.count, 1) | ||
|
||
let rootItems1 = rootItems.first! | ||
XCTAssertEqual(rootItems1["Type"], "PSChildPaneSpecifier") | ||
XCTAssertEqual(rootItems1["Title"], "name") | ||
XCTAssertEqual(rootItems1["File"], "com.mono0926.LicensePlist/name") | ||
|
||
let item1 = items.first!.1 | ||
let item1_1 = item1["PreferenceSpecifiers"]!.first! | ||
XCTAssertEqual(item1_1["Type"], "PSGroupSpecifier") | ||
XCTAssertEqual(item1_1["FooterText"], "\'<body>") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.