Skip to content

Commit

Permalink
Add support for Swift Package Manager 5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
defagos committed Feb 3, 2022
1 parent 3d365e4 commit 10d828d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 29 deletions.
51 changes: 42 additions & 9 deletions Sources/LicensePlistCore/Entity/SwiftPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@

import Foundation

public struct SwiftPackage: Decodable, Equatable {
struct State: Decodable, Equatable {
public struct SwiftPackage: Equatable {
let package: String
let repositoryURL: String
let revision: String?
let version: String?
}

struct SwiftPackageV1: Decodable {
struct State: Decodable {
let branch: String?
let revision: String?
let version: String?
Expand All @@ -19,22 +26,48 @@ public struct SwiftPackage: Decodable, Equatable {
let state: State
}

private struct ResolvedPackages: Decodable {
struct ResolvedPackagesV1: Decodable {
struct Pins: Decodable {
let pins: [SwiftPackage]
let pins: [SwiftPackageV1]
}

let object: Pins
let version: Int
}

extension SwiftPackage {
struct SwiftPackageV2: Decodable {
struct State: Decodable {
let branch: String?
let revision: String?
let version: String?
}

let identity: String
let location: String
let state: State
}

struct ResolvedPackagesV2: Decodable {
let pins: [SwiftPackageV2]
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
if let resolvedPackagesV1 = try? JSONDecoder().decode(ResolvedPackagesV1.self, from: data) {
return resolvedPackagesV1.object.pins.map {
SwiftPackage(package: $0.package, repositoryURL: $0.repositoryURL, revision: $0.state.revision, version: $0.state.version)
}
}
else if let resolvedPackagesV2 = try? JSONDecoder().decode(ResolvedPackagesV2.self, from: data) {
return resolvedPackagesV2.pins.map {
SwiftPackage(package: $0.identity, repositoryURL: $0.location, revision: $0.state.revision, version: $0.state.version)
}
}
else {
return []
}
}

func toGitHub(renames: [String: String]) -> GitHub? {
Expand All @@ -56,6 +89,6 @@ extension SwiftPackage {
return GitHub(name: name,
nameSpecified: renames[name] ?? package,
owner: owner,
version: state.version)
version: version)
}
}
93 changes: 73 additions & 20 deletions Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import XCTest
@testable import LicensePlistCore

class SwiftPackageManagerTests: XCTestCase {

func testDecoding() throws {
func testDecodingV1() throws {
let jsonString = """
{
"package": "APIKit",
Expand All @@ -25,15 +24,15 @@ class SwiftPackageManagerTests: XCTestCase {
"""

let data = try XCTUnwrap(jsonString.data(using: .utf8))
let package = try JSONDecoder().decode(SwiftPackage.self, from: data)
let package = try JSONDecoder().decode(SwiftPackageV1.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 testDecodingOfURLWithDots() throws {
func testDecodingOfURLWithDotsV1() throws {
let jsonString = """
{
"package": "R.swift.Library",
Expand All @@ -47,15 +46,15 @@ class SwiftPackageManagerTests: XCTestCase {
"""

let data = try XCTUnwrap(jsonString.data(using: .utf8))
let package = try JSONDecoder().decode(SwiftPackage.self, from: data)
let package = try JSONDecoder().decode(SwiftPackageV1.self, from: data)

XCTAssertEqual(package.package, "R.swift.Library")
XCTAssertEqual(package.repositoryURL, "https://github.com/mac-cain13/R.swift.Library")
XCTAssertEqual(package.state.revision, "3365947d725398694d6ed49f2e6622f05ca3fc0f")
XCTAssertEqual(package.state.version, nil)
XCTAssertNil(package.state.version)
}

func testDecodingOptionalVersion() throws {
func testDecodingOptionalVersionV1() throws {
let jsonString = """
{
"package": "APIKit",
Expand All @@ -69,74 +68,126 @@ class SwiftPackageManagerTests: XCTestCase {
"""

let data = try XCTUnwrap(jsonString.data(using: .utf8))
let package = try JSONDecoder().decode(SwiftPackage.self, from: data)
let package = try JSONDecoder().decode(SwiftPackageV1.self, from: data)

XCTAssertEqual(package.package, "APIKit")
XCTAssertEqual(package.repositoryURL, "https://github.com/ishkawa/APIKit.git")
XCTAssertEqual(package.state.revision, "86d51ecee0bc0ebdb53fb69b11a24169a69097ba")
XCTAssertEqual(package.state.branch, "master")
XCTAssertEqual(package.state.version, nil)
XCTAssertNil(package.state.version)
}

func testDecodingWithVersionV2() throws {
let jsonString = """
{
"identity" : "APIKit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ishkawa/APIKit.git",
"state" : {
"revision" : "86d51ecee0bc0ebdb53fb69b11a24169a69097ba",
"version" : "4.1.0"
}
}
"""

let data = try XCTUnwrap(jsonString.data(using: .utf8))
let package = try JSONDecoder().decode(SwiftPackageV2.self, from: data)

XCTAssertEqual(package.identity, "APIKit")
XCTAssertEqual(package.location, "https://github.com/ishkawa/APIKit.git")
XCTAssertEqual(package.state.revision, "86d51ecee0bc0ebdb53fb69b11a24169a69097ba")
XCTAssertNil(package.state.branch)
XCTAssertEqual(package.state.version, "4.1.0")
}

func testDecodingWithBranchV2() throws {
let jsonString = """
{
"identity" : "APIKit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ishkawa/APIKit.git",
"state" : {
"branch" : "master",
"revision" : "86d51ecee0bc0ebdb53fb69b11a24169a69097ba"
}
}
"""

let data = try XCTUnwrap(jsonString.data(using: .utf8))
let package = try JSONDecoder().decode(SwiftPackageV2.self, from: data)

XCTAssertEqual(package.identity, "APIKit")
XCTAssertEqual(package.location, "https://github.com/ishkawa/APIKit.git")
XCTAssertEqual(package.state.revision, "86d51ecee0bc0ebdb53fb69b11a24169a69097ba")
XCTAssertEqual(package.state.branch, "master")
XCTAssertNil(package.state.version)
}

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"))
revision: "e5b50ad7b2e91eeb828393e89b03577b16be7db9",
version: "0.8.0")
let result = package.toGitHub(renames: [:])
XCTAssertEqual(result, GitHub(name: "Commander", nameSpecified: "Commander", owner: "kylef", version: "0.8.0"))
}

func testConvertToGithubNameWithDots() {
let package = SwiftPackage(package: "R.swift.Library",
repositoryURL: "https://github.com/mac-cain13/R.swift.Library",
state: SwiftPackage.State(branch: nil, revision: "3365947d725398694d6ed49f2e6622f05ca3fc0f", version: nil))
revision: "3365947d725398694d6ed49f2e6622f05ca3fc0f",
version: nil)
let result = package.toGitHub(renames: [:])
XCTAssertEqual(result, GitHub(name: "R.swift.Library", nameSpecified: "R.swift.Library", owner: "mac-cain13", version: nil))
}

func testConvertToGithubSSH() {
let package = SwiftPackage(package: "LicensePlist",
repositoryURL: "[email protected]:mono0926/LicensePlist.git",
state: SwiftPackage.State(branch: nil, revision: "3365947d725398694d6ed49f2e6622f05ca3fc0e", version: nil))
revision: "3365947d725398694d6ed49f2e6622f05ca3fc0e",
version: nil)
let result = package.toGitHub(renames: [:])
XCTAssertEqual(result, GitHub(name: "LicensePlist", nameSpecified: "LicensePlist", owner: "mono0926", version: nil))
}

func testConvertToGithubPackageName() {
let package = SwiftPackage(package: "IterableSDK",
repositoryURL: "https://github.com/Iterable/swift-sdk",
state: SwiftPackage.State(branch: nil, revision: "3365947d725398694d6ed49f2e6622f05ca3fc0e", version: nil))
revision: "3365947d725398694d6ed49f2e6622f05ca3fc0e",
version: nil)
let result = package.toGitHub(renames: [:])
XCTAssertEqual(result, GitHub(name: "swift-sdk", nameSpecified: "IterableSDK", owner: "Iterable", version: nil))
}

func testConvertToGithubRenames() {
let package = SwiftPackage(package: "IterableSDK",
repositoryURL: "https://github.com/Iterable/swift-sdk",
state: SwiftPackage.State(branch: nil, revision: "3365947d725398694d6ed49f2e6622f05ca3fc0e", version: nil))
revision: "3365947d725398694d6ed49f2e6622f05ca3fc0e",
version: nil)
let result = package.toGitHub(renames: ["swift-sdk": "NAME"])
XCTAssertEqual(result, GitHub(name: "swift-sdk", nameSpecified: "NAME", owner: "Iterable", version: nil))
}

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"))
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 package = SwiftPackage(package: "Google", repositoryURL: "http://www.google.com", 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"))
revision: "",
version: "0.0.0")
let result = package.toGitHub(renames: [:])
XCTAssertNil(result)
}
Expand All @@ -153,11 +204,13 @@ class SwiftPackageManagerTests: XCTestCase {
let packageFirst = try XCTUnwrap(packages.first)
XCTAssertEqual(packageFirst, SwiftPackage(package: "APIKit",
repositoryURL: "https://github.com/ishkawa/APIKit.git",
state: SwiftPackage.State(branch: nil, revision: "c8f5320d84c4c34c0fd965da3c7957819a1ccdd4", version: "5.2.0")))
revision: "c8f5320d84c4c34c0fd965da3c7957819a1ccdd4",
version: "5.2.0"))
let packageLast = try XCTUnwrap(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")))
revision: "287f5cab7da0d92eb947b5fd8151b203ae04a9a3",
version: "3.4.4"))

}
}

0 comments on commit 10d828d

Please sign in to comment.