-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.sh
executable file
·227 lines (181 loc) · 5.02 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
# Copyright MontFerret Team 2023
# Licensed under the MIT license.
set -e
# Declare constants
readonly projectName="MontFerret"
readonly appName="lab"
readonly binName="lab"
readonly fullAppName="${projectName} $(echo ${appName} | awk '{print toupper(substr($0,0,1)) substr($0,2)}')"
readonly baseUrl="https://github.com/${projectName}/${appName}/releases/download"
# Declare default values
readonly defaultLocation="${HOME}/.ferret"
readonly defaultVersion="latest"
# Print a message to stdout
report() {
command printf "%s\n" "$*" 2>/dev/null
}
# Check if a command is available
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if a path exists
check_path() {
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
return 1
fi
report "${1}"
}
# Validate user input
validate_input() {
local location="$1"
local version="$2"
if [ -z "$location" ]; then
report "Invalid location: $location"
exit 1
fi
# Check if location exists
if [ ! -d "$location" ]; then
report "Location does not exist: $location"
exit 1
fi
# Check if location is writable
if [ ! -w "$location" ]; then
report "Location is not writable: $location"
exit 1
fi
if [ "$version" != "latest" ] && [[ ! "$version" =~ [0-9]+\.[0-9]+\.[0-9]+$ ]]; then
report "Invalid version: $version"
exit 1
fi
}
# Detect the profile file
detect_profile() {
local profile=""
local detected_profile=""
if [ "${PROFILE-}" = '/dev/null' ]; then
# the user has specifically requested NOT to have nvm touch their profile
return
fi
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
report "${PROFILE}"
return
fi
if command_exists bash; then
if [ -f "$HOME/.bashrc" ]; then
detected_profile="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
detected_profile="$HOME/.bash_profile"
fi
elif command_exists zsh; then
if [ -f "$HOME/.zshrc" ]; then
detected_profile="$HOME/.zshrc"
fi
fi
if [ -z "$detected_profile" ]; then
for profile_name in ".profile" ".bashrc" ".bash_profile" ".zshrc"; do
if detected_profile="$(check_path "${HOME}/${profile_name}")"; then
break
fi
done
fi
if [ -n "$detected_profile" ]; then
report "$detected_profile"
fi
}
# Update the profile file
update_profile() {
local location="$1"
local profile="$(detect_profile)"
if [[ ":$PATH:" == *":$location:"* ]]; then
return
fi
report "Updating profile $profile"
if [ -z "$profile" ]; then
report "Profile not found. Tried ${DETECTED_PROFILE-} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
report "Append the following lines to the correct file yourself:"
report
report "export PATH=\$PATH:${location}"
report
else
if ! grep -q "${location}" "$profile"; then
report "export PATH=\$PATH:${location}" >>"$profile"
fi
fi
}
# Get the platform-specific filename suffix
get_platform_suffix() {
local platform_name="$(uname)"
local arch_name="$(uname -m)"
local platform=""
local arch=""
case "$platform_name" in
"Darwin")
platform="_darwin"
;;
"Linux")
platform="_linux"
;;
"Windows")
platform="_windows"
;;
*)
report "$platform_name is not supported. Exiting..."
exit 1
;;
esac
case "$arch_name" in
"x86_64")
arch="_x86_64"
;;
"aarch64" | "arm64")
arch="_arm64"
;;
*)
report "$arch_name is not supported. Exiting..."
exit 1
;;
esac
echo "${platform}${arch}"
}
get_version_tag() {
local version="$1"
if [ "$version" = "latest" ]; then
local url="https://api.github.com/repos/${projectName}/${appName}/releases/latest"
curl -sSL "${url}" | grep "tag_name" | cut -d '"' -f 4
else
# Check if the version starts with a 'v'
if [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$version"
else
echo "v$version"
fi
fi
}
# Install the package
install() {
local location="${LOCATION:-$defaultLocation}"
local version=$(get_version_tag "${VERSION:-$defaultVersion}")
local tmp_dir="$(mktemp -d -t "${projectName}.${appName}.XXXXXXX")"
validate_input "$location" "$version"
report "Installing ${projectName} ${appName} ${version}..."
# Download the archive to a temporary location
local suffix="$(get_platform_suffix)"
local file_name="${appName}${suffix}"
local download_dir="${tmp_dir}/${file_name}@${version}"
mkdir -p "${download_dir}"
local download_file="${download_dir}/${file_name}.tar.gz"
local url="${baseUrl}/${version}/${file_name}.tar.gz"
report "Downloading package $url as $download_file"
curl -sSL "${url}" | tar xz --directory "${download_dir}"
local downloaded_file="${download_dir}/${binName}"
report "Copying ${downloaded_file} to ${location}"
cp "${downloaded_file}" "${location}"
local executable="${location}/${binName}"
chmod +x "${executable}"
update_profile "${location}"
report "New version of ${fullAppName} installed to ${location}"
"$executable" version
}
# Call the main function
install