Open lukasbestle opened 4 years ago
I'm also curious about this because the readme was changed at the start of 2019 to say fish
is supported but it doesn't seem like it works
https://github.com/wilmoore/php-version/commit/de588d1ee6c97e7b2386cb97ddb428d21fb03f59
In case anyone stumbles across this I worked around it by using bass
(installed using omf
) and this function:
function php-version
bass source (brew --prefix php-version)/php-version.sh ';' php-version $argv
end
I've written an implementation in native fish
for my own use, however it doesn't support all features of the original:
###
# Switches PHP versions by modifying the $PATH variable
# Based on https://github.com/wilmoore/php-version
# NOTE: Unlike the original, this fish implementation
# only supports versions installed via Homebrew
#
# Globals:
# $PATH, $MANPATH
# Arguments:
# $version string PHP version (e.g. `7`, `7.4` or `7.4.4`);
# if not given, all available versions are printed
# Returns:
# None
###
function php-version
# ensure that Homebrew is installed
if test -z (command -v brew)
echo -e "\033[31mError:\033[0m Homebrew is not available." >&2
return 1
end
# make a list of all installed PHP versions
set directories (find (brew --prefix)/opt -maxdepth 1 -name 'php@*') (brew --prefix)/opt/php
# ensure that we have found at least one PHP version
if test -z "$directories"
echo -e "\033[31mError:\033[0m No PHP versions were detected," >&2
echo -e " install some with \033[34mbrew install php@*\033[0m." >&2
return 1
end
# make a list of actual installed PHP versions
set versions
for d in $directories
set versions $versions ($d/bin/php-config --version)
end
if test -z "$argv[1]"
# no argument, print all available versions
# determine the current version
set currentVersion (php-config --version)
# print all versions and highlight the current one
for v in $versions
if test "$v" = "$currentVersion"
echo -e "\033[7m* $v\033[0m"
else
echo " $v"
end
end
else if test "$argv[1]" = "complete"
# output available versions for the completion
echo $versions
else
# switch to the specified PHP version
# find the first version (starting from the latest one)
# that starts with the specified version string
set selectedVersion
set selectedDirectory
for i in (seq (count $directories) 1)
if string match -q "$argv[1]*" "$versions[$i]"
set selectedVersion "$versions[$i]"
set selectedDirectory "$directories[$i]"
break
end
end
# ensure that we have found a version
if test -z $selectedVersion
echo -e "\033[31mError:\033[0m Could not find a matching PHP version for \033[34m$argv[1]\033[0m." >&2
echo -e " (Options: \033[34m$versions\033[0m)" >&2
return 1
end
echo -e "Switching to PHP \033[34m$selectedVersion\033[0m..."
# switch to the new version
set -x PATH $selectedDirectory/bin $selectedDirectory/sbin $PATH
set -x MANPATH $selectedDirectory/share/man $MANPATH
end
end
Matching fish
completion:
set options (php-version complete)
if test -n options
complete -c php-version -a "$options"
end
Thanks for creating this simple and useful tool. I have been using it for a few years and now wanted to switch one machine to the
fish
shell, which is supported according to theREADME
.Is this an issue in my setup or is
fish
currently not supported byphp-version
?Error:
Debug information: