From 173206bed9933bef8993628d6cd410880fadeb59 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Wed, 1 Feb 2023 19:31:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Let=20self-updater=20quit=20PHP?= =?UTF-8?q?=20Monitor=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon-updater/AppDelegate.swift | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/phpmon-updater/AppDelegate.swift b/phpmon-updater/AppDelegate.swift index 5f07354..8aa4380 100644 --- a/phpmon-updater/AppDelegate.swift +++ b/phpmon-updater/AppDelegate.swift @@ -10,12 +10,20 @@ import Cocoa class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { + print("PHP MONITOR SELF-UPDATER by Nico Verbruggen") + + // Figure out where the updater would like to find the let path = "~/config/phpmon/updater/phpmon.zip" .replacingOccurrences(of: "~", with: NSHomeDirectory()) + // Terminating all instances of PHP Monitor first + terminatePhpMon() + + // Checking if the updated file exists print("Checking path: \(path)") + // If the file does not exist, exit gracefully if !FileManager.default.fileExists(atPath: path) { print("The update has not been downloaded. Sadly, that means we will not update!") @@ -42,12 +50,28 @@ class AppDelegate: NSObject, NSApplicationDelegate { return true } + private func terminatePhpMon() { + let runningApplications = NSWorkspace.shared.runningApplications + + let ids = [ + "com.nicoverbruggen.phpmon.dev", + "com.nicoverbruggen.phpmon" + ] + + for id in ids { + if let phpmon = runningApplications + .first(where: { (application) in return application.bundleIdentifier == id }) { + phpmon.terminate() + } + } + } + private func restartPhpMon(dev: Bool) { let path = dev ? "/Applications/PHP Monitor DEV.app" : "/Applications/PHP Monitor.app" let url = NSURL(fileURLWithPath: path, isDirectory: true) as URL let configuration = NSWorkspace.OpenConfiguration() - NSWorkspace.shared.openApplication(at: url, configuration: configuration) { app, error in - print(app) + NSWorkspace.shared.openApplication(at: url, configuration: configuration) { phpmon, error in + // Once we've opened PHP Monitor again... quit the updater exit(0) } }