From 29b4fe2962d82f5912a22fc81b2f46bb2640756e Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Thu, 2 Jun 2022 20:31:01 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Load=20persisted=20revert=20&=20all?= =?UTF-8?q?ow=20revert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also moves the location of the .phpmon.conf.json file to a new location: ~/.config/phpmon/config.json. --- PHP Monitor.xcodeproj/project.pbxproj | 6 ++++ README.md | 2 +- phpmon/Domain/Menu/MainMenu+Actions.swift | 8 +++++ phpmon/Domain/Menu/MainMenu+Startup.swift | 37 ++++++++++++--------- phpmon/Domain/Menu/StatusMenu+Items.swift | 7 ++-- phpmon/Domain/Preferences/Preferences.swift | 10 +++--- phpmon/Domain/Presets/Preset.swift | 4 +++ phpmon/Domain/Presets/PresetHelper.swift | 37 +++++++++++++++++++++ 8 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 phpmon/Domain/Presets/PresetHelper.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index e040153..eff0c4f 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -123,6 +123,8 @@ C44CCD4A27AFF3BC00CE40E5 /* MainMenu+Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = C44CCD4827AFF3B700CE40E5 /* MainMenu+Async.swift */; }; C44F868E2835BD8D005C353A /* phpmon-config.json in Resources */ = {isa = PBXBuildFile; fileRef = C44F868D2835BD8D005C353A /* phpmon-config.json */; }; C459B4BD27F6093700E9B4B4 /* nginx-proxy.test in Resources */ = {isa = PBXBuildFile; fileRef = C459B4BC27F6093700E9B4B4 /* nginx-proxy.test */; }; + C463E380284930EE00422731 /* PresetHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C463E37F284930EE00422731 /* PresetHelper.swift */; }; + C463E381284930EE00422731 /* PresetHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C463E37F284930EE00422731 /* PresetHelper.swift */; }; C464ADAC275A7A3F003FCD53 /* DomainListWC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C464ADAB275A7A3F003FCD53 /* DomainListWC.swift */; }; C464ADAD275A7A3F003FCD53 /* DomainListWC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C464ADAB275A7A3F003FCD53 /* DomainListWC.swift */; }; C464ADAF275A7A69003FCD53 /* DomainListVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = C464ADAE275A7A69003FCD53 /* DomainListVC.swift */; }; @@ -360,6 +362,7 @@ C44CCD4827AFF3B700CE40E5 /* MainMenu+Async.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MainMenu+Async.swift"; sourceTree = ""; }; C44F868D2835BD8D005C353A /* phpmon-config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "phpmon-config.json"; sourceTree = ""; }; C459B4BC27F6093700E9B4B4 /* nginx-proxy.test */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "nginx-proxy.test"; sourceTree = ""; }; + C463E37F284930EE00422731 /* PresetHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresetHelper.swift; sourceTree = ""; }; C464ADAB275A7A3F003FCD53 /* DomainListWC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainListWC.swift; sourceTree = ""; }; C464ADAE275A7A69003FCD53 /* DomainListVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainListVC.swift; sourceTree = ""; }; C464ADB1275A87CA003FCD53 /* DomainListCellProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainListCellProtocol.swift; sourceTree = ""; }; @@ -561,6 +564,7 @@ isa = PBXGroup; children = ( C40C5C9B2846A40600E28255 /* Preset.swift */, + C463E37F284930EE00422731 /* PresetHelper.swift */, ); path = Presets; sourceTree = ""; @@ -1216,6 +1220,7 @@ C41E871A2763D42300161EE0 /* DomainListVC+ContextMenu.swift in Sources */, C48D0CA325CC992000CC7490 /* StatsView.swift in Sources */, C40C7F2827721FF600DDDCDC /* ActivePhpInstallation+Checks.swift in Sources */, + C463E380284930EE00422731 /* PresetHelper.swift in Sources */, C4EE55A927708B9E001DF387 /* PMHeaderView.swift in Sources */, C41C02A927E61A65009F26CB /* ValetSite+Fake.swift in Sources */, C4C0E8DF27F88AEB002D32A9 /* FakeSiteScanner.swift in Sources */, @@ -1347,6 +1352,7 @@ C4C0E8E327F88B13002D32A9 /* ValetSiteScanner.swift in Sources */, C4CCBA6D275C567B008C7055 /* PMWindowController.swift in Sources */, C4B5635F276AB09000F12CCB /* VersionExtractor.swift in Sources */, + C463E381284930EE00422731 /* PresetHelper.swift in Sources */, C46FA98C2822F08F00D78807 /* PhpConfigurationTest.swift in Sources */, C4BF90C127C57C220054E78C /* MainMenu+FixMyValet.swift in Sources */, C4C0E8EB27F88B80002D32A9 /* ValetProxy+Fake.swift in Sources */, diff --git a/README.md b/README.md index 98aaeca..5223b62 100644 --- a/README.md +++ b/README.md @@ -309,7 +309,7 @@ All of these apps should just be detected correctly, no matter their location on To see which files are checked to determine availability, see [this file](./phpmon/Domain/Helpers/Application.swift). -You can add your own apps by creating and editing a `~/.phpmon.conf.json` file, with the following entry: +You can add your own apps by creating and editing a `~/.config/phpmon/config.json` file, with the following entry:
 {
diff --git a/phpmon/Domain/Menu/MainMenu+Actions.swift b/phpmon/Domain/Menu/MainMenu+Actions.swift
index 7ee6502..ad95113 100644
--- a/phpmon/Domain/Menu/MainMenu+Actions.swift
+++ b/phpmon/Domain/Menu/MainMenu+Actions.swift
@@ -145,6 +145,14 @@ extension MainMenu {
         }
     }
 
+    @objc func rollbackPreset() {
+        asyncExecution {
+            PresetHelper.rollbackPreset?.apply()
+            PresetHelper.rollbackPreset = nil
+            MainMenu.shared.rebuild()
+        }
+    }
+
     @objc func togglePreset(sender: PresetMenuItem) {
         asyncExecution {
             sender.preset?.apply()
diff --git a/phpmon/Domain/Menu/MainMenu+Startup.swift b/phpmon/Domain/Menu/MainMenu+Startup.swift
index 43badf9..759ae3d 100644
--- a/phpmon/Domain/Menu/MainMenu+Startup.swift
+++ b/phpmon/Domain/Menu/MainMenu+Startup.swift
@@ -62,22 +62,10 @@ extension MainMenu {
         App.shared.handlePhpConfigWatcher()
 
         // Detect applications (preset + custom)
-        Log.info("Detecting applications...")
-        App.shared.detectedApplications = Application.detectPresetApplications()
+        self.loadApps()
 
-        let customApps = Preferences.custom.scanApps.map { appName in
-            return Application(appName, .user_supplied)
-        }.filter { app in
-            return app.isInstalled()
-        }
-
-        App.shared.detectedApplications.append(contentsOf: customApps)
-
-        let appNames = App.shared.detectedApplications.map { app in
-            return app.name
-        }
-
-        Log.info("Detected applications: \(appNames)")
+        // Load the rollback preset
+        PresetHelper.loadRollbackPresetFromFile()
 
         // Load the global hotkey
         App.shared.loadGlobalHotkey()
@@ -133,4 +121,23 @@ extension MainMenu {
             Task { await startup() }
         }
     }
+
+    private func loadApps() {
+        Log.info("Detecting applications...")
+        App.shared.detectedApplications = Application.detectPresetApplications()
+
+        let customApps = Preferences.custom.scanApps.map { appName in
+            return Application(appName, .user_supplied)
+        }.filter { app in
+            return app.isInstalled()
+        }
+
+        App.shared.detectedApplications.append(contentsOf: customApps)
+
+        let appNames = App.shared.detectedApplications.map { app in
+            return app.name
+        }
+
+        Log.info("Detected applications: \(appNames)")
+    }
 }
diff --git a/phpmon/Domain/Menu/StatusMenu+Items.swift b/phpmon/Domain/Menu/StatusMenu+Items.swift
index 302181d..224d729 100644
--- a/phpmon/Domain/Menu/StatusMenu+Items.swift
+++ b/phpmon/Domain/Menu/StatusMenu+Items.swift
@@ -103,8 +103,11 @@ extension StatusMenu {
         presetsMenu.addItem(NSMenuItem.separator())
         presetsMenu.addItem(NSMenuItem(
             title: "Revert to Previous Configuration...",
-            action: #selector(MainMenu.restartDnsMasq), keyEquivalent: "")
-        )
+            action: PresetHelper.rollbackPreset != nil
+                ? #selector(MainMenu.rollbackPreset)
+                : nil,
+            keyEquivalent: ""
+        ))
         presetsMenu.addItem(NSMenuItem.separator())
         presetsMenu.addItem(NSMenuItem(
             title: "\(Preferences.custom.presets.count) profiles loaded from configuration file",
diff --git a/phpmon/Domain/Preferences/Preferences.swift b/phpmon/Domain/Preferences/Preferences.swift
index 196b062..e2b009d 100644
--- a/phpmon/Domain/Preferences/Preferences.swift
+++ b/phpmon/Domain/Preferences/Preferences.swift
@@ -173,12 +173,12 @@ class Preferences {
     // MARK: - Custom Preferences
 
     private func loadCustomPreferences() {
-        let url = URL(fileURLWithPath: "/Users/\(Paths.whoami)/.phpmon.conf.json")
+        let url = URL(fileURLWithPath: "/Users/\(Paths.whoami)/.config/phpmon/config.json")
         if Filesystem.fileExists(url.path) {
-            Log.info("A custom .phpmon.conf.json file was found. Attempting to parse...")
+            Log.info("A custom ~/.config/phpmon/config.json file was found. Attempting to parse...")
             loadCustomPreferencesFile(url)
         } else {
-            Log.info("There was no .phpmon.conf.json file to be loaded.")
+            Log.info("There was no /.config/phpmon/config.json file to be loaded.")
         }
     }
 
@@ -189,10 +189,10 @@ class Preferences {
                 from: try! String(contentsOf: url, encoding: .utf8).data(using: .utf8)!
             )
 
-            Log.info("The .phpmon.conf.json file was successfully parsed.")
+            Log.info("The ~/.config/phpmon/config.json file was successfully parsed.")
             Log.info("There are \(customPreferences.presets.count) custom presets.")
         } catch {
-            Log.warn("The .phpmon.conf.json file seems to be missing or malformed.")
+            Log.warn("The ~/.config/phpmon/config.json file seems to be missing or malformed.")
         }
     }
 
diff --git a/phpmon/Domain/Presets/Preset.swift b/phpmon/Domain/Presets/Preset.swift
index f89e665..5340e7b 100644
--- a/phpmon/Domain/Presets/Preset.swift
+++ b/phpmon/Domain/Presets/Preset.swift
@@ -51,6 +51,10 @@ struct Preset: Codable {
                 }
             }
 
+            // Reload what rollback file exists
+            PresetHelper.loadRollbackPresetFromFile()
+
+            // Restart PHP FPM process (also reloads menu, which will show the preset rollback)
             Actions.restartPhpFpm()
         }
     }
diff --git a/phpmon/Domain/Presets/PresetHelper.swift b/phpmon/Domain/Presets/PresetHelper.swift
new file mode 100644
index 0000000..86bc0ae
--- /dev/null
+++ b/phpmon/Domain/Presets/PresetHelper.swift
@@ -0,0 +1,37 @@
+//
+//  PresetHelper.swift
+//  PHP Monitor
+//
+//  Created by Nico Verbruggen on 02/06/2022.
+//  Copyright © 2022 Nico Verbruggen. All rights reserved.
+//
+
+import Foundation
+
+class PresetHelper {
+
+    static var rollbackPreset: Preset?
+
+    // MARK: - Reloading Configuration
+
+    public static func loadRollbackPresetFromFile() {
+        guard let revert = try? String(
+            contentsOfFile: "/Users/\(Paths.whoami)/.config/phpmon/preset_revert.json",
+            encoding: .utf8
+        ) else {
+            PresetHelper.rollbackPreset = nil
+            return
+        }
+
+        guard let preset = try? JSONDecoder().decode(
+            Preset.self,
+            from: revert.data(using: .utf8)!
+        ) else {
+            PresetHelper.rollbackPreset = nil
+            return
+        }
+
+        PresetHelper.rollbackPreset = preset
+    }
+
+}