Skip to content

Commit

Permalink
Only look up release version for github urls
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Mar 11, 2020
1 parent fd65243 commit 6e9b993
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/ArenaCore/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension Dependency: ExpressibleByArgument {
(true, true, _): // existing path - keep as is
self = dep
case (false, _, false): // url without version - look up version
let req = Repository(url: dep.url)
let req = GithubRepository(url: dep.url)
.flatMap { Current.githubClient.latestRelease($0)?.version }
.map { .from($0) }
?? Dependency.defaultRequirement
Expand Down
9 changes: 6 additions & 3 deletions Sources/ArenaCore/Github.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Parser


struct GithubClient {
var latestRelease: (Repository) -> Release?
var latestRelease: (GithubRepository) -> Release?
}


Expand All @@ -38,12 +38,15 @@ struct Release: Decodable {
}


struct Repository: CustomStringConvertible {
struct GithubRepository: CustomStringConvertible {
let owner: String
let repository: String
var description: String { owner + "/" + repository}

init?(url: URL) {
guard url.host == "github.com" else {
return nil
}
let path = url.path
guard path.hasPrefix("/") else { return nil }
let parts = path.dropFirst().split(separator: "/")
Expand All @@ -61,7 +64,7 @@ func latestReleaseURL(repository: String) -> URL? {
}


func latestReleaseRequest(for repository: Repository) -> Release? {
func latestReleaseRequest(for repository: GithubRepository) -> Release? {
guard let url = latestReleaseURL(repository: repository.description) else {
return nil
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/ArenaTests/ArenaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,34 @@ final class ArenaTests: XCTestCase {
XCTAssertEqual(dep.packageClause, #".package(path: "/foo/bar")"#)
}
}

func test_latestRelease() throws {
Current.githubClient.latestRelease = { _ in Release(tagName: "1.2.3") }
do { // github url
Current.fileManager.fileExists = { _ in false }
let args = ["https://github.com/finestructure/gala"]
let res = try Arena.parse(args)
XCTAssertEqual(res.dependencies, [
Dependency(url: URL(string: "https://github.com/finestructure/gala")!, requirement: .from("1.2.3")),
])
}
do { // github shorthand
Current.fileManager.fileExists = { _ in false }
let args = ["finestructure/gala"]
let res = try Arena.parse(args)
XCTAssertEqual(res.dependencies, [
Dependency(url: URL(string: "https://github.com/finestructure/gala")!, requirement: .from("1.2.3")),
])
}
do { // other url
Current.fileManager.fileExists = { _ in false }
let args = ["https://gitlab.com/finestructure/foo"]
let res = try Arena.parse(args)
XCTAssertEqual(res.dependencies, [
Dependency(url: URL(string: "https://gitlab.com/finestructure/foo")!, requirement: .from("0.0.0")),
])
}
}
}


Expand Down

0 comments on commit 6e9b993

Please sign in to comment.