-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,34 @@ import Arguments | |
import Foundation | ||
import Logger | ||
|
||
extension URLSession { | ||
func synchronousDataTask(with request: URLRequest) -> (Data?, URLResponse?, Error?) { | ||
var data: Data? | ||
var response: URLResponse? | ||
var error: Error? | ||
|
||
let semaphore = DispatchSemaphore(value: 0) | ||
|
||
let dataTask = self.dataTask(with: request) { | ||
data = $0 | ||
response = $1 | ||
error = $2 | ||
|
||
semaphore.signal() | ||
} | ||
dataTask.resume() | ||
|
||
_ = semaphore.wait(timeout: .distantFuture) | ||
|
||
return (data, response, error) | ||
} | ||
} | ||
|
||
public class XPkg { | ||
let arguments: Arguments | ||
let output = Logger.stdout | ||
let verbose = Logger("verbose") | ||
var defaultOrg = "elegantchaos" // TODO: read from preference | ||
var defaultOrgs = ["elegantchaos", "samdeane"] // TODO: read from preference | ||
|
||
let commands: [String:Command] = [ | ||
"check": CheckCommand(), | ||
|
@@ -68,6 +91,14 @@ public class XPkg { | |
return xpkgURL.appendingPathComponent("code") | ||
} | ||
|
||
internal func remoteExists(_ remote: String) -> Bool { | ||
let runner = Runner() | ||
if let result = try? runner.sync(gitURL, arguments: ["ls-remote", remote, "--exit-code"]) { | ||
return result.status == 0 | ||
} | ||
return false | ||
} | ||
|
||
internal func remotePackageURL(_ package: String) -> URL { | ||
let remote : URL? | ||
if package.contains("git@") { | ||
|
@@ -79,7 +110,17 @@ public class XPkg { | |
} else if package.contains("/") { | ||
remote = URL(string: "[email protected]:\(package)") | ||
} else { | ||
remote = URL(string: "[email protected]:\(defaultOrg)/\(package)") | ||
// iterate default orgs, looking for a repo that exists | ||
// if we don't find any, we just default to the unqualified package - knowing that it's probably wrong | ||
var found: URL? = nil | ||
for org in defaultOrgs { | ||
let repo = "[email protected]:\(org)/\(package)" | ||
if remoteExists(repo) { | ||
found = URL(string: repo) | ||
break | ||
} | ||
} | ||
remote = found ?? URL(string: "[email protected]:\(package)") | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -14,7 +14,7 @@ class XPkgTests: XCTestCase { | |
func testName() { | ||
let arguments = Arguments(program: "xpkg") | ||
let engine = XPkg(arguments: arguments) | ||
engine.defaultOrg = "testorg" | ||
engine.defaultOrgs = ["testorg"] | ||
let remote = engine.remotePackageURL("test") | ||
XCTAssertEqual(remote, URL(string: "[email protected]:testorg/test")) | ||
} | ||
|