From e871a004901663797886f0a196ce1f8ee638a002 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Wed, 21 Sep 2022 21:42:03 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Add=20additional=20commentary=20?= =?UTF-8?q?to=20new=20shell=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 12 +++++- .../Shell+PATH.swift | 0 .../{Core => Pending Removal}/Shell.swift | 2 + phpmon/Domain/Preferences/CustomPrefs.swift | 1 + phpmon/Next/SystemShell.swift | 41 ++++++++++++++++--- 5 files changed, 49 insertions(+), 7 deletions(-) rename phpmon/Common/{Core => Pending Removal}/Shell+PATH.swift (100%) rename phpmon/Common/{Core => Pending Removal}/Shell.swift (96%) diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index baa1c54..455a64c 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -656,8 +656,6 @@ C4EC1E72279DFCF40010F296 /* Events.swift */, C4B5853D2770FE3900DA4FBE /* Command.swift */, C4B5853B2770FE3900DA4FBE /* Paths.swift */, - C4B5853C2770FE3900DA4FBE /* Shell.swift */, - C42E3BF328A9BF5100AFECFC /* Shell+PATH.swift */, C4C1019A27C65C6F001FACC2 /* Process.swift */, C40C7F2F27722E8D00DDDCDC /* Logger.swift */, C417DC73277614690015E6EE /* Helpers.swift */, @@ -883,6 +881,15 @@ path = Next; sourceTree = ""; }; + C46EBC4C28DB9F43007ACC74 /* Pending Removal */ = { + isa = PBXGroup; + children = ( + C4B5853C2770FE3900DA4FBE /* Shell.swift */, + C42E3BF328A9BF5100AFECFC /* Shell+PATH.swift */, + ); + path = "Pending Removal"; + sourceTree = ""; + }; C47331A0247093AC009A0597 /* Menu */ = { isa = PBXGroup; children = ( @@ -977,6 +984,7 @@ C4B5853A2770FE2500DA4FBE /* Common */ = { isa = PBXGroup; children = ( + C46EBC4C28DB9F43007ACC74 /* Pending Removal */, C40C7F2127721F7300DDDCDC /* Core */, 54B20EDF263AA22C00D3250E /* PHP */, C44CCD4327AFE93300CE40E5 /* Errors */, diff --git a/phpmon/Common/Core/Shell+PATH.swift b/phpmon/Common/Pending Removal/Shell+PATH.swift similarity index 100% rename from phpmon/Common/Core/Shell+PATH.swift rename to phpmon/Common/Pending Removal/Shell+PATH.swift diff --git a/phpmon/Common/Core/Shell.swift b/phpmon/Common/Pending Removal/Shell.swift similarity index 96% rename from phpmon/Common/Core/Shell.swift rename to phpmon/Common/Pending Removal/Shell.swift index 132309e..ba4ccde 100644 --- a/phpmon/Common/Core/Shell.swift +++ b/phpmon/Common/Pending Removal/Shell.swift @@ -7,6 +7,8 @@ import Cocoa +// TODO: Enable this to see where deprecations and replacements are needed. +// @available(*, deprecated, message: "Use the new replacement `NxtShell` instead") public class Shell { // MARK: - Invoke static functions diff --git a/phpmon/Domain/Preferences/CustomPrefs.swift b/phpmon/Domain/Preferences/CustomPrefs.swift index 22d2c24..56b091b 100644 --- a/phpmon/Domain/Preferences/CustomPrefs.swift +++ b/phpmon/Domain/Preferences/CustomPrefs.swift @@ -26,6 +26,7 @@ struct CustomPrefs: Decodable { return self.environmentVariables != nil && !self.environmentVariables!.keys.isEmpty } + @available(*, deprecated, message: "Use `setCustomEnvironmentVariables` instead") public func getEnvironmentVariables() -> String { return self.environmentVariables!.map { (key, value) in return "export \(key)=\(value)" diff --git a/phpmon/Next/SystemShell.swift b/phpmon/Next/SystemShell.swift index 0011b91..f232828 100644 --- a/phpmon/Next/SystemShell.swift +++ b/phpmon/Next/SystemShell.swift @@ -9,9 +9,26 @@ import Foundation class SystemShell: Shellable { - public var launchPath: String = "/bin/sh" + /** + The launch path of the terminal in question that is used. + On macOS, we use /bin/sh since it's pretty fast. + */ + private(set) var launchPath: String = "/bin/sh" - public var PATH: String = { + /** + For some commands, we need to know what's in the user's PATH. + The entire PATH is retrieved here, so we can set the PATH in our own terminal as necessary. + */ + private(set) var PATH: String = { return SystemShell.getPath() }() + + /** + Exports are additional environment variables set by the user via the custom configuration. + These are populated when the configuration file is being loaded. + */ + private(set) var exports: String = "" + + /** Retrieves the user's PATH by opening an interactive shell and echoing $PATH. */ + private static func getPath() -> String { let task = Process() task.launchPath = "/bin/zsh" @@ -26,10 +43,12 @@ class SystemShell: Shellable { data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8 ) ?? "" - }() - - var exports: String = "" + } + /** + Create a process that will run the required shell with the appropriate arguments. + This process still needs to be started, or one can attach output handlers. + */ private func getShellProcess(for command: String) -> Process { var completeCommand = "" @@ -49,6 +68,18 @@ class SystemShell: Shellable { return task } + // MARK: - Public API + + /** + Set custom environment variables. + These will be exported when a command is executed. + */ + public func setCustomEnvironmentVariables(_ variables: [String: String]) { + self.exports = variables.map { (key, value) in + return "export \(key)=\(value)" + }.joined(separator: "&&") + } + // MARK: - Shellable Protocol func syncPipe(_ command: String) -> String {