Skip to content

Commit

Permalink
Add feature that exports all licenses to the one page
Browse files Browse the repository at this point in the history
  • Loading branch information
NewFieldForMe committed Nov 20, 2019
1 parent 444b274 commit 6e1d588
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
4 changes: 3 additions & 1 deletion Sources/LicensePlist/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions Sources/LicensePlistCore/Entity/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [:])

Expand Down
18 changes: 18 additions & 0 deletions Sources/LicensePlistCore/Entity/LicensePlistHolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Sources/LicensePlistCore/Entity/PlistInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions Tests/LicensePlistTests/Entity/LicensePlistHolderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ class LicensePlistHolderTests: XCTestCase {
XCTAssertEqual(item1_1["Type"], "PSGroupSpecifier")
XCTAssertEqual(item1_1["FooterText"], "\'<body>")
}
func testLoad_allToRoot() {
let pods = CocoaPods(name: "name", nameSpecified: nil, version: nil)
let podsLicense = CocoaPodsLicense(library: pods, body: "'<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"], "'<body>")
}
}

0 comments on commit 6e1d588

Please sign in to comment.