diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 6ddebac..820d8ed 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -51,7 +51,7 @@ C4068CAB27B0890D00544CD5 /* MenuBarIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4068CA927B0890D00544CD5 /* MenuBarIcons.swift */; }; C406A5F3298AD2CE00B5B85A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C406A5F2298AD2CE00B5B85A /* main.swift */; }; C406A5F7298AD2CF00B5B85A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C406A5F6298AD2CF00B5B85A /* Assets.xcassets */; }; - C406A602298AD50D00B5B85A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C406A601298AD50D00B5B85A /* AppDelegate.swift */; }; + C406A602298AD50D00B5B85A /* Updater.swift in Sources */ = {isa = PBXBuildFile; fileRef = C406A601298AD50D00B5B85A /* Updater.swift */; }; C4080FF627BD8C6400BF2C6B /* BetterAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4080FF527BD8C6400BF2C6B /* BetterAlert.swift */; }; C4080FF727BD8C6400BF2C6B /* BetterAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4080FF527BD8C6400BF2C6B /* BetterAlert.swift */; }; C4080FFA27BD956700BF2C6B /* BetterAlertVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4080FF927BD956700BF2C6B /* BetterAlertVC.swift */; }; @@ -190,6 +190,8 @@ C469E700294CF7B200A82AB2 /* FakeValetProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = C469E6FD294CF7B200A82AB2 /* FakeValetProxy.swift */; }; C469E701294CF7B200A82AB2 /* FakeValetProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = C469E6FD294CF7B200A82AB2 /* FakeValetProxy.swift */; }; C469E706294CFDF700A82AB2 /* DomainsListTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C469E702294CFDF700A82AB2 /* DomainsListTest.swift */; }; + C46B2648298B324100084651 /* ReleaseManifest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46B2647298B324100084651 /* ReleaseManifest.swift */; }; + C46B2649298B324100084651 /* ReleaseManifest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46B2647298B324100084651 /* ReleaseManifest.swift */; }; C46E206D28299B3800D909D6 /* AppUpdateChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46E206C28299B3800D909D6 /* AppUpdateChecker.swift */; }; C46E206E28299B3800D909D6 /* AppUpdateChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46E206C28299B3800D909D6 /* AppUpdateChecker.swift */; }; C46E20702829D27F00D909D6 /* AppUpdaterCheckTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46E206F2829D27F00D909D6 /* AppUpdaterCheckTest.swift */; }; @@ -765,7 +767,7 @@ C406A5F2298AD2CE00B5B85A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; C406A5F6298AD2CF00B5B85A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; C406A5FB298AD2CF00B5B85A /* phpmon-updater.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "phpmon-updater.entitlements"; sourceTree = ""; }; - C406A601298AD50D00B5B85A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + C406A601298AD50D00B5B85A /* Updater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Updater.swift; sourceTree = ""; }; C4080FF527BD8C6400BF2C6B /* BetterAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BetterAlert.swift; sourceTree = ""; }; C4080FF927BD956700BF2C6B /* BetterAlertVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BetterAlertVC.swift; sourceTree = ""; }; C40C5C9B2846A40600E28255 /* Preset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preset.swift; sourceTree = ""; }; @@ -847,6 +849,7 @@ C464ADB1275A87CA003FCD53 /* DomainListCellProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainListCellProtocol.swift; sourceTree = ""; }; C469E6FD294CF7B200A82AB2 /* FakeValetProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeValetProxy.swift; sourceTree = ""; }; C469E702294CFDF700A82AB2 /* DomainsListTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DomainsListTest.swift; sourceTree = ""; }; + C46B2647298B324100084651 /* ReleaseManifest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReleaseManifest.swift; sourceTree = ""; }; C46E206C28299B3800D909D6 /* AppUpdateChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUpdateChecker.swift; sourceTree = ""; }; C46E206F2829D27F00D909D6 /* AppUpdaterCheckTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppUpdaterCheckTest.swift; sourceTree = ""; }; C46EBC4328DB95F0007ACC74 /* ShellProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellProtocol.swift; sourceTree = ""; }; @@ -1100,7 +1103,8 @@ C406A5F1298AD2CE00B5B85A /* phpmon-updater */ = { isa = PBXGroup; children = ( - C406A601298AD50D00B5B85A /* AppDelegate.swift */, + C406A601298AD50D00B5B85A /* Updater.swift */, + C46B2647298B324100084651 /* ReleaseManifest.swift */, C406A5F2298AD2CE00B5B85A /* main.swift */, C406A5F6298AD2CF00B5B85A /* Assets.xcassets */, C406A5FB298AD2CF00B5B85A /* phpmon-updater.entitlements */, @@ -2056,7 +2060,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C406A602298AD50D00B5B85A /* AppDelegate.swift in Sources */, + C406A602298AD50D00B5B85A /* Updater.swift in Sources */, + C46B2649298B324100084651 /* ReleaseManifest.swift in Sources */, C41F3D08298AED0D0042ACBF /* System.swift in Sources */, C406A5F3298AD2CE00B5B85A /* main.swift in Sources */, ); @@ -2201,6 +2206,7 @@ C4D9ADC8277611A0007277F4 /* InternalSwitcher.swift in Sources */, C4FACE83288F1F9700FC478F /* OnboardingWindowController.swift in Sources */, C4080FFA27BD956700BF2C6B /* BetterAlertVC.swift in Sources */, + C46B2648298B324100084651 /* ReleaseManifest.swift in Sources */, C4BF56AB2949381100379603 /* FakeValetInteractor.swift in Sources */, C4B5635E276AB09000F12CCB /* VersionExtractor.swift in Sources */, C451AFF62969E40F0078E617 /* HelpButton.swift in Sources */, diff --git a/phpmon-updater/ReleaseManifest.swift b/phpmon-updater/ReleaseManifest.swift new file mode 100644 index 0000000..117ba7c --- /dev/null +++ b/phpmon-updater/ReleaseManifest.swift @@ -0,0 +1,14 @@ +// +// UpdateManifest.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 02/02/2023. +// Copyright © 2023 Nico Verbruggen. All rights reserved. +// + +import Foundation + +struct ReleaseManifest: Codable { + let url: String + let sha256: String +} diff --git a/phpmon-updater/AppDelegate.swift b/phpmon-updater/Updater.swift similarity index 67% rename from phpmon-updater/AppDelegate.swift rename to phpmon-updater/Updater.swift index 6b42393..94d572b 100644 --- a/phpmon-updater/AppDelegate.swift +++ b/phpmon-updater/Updater.swift @@ -8,9 +8,10 @@ import Cocoa -class AppDelegate: NSObject, NSApplicationDelegate { +class Updater: NSObject, NSApplicationDelegate { var updaterDirectory: String = "" + var manifest: ReleaseManifest! = nil func applicationDidFinishLaunching(_ aNotification: Notification) { @@ -19,16 +20,25 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.updaterDirectory = "~/.config/phpmon/updater" .replacingOccurrences(of: "~", with: NSHomeDirectory()) + let manifestPath = "\(updaterDirectory)/update.json" + + // Read out the correct information from the manifest JSON + do { + let manifestText = try String(contentsOfFile: manifestPath) + manifest = try JSONDecoder().decode(ReleaseManifest.self, from: manifestText.data(using: .utf8)!) + } catch { + print("Parsing the manifest failed (or the manifest file doesn't exist)") + showAlert( + 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." + ) + exit(0) + } + print("Updater directory set to: \(self.updaterDirectory)") // Download the latest file - let zipPath = self.download( - // zipUrl: "https://github.com/nicoverbruggen/phpmon/releases/download/v5.7.2/phpmon.zip", - // sha256: "654dd1df64ae32b1e3b9ebed7f6d89d04ed374b0b4d6732704e6df190169214f" - - zipUrl: "https://github.com/nicoverbruggen/phpmon/releases/download/v5.7.2/phpmon-dev.zip", - sha256: "1cb147bd1b1fbd52971d90dff577465b644aee7c878f15ede57f46e8f217067a" - ) + let zipPath = self.download(manifest) // Terminating all instances of PHP Monitor first terminatePhpMon() @@ -48,12 +58,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { return false } - private func download(zipUrl: String, sha256: String) -> String { + private func download(_ manifest: ReleaseManifest) -> String { // Remove all zips system_quiet("rm -rf \(updaterDirectory)/*.zip") // Download the file (and follow redirects + no output on failure) - system_quiet("cd \(updaterDirectory) && curl \(zipUrl) -fLO") + system_quiet("cd \(updaterDirectory) && curl \(manifest.url) -fLO") // Identify the downloaded file let filename = system("cd \(updaterDirectory) && ls | grep .zip") @@ -61,19 +71,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { if filename.isEmpty { print("The update has not been downloaded. Sadly, that means that PHP Monitor cannot not updated!") - showAlert(title: "The update was not downloaded.", - description: "PHP Monitor will not be updated, but we will restart the app for you. You may not be connected to the internet or the server may be encountering issues. Please try again later!") - - if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor.app") { - restartPhpMon(dev: false) - } - else if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor DEV.app") { - restartPhpMon(dev: true) - } - else { - exit(1) - } + description: "PHP Monitor has not been updated. You may not be connected to the internet or the server may be encountering issues, or the file could not be written to disk. Please try again later!") + exit(1) } // Calculate the checksum for the downloaded file @@ -82,12 +82,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { print(""" Comparing checksums... - Expected SHA256: \(sha256) + Expected SHA256: \(manifest.sha256) Actual SHA256: \(checksum) """) // Make sure the checksum matches before we do anything with the file - if checksum != sha256 { + if checksum != manifest.sha256 { print("The checksums failed to match. Cancelling!") showAlert( title: "The downloaded update failed checksum validation", @@ -100,11 +100,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { } private func install(zipPath: String) { - system_quiet("rm -rf \(updaterDirectory)/output") - system_quiet("mkdir -p \(updaterDirectory)/output") + system_quiet("rm -rf \(updaterDirectory)/extracted") + system_quiet("mkdir -p \(updaterDirectory)/extracted") var isDirectory: ObjCBool = true - if !FileManager.default.fileExists(atPath: "\(updaterDirectory)/output", isDirectory: &isDirectory) { + if !FileManager.default.fileExists(atPath: "\(updaterDirectory)/extracted", isDirectory: &isDirectory) { showAlert( title: "The updater directory is missing", description: "The automatic updater will quit. Make sure that ` ~/.config/phpmon/updater` is writeable." @@ -112,7 +112,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { exit(0) } - system_quiet("unzip \(zipPath) -d \(updaterDirectory)/output") + system_quiet("unzip \(zipPath) -d \(updaterDirectory)/extracted") let expectedAppName = zipPath.contains("dev") ? "PHP Monitor DEV.app" @@ -121,7 +121,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { print("Removing \(expectedAppName) before replacing...") system_quiet("rm -rf \"/Applications/\(expectedAppName)\"") - system_quiet("mv \"\(updaterDirectory)/output/\(expectedAppName)\" \"/Applications/\(expectedAppName)\"") + system_quiet("mv \"\(updaterDirectory)/extracted/\(expectedAppName)\" \"/Applications/\(expectedAppName)\"") } private func terminatePhpMon() { @@ -140,6 +140,15 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } + private func smartRestartPhpMon() { + if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor.app") { + restartPhpMon(dev: false) + } + else if FileManager.default.fileExists(atPath: "/Applications/PHP Monitor DEV.app") { + restartPhpMon(dev: true) + } + } + 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 diff --git a/phpmon-updater/main.swift b/phpmon-updater/main.swift index 6c711c1..46757f2 100644 --- a/phpmon-updater/main.swift +++ b/phpmon-updater/main.swift @@ -8,10 +8,7 @@ import Cocoa -// 1 let app = NSApplication.shared -let delegate = AppDelegate() +let delegate = Updater() app.delegate = delegate - -// 2 _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)