Skip to content

Commit

Permalink
Merge pull request #93 from yuya-oc/specify-license-file
Browse files Browse the repository at this point in the history
Add ability to specify license files in the manual section
  • Loading branch information
mono0926 committed Oct 24, 2018
2 parents 37411e2 + 322a372 commit 44b36e8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/LicensePlist/main.swift
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/LicensePlistCore/Entity/Config.swift
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion Sources/LicensePlistCore/Entity/Manual.swift
Expand Up @@ -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?
Expand All @@ -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")
}
Expand Down
9 changes: 5 additions & 4 deletions Tests/LicensePlistTests/Entity/ConfigTests.swift
Expand Up @@ -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"]))
}
Expand Down
4 changes: 4 additions & 0 deletions 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.
4 changes: 4 additions & 0 deletions Tests/LicensePlistTests/Resources/license_plist.yml
Expand Up @@ -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
Expand Down

0 comments on commit 44b36e8

Please sign in to comment.