3 # shellcheck disable=2001
4 # because I prefer to use sed rather than bash replace
5 # (there's too little space in my head to learn both syntaxes)
7 function __bootstrap_webi() {
15 # TODO should this be BASEURL instead?
26 # TODO not sure if BUILD is the best name for this
42 ## Set up tmp, download, and install directories
45 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-"${WEBI_PKG:-}".XXXXXXXX)"}
46 export _webi_tmp="${_webi_tmp:-"$HOME/.local/opt/webi-tmp.d"}"
48 mkdir -p "$HOME/Downloads"
49 mkdir -p "$HOME/.local/bin"
50 mkdir -p "$HOME/.local/opt"
56 WEBI_CURL="$(command -v curl)"
58 WEBI_WGET="$(command -v wget)"
62 # get the special formatted version (i.e. "go is go1.14" while node is "node v12.10.8")
64 _webi_canonical_name() {
65 if [ -n "$my_versioned_name" ]; then
66 echo "$my_versioned_name"
70 if [ -n "$(command -v pkg_format_cmd_version)" ]; then
71 my_versioned_name="$(pkg_format_cmd_version "$WEBI_VERSION")"
73 my_versioned_name="'$pkg_cmd_name' v$WEBI_VERSION"
76 echo "$my_versioned_name"
79 # update symlinks according to $HOME/.local/opt and $HOME/.local/bin install paths.
80 # shellcheck disable=2120
81 # webi_link may be used in the templated install script
83 if [ -n "$(command -v pkg_link)" ]; then
88 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
90 ln -s "$pkg_src_cmd" "$pkg_dst_cmd"
92 # 'pkg_dst' will default to $HOME/.local/opt/<pkg>
93 # 'pkg_src' will be the installed version, such as to $HOME/.local/opt/<pkg>-<version>
95 ln -s "$pkg_src" "$pkg_dst"
99 # detect if this program is already installed or if an installed version may cause conflict
101 # Test for existing version
104 PATH="$(dirname "$pkg_dst_cmd"):$PATH"
106 my_current_cmd="$(command -v "$pkg_cmd_name")"
108 if [ -n "$my_current_cmd" ]; then
109 pkg_current_version="$(pkg_get_current_version 2> /dev/null | head -n 1)"
110 # remove trailing '.0's for golang's sake
111 my_current_version="$(echo "$pkg_current_version" | sed 's:\.0::g')"
112 my_src_version="$(echo "$WEBI_VERSION" | sed 's:\.0::g')"
113 my_canonical_name="$(_webi_canonical_name)"
114 if [ "$my_src_version" == "$my_current_version" ]; then
115 echo "$my_canonical_name already installed at $my_current_cmd"
118 if [ "$my_current_cmd" != "$pkg_dst_cmd" ]; then
119 echo >&2 "WARN: possible conflict between $my_canonical_name and $pkg_current_version at $my_current_cmd"
121 if [ -x "$pkg_src_cmd" ]; then
122 # shellcheck disable=2119
123 # this function takes no args
125 echo "switched to $my_canonical_name at $pkg_src"
130 export PATH="$my_path"
133 # detect if file is downloaded, and how to download it
135 if [ -n "${1:-}" ]; then
138 if [ "error" == "$WEBI_CHANNEL" ]; then
139 # TODO pass back requested OS / Arch / Version
140 echo >&2 "Error: no '$PKG_NAME' release for '${WEBI_OS:-}' on '$WEBI_ARCH' as one of '$WEBI_FORMATS' by the tag '${WEBI_TAG:-}'"
141 echo >&2 " '$PKG_NAME' is available for '$PKG_OSES' on '$PKG_ARCHES' as one of '$PKG_FORMATS'"
142 echo >&2 " (check that the package name and version are correct)"
144 echo >&2 " Double check at $(echo "$WEBI_RELEASES" | sed 's:\?.*::')"
148 my_url="$WEBI_PKG_URL"
150 if [ -n "${2:-}" ]; then
153 my_dl="$HOME/Downloads/$WEBI_PKG_FILE"
156 WEBI_PKG_DOWNLOAD="${my_dl}"
157 export WEBI_PKG_DOWNLOAD
159 if [ -e "$my_dl" ]; then
164 echo "Downloading $PKG_NAME from"
167 # It's only 2020, we can't expect to have reliable CLI tools
168 # to tell us the size of a file as part of a base system...
169 if [ -n "$WEBI_WGET" ]; then
170 # wget has resumable downloads
171 # TODO wget -c --content-disposition "$my_url"
174 if [[ $- == *i* ]]; then
175 my_show_progress="--show-progress"
177 if ! wget -q $my_show_progress --user-agent="wget $WEBI_UA" -c "$my_url" -O "$my_dl.part"; then
178 echo >&2 "failed to download from $WEBI_PKG_URL"
183 # Neither GNU nor BSD curl have sane resume download options, hence we don't bother
184 # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
185 my_show_progress="-#"
186 if [[ $- == *i* ]]; then
189 # shellcheck disable=SC2086
190 # we want the flags to be split
191 curl -fSL $my_show_progress -H "User-Agent: curl $WEBI_UA" "$my_url" -o "$my_dl.part"
193 mv "$my_dl.part" "$my_dl"
196 echo "Saved as $my_dl"
199 # detect which archives can be used
201 pushd "$WEBI_TMP" > /dev/null 2>&1
202 if [ "tar" == "$WEBI_EXT" ]; then
203 echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
204 tar xf "$HOME/Downloads/$WEBI_PKG_FILE"
205 elif [ "zip" == "$WEBI_EXT" ]; then
206 echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
207 unzip "$HOME/Downloads/$WEBI_PKG_FILE" > __unzip__.log
208 elif [ "exe" == "$WEBI_EXT" ]; then
209 echo "Moving $HOME/Downloads/$WEBI_PKG_FILE"
210 mv "$HOME/Downloads/$WEBI_PKG_FILE" .
211 elif [ "xz" == "$WEBI_EXT" ]; then
212 echo "Inflating $HOME/Downloads/$WEBI_PKG_FILE"
213 unxz -c "$HOME/Downloads/$WEBI_PKG_FILE" > "$(basename "$WEBI_PKG_FILE")"
216 echo "Failed to extract $HOME/Downloads/$WEBI_PKG_FILE"
219 popd > /dev/null 2>&1
222 # use 'pathman' to update $HOME/.config/envman/PATH.env
224 # make sure that we don't recursively install pathman with webi
226 export PATH="$HOME/.local/bin:$PATH"
228 # install pathman if not already installed
229 if [ -z "$(command -v pathman)" ]; then
230 "$HOME/.local/bin/webi" pathman > /dev/null
233 export PATH="$my_path"
235 # in case pathman was recently installed and the PATH not updated
236 mkdir -p "$_webi_tmp"
237 # prevent "too few arguments" output on bash when there are 0 lines of stdout
238 "$HOME/.local/bin/pathman" add "$1" | grep "export" 2> /dev/null >> "$_webi_tmp/.PATH.env" || true
241 # group common pre-install tasks as default
248 # move commands from the extracted archive directory to $HOME/.local/opt or $HOME/.local/bin
249 # shellcheck disable=2120
250 # webi_install may be sourced and used elsewhere
252 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
253 mkdir -p "$(dirname "$pkg_src_cmd")"
254 mv ./"$pkg_cmd_name"* "$pkg_src_cmd"
257 mv ./"$pkg_cmd_name"* "$pkg_src"
261 # run post-install functions - just updating PATH by default
262 webi_post_install() {
263 webi_path_add "$(dirname "$pkg_dst_cmd")"
266 _webi_enable_exec() {
267 if [ -n "$(command -v spctl)" ] && [ -n "$(command -v xattr)" ]; then
268 # note: some packages contain files that cannot be affected by xattr
269 xattr -r -d com.apple.quarantine "$pkg_src" || true
272 # TODO need to test that the above actually worked
273 # (and proceed to this below if it did not)
274 if [ -n "$(command -v spctl)" ]; then
275 echo "Checking permission to execute '$pkg_cmd_name' on macOS 11+"
277 is_allowed="$(spctl -a "$pkg_src_cmd" 2>&1 | grep valid)"
279 if [ -z "$is_allowed" ]; then
281 echo "##########################################"
282 echo "# IMPORTANT: Permission Grant Required #"
283 echo "##########################################"
285 echo "Requesting permission to execute '$pkg_cmd_name' on macOS 10.14+"
288 spctl --add "$pkg_src_cmd"
293 # a friendly message when all is well, showing the final install path in $HOME/.local
294 _webi_done_message() {
295 echo "Installed $(_webi_canonical_name) as $pkg_dst_cmd"
300 ## BEGIN custom override functions from <package>/install.sh
306 if [[ -z ${WEBI_WELCOME:-} ]]; then
308 printf "Thanks for using webi to install '\e[32m${WEBI_PKG:-}\e[0m' on '\e[31m$(uname -s)/$(uname -m)\e[0m'.\n"
309 echo "Have a problem? Experience a bug? Please let us know:"
310 echo " https://github.com/webinstall/webi-installers/issues"
312 printf "\e[31mLovin'\e[0m it? Say thanks with a \e[34mStar on GitHub\e[0m:\n"
313 printf " \e[32mhttps://github.com/webinstall/webi-installers\e[0m\n"
317 function __init_installer() {
319 # do nothing - to satisfy parser prior to templating
330 ## END custom override functions
334 # run everything with defaults or overrides as needed
335 if [ -n "$(command -v pkg_get_current_version)" ]; then
336 pkg_cmd_name="${pkg_cmd_name:-$PKG_NAME}"
338 if [ -n "$WEBI_SINGLE" ]; then
339 pkg_dst_cmd="${pkg_dst_cmd:-$HOME/.local/bin/$pkg_cmd_name}"
340 pkg_dst="$pkg_dst_cmd" # "$(dirname "$(dirname $pkg_dst_cmd)")"
342 #pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name-v$WEBI_VERSION}"
343 pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name}"
344 pkg_src="$pkg_src_cmd" # "$(dirname "$(dirname $pkg_src_cmd)")"
346 pkg_dst="${pkg_dst:-$HOME/.local/opt/$pkg_cmd_name}"
347 pkg_dst_cmd="${pkg_dst_cmd:-$pkg_dst/bin/$pkg_cmd_name}"
349 pkg_src="${pkg_src:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION}"
350 pkg_src_cmd="${pkg_src_cmd:-$pkg_src/bin/$pkg_cmd_name}"
352 # this script is templated and these are used elsewhere
353 # shellcheck disable=SC2034
354 pkg_src_bin="$(dirname "$pkg_src_cmd")"
355 # shellcheck disable=SC2034
356 pkg_dst_bin="$(dirname "$pkg_dst_cmd")"
358 if [[ -n "$(command -v pkg_pre_install)" ]]; then pkg_pre_install; else webi_pre_install; fi
360 pushd "$WEBI_TMP" > /dev/null 2>&1
361 echo "Installing to $pkg_src_cmd"
362 if [[ -n "$(command -v pkg_install)" ]]; then pkg_install; else webi_install; fi
364 chmod a+x "$pkg_src_cmd"
365 popd > /dev/null 2>&1
370 pushd "$WEBI_TMP" > /dev/null 2>&1
371 if [[ -n "$(command -v pkg_post_install)" ]]; then pkg_post_install; else webi_post_install; fi
372 popd > /dev/null 2>&1
374 pushd "$WEBI_TMP" > /dev/null 2>&1
375 if [[ -n "$(command -v pkg_done_message)" ]]; then pkg_done_message; else _webi_done_message; fi
376 popd > /dev/null 2>&1
381 webi_path_add "$HOME/.local/bin"
382 if [[ -z ${_WEBI_CHILD:-} ]] && [[ -f "$_webi_tmp/.PATH.env" ]]; then
383 if [[ -n $(cat "$_webi_tmp/.PATH.env") ]]; then
384 echo "You need to update your PATH to use $PKG_NAME:"
386 sort -u "$_webi_tmp/.PATH.env"
387 rm -f "$_webi_tmp/.PATH.env"
391 # cleanup the temp directory
394 # See? No magic. Just downloading and moving files.