Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mono0926 committed Sep 24, 2019
1 parent fb50cca commit f604e57
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ included:
- Sources
- Tests

line_length: 180
line_length: 190
nesting:
type_level: 3
4 changes: 2 additions & 2 deletions Sources/LicensePlistCore/Entity/GitHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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 {
return r
}
return load(content, renames: renames, mark: mark, quotes: quotes, version: false)
}

public static func load(_ content: String,
renames: [String: String],
mark: String,
Expand Down
4 changes: 2 additions & 2 deletions Sources/LicensePlistCore/Entity/PlistInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/LicensePlistCore/Entity/SwiftPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 27 additions & 16 deletions Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import XCTest
@testable import LicensePlistCore

class SwiftPackageManagerTests: XCTestCase {

func testDecoding() {
let jsonString = """
{
Expand All @@ -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://[email protected]/mbuchetics/adventofcode2018.git", state: SwiftPackage.State(branch: nil, revision: "", version: "0.0.0"))
let package = SwiftPackage(package: "Bitbucket",
repositoryURL: "https://[email protected]/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")))

}
}

0 comments on commit f604e57

Please sign in to comment.