Skip to content

Commit 6a62d19

Browse files
committed
Refactor installation and test scripts: improve POSIX compatibility, enhance error handling, and add auto-switch functionality for PHP versions
1 parent 2e6fc31 commit 6a62d19

File tree

3 files changed

+467
-126
lines changed

3 files changed

+467
-126
lines changed

install.sh

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
#!/usr/bin/env bash
2-
3-
{ # Ensure the entire script is downloaded and executed
4-
5-
set -euo pipefail
1+
#!/bin/sh
2+
{
3+
# Ensure the entire script is downloaded and executed
4+
set -e
65

76
phpvm_has() {
8-
type "$1" >/dev/null 2>&1
7+
command -v "$1" >/dev/null 2>&1
98
}
109

1110
phpvm_echo() {
12-
command printf "\e[32m%s\e[0m\n" "$*"
11+
printf "\033[32m%s\033[0m\n" "$*"
1312
}
1413

1514
phpvm_err() {
16-
command >&2 printf "\e[31mError: %s\e[0m\n" "$*"
15+
printf "\033[31mError: %s\033[0m\n" "$*" >&2
1716
}
1817

1918
phpvm_warn() {
20-
command >&2 printf "\e[33mWarning: %s\e[0m\n" "$*"
19+
printf "\033[33mWarning: %s\033[0m\n" "$*" >&2
2120
}
2221

22+
# Default installation directory
2323
PHPVM_DIR="${PHPVM_DIR:-$HOME/.phpvm}"
2424
PHPVM_SCRIPT="$PHPVM_DIR/phpvm.sh"
2525
GITHUB_REPO_URL="https://raw.githubusercontent.com/Thavarshan/phpvm/main/phpvm.sh"
@@ -36,7 +36,8 @@
3636
if phpvm_has "curl"; then
3737
curl --fail --compressed -q "$@"
3838
elif phpvm_has "wget"; then
39-
wget "$@"
39+
# Adding -O- to output to stdout for wget
40+
wget -O- "$@"
4041
else
4142
phpvm_err "curl or wget is required to install phpvm."
4243
exit 1
@@ -49,11 +50,20 @@
4950
return
5051
fi
5152

52-
local DETECTED_PROFILE=''
53-
if [ "${SHELL#*zsh}" != "$SHELL" ]; then
54-
if [ -f "$HOME/.zshrc" ]; then DETECTED_PROFILE="$HOME/.zshrc"; fi
55-
elif [ "${SHELL#*bash}" != "$SHELL" ]; then
56-
if [ -f "$HOME/.bashrc" ]; then DETECTED_PROFILE="$HOME/.bashrc"; fi
53+
local DETECTED_PROFILE=""
54+
local SHELL_NAME=""
55+
56+
# Get the shell from PS1 or SHELL variable
57+
SHELL_NAME="$(basename "$SHELL")"
58+
59+
if [ "$SHELL_NAME" = "zsh" ]; then
60+
if [ -f "$HOME/.zshrc" ]; then
61+
DETECTED_PROFILE="$HOME/.zshrc"
62+
fi
63+
elif [ "$SHELL_NAME" = "bash" ]; then
64+
if [ -f "$HOME/.bashrc" ]; then
65+
DETECTED_PROFILE="$HOME/.bashrc"
66+
fi
5767
fi
5868

5969
if [ -z "$DETECTED_PROFILE" ]; then
@@ -71,10 +81,14 @@
7181
install_phpvm_as_script() {
7282
local INSTALL_DIR
7383
INSTALL_DIR="$(phpvm_install_dir)"
74-
7584
mkdir -p "$INSTALL_DIR/bin"
85+
7686
phpvm_echo "Downloading phpvm script from $GITHUB_REPO_URL..."
77-
phpvm_download -fsSL "$GITHUB_REPO_URL" -o "$INSTALL_DIR/phpvm.sh"
87+
phpvm_download "$GITHUB_REPO_URL" >"$INSTALL_DIR/phpvm.sh" || {
88+
phpvm_err "Failed to download phpvm script"
89+
exit 1
90+
}
91+
7892
chmod +x "$INSTALL_DIR/phpvm.sh"
7993
ln -sf "$INSTALL_DIR/phpvm.sh" "$INSTALL_DIR/bin/phpvm"
8094
}
@@ -86,19 +100,23 @@
86100
local PROFILE
87101
PROFILE="$(phpvm_detect_profile)"
88102

89-
if [ -n "$PROFILE" ] && ! grep -qc 'phpvm.sh' "$PROFILE"; then
103+
if [ -n "$PROFILE" ] && ! grep -q 'phpvm.sh' "$PROFILE"; then
90104
phpvm_echo "Appending phpvm source to $PROFILE"
91-
echo -e "\nexport PHPVM_DIR=\"$(phpvm_install_dir)\"\nexport PATH=\"\$PHPVM_DIR/bin:\$PATH\"\n[ -s \"\$PHPVM_DIR/phpvm.sh\" ] && \\. \"\$PHPVM_DIR/phpvm.sh\"" >>"$PROFILE"
105+
printf "\nexport PHPVM_DIR=\"%s\"\nexport PATH=\"\$PHPVM_DIR/bin:\$PATH\"\n[ -s \"\$PHPVM_DIR/phpvm.sh\" ] && . \"\$PHPVM_DIR/phpvm.sh\"\n" "$(phpvm_install_dir)" >>"$PROFILE"
92106
fi
93107

94108
phpvm_echo "Applying changes..."
95109
export PATH="$PHPVM_DIR/bin:$PATH"
96-
source "$PROFILE" || true
110+
111+
# Only source the profile if it exists
112+
if [ -f "$PROFILE" ]; then
113+
# Use . instead of source for POSIX compatibility
114+
. "$PROFILE" || true
115+
fi
97116

98117
phpvm_echo "phpvm installation complete!"
99118
phpvm_echo "Run: phpvm use 8.4"
100119
}
101120

102121
phpvm_do_install
103-
104122
} # End of script

0 commit comments

Comments
 (0)