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
41 ## Set up tmp, download, and install directories
44 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-"${WEBI_PKG:-}".XXXXXXXX)"}
45 export _webi_tmp="${_webi_tmp:-"$HOME/.local/opt/webi-tmp.d"}"
47 mkdir -p "$HOME/Downloads"
48 mkdir -p "$HOME/.local/bin"
49 mkdir -p "$HOME/.local/opt"
55 WEBI_CURL="$(command -v curl)"
57 WEBI_WGET="$(command -v wget)"
61 # get the special formatted version (i.e. "go is go1.14" while node is "node v12.10.8")
63 _webi_canonical_name() {
64 if [ -n "$my_versioned_name" ]; then
65 echo "$my_versioned_name"
69 if [ -n "$(command -v pkg_format_cmd_version)" ]; then
70 my_versioned_name="$(pkg_format_cmd_version "$WEBI_VERSION")"
72 my_versioned_name="'$pkg_cmd_name' v$WEBI_VERSION"
75 echo "$my_versioned_name"
78 # update symlinks according to $HOME/.local/opt and $HOME/.local/bin install paths.
79 # shellcheck disable=2120
80 # webi_link may be used in the templated install script
82 if [ -n "$(command -v pkg_link)" ]; then
87 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
89 ln -s "$pkg_src_cmd" "$pkg_dst_cmd"
91 # 'pkg_dst' will default to $HOME/.local/opt/<pkg>
92 # 'pkg_src' will be the installed version, such as to $HOME/.local/opt/<pkg>-<version>
94 ln -s "$pkg_src" "$pkg_dst"
98 # detect if this program is already installed or if an installed version may cause conflict
100 # Test for existing version
103 PATH="$(dirname "$pkg_dst_cmd"):$PATH"
105 my_current_cmd="$(command -v "$pkg_cmd_name")"
107 if [ -n "$my_current_cmd" ]; then
108 pkg_current_version="$(pkg_get_current_version 2> /dev/null | head -n 1)"
109 # remove trailing '.0's for golang's sake
110 my_current_version="$(echo "$pkg_current_version" | sed 's:\.0::g')"
111 my_src_version="$(echo "$WEBI_VERSION" | sed 's:\.0::g')"
112 my_canonical_name="$(_webi_canonical_name)"
113 if [ "$my_src_version" == "$my_current_version" ]; then
114 echo "$my_canonical_name already installed at $my_current_cmd"
117 if [ "$my_current_cmd" != "$pkg_dst_cmd" ]; then
118 echo >&2 "WARN: possible conflict between $my_canonical_name and $pkg_current_version at $my_current_cmd"
120 if [ -x "$pkg_src_cmd" ]; then
121 # shellcheck disable=2119
122 # this function takes no args
124 echo "switched to $my_canonical_name at $pkg_src"
129 export PATH="$my_path"
132 # detect if file is downloaded, and how to download it
134 if [ -n "${1:-}" ]; then
137 if [ "error" == "$WEBI_CHANNEL" ]; then
138 # TODO pass back requested OS / Arch / Version
139 echo >&2 "Error: no '$PKG_NAME' release for '${WEBI_OS:-}' on '$WEBI_ARCH' as one of '$WEBI_FORMATS' by the tag '${WEBI_TAG:-}'"
140 echo >&2 " '$PKG_NAME' is available for '$PKG_OSES' on '$PKG_ARCHES' as one of '$PKG_FORMATS'"
141 echo >&2 " (check that the package name and version are correct)"
143 echo >&2 " Double check at $(echo "$WEBI_RELEASES" | sed 's:\?.*::')"
147 my_url="$WEBI_PKG_URL"
149 if [ -n "${2:-}" ]; then
152 my_dl="$HOME/Downloads/$WEBI_PKG_FILE"
155 if [ -e "$my_dl" ]; then
160 echo "Downloading $PKG_NAME from"
163 # It's only 2020, we can't expect to have reliable CLI tools
164 # to tell us the size of a file as part of a base system...
165 if [ -n "$WEBI_WGET" ]; then
166 # wget has resumable downloads
167 # TODO wget -c --content-disposition "$my_url"
170 if [[ $- == *i* ]]; then
171 my_show_progress="--show-progress"
173 if ! wget -q $my_show_progress --user-agent="wget $WEBI_UA" -c "$my_url" -O "$my_dl.part"; then
174 echo >&2 "failed to download from $WEBI_PKG_URL"
179 # Neither GNU nor BSD curl have sane resume download options, hence we don't bother
180 # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
181 my_show_progress="-#"
182 if [[ $- == *i* ]]; then
185 # shellcheck disable=SC2086
186 # we want the flags to be split
187 curl -fSL $my_show_progress -H "User-Agent: curl $WEBI_UA" "$my_url" -o "$my_dl.part"
189 mv "$my_dl.part" "$my_dl"
192 echo "Saved as $my_dl"
195 # detect which archives can be used
197 pushd "$WEBI_TMP" > /dev/null 2>&1
198 if [ "tar" == "$WEBI_EXT" ]; then
199 echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
200 tar xf "$HOME/Downloads/$WEBI_PKG_FILE"
201 elif [ "zip" == "$WEBI_EXT" ]; then
202 echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
203 unzip "$HOME/Downloads/$WEBI_PKG_FILE" > __unzip__.log
204 elif [ "exe" == "$WEBI_EXT" ]; then
205 echo "Moving $HOME/Downloads/$WEBI_PKG_FILE"
206 mv "$HOME/Downloads/$WEBI_PKG_FILE" .
207 elif [ "xz" == "$WEBI_EXT" ]; then
208 echo "Inflating $HOME/Downloads/$WEBI_PKG_FILE"
209 unxz -c "$HOME/Downloads/$WEBI_PKG_FILE" > "$(basename "$WEBI_PKG_FILE")"
212 echo "Failed to extract $HOME/Downloads/$WEBI_PKG_FILE"
215 popd > /dev/null 2>&1
218 # use 'pathman' to update $HOME/.config/envman/PATH.env
220 # make sure that we don't recursively install pathman with webi
222 export PATH="$HOME/.local/bin:$PATH"
224 # install pathman if not already installed
225 if [ -z "$(command -v pathman)" ]; then
226 "$HOME/.local/bin/webi" pathman > /dev/null
229 export PATH="$my_path"
231 # in case pathman was recently installed and the PATH not updated
232 mkdir -p "$_webi_tmp"
233 # prevent "too few arguments" output on bash when there are 0 lines of stdout
234 "$HOME/.local/bin/pathman" add "$1" | grep "export" 2> /dev/null >> "$_webi_tmp/.PATH.env" || true
237 # group common pre-install tasks as default
244 # move commands from the extracted archive directory to $HOME/.local/opt or $HOME/.local/bin
245 # shellcheck disable=2120
246 # webi_install may be sourced and used elsewhere
248 if [ -n "$WEBI_SINGLE" ] || [ "single" == "${1:-}" ]; then
249 mkdir -p "$(dirname "$pkg_src_cmd")"
250 mv ./"$pkg_cmd_name"* "$pkg_src_cmd"
253 mv ./"$pkg_cmd_name"* "$pkg_src"
257 # run post-install functions - just updating PATH by default
258 webi_post_install() {
259 webi_path_add "$(dirname "$pkg_dst_cmd")"
262 _webi_enable_exec() {
263 if [ -n "$(command -v spctl)" ] && [ -n "$(command -v xattr)" ]; then
264 # note: some packages contain files that cannot be affected by xattr
265 xattr -r -d com.apple.quarantine "$pkg_src" || true
268 # TODO need to test that the above actually worked
269 # (and proceed to this below if it did not)
270 if [ -n "$(command -v spctl)" ]; then
271 echo "Checking permission to execute '$pkg_cmd_name' on macOS 11+"
273 is_allowed="$(spctl -a "$pkg_src_cmd" 2>&1 | grep valid)"
275 if [ -z "$is_allowed" ]; then
277 echo "##########################################"
278 echo "# IMPORTANT: Permission Grant Required #"
279 echo "##########################################"
281 echo "Requesting permission to execute '$pkg_cmd_name' on macOS 10.14+"
284 spctl --add "$pkg_src_cmd"
289 # a friendly message when all is well, showing the final install path in $HOME/.local
290 _webi_done_message() {
291 echo "Installed $(_webi_canonical_name) as $pkg_dst_cmd"
296 ## BEGIN custom override functions from <package>/install.sh
302 if [[ -z ${WEBI_WELCOME:-} ]]; then
304 echo "Thanks for using webi to install '$PKG_NAME' on '$WEBI_OS/$WEBI_ARCH'."
305 echo "Have a problem? Experience a bug? Please let us know:"
306 echo " https://github.com/webinstall/packages/issues"
310 function __init_installer() {
312 # do nothing - to satisfy parser prior to templating
323 ## END custom override functions
327 # run everything with defaults or overrides as needed
328 if [ -n "$(command -v pkg_get_current_version)" ]; then
329 pkg_cmd_name="${pkg_cmd_name:-$PKG_NAME}"
331 if [ -n "$WEBI_SINGLE" ]; then
332 pkg_dst_cmd="${pkg_dst_cmd:-$HOME/.local/bin/$pkg_cmd_name}"
333 pkg_dst="$pkg_dst_cmd" # "$(dirname "$(dirname $pkg_dst_cmd)")"
335 #pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name-v$WEBI_VERSION}"
336 pkg_src_cmd="${pkg_src_cmd:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION/bin/$pkg_cmd_name}"
337 pkg_src="$pkg_src_cmd" # "$(dirname "$(dirname $pkg_src_cmd)")"
339 pkg_dst="${pkg_dst:-$HOME/.local/opt/$pkg_cmd_name}"
340 pkg_dst_cmd="${pkg_dst_cmd:-$pkg_dst/bin/$pkg_cmd_name}"
342 pkg_src="${pkg_src:-$HOME/.local/opt/$pkg_cmd_name-v$WEBI_VERSION}"
343 pkg_src_cmd="${pkg_src_cmd:-$pkg_src/bin/$pkg_cmd_name}"
345 # this script is templated and these are used elsewhere
346 # shellcheck disable=SC2034
347 pkg_src_bin="$(dirname "$pkg_src_cmd")"
348 # shellcheck disable=SC2034
349 pkg_dst_bin="$(dirname "$pkg_dst_cmd")"
351 if [[ -n "$(command -v pkg_pre_install)" ]]; then pkg_pre_install; else webi_pre_install; fi
353 pushd "$WEBI_TMP" > /dev/null 2>&1
354 echo "Installing to $pkg_src_cmd"
355 if [[ -n "$(command -v pkg_install)" ]]; then pkg_install; else webi_install; fi
357 chmod a+x "$pkg_src_cmd"
358 popd > /dev/null 2>&1
363 pushd "$WEBI_TMP" > /dev/null 2>&1
364 if [[ -n "$(command -v pkg_post_install)" ]]; then pkg_post_install; else webi_post_install; fi
365 popd > /dev/null 2>&1
367 pushd "$WEBI_TMP" > /dev/null 2>&1
368 if [[ -n "$(command -v pkg_done_message)" ]]; then pkg_done_message; else _webi_done_message; fi
369 popd > /dev/null 2>&1
374 webi_path_add "$HOME/.local/bin"
375 if [[ -z ${_WEBI_CHILD:-} ]] && [[ -f "$_webi_tmp/.PATH.env" ]]; then
376 if [[ -n $(cat "$_webi_tmp/.PATH.env") ]]; then
377 echo "You need to update your PATH to use $PKG_NAME:"
379 sort -u "$_webi_tmp/.PATH.env"
380 rm -f "$_webi_tmp/.PATH.env"
384 # cleanup the temp directory
387 # See? No magic. Just downloading and moving files.