Skip to content

Commit

Permalink
moved standard hook support into XPkgAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Jul 16, 2019
1 parent b85745d commit bf936fa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
44 changes: 38 additions & 6 deletions Sources/XPkgAPI/InstalledPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ public struct InstalledPackage {
self.verbose = verbose
}

public init(fromCommandLine arguments: [String]) {
guard arguments.count > 3 else {
exit(1)
}

// let command = arguments[0]
// let name = arguments[1]

let localPath = arguments[2]
let localURL = URL(fileURLWithPath: localPath)

self.local = localURL
self.output = Logger.stdout
self.verbose = Channel("verbose")
}

public func performAction(fromCommandLine arguments: [String], links: [ManifestLink], commands: [ManifestLink] = []) throws {
let action = arguments[3]
switch action {
case "install":
manageLinks(creating: links)
try run(commands: commands)

case "remove":
try run(commands: commands)
manageLinks(removing: links)

default:
output.log("Unrecognised action \(action).")
}

}
/**
The directory to use for binary links.
By default we use the user's local bin.
Expand Down Expand Up @@ -78,7 +110,7 @@ public struct InstalledPackage {
Run through a list of linkSpecs and create each one.
*/

public func links(create links:[ManifestLink]?) {
public func manageLinks(creating links:[ManifestLink]?) {
let fileManager = FileManager.default
if let links = links {
for link in links {
Expand All @@ -101,7 +133,7 @@ public struct InstalledPackage {
Run through a list of linkSpecs and remove each one.
*/

public func links(remove links:[ManifestLink]?) {
public func manageLinks(removing links:[ManifestLink]?) {
let fileManager = FileManager.default
if let links = links {
for link in links {
Expand Down Expand Up @@ -155,12 +187,12 @@ public struct InstalledPackage {
if let manifest = try? decoder.decode(Manifest.self, from: Data(contentsOf: url)) {
switch (action) {
case "install":
links(create: manifest.links)
manageLinks(creating: manifest.links)
try run(commands: manifest.install)

case "remove":
try run(commands: manifest.remove)
links(remove: manifest.links)
manageLinks(removing: manifest.links)

default:
output.log("Unknown action \(action).")
Expand All @@ -181,8 +213,8 @@ public struct InstalledPackage {
let tool = command[0]
let arguments = Array(command.dropFirst())
switch(tool) {
case "link": links(create: [arguments])
case "unlink": links(remove: [arguments])
case "link": manageLinks(creating: [arguments])
case "unlink": manageLinks(removing: [arguments])
default: try external(command: tool, arguments: arguments)
}
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/XPkgCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,11 @@ struct Package: Decodable {
do {
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 {
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) {
Expand Down
13 changes: 10 additions & 3 deletions Sources/XPkgCore/ReinstallCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ struct ReinstallCommand: Command {
let manifest = engine.loadManifest()
let package = engine.existingPackage(manifest: manifest)

engine.attempt(action: "Reinstall") {
try package.run(action: "remove", engine: engine)
try package.run(action: "install", engine: engine)
engine.attempt(action: "Reinstalling \(package.name).") {
do {
engine.verbose.log("Uninstalling \(package.name)")
try package.run(action: "remove", engine: engine)
engine.verbose.log("Installing \(package.name)")
try package.run(action: "install", engine: engine)
} catch {
engine.output.log("Reinstall of \(package.name) failed.")
engine.verbose.log(error)
}
}
}
}

0 comments on commit bf936fa

Please sign in to comment.