Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work with fish 3.1.0 #75

Open
lukasbestle opened this issue Apr 10, 2020 · 2 comments
Open

Does not work with fish 3.1.0 #75

lukasbestle opened this issue Apr 10, 2020 · 2 comments

Comments

@lukasbestle
Copy link

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 the README.

Is this an issue in my setup or is fish currently not supported by php-version?

Error:

$ source (brew --prefix php-version)/php-version.sh
/usr/local/opt/php-version/php-version.sh (line 47): Unexpected ')' found, expecting '}'
    -h|--help|-u|--usage)
                        ^
from sourcing file /usr/local/opt/php-version/php-version.sh
	called on line 11 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
	called during startup
source: Error while reading file '/usr/local/opt/php-version/php-version.sh'

Debug information:

## System

  Darwin <hostname> 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64

## Script

  TYPE:    NOT FOUND
  VERSION: NOT FOUND

## Environment

  SHELL:        /usr/local/bin/fish
  PATH:         /Users/lux/.composer/vendor/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin
  PHPS:         
  PHP_VERSIONS:

## Homebrew

  VERSION: Homebrew 2.2.11-114-g28a1497
           Homebrew/homebrew-core (git revision 1f6b068; last commit 2020-04-05)
           Homebrew/homebrew-cask (git revision a5780; last commit 2020-04-05)
  PATH:    /usr/local/bin/brew
  PHPS:    /usr/local/Cellar/php
           /usr/local/Cellar/php@7.2
           /usr/local/Cellar/php@7.3

## PHP

  VERSION: 7.4.4

## INI

  Configuration File (php.ini) Path: /usr/local/etc/php/7.4
  Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
  Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
  Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/ext-apcu.ini,
  /usr/local/etc/php/7.4/conf.d/ext-opcache.ini,
  /usr/local/etc/php/7.4/conf.d/ext-xdebug.ini
@stableford-catch
Copy link

stableford-catch commented Nov 26, 2020

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

de588d1

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

@lukasbestle
Copy link
Author

lukasbestle commented Nov 26, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants