Skip to content

Commit a6491f5

Browse files
committed
Add VSCode settings and implement uninstall functionality in phpvm.sh
1 parent 360aaa2 commit a6491f5

File tree

2 files changed

+162
-21
lines changed

2 files changed

+162
-21
lines changed

.vscode/settings.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"files.exclude": {
3+
"**/.git": true,
4+
"**/.svn": true,
5+
"**/.hg": true,
6+
"**/.DS_Store": true,
7+
"**/Thumbs.db": true,
8+
"**/*.js": {
9+
"when": "$(basename).ts"
10+
},
11+
"**/*.js.map": true,
12+
".mule": true
13+
}
14+
}

phpvm.sh

Lines changed: 148 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
# Define a debug mode for testing
77
if [ "${BATS_TEST_FILENAME:-}" != "" ]; then
8-
# Script is being tested
9-
# Don't execute main automatically
10-
PHPVM_TEST_MODE=true
11-
echo "TEST MODE ACTIVE" >&2
8+
# Script is being tested
9+
# Don't execute main automatically
10+
PHPVM_TEST_MODE=true
11+
echo "TEST MODE ACTIVE" >&2
1212
else
13-
PHPVM_TEST_MODE=false
13+
PHPVM_TEST_MODE=false
1414
fi
1515

1616
# Fix to prevent shell crash when sourced
@@ -184,13 +184,13 @@ use_php_version() {
184184
# Handle test mode specifically
185185
if [ "${PHPVM_TEST_MODE}" = "true" ]; then
186186
if [ "$version" = "system" ]; then
187-
echo "system" > "$PHPVM_ACTIVE_VERSION_FILE"
187+
echo "system" >"$PHPVM_ACTIVE_VERSION_FILE"
188188
phpvm_echo "Switched to system PHP."
189189
return 0
190190
fi
191191

192192
if [ -d "${TEST_PREFIX:-/tmp}/opt/homebrew/Cellar/php@$version" ]; then
193-
echo "$version" > "$PHPVM_ACTIVE_VERSION_FILE"
193+
echo "$version" >"$PHPVM_ACTIVE_VERSION_FILE"
194194
phpvm_echo "Switched to PHP $version."
195195
return 0
196196
else
@@ -543,7 +543,7 @@ EOF
543543
output_file=$(mktemp)
544544

545545
# Run the command, redirecting both stdout and stderr to the temporary file
546-
"$@" > "$output_file" 2>&1
546+
"$@" >"$output_file" 2>&1
547547

548548
# Check if the output contains the expected text
549549
if grep -q "$expected" "$output_file"; then
@@ -562,7 +562,7 @@ EOF
562562
local dir="$1"
563563
shift
564564

565-
"$@" > /dev/null 2>&1
565+
"$@" >/dev/null 2>&1
566566
if [ -d "$dir" ]; then
567567
return 0
568568
else
@@ -580,9 +580,9 @@ EOF
580580
# Test output functions with timestamps
581581
test_output_functions() {
582582
# Check that output contains timestamp format [YYYY-MM-DD HH:MM:SS]
583-
assert_output_contains "[INFO]" phpvm_echo "Test message" && \
584-
assert_output_contains "[ERROR]" phpvm_err "Test error" && \
585-
assert_output_contains "[WARNING]" phpvm_warn "Test warning"
583+
assert_output_contains "[INFO]" phpvm_echo "Test message" &&
584+
assert_output_contains "[ERROR]" phpvm_err "Test error" &&
585+
assert_output_contains "[WARNING]" phpvm_warn "Test warning"
586586
}
587587

588588
# Test timestamp format in logs
@@ -638,7 +638,7 @@ EOF
638638

639639
# Test install_php
640640
test_install_php() {
641-
install_php "7.4" > /dev/null
641+
install_php "7.4" >/dev/null
642642
local status=$?
643643

644644
# Check for success and file existence
@@ -651,7 +651,7 @@ EOF
651651
mkdir -p "$TEST_DIR/opt/homebrew/Cellar/[email protected]/bin"
652652

653653
# Test switching
654-
use_php_version "7.4" > /dev/null
654+
use_php_version "7.4" >/dev/null
655655
local status=$?
656656

657657
# Check for success and correct active version
@@ -660,7 +660,7 @@ EOF
660660

661661
# Test system_php_version
662662
test_system_php_version() {
663-
system_php_version > /dev/null
663+
system_php_version >/dev/null
664664
local status=$?
665665

666666
# Check for success and correct active version
@@ -674,13 +674,13 @@ EOF
674674

675675
# Create a project with .phpvmrc
676676
mkdir -p "$HOME/project"
677-
echo "7.4" > "$HOME/project/.phpvmrc"
677+
echo "7.4" >"$HOME/project/.phpvmrc"
678678

679679
# Change to the project directory
680680
cd "$HOME/project"
681681

682682
# Test auto-switching
683-
auto_switch_php_version > /dev/null
683+
auto_switch_php_version >/dev/null
684684
local status=$?
685685

686686
# Check for success and correct active version
@@ -760,6 +760,88 @@ EOF
760760
fi
761761
}
762762

763+
# Uninstall a specific PHP version
764+
uninstall_php() {
765+
version="$1"
766+
[ -z "$version" ] && {
767+
phpvm_err "No PHP version specified for uninstallation."
768+
return 1
769+
}
770+
771+
phpvm_echo "Uninstalling PHP $version..."
772+
773+
# If in test mode, just remove the mock directory
774+
if [ "${PHPVM_TEST_MODE}" = "true" ]; then
775+
rm -rf "${TEST_PREFIX:-/tmp}/opt/homebrew/Cellar/php@$version"
776+
phpvm_echo "PHP $version uninstalled."
777+
return 0
778+
fi
779+
780+
case "$PKG_MANAGER" in
781+
brew)
782+
if brew list --versions php@"$version" >/dev/null 2>&1; then
783+
brew uninstall php@"$version" || {
784+
phpvm_err "Failed to uninstall PHP $version with Homebrew."
785+
return 1
786+
}
787+
phpvm_echo "PHP $version uninstalled."
788+
else
789+
phpvm_warn "PHP $version is not installed via Homebrew."
790+
return 1
791+
fi
792+
;;
793+
apt)
794+
if dpkg -l | grep -q "php$version"; then
795+
run_with_sudo apt-get remove -y php"$version" || {
796+
phpvm_err "Failed to uninstall PHP $version with apt."
797+
return 1
798+
}
799+
phpvm_echo "PHP $version uninstalled."
800+
else
801+
phpvm_warn "PHP $version is not installed via apt."
802+
return 1
803+
fi
804+
;;
805+
dnf | yum)
806+
if $PKG_MANAGER list installed | grep -q "php$version"; then
807+
run_with_sudo $PKG_MANAGER remove -y php"$version" || {
808+
phpvm_err "Failed to uninstall PHP $version with $PKG_MANAGER."
809+
return 1
810+
}
811+
phpvm_echo "PHP $version uninstalled."
812+
else
813+
phpvm_warn "PHP $version is not installed via $PKG_MANAGER."
814+
return 1
815+
fi
816+
;;
817+
pacman)
818+
if pacman -Q | grep -q "^php$version"; then
819+
run_with_sudo pacman -R --noconfirm php"$version" || {
820+
phpvm_err "Failed to uninstall PHP $version with pacman."
821+
return 1
822+
}
823+
phpvm_echo "PHP $version uninstalled."
824+
else
825+
phpvm_warn "PHP $version is not installed via pacman."
826+
return 1
827+
fi
828+
;;
829+
*)
830+
phpvm_err "Uninstall not supported for this package manager."
831+
return 1
832+
;;
833+
esac
834+
835+
# Clean up symlink and active version if needed
836+
if [ -f "$PHPVM_ACTIVE_VERSION_FILE" ] && [ "$(cat "$PHPVM_ACTIVE_VERSION_FILE")" = "$version" ]; then
837+
rm -f "$PHPVM_CURRENT_SYMLINK"
838+
rm -f "$PHPVM_ACTIVE_VERSION_FILE"
839+
phpvm_warn "Active PHP version was uninstalled. Please select another version."
840+
fi
841+
842+
return 0
843+
}
844+
763845
# Main function to handle commands
764846
main() {
765847
# Only run if not being sourced
@@ -790,6 +872,13 @@ main() {
790872
fi
791873
install_php "$@"
792874
;;
875+
uninstall)
876+
if [ "$#" -eq 0 ]; then
877+
phpvm_err "Missing PHP version argument for 'uninstall' command."
878+
exit 1
879+
fi
880+
uninstall_php "$@"
881+
;;
793882
system)
794883
system_php_version
795884
;;
@@ -813,8 +902,46 @@ main() {
813902
esac
814903
}
815904

816-
# This allows the script to be sourced without running main
817-
if [ "$PHPVM_TEST_MODE" != "true" ] && [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
818-
# Script is being executed directly and not in test mode
819-
main "$@"
905+
# Robust execution detection with multiple fallbacks
906+
phpvm_should_execute_main() {
907+
# Layer 1: Explicit override (highest priority)
908+
case "${PHPVM_SOURCED:-auto}" in
909+
true | 1 | yes) return 1 ;; # Don't execute
910+
false | 0 | no) return 0 ;; # Execute
911+
esac
912+
913+
# Layer 2: Test mode
914+
[ "$PHPVM_TEST_MODE" = "true" ] && return 1
915+
916+
# Layer 3: Return test (most reliable POSIX method)
917+
if (return 0 2>/dev/null); then
918+
return 1 # Sourced
919+
else
920+
return 0 # Executed
921+
fi
922+
}
923+
924+
# Safe main execution with error handling
925+
if phpvm_should_execute_main "$@"; then
926+
phpvm_debug "Executing main with $# arguments"
927+
928+
# Verify main function exists
929+
if command -v main >/dev/null 2>&1; then
930+
main "$@"
931+
else
932+
phpvm_err "main function not found - script may be corrupted"
933+
exit 1
934+
fi
935+
else
936+
phpvm_debug "Script sourced for function loading"
937+
938+
# Set environment for shell integration
939+
PHPVM_FUNCTIONS_LOADED=true
940+
export PHPVM_FUNCTIONS_LOADED
941+
942+
# Auto-use .phpvmrc if enabled and present
943+
if [ "${PHPVM_AUTO_USE:-true}" = "true" ] && [ -f ".phpvmrc" ]; then
944+
command -v auto_switch_php_version >/dev/null 2>&1 &&
945+
auto_switch_php_version 2>/dev/null || true
946+
fi
820947
fi

0 commit comments

Comments
 (0)