From 322a372483c26f92d6f71ea4dce59d9a0579b591 Mon Sep 17 00:00:00 2001 From: Yuya Ochiai Date: Tue, 23 Oct 2018 01:46:13 +0900 Subject: [PATCH] Add ability to specify license files --- Sources/LicensePlist/main.swift | 2 +- Sources/LicensePlistCore/Entity/Config.swift | 4 ++-- Sources/LicensePlistCore/Entity/Manual.swift | 5 ++++- Tests/LicensePlistTests/Entity/ConfigTests.swift | 9 +++++---- Tests/LicensePlistTests/Resources/dummy_license.txt | 4 ++++ Tests/LicensePlistTests/Resources/license_plist.yml | 4 ++++ 6 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 Tests/LicensePlistTests/Resources/dummy_license.txt diff --git a/Sources/LicensePlist/main.swift b/Sources/LicensePlist/main.swift index b22cf963..420e5a0b 100644 --- a/Sources/LicensePlist/main.swift +++ b/Sources/LicensePlist/main.swift @@ -5,7 +5,7 @@ import LoggerAPI private func loadConfig(configPath: URL) -> Config { if let yaml = configPath.lp.read() { - return Config(yaml: yaml) + return Config(yaml: yaml, configBasePath: configPath.deletingLastPathComponent()) } return Config.empty } diff --git a/Sources/LicensePlistCore/Entity/Config.swift b/Sources/LicensePlistCore/Entity/Config.swift index f9c5c571..7a7629d6 100644 --- a/Sources/LicensePlistCore/Entity/Config.swift +++ b/Sources/LicensePlistCore/Entity/Config.swift @@ -13,7 +13,7 @@ public struct Config { public static let empty = Config(githubs: [], manuals: [], excludes: [], renames: [:]) - public init(yaml: String) { + public init(yaml: String, configBasePath: URL) { let value = try! Yaml.load(yaml) let excludes = value["exclude"].array?.map { $0.string! } ?? [] let renames = value["rename"].dictionary?.reduce([String: String]()) { sum, e in @@ -23,7 +23,7 @@ public struct Config { return sum } ?? [:] let manuals = value["manual"].array ?? [] - let manualList = Manual.load(manuals, renames: renames) + let manualList = Manual.load(manuals, renames: renames, configBasePath: configBasePath) let githubs = value["github"].array?.map { $0.string }.flatMap { $0 } ?? [] let gitHubList = githubs.map { GitHub.load($0, renames: renames, mark: "", quotes: "") }.flatMap { $0 } gitHubList.forEach { diff --git a/Sources/LicensePlistCore/Entity/Manual.swift b/Sources/LicensePlistCore/Entity/Manual.swift index 536a3cd2..3409ac8f 100644 --- a/Sources/LicensePlistCore/Entity/Manual.swift +++ b/Sources/LicensePlistCore/Entity/Manual.swift @@ -35,7 +35,7 @@ extension Manual: CustomStringConvertible { extension Manual { public static func load(_ raw: [Yaml], - renames: [String: String]) -> [Manual] { + renames: [String: String], configBasePath: URL) -> [Manual] { return raw.map { (manualEntry) -> Manual in var name = "" var body: String? @@ -51,6 +51,9 @@ extension Manual { version = valuePair.value.string case "body": body = valuePair.value.string + case "file": + let url = configBasePath.appendingPathComponent(valuePair.value.string!) + body = try! String(contentsOf: url) default: Log.warning("Tried to parse an unknown YAML key") } diff --git a/Tests/LicensePlistTests/Entity/ConfigTests.swift b/Tests/LicensePlistTests/Entity/ConfigTests.swift index a8fccdf2..a2bb8953 100644 --- a/Tests/LicensePlistTests/Entity/ConfigTests.swift +++ b/Tests/LicensePlistTests/Entity/ConfigTests.swift @@ -5,17 +5,18 @@ import XCTest class ConfigTests: XCTestCase { func testInit_empty_yaml() { - XCTAssertEqual(Config(yaml: ""), Config(githubs: [], manuals: [], excludes: [], renames: [:])) + XCTAssertEqual(Config(yaml: "", configBasePath: URL(fileURLWithPath: "")), Config(githubs: [], manuals: [], excludes: [], renames: [:])) } func testInit_sample() { - let path = "https://raw.githubusercontent.com/mono0926/LicensePlist/master/Tests/LicensePlistTests/Resources/license_plist.yml" - XCTAssertEqual(Config(yaml: URL(string: path)!.lp.download().resultSync().value!), + let url = URL(string: "https://raw.githubusercontent.com/mono0926/LicensePlist/master/Tests/LicensePlistTests/Resources/license_plist.yml")! + XCTAssertEqual(Config(yaml: url.lp.download().resultSync().value!, configBasePath: url.deletingLastPathComponent()), Config(githubs: [GitHub(name: "LicensePlist", nameSpecified: "License Plist", owner: "mono0926", version: "1.2.0"), GitHub(name: "NativePopup", nameSpecified: nil, owner: "mono0926", version: nil)], manuals: [Manual(name: "WebRTC", source: "https://webrtc.googlesource.com/src", nameSpecified: "Web RTC", - version: "M61")], + version: "M61"), + Manual(name: "Dummy License File", source: nil, nameSpecified: nil, version: nil)], excludes: ["RxSwift", "ios-license-generator", "/^Core.*$/"], renames: ["LicensePlist": "License Plist", "WebRTC": "Web RTC"])) } diff --git a/Tests/LicensePlistTests/Resources/dummy_license.txt b/Tests/LicensePlistTests/Resources/dummy_license.txt new file mode 100644 index 00000000..18df47c4 --- /dev/null +++ b/Tests/LicensePlistTests/Resources/dummy_license.txt @@ -0,0 +1,4 @@ +A LICENSE for Testing + +This text file is used for testing manual configuration. +The file is specified with "file" key in the manual section. diff --git a/Tests/LicensePlistTests/Resources/license_plist.yml b/Tests/LicensePlistTests/Resources/license_plist.yml index 9d3b0a1e..22d4d7c7 100644 --- a/Tests/LicensePlistTests/Resources/license_plist.yml +++ b/Tests/LicensePlistTests/Resources/license_plist.yml @@ -40,6 +40,10 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ' +# Specify license files manually + - name: "Dummy License File" + file: "dummy_license.txt" + # Specify libraries to be excluded. exclude: - RxSwift