diff --git a/.swiftlint.yml b/.swiftlint.yml index 244ba276..316924fc 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -8,6 +8,6 @@ included: - Sources - Tests -line_length: 180 +line_length: 190 nesting: type_level: 3 diff --git a/Sources/LicensePlistCore/Entity/GitHub.swift b/Sources/LicensePlistCore/Entity/GitHub.swift index b252ddf1..04245498 100644 --- a/Sources/LicensePlistCore/Entity/GitHub.swift +++ b/Sources/LicensePlistCore/Entity/GitHub.swift @@ -28,7 +28,7 @@ extension GitHub { public static func load(_ content: String, renames: [String: String] = [:]) -> [GitHub] { return load(content, renames: renames, mark: "github ") } - + public static func load(_ content: String, renames: [String: String], mark: String, quotes: String = "\"") -> [GitHub] { let r = load(content, renames: renames, mark: mark, quotes: quotes, version: true) if !r.isEmpty { @@ -36,7 +36,7 @@ extension GitHub { } return load(content, renames: renames, mark: mark, quotes: quotes, version: false) } - + public static func load(_ content: String, renames: [String: String], mark: String, diff --git a/Sources/LicensePlistCore/Entity/PlistInfo.swift b/Sources/LicensePlistCore/Entity/PlistInfo.swift index 2944eba4..64697742 100644 --- a/Sources/LicensePlistCore/Entity/PlistInfo.swift +++ b/Sources/LicensePlistCore/Entity/PlistInfo.swift @@ -32,13 +32,13 @@ struct PlistInfo { Log.info("Carthage License collect start") githubLibraries = options.config.apply(githubs: GitHub.load(cartfile ?? "", renames: options.config.renames)).sorted() } - + mutating func loadSwiftPackageLibraries(packageFile: String?) { Log.info("Swift Package Manager License collect start") let packages = SwiftPackage.loadPackages(packageFile ?? "") let packagesAsGithubLibraries = packages.compactMap { $0.toGitHub(renames: options.config.renames) }.sorted() - + githubLibraries = (githubLibraries ?? []) + packagesAsGithubLibraries } diff --git a/Sources/LicensePlistCore/Entity/SwiftPackage.swift b/Sources/LicensePlistCore/Entity/SwiftPackage.swift index cef520c8..1aed4a20 100644 --- a/Sources/LicensePlistCore/Entity/SwiftPackage.swift +++ b/Sources/LicensePlistCore/Entity/SwiftPackage.swift @@ -13,43 +13,43 @@ public struct SwiftPackage: Decodable, Equatable { let revision: String? let version: String } - + let package: String let repositoryURL: String let state: State } -fileprivate struct ResolvedPackages: Decodable { +private struct ResolvedPackages: Decodable { struct Pins: Decodable { let pins: [SwiftPackage] } - + let object: Pins let version: Int } extension SwiftPackage { - + static func loadPackages(_ content: String) -> [SwiftPackage] { guard let data = content.data(using: .utf8) else { return [] } guard let resolvedPackages = try? JSONDecoder().decode(ResolvedPackages.self, from: data) else { return [] } - + return resolvedPackages.object.pins } - + func toGitHub(renames: [String: String]) -> GitHub? { guard repositoryURL.contains("github.com") else { return nil } - + let urlParts = repositoryURL .replacingOccurrences(of: "https://", with: "") .replacingOccurrences(of: "http://", with: "") .components(separatedBy: "/") - + guard urlParts.count >= 3 else { return nil } - + let name = urlParts.last?.components(separatedBy: ".").first ?? "" let owner = urlParts[urlParts.count - 2] - + return GitHub(name: name, nameSpecified: renames[name], owner: owner, diff --git a/Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift b/Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift index acffba30..71300a3a 100644 --- a/Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift +++ b/Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift @@ -10,7 +10,7 @@ import XCTest @testable import LicensePlistCore class SwiftPackageManagerTests: XCTestCase { - + func testDecoding() { let jsonString = """ { @@ -23,53 +23,64 @@ class SwiftPackageManagerTests: XCTestCase { } } """ - + let data = jsonString.data(using: .utf8)! let package = try! JSONDecoder().decode(SwiftPackage.self, from: data) - + XCTAssertEqual(package.package, "APIKit") XCTAssertEqual(package.repositoryURL, "https://github.com/ishkawa/APIKit.git") XCTAssertEqual(package.state.revision, "86d51ecee0bc0ebdb53fb69b11a24169a69097ba") XCTAssertEqual(package.state.version, "4.1.0") } - + func testConvertToGithub() { - let package = SwiftPackage(package: "Commander", repositoryURL: "https://github.com/kylef/Commander.git", state: SwiftPackage.State(branch: nil, revision: "e5b50ad7b2e91eeb828393e89b03577b16be7db9", version: "0.8.0")) + let package = SwiftPackage(package: "Commander", + repositoryURL: "https://github.com/kylef/Commander.git", + state: SwiftPackage.State(branch: nil, revision: "e5b50ad7b2e91eeb828393e89b03577b16be7db9", version: "0.8.0")) let result = package.toGitHub(renames: [:]) XCTAssertEqual(result, GitHub(name: "Commander", nameSpecified: nil, owner: "kylef", version: "0.8.0")) } - + func testRename() { - let package = SwiftPackage(package: "Commander", repositoryURL: "https://github.com/kylef/Commander.git", state: SwiftPackage.State(branch: nil, revision: "e5b50ad7b2e91eeb828393e89b03577b16be7db9", version: "0.8.0")) + let package = SwiftPackage(package: "Commander", + repositoryURL: "https://github.com/kylef/Commander.git", + state: SwiftPackage.State(branch: nil, + revision: "e5b50ad7b2e91eeb828393e89b03577b16be7db9", version: "0.8.0")) let result = package.toGitHub(renames: ["Commander": "RenamedCommander"]) XCTAssertEqual(result, GitHub(name: "Commander", nameSpecified: "RenamedCommander", owner: "kylef", version: "0.8.0")) } - + func testInvalidURL() { let package = SwiftPackage(package: "Google", repositoryURL: "http://www.google.com", state: SwiftPackage.State(branch: nil, revision: "", version: "0.0.0")) let result = package.toGitHub(renames: [:]) XCTAssertNil(result) } - + func testNonGithub() { - let package = SwiftPackage(package: "Bitbucket", repositoryURL: "https://mbuchetics@bitbucket.org/mbuchetics/adventofcode2018.git", state: SwiftPackage.State(branch: nil, revision: "", version: "0.0.0")) + let package = SwiftPackage(package: "Bitbucket", + repositoryURL: "https://mbuchetics@bitbucket.org/mbuchetics/adventofcode2018.git", + state: SwiftPackage.State(branch: nil, revision: "", version: "0.0.0")) let result = package.toGitHub(renames: [:]) XCTAssertNil(result) } - + func testParse() { let path = "https://raw.githubusercontent.com/mono0926/LicensePlist/master/Package.resolved" //let path = "https://raw.githubusercontent.com/mono0926/LicensePlist/master/Tests/LicensePlistTests/Resources/Package.resolved" let content = try! String(contentsOf: URL(string: path)!) let packages = SwiftPackage.loadPackages(content) - + XCTAssertFalse(packages.isEmpty) XCTAssertEqual(packages.count, 8) - + let packageFirst = packages.first! - XCTAssertEqual(packageFirst, SwiftPackage(package: "APIKit", repositoryURL: "https://github.com/ishkawa/APIKit.git", state: SwiftPackage.State(branch: nil, revision: "86d51ecee0bc0ebdb53fb69b11a24169a69097ba", version: "4.1.0"))) + XCTAssertEqual(packageFirst, SwiftPackage(package: "APIKit", + repositoryURL: "https://github.com/ishkawa/APIKit.git", + state: SwiftPackage.State(branch: nil, revision: "86d51ecee0bc0ebdb53fb69b11a24169a69097ba", version: "4.1.0"))) let packageLast = packages.last! - XCTAssertEqual(packageLast, SwiftPackage(package: "Yaml", repositoryURL: "https://github.com/behrang/YamlSwift.git", state: SwiftPackage.State(branch: nil, revision: "287f5cab7da0d92eb947b5fd8151b203ae04a9a3", version: "3.4.4"))) - + XCTAssertEqual(packageLast, SwiftPackage(package: "Yaml", + repositoryURL: "https://github.com/behrang/YamlSwift.git", + state: SwiftPackage.State(branch: nil, revision: "287f5cab7da0d92eb947b5fd8151b203ae04a9a3", version: "3.4.4"))) + } }