Skip to content

Commit

Permalink
Refactor install.sh to improve installation process and add version f…
Browse files Browse the repository at this point in the history
…etching
  • Loading branch information
Thavarshan committed Oct 6, 2024
1 parent 273b802 commit f29e3f6
Showing 1 changed file with 89 additions and 30 deletions.
119 changes: 89 additions & 30 deletions bin/install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

{
{ # this ensures the entire script is downloaded #

phpvm_has() {
type "$1" >/dev/null 2>&1
}
Expand All @@ -21,25 +22,34 @@
fi
}

phpvm_latest_version() {
latest_version=$(curl -s https://api.github.com/repos/Thavarshan/phpvm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$latest_version" ]; then
latest_version="main"
fi
phpvm_echo "$latest_version"
}

phpvm_download() {
if phpvm_has "curl"; then
curl --fail --compressed -q "$@"
elif phpvm_has "wget"; then
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 /')
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 /')
eval wget $ARGS
fi
}

install_phpvm_from_git() {
local INSTALL_DIR
INSTALL_DIR="$(phpvm_install_dir)"
local PHPVM_VERSION
PHPVM_VERSION="${PHPVM_INSTALL_VERSION:-$(phpvm_latest_version)}"

if [ -d "$INSTALL_DIR/.git" ]; then
phpvm_echo "=> phpvm is already installed in $INSTALL_DIR, updating using git"
command git -C "$INSTALL_DIR" pull --ff-only || {
phpvm_echo >&2 'Failed to update phpvm. Please report this!'
exit 1
}
command printf '\r=> '
else
phpvm_echo "=> Downloading phpvm from git to '$INSTALL_DIR'"
mkdir -p "${INSTALL_DIR}"
Expand All @@ -49,37 +59,46 @@
}
fi

# Install Node.js dependencies
command git -C "$INSTALL_DIR" checkout "$PHPVM_VERSION" || {
phpvm_echo >&2 "Failed to checkout the version $PHPVM_VERSION. Please report this!"
exit 1
}

phpvm_echo "=> Cleaning up git repository"
command git -C "$INSTALL_DIR" gc --auto --aggressive --prune=now || {
phpvm_echo >&2 'Failed to clean up git repository. Please report this!'
exit 1
}

if ! phpvm_has "node"; then
phpvm_echo "Node.js is required to run phpvm. Please install Node.js and try again."
exit 1
fi

phpvm_echo "=> Installing Node.js dependencies"
command npm install --prefix "$INSTALL_DIR" || {
phpvm_echo >&2 'Failed to install Node.js dependencies. Please report this!'
exit 1
}

phpvm_create_launcher
}

inject_phpvm_config() {
local PHPVM_PROFILE
PHPVM_PROFILE="$(phpvm_detect_profile)"
local PROFILE_INSTALL_DIR
PROFILE_INSTALL_DIR="$(phpvm_install_dir | command sed "s:^$HOME:\$HOME:")"
phpvm_create_launcher() {
local INSTALL_DIR
INSTALL_DIR="$(phpvm_install_dir)"

PHPVM_CONFIG_STR="
# Load PHPVM if necessary (this will allow phpvm to be invoked manually)
if [ -s \"\$PHPVM_DIR/index.js\" ]; then
export PATH=\"\$PHPVM_DIR/bin:\$PATH\"
fi
"
# Create the bin directory if it doesn't exist
mkdir -p "$INSTALL_DIR/bin"

if [ -n "$PHPVM_PROFILE" ]; then
if ! command grep -qc '/phpvm/index.js' "$PHPVM_PROFILE"; then
phpvm_echo "=> Injecting phpvm config into $PHPVM_PROFILE"
echo -e "$PHPVM_CONFIG_STR" >>"$PHPVM_PROFILE"
else
phpvm_echo "=> phpvm config already exists in $PHPVM_PROFILE"
fi
else
phpvm_echo "=> No profile found for phpvm config injection"
fi
# Create a shell script that runs the index.js
cat <<EOL >"$INSTALL_DIR/bin/phpvm"
#!/usr/bin/env bash
node "\$PHPVM_DIR/index.js" "\$@"
EOL

# Make the shell script executable
chmod +x "$INSTALL_DIR/bin/phpvm"
}

phpvm_detect_profile() {
Expand Down Expand Up @@ -110,11 +129,51 @@ fi
fi
}

inject_phpvm_config() {
local PHPVM_PROFILE
PHPVM_PROFILE="$(phpvm_detect_profile)"
local PROFILE_INSTALL_DIR
PROFILE_INSTALL_DIR="$(phpvm_install_dir | command sed "s:^$HOME:\$HOME:")"

PHPVM_CONFIG_STR="
# Load PHPVM if necessary (this will allow phpvm to be invoked manually)
if [ -s \"\$PHPVM_DIR/bin/phpvm\" ]; then
export PATH=\"\$PHPVM_DIR/bin:\$PATH\"
fi
"

if [ -n "$PHPVM_PROFILE" ]; then
if ! command grep -qc '/phpvm/bin/phpvm' "$PHPVM_PROFILE"; then
phpvm_echo "=> Injecting phpvm config into $PHPVM_PROFILE"
echo -e "$PHPVM_CONFIG_STR" >>"$PHPVM_PROFILE"
else
phpvm_echo "=> phpvm config already exists in $PHPVM_PROFILE"
fi
else
phpvm_echo "=> No profile found for phpvm config injection"
fi
}

phpvm_do_install() {
install_phpvm_from_git
if [ -z "${METHOD}" ]; then
if phpvm_has git; then
install_phpvm_from_git
elif phpvm_has curl || phpvm_has wget; then
install_phpvm_as_script
else
phpvm_echo >&2 'You need git, curl, or wget to install phpvm'
exit 1
fi
else
phpvm_echo >&2 "Unexpected install method: $METHOD"
exit 1
fi

inject_phpvm_config

phpvm_echo "=> phpvm installation completed successfully!"
}

phpvm_do_install
}

} # this ensures the entire script is downloaded #

0 comments on commit f29e3f6

Please sign in to comment.