Skip to content

Commit

Permalink
Merge pull request #6 from kisztof/feature/list-available-php-version…
Browse files Browse the repository at this point in the history
…s-with-prompt

#2 Enhancements to PHP Version Switching Script
  • Loading branch information
kisztof committed Sep 8, 2023
2 parents a146325 + 346edb9 commit 5ad7ef6
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions bin/swtp
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
#!/bin/bash

# Declare two arrays to hold the PHP versions and their corresponding full versions
declare -a PHP_VERSIONS=()
declare -a FULL_VERSIONS=()

# Function to list installed PHP versions
list_installed_php_versions() {
local i=1
while read -r path; do
full_version=$(basename "$path")
major_minor_version=$(echo "$full_version" | awk -F '.' '{print $1 "." $2}')
PHP_VERSIONS+=("$major_minor_version")
FULL_VERSIONS+=("$full_version")
done < <(find /opt/homebrew/Cellar/php* -type d -maxdepth 1 -mindepth 1 | awk -F '/' '{print $NF}' | sort -V)

for index in "${!FULL_VERSIONS[@]}"; do
full_version="${FULL_VERSIONS[$index]}"
major_minor_version="${PHP_VERSIONS[$index]}"
if [ "$major_minor_version" == "$CURRENT_PHP_VERSION" ]; then
echo -e "\033[0;32m* [$i] $full_version\033[0m"
else
echo "* [$i] $full_version"
fi
i=$((i + 1))
done
}

# Find the currently linked PHP version
CURRENT_PHP_VERSION=$(php -v | head -n 1 | awk -F " " '{print $2}' | awk -F "." '{print $1 "." $2}')
echo "Currently linked PHP version: $CURRENT_PHP_VERSION"

# Check if a version number is provided
if [ -z "$1" ]; then
echo "Please provide a PHP version to switch to."
exit 1
echo "Available PHP versions:"
list_installed_php_versions
read -p "Please choose a PHP version to switch to (enter the number): " choice
PHP_VERSION="${PHP_VERSIONS[$((choice - 1))]}"
if [ -z "$PHP_VERSION" ]; then
echo "Invalid selection. Exiting."
exit 1
fi
else
PHP_VERSION="$1"
fi

# The PHP version to switch to
PHP_VERSION="$1"

# Check if the requested version is the same as the current version
if [ "$CURRENT_PHP_VERSION" == "$PHP_VERSION" ]; then
echo "The requested PHP version is already active."
exit 0
fi

# Check if the requested PHP version is installed
if ! brew list --versions | grep -q "php@$PHP_VERSION"; then
if ! echo "${PHP_VERSIONS[@]}" | grep -q -w "$PHP_VERSION"; then
read -p "PHP version $PHP_VERSION is not installed. Would you like to install it now? (y/n): " choice
case "$choice" in
y|Y )
Expand Down

0 comments on commit 5ad7ef6

Please sign in to comment.