|  | 
| 1 | 1 | #!/usr/bin/env bash | 
| 2 | 2 | 
 | 
| 3 |  | -{ | 
|  | 3 | +{ # this ensures the entire script is downloaded # | 
|  | 4 | + | 
| 4 | 5 |     phpvm_has() { | 
| 5 | 6 |         type "$1" >/dev/null 2>&1 | 
| 6 | 7 |     } | 
|  | 
| 21 | 22 |         fi | 
| 22 | 23 |     } | 
| 23 | 24 | 
 | 
|  | 25 | +    phpvm_latest_version() { | 
|  | 26 | +        latest_version=$(curl -s https://api.github.com/repos/Thavarshan/phpvm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') | 
|  | 27 | +        if [ -z "$latest_version" ]; then | 
|  | 28 | +            latest_version="main" | 
|  | 29 | +        fi | 
|  | 30 | +        phpvm_echo "$latest_version" | 
|  | 31 | +    } | 
|  | 32 | + | 
| 24 | 33 |     phpvm_download() { | 
| 25 | 34 |         if phpvm_has "curl"; then | 
| 26 | 35 |             curl --fail --compressed -q "$@" | 
| 27 | 36 |         elif phpvm_has "wget"; then | 
| 28 |  | -            ARGS=$(phpvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' -e 's/--compressed //' -e 's/--fail //' -e 's/-L //' -e 's/-I /--server-response /' -e 's/-s /-q /' -e 's/-sS /-nv /' -e 's/-o /-O /' -e 's/-C - /-c /') | 
|  | 37 | +            ARGS=$(phpvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \ | 
|  | 38 | +                -e 's/--compressed //' -e 's/--fail //' -e 's/-L //' -e 's/-I /--server-response /' \ | 
|  | 39 | +                -e 's/-s /-q /' -e 's/-sS /-nv /' -e 's/-o /-O /' -e 's/-C - /-c /') | 
| 29 | 40 |             eval wget $ARGS | 
| 30 | 41 |         fi | 
| 31 | 42 |     } | 
| 32 | 43 | 
 | 
| 33 | 44 |     install_phpvm_from_git() { | 
| 34 | 45 |         local INSTALL_DIR | 
| 35 | 46 |         INSTALL_DIR="$(phpvm_install_dir)" | 
|  | 47 | +        local PHPVM_VERSION | 
|  | 48 | +        PHPVM_VERSION="${PHPVM_INSTALL_VERSION:-$(phpvm_latest_version)}" | 
| 36 | 49 | 
 | 
| 37 | 50 |         if [ -d "$INSTALL_DIR/.git" ]; then | 
| 38 | 51 |             phpvm_echo "=> phpvm is already installed in $INSTALL_DIR, updating using git" | 
| 39 |  | -            command git -C "$INSTALL_DIR" pull --ff-only || { | 
| 40 |  | -                phpvm_echo >&2 'Failed to update phpvm. Please report this!' | 
| 41 |  | -                exit 1 | 
| 42 |  | -            } | 
|  | 52 | +            command printf '\r=> ' | 
| 43 | 53 |         else | 
| 44 | 54 |             phpvm_echo "=> Downloading phpvm from git to '$INSTALL_DIR'" | 
| 45 | 55 |             mkdir -p "${INSTALL_DIR}" | 
|  | 
| 49 | 59 |             } | 
| 50 | 60 |         fi | 
| 51 | 61 | 
 | 
| 52 |  | -        # Install Node.js dependencies | 
|  | 62 | +        command git -C "$INSTALL_DIR" checkout "$PHPVM_VERSION" || { | 
|  | 63 | +            phpvm_echo >&2 "Failed to checkout the version $PHPVM_VERSION. Please report this!" | 
|  | 64 | +            exit 1 | 
|  | 65 | +        } | 
|  | 66 | + | 
|  | 67 | +        phpvm_echo "=> Cleaning up git repository" | 
|  | 68 | +        command git -C "$INSTALL_DIR" gc --auto --aggressive --prune=now || { | 
|  | 69 | +            phpvm_echo >&2 'Failed to clean up git repository. Please report this!' | 
|  | 70 | +            exit 1 | 
|  | 71 | +        } | 
|  | 72 | + | 
|  | 73 | +        if ! phpvm_has "node"; then | 
|  | 74 | +            phpvm_echo "Node.js is required to run phpvm. Please install Node.js and try again." | 
|  | 75 | +            exit 1 | 
|  | 76 | +        fi | 
|  | 77 | + | 
| 53 | 78 |         phpvm_echo "=> Installing Node.js dependencies" | 
| 54 | 79 |         command npm install --prefix "$INSTALL_DIR" || { | 
| 55 | 80 |             phpvm_echo >&2 'Failed to install Node.js dependencies. Please report this!' | 
| 56 | 81 |             exit 1 | 
| 57 | 82 |         } | 
|  | 83 | + | 
|  | 84 | +        phpvm_create_launcher | 
| 58 | 85 |     } | 
| 59 | 86 | 
 | 
| 60 |  | -    inject_phpvm_config() { | 
| 61 |  | -        local PHPVM_PROFILE | 
| 62 |  | -        PHPVM_PROFILE="$(phpvm_detect_profile)" | 
| 63 |  | -        local PROFILE_INSTALL_DIR | 
| 64 |  | -        PROFILE_INSTALL_DIR="$(phpvm_install_dir | command sed "s:^$HOME:\$HOME:")" | 
|  | 87 | +    phpvm_create_launcher() { | 
|  | 88 | +        local INSTALL_DIR | 
|  | 89 | +        INSTALL_DIR="$(phpvm_install_dir)" | 
| 65 | 90 | 
 | 
| 66 |  | -        PHPVM_CONFIG_STR=" | 
| 67 |  | -# Load PHPVM if necessary (this will allow phpvm to be invoked manually) | 
| 68 |  | -if [ -s \"\$PHPVM_DIR/index.js\" ]; then | 
| 69 |  | -    export PATH=\"\$PHPVM_DIR/bin:\$PATH\" | 
| 70 |  | -fi | 
| 71 |  | -" | 
|  | 91 | +        # Create the bin directory if it doesn't exist | 
|  | 92 | +        mkdir -p "$INSTALL_DIR/bin" | 
| 72 | 93 | 
 | 
| 73 |  | -        if [ -n "$PHPVM_PROFILE" ]; then | 
| 74 |  | -            if ! command grep -qc '/phpvm/index.js' "$PHPVM_PROFILE"; then | 
| 75 |  | -                phpvm_echo "=> Injecting phpvm config into $PHPVM_PROFILE" | 
| 76 |  | -                echo -e "$PHPVM_CONFIG_STR" >>"$PHPVM_PROFILE" | 
| 77 |  | -            else | 
| 78 |  | -                phpvm_echo "=> phpvm config already exists in $PHPVM_PROFILE" | 
| 79 |  | -            fi | 
| 80 |  | -        else | 
| 81 |  | -            phpvm_echo "=> No profile found for phpvm config injection" | 
| 82 |  | -        fi | 
|  | 94 | +        # Create a shell script that runs the index.js | 
|  | 95 | +        cat <<EOL >"$INSTALL_DIR/bin/phpvm" | 
|  | 96 | +#!/usr/bin/env bash | 
|  | 97 | +node "\$PHPVM_DIR/index.js" "\$@" | 
|  | 98 | +EOL | 
|  | 99 | + | 
|  | 100 | +        # Make the shell script executable | 
|  | 101 | +        chmod +x "$INSTALL_DIR/bin/phpvm" | 
| 83 | 102 |     } | 
| 84 | 103 | 
 | 
| 85 | 104 |     phpvm_detect_profile() { | 
|  | 
| 110 | 129 |         fi | 
| 111 | 130 |     } | 
| 112 | 131 | 
 | 
|  | 132 | +    inject_phpvm_config() { | 
|  | 133 | +        local PHPVM_PROFILE | 
|  | 134 | +        PHPVM_PROFILE="$(phpvm_detect_profile)" | 
|  | 135 | +        local PROFILE_INSTALL_DIR | 
|  | 136 | +        PROFILE_INSTALL_DIR="$(phpvm_install_dir | command sed "s:^$HOME:\$HOME:")" | 
|  | 137 | + | 
|  | 138 | +        PHPVM_CONFIG_STR=" | 
|  | 139 | +# Load PHPVM if necessary (this will allow phpvm to be invoked manually) | 
|  | 140 | +if [ -s \"\$PHPVM_DIR/bin/phpvm\" ]; then | 
|  | 141 | +    export PATH=\"\$PHPVM_DIR/bin:\$PATH\" | 
|  | 142 | +fi | 
|  | 143 | +" | 
|  | 144 | + | 
|  | 145 | +        if [ -n "$PHPVM_PROFILE" ]; then | 
|  | 146 | +            if ! command grep -qc '/phpvm/bin/phpvm' "$PHPVM_PROFILE"; then | 
|  | 147 | +                phpvm_echo "=> Injecting phpvm config into $PHPVM_PROFILE" | 
|  | 148 | +                echo -e "$PHPVM_CONFIG_STR" >>"$PHPVM_PROFILE" | 
|  | 149 | +            else | 
|  | 150 | +                phpvm_echo "=> phpvm config already exists in $PHPVM_PROFILE" | 
|  | 151 | +            fi | 
|  | 152 | +        else | 
|  | 153 | +            phpvm_echo "=> No profile found for phpvm config injection" | 
|  | 154 | +        fi | 
|  | 155 | +    } | 
|  | 156 | + | 
| 113 | 157 |     phpvm_do_install() { | 
| 114 |  | -        install_phpvm_from_git | 
|  | 158 | +        if [ -z "${METHOD}" ]; then | 
|  | 159 | +            if phpvm_has git; then | 
|  | 160 | +                install_phpvm_from_git | 
|  | 161 | +            elif phpvm_has curl || phpvm_has wget; then | 
|  | 162 | +                install_phpvm_as_script | 
|  | 163 | +            else | 
|  | 164 | +                phpvm_echo >&2 'You need git, curl, or wget to install phpvm' | 
|  | 165 | +                exit 1 | 
|  | 166 | +            fi | 
|  | 167 | +        else | 
|  | 168 | +            phpvm_echo >&2 "Unexpected install method: $METHOD" | 
|  | 169 | +            exit 1 | 
|  | 170 | +        fi | 
|  | 171 | + | 
| 115 | 172 |         inject_phpvm_config | 
|  | 173 | + | 
| 116 | 174 |         phpvm_echo "=> phpvm installation completed successfully!" | 
| 117 | 175 |     } | 
| 118 | 176 | 
 | 
| 119 | 177 |     phpvm_do_install | 
| 120 |  | -} | 
|  | 178 | + | 
|  | 179 | +} # this ensures the entire script is downloaded # | 
0 commit comments