From b9c7cdb3cc3b3eafa8e5b2ca7eb455e406565a23 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 20 Oct 2023 17:34:19 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Generate=20Fish-friendly=20scripts?= =?UTF-8?q?=20(#264)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 12 +++++----- phpmon/Common/Core/Paths.swift | 7 ++++++ phpmon/Common/Helpers/System.swift | 8 +++++++ phpmon/Common/PHP/PHP Version/PhpHelper.swift | 24 ++++++++++++++++--- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 283cad40..7ee04070 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -3492,7 +3492,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1330; + CURRENT_PROJECT_VERSION = 1335; DEAD_CODE_STRIPPING = YES; DEBUG = YES; DEVELOPMENT_TEAM = 8M54J5J787; @@ -3523,7 +3523,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1330; + CURRENT_PROJECT_VERSION = 1335; DEAD_CODE_STRIPPING = YES; DEBUG = NO; DEVELOPMENT_TEAM = 8M54J5J787; @@ -3763,7 +3763,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1330; + CURRENT_PROJECT_VERSION = 1335; DEAD_CODE_STRIPPING = YES; DEBUG = NO; DEVELOPMENT_TEAM = 8M54J5J787; @@ -3879,7 +3879,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1330; + CURRENT_PROJECT_VERSION = 1335; DEAD_CODE_STRIPPING = YES; DEBUG = YES; DEVELOPMENT_TEAM = 8M54J5J787; @@ -3995,7 +3995,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1330; + CURRENT_PROJECT_VERSION = 1335; DEAD_CODE_STRIPPING = YES; DEBUG = YES; DEVELOPMENT_TEAM = 8M54J5J787; @@ -4176,7 +4176,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1330; + CURRENT_PROJECT_VERSION = 1335; DEAD_CODE_STRIPPING = YES; DEBUG = NO; DEVELOPMENT_TEAM = 8M54J5J787; diff --git a/phpmon/Common/Core/Paths.swift b/phpmon/Common/Core/Paths.swift index 41bfda90..1ea55793 100644 --- a/phpmon/Common/Core/Paths.swift +++ b/phpmon/Common/Core/Paths.swift @@ -17,6 +17,7 @@ public class Paths { internal var baseDir: Paths.HomebrewDir private var userName: String + private var preferredShell: String init() { // Assume the default directory is correct @@ -31,9 +32,11 @@ public class Paths { } userName = identity() + preferredShell = preferred_shell() if !isRunningSwiftUIPreview { Log.info("The current username is `\(userName)`.") + Log.info("The user's shell is `\(preferredShell)`.") } } @@ -104,6 +107,10 @@ public class Paths { + (App.identifier.contains(".dev") ? "phpmon-dev" : "phpmon") } + public static var shell: String { + return shared.preferredShell + } + // MARK: - Flexible Binaries // (these can be in multiple locations, so we scan common places because) // (PHP Monitor will not use the user's own PATH) diff --git a/phpmon/Common/Helpers/System.swift b/phpmon/Common/Helpers/System.swift index f3a13056..5bbba22f 100644 --- a/phpmon/Common/Helpers/System.swift +++ b/phpmon/Common/Helpers/System.swift @@ -65,3 +65,11 @@ public func identity() -> String { return output.trimmingCharacters(in: .whitespacesAndNewlines) } + +/** + Retrieves the user's preferred shell. + */ +public func preferred_shell() -> String { + return system("dscl . -read ~/ UserShell | sed 's/UserShell: //'") + .trimmingCharacters(in: .whitespacesAndNewlines) +} diff --git a/phpmon/Common/PHP/PHP Version/PhpHelper.swift b/phpmon/Common/PHP/PHP Version/PhpHelper.swift index be41b47c..dce0c595 100644 --- a/phpmon/Common/PHP/PHP Version/PhpHelper.swift +++ b/phpmon/Common/PHP/PHP Version/PhpHelper.swift @@ -49,8 +49,10 @@ class PhpHelper { let path = URL(fileURLWithPath: "\(Paths.optPath)/php@\(version)/bin") .resolvingSymlinksInPath().path - // The contents of the script! - let script = script(path, keyPhrase, version, dotless) + // Check if the user uses Fish + let script = Paths.shell.contains("/fish") + ? fishScript(path, keyPhrase, version, dotless) + : zshScript(path, keyPhrase, version, dotless) Task { @MainActor in try FileSystem.writeAtomicallyToFile(destination, content: script) @@ -78,7 +80,7 @@ class PhpHelper { } } - private static func script( + private static func zshScript( _ path: String, _ keyPhrase: String, _ version: String, @@ -96,6 +98,22 @@ class PhpHelper { """ } + private static func fishScript( + _ path: String, + _ keyPhrase: String, + _ version: String, + _ dotless: String + ) -> String { + return """ + #!\(Paths.binPath)/fish + # \(keyPhrase) + # It reflects the location of PHP \(version)'s binaries on your system. + # Usage: . pm\(dotless) + echo "PHP Monitor has enabled this terminal to use PHP \(version)."; \\ + set -x PATH \(path) $PATH + """ + } + private static func createSymlink(_ dotless: String) async { let source = "\(Paths.homePath)/.config/phpmon/bin/pm\(dotless)" let destination = "/usr/local/bin/pm\(dotless)"