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
39 WEBI_PKG_PATH="${HOME}/Downloads/webi/${PKG_NAME:-error}/${WEBI_VERSION:-latest}"
43 ## Set up tmp, download, and install directories
46 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-"${WEBI_PKG:-}".XXXXXXXX)"}
47 export _webi_tmp="${_webi_tmp:-"$HOME/.local/opt/webi-tmp.d"}"
49 mkdir -p "${WEBI_PKG_PATH}"
50 mkdir -p "$HOME/.local/bin"
51 mkdir -p "$HOME/.local/opt"
57 WEBI_CURL="$(command -v curl)"
59 WEBI_WGET="$(command -v wget)"
63 # get the special formatted version (i.e. "go is go1.14" while node is "node v12.10.8")
65 _webi_canonical_name() {
66 if [ -n "$my_versioned_name" ]; then
67 echo "$my_versioned_name"
71 if [ -n "$(command -v pkg_format_cmd_version)" ]; then
72 my_versioned_name="$(pkg_format_cmd_version "$WEBI_VERSION")"
74 my_versioned_name="'$pkg_cmd_name' v$WEBI_VERSION"
77 echo "$my_versioned_name"
80 # update symlinks according to $HOME/.local/opt and $HOME/.local/bin install paths.
81 # shellcheck disable=2120
82 # webi_link may be used in the templated install script
84 if [ -n "$(command -v pkg_link)" ]; then
89 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
91 ln -s "$pkg_src_cmd" "$pkg_dst_cmd"
93 # 'pkg_dst' will default to $HOME/.local/opt/<pkg>
94 # 'pkg_src' will be the installed version, such as to $HOME/.local/opt/<pkg>-<version>
96 ln -s "$pkg_src" "$pkg_dst"
100 # detect if this program is already installed or if an installed version may cause conflict
102 # Test for existing version
105 PATH="$(dirname "$pkg_dst_cmd"):$PATH"
107 my_current_cmd="$(command -v "$pkg_cmd_name")"
109 if [ -n "$my_current_cmd" ]; then
110 # TODO get version from symlink?
111 pkg_current_version=""
112 if [ -n "$(command -v pkg_get_current_version)" ]; then
113 pkg_current_version="$(
114 pkg_get_current_version 2> /dev/null |
118 # remove trailing '.0's for golang's sake
119 my_current_version="$(echo "$pkg_current_version" | sed 's:\.0::g')"
120 my_src_version="$(echo "$WEBI_VERSION" | sed 's:\.0::g')"
121 my_canonical_name="$(_webi_canonical_name)"
122 if [ "$my_src_version" == "$my_current_version" ]; then
123 echo "$my_canonical_name already installed at $my_current_cmd"
126 if [ "$my_current_cmd" != "$pkg_dst_cmd" ]; then
127 echo >&2 "WARN: possible conflict between $my_canonical_name and $pkg_current_version at $my_current_cmd"
129 if [ -x "$pkg_src_cmd" ]; then
130 # shellcheck disable=2119
131 # this function takes no args
133 echo "switched to $my_canonical_name at $pkg_src"
138 export PATH="$my_path"
141 # detect if file is downloaded, and how to download it
143 if [ -n "${1:-}" ]; then
146 if [ "error" == "$WEBI_CHANNEL" ]; then
147 # TODO pass back requested OS / Arch / Version
148 echo >&2 "Error: no '$PKG_NAME' release for '${WEBI_OS:-}' on '$WEBI_ARCH' as one of '$WEBI_FORMATS' by the tag '${WEBI_TAG:-}'"
149 echo >&2 " '$PKG_NAME' is available for '$PKG_OSES' on '$PKG_ARCHES' as one of '$PKG_FORMATS'"
150 echo >&2 " (check that the package name and version are correct)"
152 echo >&2 " Double check at $(echo "$WEBI_RELEASES" | sed 's:\?.*::')"
156 my_url="$WEBI_PKG_URL"
158 if [ -n "${2:-}" ]; then
161 my_dl="${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
164 WEBI_PKG_DOWNLOAD="${my_dl}"
165 export WEBI_PKG_DOWNLOAD
167 if [ -e "$my_dl" ]; then
172 echo "Downloading $PKG_NAME from"
175 # It's only 2020, we can't expect to have reliable CLI tools
176 # to tell us the size of a file as part of a base system...
177 if [ -n "$WEBI_WGET" ]; then
178 # wget has resumable downloads
179 # TODO wget -c --content-disposition "$my_url"
182 if [[ $- == *i* ]]; then
183 my_show_progress="--show-progress"
185 if ! wget -q $my_show_progress --user-agent="wget $WEBI_UA" -c "$my_url" -O "$my_dl.part"; then
186 echo >&2 "failed to download from $WEBI_PKG_URL"
191 # Neither GNU nor BSD curl have sane resume download options, hence we don't bother
192 # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
193 my_show_progress="-#"
194 if [[ $- == *i* ]]; then
197 # shellcheck disable=SC2086
198 # we want the flags to be split
199 curl -fSL $my_show_progress -H "User-Agent: curl $WEBI_UA" "$my_url" -o "$my_dl.part"
201 mv "$my_dl.part" "$my_dl"
204 echo "Saved as $my_dl"
207 # detect which archives can be used
209 pushd "$WEBI_TMP" > /dev/null 2>&1
210 if [ "tar" == "$WEBI_EXT" ]; then
211 echo "Extracting ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
212 tar xf "${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
213 elif [ "zip" == "$WEBI_EXT" ]; then
214 echo "Extracting ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
215 unzip "${WEBI_PKG_PATH}/$WEBI_PKG_FILE" > __unzip__.log
216 elif [ "exe" == "$WEBI_EXT" ]; then
217 echo "Moving ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
218 mv "${WEBI_PKG_PATH}/$WEBI_PKG_FILE" .
219 elif [ "xz" == "$WEBI_EXT" ]; then
220 echo "Inflating ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
221 unxz -c "${WEBI_PKG_PATH}/$WEBI_PKG_FILE" > "$(basename "$WEBI_PKG_FILE")"
224 echo "Failed to extract ${WEBI_PKG_PATH}/$WEBI_PKG_FILE"
227 popd > /dev/null 2>&1
230 # use 'pathman' to update $HOME/.config/envman/PATH.env
232 # make sure that we don't recursively install pathman with webi
234 export PATH="$HOME/.local/bin:$PATH"
236 # install pathman if not already installed
237 if [ -z "$(command -v pathman)" ]; then
238 "$HOME/.local/bin/webi" pathman > /dev/null
241 export PATH="$my_path"
243 # in case pathman was recently installed and the PATH not updated
244 mkdir -p "$_webi_tmp"
245 # prevent "too few arguments" output on bash when there are 0 lines of stdout
246 "$HOME/.local/bin/pathman" add "$1" | grep "export" 2> /dev/null >> "$_webi_tmp/.PATH.env" || true
249 # group common pre-install tasks as default
256 # move commands from the extracted archive directory to $HOME/.local/opt or $HOME/.local/bin
257 # shellcheck disable=2120
258 # webi_install may be sourced and used elsewhere
260 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
261 mkdir -p "$(dirname "$pkg_src_cmd")"
262 mv ./"$pkg_cmd_name"* "$pkg_src_cmd"
265 mv ./"$pkg_cmd_name"* "$pkg_src"
269 # run post-install functions - just updating PATH by default
270 webi_post_install() {
271 webi_path_add "$(dirname "$pkg_dst_cmd")"
274 _webi_enable_exec() {
275 if [ -n "$(command -v spctl)" ] && [ -n "$(command -v xattr)" ]; then
276 # note: some packages contain files that cannot be affected by xattr
277 xattr -r -d com.apple.quarantine "$pkg_src" || true
280 # TODO need to test that the above actually worked
281 # (and proceed to this below if it did not)
282 if [ -n "$(command -v spctl)" ]; then
283 echo "Checking permission to execute '$pkg_cmd_name' on macOS 11+"
285 is_allowed="$(spctl -a "$pkg_src_cmd" 2>&1 | grep valid)"
287 if [ -z "$is_allowed" ]; then
289 echo "##########################################"
290 echo "# IMPORTANT: Permission Grant Required #"
291 echo "##########################################"
293 echo "Requesting permission to execute '$pkg_cmd_name' on macOS 10.14+"
296 spctl --add "$pkg_src_cmd"
301 # a friendly message when all is well, showing the final install path in $HOME/.local
302 _webi_done_message() {
303 echo "Installed $(_webi_canonical_name) as $pkg_dst_cmd"
308 ## BEGIN custom override functions from <package>/install.sh
314 if [[ -z ${WEBI_WELCOME:-} ]]; then
316 printf "Thanks for using webi to install '\e[32m${WEBI_PKG:-}\e[0m' on '\e[31m$(uname -s)/$(uname -m)\e[0m'.\n"
317 echo "Have a problem? Experience a bug? Please let us know:"
318 echo " https://github.com/webinstall/webi-installers/issues"
320 printf "\e[31mLovin'\e[0m it? Say thanks with a \e[34mStar on GitHub\e[0m:\n"
321 printf " \e[32mhttps://github.com/webinstall/webi-installers\e[0m\n"
325 function __init_installer() {
327 # do nothing - to satisfy parser prior to templating
338 ## END custom override functions
342 # run everything with defaults or overrides as needed
343 if [ -n "$(command -v pkg_get_current_version)" ]; then
344 pkg_cmd_name="${pkg_cmd_name:-$PKG_NAME}"
346 if [ -n "$WEBI_SINGLE" ]; then
347 pkg_dst_cmd="${pkg_dst_cmd:-$HOME/.local/bin/$pkg_cmd_name}"
348 pkg_dst="$pkg_dst_cmd" # "$(dirname "$(dirname $pkg_dst_cmd)")"
350 #pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name-v$WEBI_VERSION}"
351 pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name}"
352 pkg_src="$pkg_src_cmd" # "$(dirname "$(dirname $pkg_src_cmd)")"
354 pkg_dst="${pkg_dst:-$HOME/.local/opt/$pkg_cmd_name}"
355 pkg_dst_cmd="${pkg_dst_cmd:-$pkg_dst/bin/$pkg_cmd_name}"
357 pkg_src="${pkg_src:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION}"
358 pkg_src_cmd="${pkg_src_cmd:-$pkg_src/bin/$pkg_cmd_name}"
360 # this script is templated and these are used elsewhere
361 # shellcheck disable=SC2034
362 pkg_src_bin="$(dirname "$pkg_src_cmd")"
363 # shellcheck disable=SC2034
364 pkg_dst_bin="$(dirname "$pkg_dst_cmd")"
366 if [[ -n "$(command -v pkg_pre_install)" ]]; then pkg_pre_install; else webi_pre_install; fi
368 pushd "$WEBI_TMP" > /dev/null 2>&1
369 echo "Installing to $pkg_src_cmd"
370 if [[ -n "$(command -v pkg_install)" ]]; then pkg_install; else webi_install; fi
372 chmod a+x "$pkg_src_cmd"
373 popd > /dev/null 2>&1
378 pushd "$WEBI_TMP" > /dev/null 2>&1
379 if [[ -n "$(command -v pkg_post_install)" ]]; then pkg_post_install; else webi_post_install; fi
380 popd > /dev/null 2>&1
382 pushd "$WEBI_TMP" > /dev/null 2>&1
383 if [[ -n "$(command -v pkg_done_message)" ]]; then pkg_done_message; else _webi_done_message; fi
384 popd > /dev/null 2>&1
389 webi_path_add "$HOME/.local/bin"
390 if [[ -z ${_WEBI_CHILD:-} ]] && [[ -f "$_webi_tmp/.PATH.env" ]]; then
391 if [[ -n $(cat "$_webi_tmp/.PATH.env") ]]; then
392 echo "You need to update your PATH to use $PKG_NAME:"
394 sort -u "$_webi_tmp/.PATH.env"
395 rm -f "$_webi_tmp/.PATH.env"
399 # cleanup the temp directory
402 # See? No magic. Just downloading and moving files.