Skip to content

Commit

Permalink
look up multiple default orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Jun 20, 2019
1 parent f622da7 commit 55f0d19
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .swiftpm/xcode/xcshareddata/xcschemes/xpkg.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@
<CommandLineArguments>
<CommandLineArgument
argument = "install"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "remove"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "settings-shell"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "bogus"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "path"
Expand Down
45 changes: 43 additions & 2 deletions Sources/XPkgCore/XPkg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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@") {
Expand All @@ -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)")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/XPkgTests/XPkgTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down

0 comments on commit 55f0d19

Please sign in to comment.