-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from mbuchetics/spm
Support for Swift Package Manager (Package.swift) dependencies
- Loading branch information
Showing
10 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// SwiftPackage.swift | ||
// LicensePlistCore | ||
// | ||
// Created by Matthias Buchetics on 20.09.19. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct SwiftPackage: Decodable, Equatable { | ||
struct State: Decodable, Equatable { | ||
let branch: String? | ||
let revision: String? | ||
let version: String | ||
} | ||
|
||
let package: String | ||
let repositoryURL: String | ||
let state: State | ||
} | ||
|
||
fileprivate 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, | ||
version: state.version) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
Tests/LicensePlistTests/Entity/SwiftPackageManagerTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// SwiftPackageManagerTests.swift | ||
// APIKit | ||
// | ||
// Created by Matthias Buchetics on 20.09.19. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import LicensePlistCore | ||
|
||
class SwiftPackageManagerTests: XCTestCase { | ||
|
||
func testDecoding() { | ||
let jsonString = """ | ||
{ | ||
"package": "APIKit", | ||
"repositoryURL": "https://github.com/ishkawa/APIKit.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "86d51ecee0bc0ebdb53fb69b11a24169a69097ba", | ||
"version": "4.1.0" | ||
} | ||
} | ||
""" | ||
|
||
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 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 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 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"))) | ||
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"))) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "APIKit", | ||
"repositoryURL": "https://github.com/ishkawa/APIKit.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "86d51ecee0bc0ebdb53fb69b11a24169a69097ba", | ||
"version": "4.1.0" | ||
} | ||
}, | ||
{ | ||
"package": "Commander", | ||
"repositoryURL": "https://github.com/kylef/Commander.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "e5b50ad7b2e91eeb828393e89b03577b16be7db9", | ||
"version": "0.8.0" | ||
} | ||
}, | ||
{ | ||
"package": "HeliumLogger", | ||
"repositoryURL": "https://github.com/IBM-Swift/HeliumLogger.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "779865e83149a59894b14950aa83f70b7e81bc27", | ||
"version": "1.8.1" | ||
} | ||
}, | ||
{ | ||
"package": "LoggerAPI", | ||
"repositoryURL": "https://github.com/IBM-Swift/LoggerAPI.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "e29073bb7cecf3673e56bcb16180e8fd0cb091f6", | ||
"version": "1.8.1" | ||
} | ||
}, | ||
{ | ||
"package": "Result", | ||
"repositoryURL": "https://github.com/antitypical/Result.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "2ca499ba456795616fbc471561ff1d963e6ae160", | ||
"version": "4.1.0" | ||
} | ||
}, | ||
{ | ||
"package": "Spectre", | ||
"repositoryURL": "https://github.com/kylef/Spectre.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "f14ff47f45642aa5703900980b014c2e9394b6e5", | ||
"version": "0.9.0" | ||
} | ||
}, | ||
{ | ||
"package": "HTMLEntities", | ||
"repositoryURL": "https://github.com/IBM-Swift/swift-html-entities.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "3b778b3ab061684db024eaf38c576887b42918aa", | ||
"version": "3.0.13" | ||
} | ||
}, | ||
{ | ||
"package": "Yaml", | ||
"repositoryURL": "https://github.com/behrang/YamlSwift.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "287f5cab7da0d92eb947b5fd8151b203ae04a9a3", | ||
"version": "3.4.4" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |