From cf27f0e071f50b8e83bd2e7c86d2f4f4abc4fb18 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 3 Mar 2026 17:28:00 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Use=20protocol=20so=20custom=20expo?= =?UTF-8?q?rts=20are=20easier=20to=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Core/Actions.swift | 3 +-- phpmon/Common/Monitoring/TrackedShell.swift | 23 +++------------------ phpmon/Common/Shell/ShellProtocol.swift | 8 ++++++- phpmon/Domain/Preferences/CustomPrefs.swift | 5 ++--- 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/phpmon/Common/Core/Actions.swift b/phpmon/Common/Core/Actions.swift index 7a4342db..861cb31f 100644 --- a/phpmon/Common/Core/Actions.swift +++ b/phpmon/Common/Core/Actions.swift @@ -98,8 +98,7 @@ class Actions { public func openGlobalComposerFolder() { // Check if we have a custom COMPOSER_HOME set - if let shell = App.shared.container.shell as? TrackedShell, - let folder = shell.customExports["COMPOSER_HOME"] { + if let folder = App.shared.container.shell.exports["COMPOSER_HOME"] { let file = URL(string: "file://\(folder)/composer.json".replacingTildeWithHomeDirectory)! return NSWorkspace.shared.activateFileViewerSelecting([file] as [URL]) } diff --git a/phpmon/Common/Monitoring/TrackedShell.swift b/phpmon/Common/Monitoring/TrackedShell.swift index 9d96b34a..52012e32 100644 --- a/phpmon/Common/Monitoring/TrackedShell.swift +++ b/phpmon/Common/Monitoring/TrackedShell.swift @@ -60,27 +60,10 @@ final class TrackedShell: ShellProtocol { } // MARK: - Custom exports - // (probably should be part of the protocol?) - var customExports: [String: String] { - if let shell = self.shell as? RealShell { - return shell.exports - } else if let shell = self.shell as? TestableShell { - return shell.exports - } else { - assertionFailure("This shell does not support retrieving custom exports.") - return [:] - } - } - - func setCustomExports(_ variables: [String: String]) { - if let shell = self.shell as? RealShell { - shell.exports = variables - } else if let shell = self.shell as? TestableShell { - shell.exports = variables - } else { - assertionFailure("Custom exports were set in `config.json`, but not applied because the shell does not support it.") - } + var exports: [String: String] { + get { shell.exports } + set { shell.exports = newValue } } func reloadEnvPath() { diff --git a/phpmon/Common/Shell/ShellProtocol.swift b/phpmon/Common/Shell/ShellProtocol.swift index 2e414fdd..9bdfb59e 100644 --- a/phpmon/Common/Shell/ShellProtocol.swift +++ b/phpmon/Common/Shell/ShellProtocol.swift @@ -8,12 +8,18 @@ import Foundation -protocol ShellProtocol { +protocol ShellProtocol: AnyObject { /** The PATH for the current shell. */ var PATH: String { get } + /** + Exports are additional environment variables set by the user via the custom configuration. + These are populated when the configuration file is being loaded. + */ + var exports: [String: String] { get set } + /** Run a command synchronously. Use with caution! diff --git a/phpmon/Domain/Preferences/CustomPrefs.swift b/phpmon/Domain/Preferences/CustomPrefs.swift index 3a09a1e0..ea2e0f20 100644 --- a/phpmon/Domain/Preferences/CustomPrefs.swift +++ b/phpmon/Domain/Preferences/CustomPrefs.swift @@ -91,9 +91,8 @@ extension Preferences { Log.info("Configuring the additional exports...") Log.info("Custom exports: \(exports.description)") - if let shell = App.shared.container.shell as? TrackedShell { - shell.setCustomExports(exports) - } + // Assign the new exports values + container.shell.exports = exports } } }