Skip to content

Commit 273b802

Browse files
committed
Refactor install.sh to remove unnecessary code and improve installation process
1 parent 757b0e1 commit 273b802

File tree

1 file changed

+30
-89
lines changed

1 file changed

+30
-89
lines changed

bin/install.sh

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

3-
{ # this ensures the entire script is downloaded #
4-
3+
{
54
phpvm_has() {
65
type "$1" >/dev/null 2>&1
76
}
@@ -22,34 +21,25 @@
2221
fi
2322
}
2423

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-
3324
phpvm_download() {
3425
if phpvm_has "curl"; then
3526
curl --fail --compressed -q "$@"
3627
elif phpvm_has "wget"; then
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 /')
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 /')
4029
eval wget $ARGS
4130
fi
4231
}
4332

4433
install_phpvm_from_git() {
4534
local INSTALL_DIR
4635
INSTALL_DIR="$(phpvm_install_dir)"
47-
local PHPVM_VERSION
48-
PHPVM_VERSION="${PHPVM_INSTALL_VERSION:-$(phpvm_latest_version)}"
4936

5037
if [ -d "$INSTALL_DIR/.git" ]; then
5138
phpvm_echo "=> phpvm is already installed in $INSTALL_DIR, updating using git"
52-
command printf '\r=> '
39+
command git -C "$INSTALL_DIR" pull --ff-only || {
40+
phpvm_echo >&2 'Failed to update phpvm. Please report this!'
41+
exit 1
42+
}
5343
else
5444
phpvm_echo "=> Downloading phpvm from git to '$INSTALL_DIR'"
5545
mkdir -p "${INSTALL_DIR}"
@@ -59,44 +49,37 @@
5949
}
6050
fi
6151

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-
52+
# Install Node.js dependencies
7853
phpvm_echo "=> Installing Node.js dependencies"
7954
command npm install --prefix "$INSTALL_DIR" || {
8055
phpvm_echo >&2 'Failed to install Node.js dependencies. Please report this!'
8156
exit 1
8257
}
83-
84-
# Ensure the bin directory and executable exist
85-
phpvm_create_bin
8658
}
8759

88-
phpvm_create_bin() {
89-
local INSTALL_DIR
90-
INSTALL_DIR="$(phpvm_install_dir)"
91-
92-
# Create the bin directory if it doesn't exist
93-
mkdir -p "$INSTALL_DIR/bin"
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:")"
9465

95-
# Create a symlink to the phpvm executable
96-
ln -sf "$INSTALL_DIR/index.js" "$INSTALL_DIR/bin/phpvm"
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+
"
9772

98-
# Make the symlink executable
99-
chmod +x "$INSTALL_DIR/bin/phpvm"
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
10083
}
10184

10285
phpvm_detect_profile() {
@@ -127,53 +110,11 @@
127110
fi
128111
}
129112

130-
inject_phpvm_config() {
131-
local PHPVM_PROFILE
132-
PHPVM_PROFILE="$(phpvm_detect_profile)"
133-
local PROFILE_INSTALL_DIR
134-
PROFILE_INSTALL_DIR="$(phpvm_install_dir | command sed "s:^$HOME:\$HOME:")"
135-
136-
PHPVM_CONFIG_STR="
137-
# Load PHPVM if necessary (this will allow phpvm to be invoked manually)
138-
if [ -s \"\$PHPVM_DIR/index.js\" ]; then
139-
export PATH=\"\$PHPVM_DIR/bin:\$PATH\"
140-
fi
141-
"
142-
143-
if [ -n "$PHPVM_PROFILE" ]; then
144-
if ! command grep -qc '/phpvm/index.js' "$PHPVM_PROFILE"; then
145-
phpvm_echo "=> Injecting phpvm config into $PHPVM_PROFILE"
146-
echo -e "$PHPVM_CONFIG_STR" >>"$PHPVM_PROFILE"
147-
else
148-
phpvm_echo "=> phpvm config already exists in $PHPVM_PROFILE"
149-
fi
150-
else
151-
phpvm_echo "=> No profile found for phpvm config injection"
152-
fi
153-
}
154-
155113
phpvm_do_install() {
156-
if [ -z "${METHOD}" ]; then
157-
if phpvm_has git; then
158-
install_phpvm_from_git
159-
elif phpvm_has curl || phpvm_has wget; then
160-
install_phpvm_as_script
161-
else
162-
phpvm_echo >&2 'You need git, curl, or wget to install phpvm'
163-
exit 1
164-
fi
165-
else
166-
phpvm_echo >&2 "Unexpected install method: $METHOD"
167-
exit 1
168-
fi
169-
114+
install_phpvm_from_git
170115
inject_phpvm_config
171-
172-
node "$(phpvm_install_dir)/index.js"
173-
174116
phpvm_echo "=> phpvm installation completed successfully!"
175117
}
176118

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

0 commit comments

Comments
 (0)