Skip to content

Commit

Permalink
Merge pull request #49 from mono0926/use-property-list-api
Browse files Browse the repository at this point in the history
Use property list api
  • Loading branch information
mono0926 committed May 15, 2017
2 parents 8955425 + 9a87ad4 commit 7fac5bd
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Sources/LicensePlistCore/Consts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public struct Consts {
public static let prefix = "com.mono0926.LicensePlist"
public static let outputPath = "\(prefix).Output"
public static let configPath = "license_plist.yml"
public static let version = "1.3.2"
public static let version = "1.3.5"
public static let encoding = String.Encoding.utf8
}
43 changes: 43 additions & 0 deletions Sources/LicensePlistCore/Entity/LicensePlistHolder.swift
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)")
}

}
}
22 changes: 5 additions & 17 deletions Sources/LicensePlistCore/Entity/PlistInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,16 @@ struct PlistInfo {

func outputPlist() {
guard let licenses = licenses else { preconditionFailure() }

let tm = TemplateManager.shared

let outputPath = options.outputPath
let plistPath = outputPath.appendingPathComponent(Consts.prefix)
if plistPath.lp.deleteIfExits() {
let itemsPath = outputPath.appendingPathComponent(Consts.prefix)
if itemsPath.lp.deleteIfExits() {
Log.info("Deleted exiting plist within \(Consts.prefix)")
}
plistPath.lp.createDirectory()
itemsPath.lp.createDirectory()
Log.info("Directory created: \(outputPath)")

let licensListItems = licenses.map {
return tm.licenseListItem.applied(["Title": $0.name(withVersion: options.config.addVersionNumbers),
"FileName": "\(Consts.prefix)/\($0.name)"])
}
let licenseListPlist = tm.licenseList.applied(["Item": licensListItems.joined(separator: "\n")])
outputPath.appendingPathComponent("\(Consts.prefix).plist").lp.write(content: licenseListPlist)

licenses.forEach {
plistPath.appendingPathComponent("\($0.name).plist")
.lp.write(content: tm.license.applied(["Body": $0.bodyEscaped]))
}
let holder = LicensePlistHolder.load(licenses: licenses, config: options.config)
holder.write(to: outputPath.appendingPathComponent("\(Consts.prefix).plist"), itemsPath: itemsPath)
}

func reportMissings() {
Expand Down
55 changes: 0 additions & 55 deletions Sources/LicensePlistCore/TemplateManager.swift

This file was deleted.

15 changes: 0 additions & 15 deletions Template/License.plist

This file was deleted.

10 changes: 0 additions & 10 deletions Template/LicenseList.plist

This file was deleted.

8 changes: 0 additions & 8 deletions Template/LicenseListItem.plist

This file was deleted.

32 changes: 32 additions & 0 deletions Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift
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>")
}
}
2 changes: 1 addition & 1 deletion Tests/LicensePlistTests/Entity/PlistInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PlistInfoTests: XCTestCase {
target.compareWithLatestSummary()

XCTAssertEqual(target.summary,
"add-version-numbers: false\n\nLicensePlist Version: 1.3.2")
"add-version-numbers: false\n\nLicensePlist Version: 1.3.5")
XCTAssertNotNil(target.summaryPath)
}

Expand Down
33 changes: 0 additions & 33 deletions Tests/LicensePlistTests/TemplateManagerTests.swift

This file was deleted.

0 comments on commit 7fac5bd

Please sign in to comment.