Skip to content

Commit

Permalink
look up explicit version number when installing
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Jul 16, 2019
1 parent bf936fa commit 937ef2b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .swiftpm/xcode/xcshareddata/xcschemes/xpkg.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<CommandLineArguments>
<CommandLineArgument
argument = "install"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-logs +output,+verbose"
Expand All @@ -94,11 +94,11 @@
</CommandLineArgument>
<CommandLineArgument
argument = "list"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "xpkg-shell"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "shell-hooks"
Expand Down
74 changes: 39 additions & 35 deletions Sources/XPkgCore/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,43 @@ public class Engine {
return xpkgURL.appendingPathComponent("code")
}

internal func remoteExists(_ remote: String) -> Bool {
internal func latestVersion(_ url: URL) -> String? {
let runner = Runner(for: gitURL)
if let result = try? runner.sync(arguments: ["ls-remote", remote, "--exit-code"]) {
return result.status == 0
guard let result = try? runner.sync(arguments: ["ls-remote", "--tags", "--refs", "--sort=v:refname", "--exit-code", url.absoluteString ]), result.status == 0 else {
return nil
}
return false

guard let version = result.stdout.split(separator: "v").last else {
return ""
}

return String(version.trimmingCharacters(in: .whitespacesAndNewlines))
}

internal func remotePackageURL(_ package: String, skipValidation: Bool = false) -> URL {
let remote : URL?
if package.contains("git@") {
remote = URL(string: package)
} else {
let local = URL(fileURLWithPath: package)
if fileManager.fileExists(at: local) {
remote = local
} else if package.contains("/") {
remote = URL(string: "[email protected]:\(package)")
internal func remotePackageURL(_ package: String, skipValidation: Bool = false) -> (URL, String?) {
func validate(_ remote: URL) -> String? {
if skipValidation {
return nil
} else {
// 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 skipValidation || remoteExists(repo) {
found = URL(string: repo)
output.log("Found remote package \(org)/\(package).")
break
}
}
remote = found ?? URL(string: "[email protected]:\(package)")
return latestVersion(remote)
}
}

return remote! // assertion is that this can't fail for a properly formed package name...

if let remote = URL(string: package), let version = validate(remote) {
return (remote, version)
}

if let remote = URL(string: "[email protected]:\(package)"), let version = validate(remote) {
return (remote, version)
}

for org in defaultOrgs {
if let remote = URL(string: "[email protected]:\(org)/\(package)"), let version = validate(remote) {
return (remote, version)
}
}

return (URL(string: "[email protected]:\(package)")!, nil)
}

internal var vaultURL: URL {
Expand Down Expand Up @@ -261,7 +263,7 @@ let package = Package(
try manifestText.write(to: url, atomically: true, encoding: .utf8)
removeManifestCache()
} catch {
print(error)
verbose.log(error)
}
}

Expand Down Expand Up @@ -294,10 +296,11 @@ let package = Package(
for package in after {
if !beforeSet.contains(package) {
do {
try package.run(action: "install", engine: self)
print("Added \(package.name)")
if try package.run(action: "install", engine: self) {
output.log("Added \(package.name)")
}
} catch {
print("Install action for \(package.name) failed.")
output.log("Install action for \(package.name) failed.")
}
}
}
Expand All @@ -306,10 +309,11 @@ let package = Package(
for package in before {
if !afterSet.contains(package) {
do {
try package.run(action:"remove", engine: self)
print("Removed \(package.name).")
if try package.run(action:"remove", engine: self) {
output.log("Removed \(package.name).")
}
} catch {
print("Remove action for \(package.name) failed.")
output.log("Remove action for \(package.name) failed.")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/XPkgCore/InstallCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ struct InstallCommand: Command {
return
}

// resolve the spec to a full url
// resolve the spec to a full url and a version
output.log("Searching for package \(packageSpec)...")
let url = engine.remotePackageURL(packageSpec)
let (url, version) = engine.remotePackageURL(packageSpec)
var updatedManifest = manifest

// add the package to the manifest
engine.verbose.log("Adding package to manifest.")
let newPackage = Package(url: url, version: "1.0.0")
let newPackage = Package(url: url, version: version ?? "")
updatedManifest = manifest
updatedManifest.add(package: newPackage)

Expand Down
32 changes: 21 additions & 11 deletions Sources/XPkgCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,27 +337,37 @@ struct Package: Decodable {
// self.name = newName
}

func run(action: String, engine: Engine) throws {
func run(action: String, engine: Engine) throws -> Bool {
do {
// run as a new-style package
let runner = Runner(for: engine.swiftURL, cwd: engine.vaultURL)
let result = try runner.sync(arguments: ["run", "\(name)-xpkg-hooks", name, path, action])
engine.verbose.log(result.stdout)
if result.status != 0 {
if result.status == 0 {
return true
}

if !result.stdout.contains("no exexcutable product") {
engine.verbose.log("Failed to run \(action) hooks for \(name).")
engine.verbose.log(result.stdout)
engine.verbose.log(result.stderr)
engine.verbose.log("Couldn't run action \(action) - trying fallback.")

// fallback to old method?
let configURL = local.appendingPathComponent(".xpkg.json")
if engine.fileManager.fileExists(atPath: configURL.path) {
let installed = InstalledPackage(local: local, output: engine.output, verbose: engine.verbose)
try installed.run(legacyAction: action, config: configURL)
}
}

// fallback to old method?
let configURL = local.appendingPathComponent(".xpkg.json")
if engine.fileManager.fileExists(atPath: configURL.path) {
let installed = InstalledPackage(local: local, output: engine.output, verbose: engine.verbose)
try installed.run(legacyAction: action, config: configURL)
return true
}

} catch {
engine.output.log("Couldn't run action \(action).")
engine.verbose.log(error)
throw error
}

engine.verbose.log("Ignoring \(name) as it isn't an xpkg package.")
return false
}
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/XPkgCore/ReinstallCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ struct ReinstallCommand: Command {
engine.attempt(action: "Reinstalling \(package.name).") {
do {
engine.verbose.log("Uninstalling \(package.name)")
try package.run(action: "remove", engine: engine)
if try package.run(action: "remove", engine: engine) {
engine.output.log("Removed \(package.name).")
}
engine.verbose.log("Installing \(package.name)")
try package.run(action: "install", engine: engine)
if try package.run(action: "install", engine: engine) {
engine.output.log("Reinstalled \(package.name).")
}
} catch {
engine.output.log("Reinstall of \(package.name) failed.")
engine.verbose.log(error)
Expand Down
6 changes: 3 additions & 3 deletions Tests/XPkgTests/XPkgTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ class XPkgTests: XCTestCase {
let arguments = Arguments(program: "xpkg")
let engine = Engine(arguments: arguments)
engine.defaultOrgs = ["testorg"]
let remote = engine.remotePackageURL("test", skipValidation: true)
let (remote, _) = engine.remotePackageURL("test", skipValidation: true)
XCTAssertEqual(remote, URL(string: "[email protected]:testorg/test"))
}

func testNameOrg() {
let arguments = Arguments(program: "xpkg")
let engine = Engine(arguments: arguments)
let remote = engine.remotePackageURL("someorg/someproj")
let (remote, _) = engine.remotePackageURL("someorg/someproj")
XCTAssertEqual(remote, URL(string: "[email protected]:someorg/someproj"))
}

func testRepo() {
let arguments = Arguments(program: "xpkg")
let engine = Engine(arguments: arguments)
let remote = engine.remotePackageURL("[email protected]:someorg/someproj")
let (remote, _) = engine.remotePackageURL("[email protected]:someorg/someproj")
XCTAssertEqual(remote, URL(string: "[email protected]:someorg/someproj"))
}

Expand Down

0 comments on commit 937ef2b

Please sign in to comment.