Skip to content

Commit

Permalink
✨ Generate Fish-friendly scripts (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoverbruggen committed Oct 20, 2023
1 parent 00b4760 commit b9c7cdb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
12 changes: 6 additions & 6 deletions PHP Monitor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions phpmon/Common/Core/Paths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)`.")
}
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions phpmon/Common/Helpers/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 21 additions & 3 deletions phpmon/Common/PHP/PHP Version/PhpHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -78,7 +80,7 @@ class PhpHelper {
}
}

private static func script(
private static func zshScript(
_ path: String,
_ keyPhrase: String,
_ version: String,
Expand All @@ -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)"
Expand Down

0 comments on commit b9c7cdb

Please sign in to comment.