|
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 |
6 | 5 |
|
7 | 6 | phpvm_has() {
|
8 |
| - type "$1" >/dev/null 2>&1 |
| 7 | + command -v "$1" >/dev/null 2>&1 |
9 | 8 | }
|
10 | 9 |
|
11 | 10 | phpvm_echo() {
|
12 |
| - command printf "\e[32m%s\e[0m\n" "$*" |
| 11 | + printf "\033[32m%s\033[0m\n" "$*" |
13 | 12 | }
|
14 | 13 |
|
15 | 14 | phpvm_err() {
|
16 |
| - command >&2 printf "\e[31mError: %s\e[0m\n" "$*" |
| 15 | + printf "\033[31mError: %s\033[0m\n" "$*" >&2 |
17 | 16 | }
|
18 | 17 |
|
19 | 18 | phpvm_warn() {
|
20 |
| - command >&2 printf "\e[33mWarning: %s\e[0m\n" "$*" |
| 19 | + printf "\033[33mWarning: %s\033[0m\n" "$*" >&2 |
21 | 20 | }
|
22 | 21 |
|
| 22 | + # Default installation directory |
23 | 23 | PHPVM_DIR="${PHPVM_DIR:-$HOME/.phpvm}"
|
24 | 24 | PHPVM_SCRIPT="$PHPVM_DIR/phpvm.sh"
|
25 | 25 | GITHUB_REPO_URL="https://raw.githubusercontent.com/Thavarshan/phpvm/main/phpvm.sh"
|
|
36 | 36 | if phpvm_has "curl"; then
|
37 | 37 | curl --fail --compressed -q "$@"
|
38 | 38 | elif phpvm_has "wget"; then
|
39 |
| - wget "$@" |
| 39 | + # Adding -O- to output to stdout for wget |
| 40 | + wget -O- "$@" |
40 | 41 | else
|
41 | 42 | phpvm_err "curl or wget is required to install phpvm."
|
42 | 43 | exit 1
|
|
49 | 50 | return
|
50 | 51 | fi
|
51 | 52 |
|
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 |
57 | 67 | fi
|
58 | 68 |
|
59 | 69 | if [ -z "$DETECTED_PROFILE" ]; then
|
|
71 | 81 | install_phpvm_as_script() {
|
72 | 82 | local INSTALL_DIR
|
73 | 83 | INSTALL_DIR="$(phpvm_install_dir)"
|
74 |
| - |
75 | 84 | mkdir -p "$INSTALL_DIR/bin"
|
| 85 | + |
76 | 86 | 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 | + |
78 | 92 | chmod +x "$INSTALL_DIR/phpvm.sh"
|
79 | 93 | ln -sf "$INSTALL_DIR/phpvm.sh" "$INSTALL_DIR/bin/phpvm"
|
80 | 94 | }
|
|
86 | 100 | local PROFILE
|
87 | 101 | PROFILE="$(phpvm_detect_profile)"
|
88 | 102 |
|
89 |
| - if [ -n "$PROFILE" ] && ! grep -qc 'phpvm.sh' "$PROFILE"; then |
| 103 | + if [ -n "$PROFILE" ] && ! grep -q 'phpvm.sh' "$PROFILE"; then |
90 | 104 | 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" |
92 | 106 | fi
|
93 | 107 |
|
94 | 108 | phpvm_echo "Applying changes..."
|
95 | 109 | 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 |
97 | 116 |
|
98 | 117 | phpvm_echo "phpvm installation complete!"
|
99 | 118 | phpvm_echo "Run: phpvm use 8.4"
|
100 | 119 | }
|
101 | 120 |
|
102 | 121 | phpvm_do_install
|
103 |
| - |
104 | 122 | } # End of script
|
0 commit comments