mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 03:50:08 +02:00
🏗 Do not make assumptions about zip contents
This commit is contained in:
@ -14,14 +14,17 @@ class Updater: NSObject, NSApplicationDelegate {
|
|||||||
var manifest: ReleaseManifest! = nil
|
var manifest: ReleaseManifest! = nil
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||||
|
|
||||||
print("PHP MONITOR SELF-UPDATER by Nico Verbruggen")
|
print("PHP MONITOR SELF-UPDATER by Nico Verbruggen")
|
||||||
|
|
||||||
self.updaterDirectory = "~/.config/phpmon/updater"
|
self.updaterDirectory = "~/.config/phpmon/updater"
|
||||||
.replacingOccurrences(of: "~", with: NSHomeDirectory())
|
.replacingOccurrences(of: "~", with: NSHomeDirectory())
|
||||||
|
|
||||||
|
print("Updater directory set to: \(self.updaterDirectory)")
|
||||||
|
|
||||||
let manifestPath = "\(updaterDirectory)/update.json"
|
let manifestPath = "\(updaterDirectory)/update.json"
|
||||||
|
|
||||||
|
print("Checking manifest file at \(manifestPath)")
|
||||||
|
|
||||||
// Read out the correct information from the manifest JSON
|
// Read out the correct information from the manifest JSON
|
||||||
do {
|
do {
|
||||||
let manifestText = try String(contentsOfFile: manifestPath)
|
let manifestText = try String(contentsOfFile: manifestPath)
|
||||||
@ -30,24 +33,22 @@ class Updater: NSObject, NSApplicationDelegate {
|
|||||||
print("Parsing the manifest failed (or the manifest file doesn't exist)")
|
print("Parsing the manifest failed (or the manifest file doesn't exist)")
|
||||||
showAlert(
|
showAlert(
|
||||||
title: "Key information about the update is missing",
|
title: "Key information about the update is missing",
|
||||||
description: "The self-updater only works in combination with PHP Monitor. Please try searching for updates again in PHP Monitor. The app has not been updated."
|
description: "The app has not been updated. The self-updater only works in combination with PHP Monitor. Please try searching for updates again in PHP Monitor."
|
||||||
)
|
)
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Updater directory set to: \(self.updaterDirectory)")
|
|
||||||
|
|
||||||
// Download the latest file
|
// Download the latest file
|
||||||
let zipPath = self.download(manifest)
|
let zipPath = self.download(manifest)
|
||||||
|
|
||||||
// Terminating all instances of PHP Monitor first
|
// Terminate all instances of PHP Monitor first
|
||||||
terminatePhpMon()
|
terminatePhpMon()
|
||||||
|
|
||||||
// We made it!
|
// Install the app based on the zip
|
||||||
install(zipPath: zipPath)
|
let appPath = extractAndInstall(zipPath: zipPath)
|
||||||
|
|
||||||
// Restart PHP Monitor, this will also close the updater
|
// Restart PHP Monitor, this will also close the updater
|
||||||
restartPhpMon(dev: zipPath.contains("dev"))
|
restartPhpMon(at: appPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func applicationWillTerminate(_ aNotification: Notification) {
|
func applicationWillTerminate(_ aNotification: Notification) {
|
||||||
@ -99,10 +100,14 @@ class Updater: NSObject, NSApplicationDelegate {
|
|||||||
return "\(updaterDirectory)/\(filename)"
|
return "\(updaterDirectory)/\(filename)"
|
||||||
}
|
}
|
||||||
|
|
||||||
private func install(zipPath: String) {
|
private func extractAndInstall(zipPath: String) -> String {
|
||||||
|
// Remove the directory that will contain the extracted update
|
||||||
system_quiet("rm -rf \(updaterDirectory)/extracted")
|
system_quiet("rm -rf \(updaterDirectory)/extracted")
|
||||||
|
|
||||||
|
// Recreate the directory where we will unzip the .app file
|
||||||
system_quiet("mkdir -p \(updaterDirectory)/extracted")
|
system_quiet("mkdir -p \(updaterDirectory)/extracted")
|
||||||
|
|
||||||
|
// Make sure the updater directory exists
|
||||||
var isDirectory: ObjCBool = true
|
var isDirectory: ObjCBool = true
|
||||||
if !FileManager.default.fileExists(atPath: "\(updaterDirectory)/extracted", isDirectory: &isDirectory) {
|
if !FileManager.default.fileExists(atPath: "\(updaterDirectory)/extracted", isDirectory: &isDirectory) {
|
||||||
showAlert(
|
showAlert(
|
||||||
@ -112,29 +117,46 @@ class Updater: NSObject, NSApplicationDelegate {
|
|||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unzip the file
|
||||||
system_quiet("unzip \(zipPath) -d \(updaterDirectory)/extracted")
|
system_quiet("unzip \(zipPath) -d \(updaterDirectory)/extracted")
|
||||||
|
|
||||||
let expectedAppName = zipPath.contains("dev")
|
// Find the .app file
|
||||||
? "PHP Monitor DEV.app"
|
let app = system("ls \(updaterDirectory)/extracted | grep .app")
|
||||||
: "PHP Monitor.app"
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
|
||||||
print("Removing \(expectedAppName) before replacing...")
|
print("Finished extracting: \(updaterDirectory)/extracted/\(app)")
|
||||||
|
|
||||||
system_quiet("rm -rf \"/Applications/\(expectedAppName)\"")
|
// Make sure the file was extracted
|
||||||
system_quiet("mv \"\(updaterDirectory)/extracted/\(expectedAppName)\" \"/Applications/\(expectedAppName)\"")
|
if app.isEmpty {
|
||||||
|
showAlert(
|
||||||
|
title: "The downloaded file could not be extracted",
|
||||||
|
description: "The automatic updater will quit. Make sure that ` ~/.config/phpmon/updater` is writeable."
|
||||||
|
)
|
||||||
|
exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
print("Removing \(app) before replacing...")
|
||||||
|
|
||||||
|
system_quiet("rm -rf \"/Applications/\(app)\"")
|
||||||
|
system_quiet("mv \"\(updaterDirectory)/extracted/\(app)\" \"/Applications/\(app)\"")
|
||||||
|
|
||||||
|
return "/Applications/\(app)"
|
||||||
}
|
}
|
||||||
|
|
||||||
private func terminatePhpMon() {
|
private func terminatePhpMon() {
|
||||||
let runningApplications = NSWorkspace.shared.runningApplications
|
let runningApplications = NSWorkspace.shared.runningApplications
|
||||||
|
|
||||||
|
// Look for these instances
|
||||||
let ids = [
|
let ids = [
|
||||||
"com.nicoverbruggen.phpmon.dev",
|
"com.nicoverbruggen.phpmon.dev",
|
||||||
"com.nicoverbruggen.phpmon"
|
"com.nicoverbruggen.phpmon"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// Terminate all instances found
|
||||||
for id in ids {
|
for id in ids {
|
||||||
if let phpmon = runningApplications
|
if let phpmon = runningApplications.first(where: {
|
||||||
.first(where: { (application) in return application.bundleIdentifier == id }) {
|
(application) in return application.bundleIdentifier == id
|
||||||
|
}) {
|
||||||
phpmon.terminate()
|
phpmon.terminate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,19 +164,17 @@ class Updater: NSObject, NSApplicationDelegate {
|
|||||||
|
|
||||||
private func smartRestartPhpMon() {
|
private func smartRestartPhpMon() {
|
||||||
if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor.app") {
|
if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor.app") {
|
||||||
restartPhpMon(dev: false)
|
restartPhpMon(at: "/Applications/PHP Monitor.app")
|
||||||
}
|
}
|
||||||
else if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor DEV.app") {
|
else if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor DEV.app") {
|
||||||
restartPhpMon(dev: true)
|
restartPhpMon(at: "/Applications/PHP Monitor DEV.app")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func restartPhpMon(dev: Bool) {
|
private func restartPhpMon(at path: String) {
|
||||||
let path = dev ? "/Applications/PHP Monitor DEV.app" : "/Applications/PHP Monitor.app"
|
|
||||||
let url = NSURL(fileURLWithPath: path, isDirectory: true) as URL
|
let url = NSURL(fileURLWithPath: path, isDirectory: true) as URL
|
||||||
let configuration = NSWorkspace.OpenConfiguration()
|
let configuration = NSWorkspace.OpenConfiguration()
|
||||||
NSWorkspace.shared.openApplication(at: url, configuration: configuration) { phpmon, error in
|
NSWorkspace.shared.openApplication(at: url, configuration: configuration) { phpmon, error in
|
||||||
// Once we've opened PHP Monitor again... quit the updater
|
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user