From 6e1d58837b170163a3041001ae58dd4c6ba05e80 Mon Sep 17 00:00:00 2001 From: RyoYamada Date: Wed, 20 Nov 2019 21:00:37 +0900 Subject: [PATCH] Add feature that exports all licenses to the one page --- README.md | 5 +++++ Sources/LicensePlist/main.swift | 4 +++- Sources/LicensePlistCore/Entity/Config.swift | 1 + .../Entity/LicensePlistHolder.swift | 18 ++++++++++++++++++ .../LicensePlistCore/Entity/PlistInfo.swift | 4 +++- .../Entity/LicensePlistHolderTests.swift | 14 ++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 33b835bb..ce25b277 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,11 @@ You can see options by `license-plist --help`. - Default: false - Only when the files are created or updated, the terminal or the finder opens. By adding `--suppress-opening-directory` flag, this behavior is suppressed. +#### `--export-all-to-root` + +- Default: false +- All licenses are displayed in one page, not a list. + ### Integrate into build Add a `Run Script Phase` to `Build Phases`: diff --git a/Sources/LicensePlist/main.swift b/Sources/LicensePlist/main.swift index 0ce3f15e..aeb3dc34 100644 --- a/Sources/LicensePlist/main.swift +++ b/Sources/LicensePlist/main.swift @@ -22,13 +22,15 @@ let main = command(Option("cartfile-path", default: Consts.cartfileName), Option("markdown-path", default: ""), Flag("force"), Flag("add-version-numbers"), - Flag("suppress-opening-directory")) { cartfile, podsPath, packagePath, xcodeprojPath, output, gitHubToken, configPath, prefix, htmlPath, markdownPath, force, version, suppressOpen in + Flag("suppress-opening-directory"), + Flag("export-all-to-root")) { cartfile, podsPath, packagePath, xcodeprojPath, output, gitHubToken, configPath, prefix, htmlPath, markdownPath, force, version, suppressOpen, exportAllToRoot in Logger.configure() var config = loadConfig(configPath: URL(fileURLWithPath: configPath)) config.force = force config.addVersionNumbers = version config.suppressOpeningDirectory = suppressOpen + config.exportAllToRoot = exportAllToRoot let options = Options(outputPath: URL(fileURLWithPath: output), cartfilePath: URL(fileURLWithPath: cartfile), podsPath: URL(fileURLWithPath: podsPath), diff --git a/Sources/LicensePlistCore/Entity/Config.swift b/Sources/LicensePlistCore/Entity/Config.swift index 121446b5..e5b5f6ec 100644 --- a/Sources/LicensePlistCore/Entity/Config.swift +++ b/Sources/LicensePlistCore/Entity/Config.swift @@ -10,6 +10,7 @@ public struct Config { public var force = false public var addVersionNumbers = false public var suppressOpeningDirectory = false + public var exportAllToRoot = false public static let empty = Config(githubs: [], manuals: [], excludes: [], renames: [:]) diff --git a/Sources/LicensePlistCore/Entity/LicensePlistHolder.swift b/Sources/LicensePlistCore/Entity/LicensePlistHolder.swift index 8b7f158d..be413c62 100644 --- a/Sources/LicensePlistCore/Entity/LicensePlistHolder.swift +++ b/Sources/LicensePlistCore/Entity/LicensePlistHolder.swift @@ -24,6 +24,24 @@ struct LicensePlistHolder { return LicensePlistHolder(root: root, items: items) } + static func loadAllToRoot(licenses: [LicenseInfo]) -> LicensePlistHolder { + let rootItem: [[String: String]] = { + guard !licenses.isEmpty else { return [] } + let body = licenses + .compactMap { lincense in + return ["Type": "PSGroupSpecifier", + "Title": lincense.name, + "FooterText": lincense.body + ] + } + return body + }() + let root = try! PropertyListSerialization.data(fromPropertyList: ["PreferenceSpecifiers": rootItem], + format: .xml, + options: 0) + return LicensePlistHolder(root: root, 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 diff --git a/Sources/LicensePlistCore/Entity/PlistInfo.swift b/Sources/LicensePlistCore/Entity/PlistInfo.swift index 01082e82..b9a03557 100644 --- a/Sources/LicensePlistCore/Entity/PlistInfo.swift +++ b/Sources/LicensePlistCore/Entity/PlistInfo.swift @@ -102,7 +102,9 @@ struct PlistInfo { itemsPath.lp.createDirectory() Log.info("Directory created: \(outputPath)") - let holder = LicensePlistHolder.load(licenses: licenses, options: options) + let holder = options.config.exportAllToRoot ? + LicensePlistHolder.loadAllToRoot(licenses: licenses) : + LicensePlistHolder.load(licenses: licenses, options: options) holder.write(to: outputPath.appendingPathComponent("\(options.prefix).plist"), itemsPath: itemsPath) if let markdownPath = options.markdownPath { diff --git a/Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift b/Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift index 3c38b8af..7d877da9 100644 --- a/Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift +++ b/Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift @@ -33,4 +33,18 @@ class LicensePlistHolderTests: XCTestCase { XCTAssertEqual(item1_1["Type"], "PSGroupSpecifier") XCTAssertEqual(item1_1["FooterText"], "\'") } + func testLoad_allToRoot() { + let pods = CocoaPods(name: "name", nameSpecified: nil, version: nil) + let podsLicense = CocoaPodsLicense(library: pods, body: "'") + let result = LicensePlistHolder.loadAllToRoot(licenses: [podsLicense]) + let (root, items) = result.deserialized() + let rootItems = root["PreferenceSpecifiers"]! + XCTAssertEqual(rootItems.count, 1) + XCTAssertEqual(items.count, 0) + + let rootItems1 = rootItems[0] + XCTAssertEqual(rootItems1["Type"], "PSGroupSpecifier") + XCTAssertEqual(rootItems1["Title"], "name") + XCTAssertEqual(rootItems1["FooterText"], "'") + } }