Skip to content

Commit

Permalink
Refactor installation and test scripts: improve POSIX compatibility, …
Browse files Browse the repository at this point in the history
…enhance error handling, and add auto-switch functionality for PHP versions
  • Loading branch information
Thavarshan committed Feb 15, 2025
1 parent 2e6fc31 commit 6a62d19
Show file tree
Hide file tree
Showing 3 changed files with 467 additions and 126 deletions.
60 changes: 39 additions & 21 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#!/usr/bin/env bash

{ # Ensure the entire script is downloaded and executed

set -euo pipefail
#!/bin/sh
{
# Ensure the entire script is downloaded and executed
set -e

phpvm_has() {
type "$1" >/dev/null 2>&1
command -v "$1" >/dev/null 2>&1
}

phpvm_echo() {
command printf "\e[32m%s\e[0m\n" "$*"
printf "\033[32m%s\033[0m\n" "$*"
}

phpvm_err() {
command >&2 printf "\e[31mError: %s\e[0m\n" "$*"
printf "\033[31mError: %s\033[0m\n" "$*" >&2
}

phpvm_warn() {
command >&2 printf "\e[33mWarning: %s\e[0m\n" "$*"
printf "\033[33mWarning: %s\033[0m\n" "$*" >&2
}

# Default installation directory
PHPVM_DIR="${PHPVM_DIR:-$HOME/.phpvm}"
PHPVM_SCRIPT="$PHPVM_DIR/phpvm.sh"
GITHUB_REPO_URL="https://raw.githubusercontent.com/Thavarshan/phpvm/main/phpvm.sh"
Expand All @@ -36,7 +36,8 @@
if phpvm_has "curl"; then
curl --fail --compressed -q "$@"
elif phpvm_has "wget"; then
wget "$@"
# Adding -O- to output to stdout for wget
wget -O- "$@"
else
phpvm_err "curl or wget is required to install phpvm."
exit 1
Expand All @@ -49,11 +50,20 @@
return
fi

local DETECTED_PROFILE=''
if [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "$HOME/.zshrc" ]; then DETECTED_PROFILE="$HOME/.zshrc"; fi
elif [ "${SHELL#*bash}" != "$SHELL" ]; then
if [ -f "$HOME/.bashrc" ]; then DETECTED_PROFILE="$HOME/.bashrc"; fi
local DETECTED_PROFILE=""
local SHELL_NAME=""

# Get the shell from PS1 or SHELL variable
SHELL_NAME="$(basename "$SHELL")"

if [ "$SHELL_NAME" = "zsh" ]; then
if [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
fi
elif [ "$SHELL_NAME" = "bash" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
fi
fi

if [ -z "$DETECTED_PROFILE" ]; then
Expand All @@ -71,10 +81,14 @@
install_phpvm_as_script() {
local INSTALL_DIR
INSTALL_DIR="$(phpvm_install_dir)"

mkdir -p "$INSTALL_DIR/bin"

phpvm_echo "Downloading phpvm script from $GITHUB_REPO_URL..."
phpvm_download -fsSL "$GITHUB_REPO_URL" -o "$INSTALL_DIR/phpvm.sh"
phpvm_download "$GITHUB_REPO_URL" >"$INSTALL_DIR/phpvm.sh" || {
phpvm_err "Failed to download phpvm script"
exit 1
}

chmod +x "$INSTALL_DIR/phpvm.sh"
ln -sf "$INSTALL_DIR/phpvm.sh" "$INSTALL_DIR/bin/phpvm"
}
Expand All @@ -86,19 +100,23 @@
local PROFILE
PROFILE="$(phpvm_detect_profile)"

if [ -n "$PROFILE" ] && ! grep -qc 'phpvm.sh' "$PROFILE"; then
if [ -n "$PROFILE" ] && ! grep -q 'phpvm.sh' "$PROFILE"; then
phpvm_echo "Appending phpvm source to $PROFILE"
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"
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"
fi

phpvm_echo "Applying changes..."
export PATH="$PHPVM_DIR/bin:$PATH"
source "$PROFILE" || true

# Only source the profile if it exists
if [ -f "$PROFILE" ]; then
# Use . instead of source for POSIX compatibility
. "$PROFILE" || true
fi

phpvm_echo "phpvm installation complete!"
phpvm_echo "Run: phpvm use 8.4"
}

phpvm_do_install

} # End of script
Loading

0 comments on commit 6a62d19

Please sign in to comment.