Skip to content

Commit f29e3f6

Browse files
committed
Refactor install.sh to improve installation process and add version fetching
1 parent 273b802 commit f29e3f6

File tree

1 file changed

+89
-30
lines changed

1 file changed

+89
-30
lines changed

bin/install.sh

Lines changed: 89 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

3-
{
3+
{ # this ensures the entire script is downloaded #
4+
45
phpvm_has() {
56
type "$1" >/dev/null 2>&1
67
}
@@ -21,25 +22,34 @@
2122
fi
2223
}
2324

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+
2433
phpvm_download() {
2534
if phpvm_has "curl"; then
2635
curl --fail --compressed -q "$@"
2736
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 /')
2940
eval wget $ARGS
3041
fi
3142
}
3243

3344
install_phpvm_from_git() {
3445
local INSTALL_DIR
3546
INSTALL_DIR="$(phpvm_install_dir)"
47+
local PHPVM_VERSION
48+
PHPVM_VERSION="${PHPVM_INSTALL_VERSION:-$(phpvm_latest_version)}"
3649

3750
if [ -d "$INSTALL_DIR/.git" ]; then
3851
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=> '
4353
else
4454
phpvm_echo "=> Downloading phpvm from git to '$INSTALL_DIR'"
4555
mkdir -p "${INSTALL_DIR}"
@@ -49,37 +59,46 @@
4959
}
5060
fi
5161

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+
5378
phpvm_echo "=> Installing Node.js dependencies"
5479
command npm install --prefix "$INSTALL_DIR" || {
5580
phpvm_echo >&2 'Failed to install Node.js dependencies. Please report this!'
5681
exit 1
5782
}
83+
84+
phpvm_create_launcher
5885
}
5986

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)"
6590

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"
7293

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"
83102
}
84103

85104
phpvm_detect_profile() {
@@ -110,11 +129,51 @@ fi
110129
fi
111130
}
112131

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+
113157
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+
115172
inject_phpvm_config
173+
116174
phpvm_echo "=> phpvm installation completed successfully!"
117175
}
118176

119177
phpvm_do_install
120-
}
178+
179+
} # this ensures the entire script is downloaded #

0 commit comments

Comments
 (0)