Compare commits

..

2 Commits

Author SHA1 Message Date
AJ ONeal
b96f457496 wip: feat: log whenever directories are created 2023-11-20 00:05:35 +00:00
AJ ONeal
0b3f73f661 wip: feat: add convenience functions to webi script 2023-11-20 00:05:35 +00:00
8 changed files with 386 additions and 932 deletions

View File

@@ -126,14 +126,9 @@ fn_wget() { (
a_url="${1}"
a_path="${2}"
cmd_wget="wget -c -q --user-agent"
cmd_wget="wget -q --user-agent"
if fn_is_tty; then
cmd_wget="wget -c -q --show-progress --user-agent"
fi
# busybox wget doesn't support --show-progress
# See <https://github.com/webinstall/webi-installers/pull/772>
if readlink "$(command -v wget)" | grep -q busybox; then
cmd_wget="wget --user-agent"
cmd_wget="wget -q --show-progress --user-agent"
fi
b_triple_ua="$(fn_get_target_triple_user_agent)"
@@ -142,9 +137,9 @@ fn_wget() { (
b_agent="webi/wget+curl ${b_triple_ua}"
fi
if ! $cmd_wget "${b_agent}" "${a_url}" -O "${a_path}"; then
if ! $cmd_wget "${b_agent}" -c "${a_url}" -O "${a_path}"; then
echo >&2 " $(t_err "failed to download (wget)") '$(t_url "${a_url}")'"
echo >&2 " $cmd_wget '${b_agent}' '${a_url}' -O '${a_path}'"
echo >&2 " $cmd_wget '${b_agent}' -c '${a_url}' -O '${a_path}'"
echo >&2 " $(wget -V)"
return 1
fi
@@ -187,10 +182,10 @@ fn_download_to_path() { (
a_path="${2}"
mkdir -p "$(dirname "${a_path}")"
if command -v curl > /dev/null; then
fn_curl "${a_url}" "${a_path}.part"
elif command -v wget > /dev/null; then
if command -v wget > /dev/null; then
fn_wget "${a_url}" "${a_path}.part"
elif command -v curl > /dev/null; then
fn_curl "${a_url}" "${a_path}.part"
else
echo >&2 " $(t_err "failed to detect HTTP client (curl, wget)")"
return 1
@@ -230,6 +225,11 @@ webi_bootstrap() { (
echo " Updating $(t_path "${b_path_rel}")"
fi
b_download_dir="$(dirname "${a_path}")"
if ! test -w "${b_download_dir}"; then
echo " Creating $(t_path "${b_download_dir}/")"
mkdir -p "${b_download_dir}"
fi
echo " Downloading $(t_url "${b_webi_file_url}")"
echo " to $(t_path "${b_path_rel}")"
fn_download_to_path "${b_webi_file_url}" "${a_path}"

View File

@@ -258,8 +258,6 @@ __bootstrap_webi() {
)"
my_export="export PATH=\"$my_path_export:\$PATH\""
touch -a ~/.config/envman/PATH.env
if grep -q -F "${my_export}" ~/.config/envman/PATH.env; then
return 0
fi
@@ -278,6 +276,85 @@ __bootstrap_webi() {
fi
}
fn_envman_init() {
mkdir -p ~/.config/envman/
if ! test -e ~/.config/envman/PATH.env; then
touch ~/.config/envman/PATH.env
fi
if ! test -e ~/.config/envman/load.sh; then
# shellcheck disable=SC2016
{
echo '# Generated for envman. Do not edit.'
echo 'for x in ~/.config/envman/*.env; do'
echo ' my_basename="$(basename "${x}")"'
echo ' if [ "*.env" = "${my_basename}" ]; then'
echo ' continue'
echo ' fi'
echo ''
echo ' # shellcheck source=/dev/null'
echo ' . "${x}"'
echo 'done'
} > ~/.config/envman/load.sh
fi
if command -v sh > /dev/null; then
if test -e ~/.profile; then
if ! grep -q -F '/.config/envman/load.sh' ~/.profile; then
fn_echo_load_sh >> ~/.profile
fi
fi
fi
if command -v bash > /dev/null; then
if test -e ~/.bashrc; then
if ! grep -q -F '/.config/envman/load.sh' ~/.bashrc; then
fn_echo_load_sh >> ~/.bashrc
fi
fi
fi
if command -v zsh > /dev/null; then
if test -e ~/.zshrc; then
if ! grep -q -F '/.config/envman/load.sh' ~/.zshrc; then
fn_echo_load_sh >> ~/.zshrc
fi
fi
fi
if command -v fish > /dev/null; then
if test ! -e ~/.config/envman/load.fish; then
# shellcheck disable=SC2016
{
echo '# Generated for envman. Do not edit.'
echo 'for x in ~/.config/envman/*.env'
echo ' source "$x"'
echo 'end'
} > ~/.config/envman/load.fish
fi
mkdir -p ~/.config/fish
if test -e ~/.config/fish/config.fish; then
touch ~/.config/fish/config.fish
fi
if ! grep -q -F '/.config/envman/load.fish' ~/.config/fish/config.fish; then
fn_echo_load_fish >> ~/.config/fish/config.fish
fi
fi
}
fn_echo_load_fish() {
echo ''
echo '# Generated for envman. Do not edit.'
# shellcheck disable=SC2016
echo 'test -s "$HOME/.config/envman/load.fish"; and source "$HOME/.config/envman/load.fish"'
}
fn_echo_load_sh() {
echo ''
echo '# Generated for envman. Do not edit.'
# shellcheck disable=SC2016
echo '[ -s "$HOME/.config/envman/load.sh" ] && source "$HOME/.config/envman/load.sh"'
}
fn_is_defined_in_all_shells() {
my_path="${1}"
@@ -305,12 +382,10 @@ __bootstrap_webi() {
echo "${HOME}/.config/fish/config.fish"
)"
for my_conf in $my_confs; do
if ! test -e "${my_conf}"; then
continue
fi
if ! grep -q -F "${my_paths}" "${my_conf}"; then
return 1
if test -e "${my_conf}"; then
if ! grep -q -F "${my_paths}" "${my_conf}"; then
return 1
fi
fi
done
}
@@ -494,7 +569,7 @@ __bootstrap_webi() {
if [ -z "${_WEBI_CHILD-}" ] && [ -f "$_webi_tmp/.PATH.env" ]; then
if test -s "$_webi_tmp/.PATH.env"; then
# shellcheck disable=SC2088 # ~ should not expand here
echo " Edit $(t_path '~/.config/envman/PATH.env') to add:"
echo " Updated $(t_path '~/.config/envman/PATH.env') updated with:"
sort -u "$_webi_tmp/.PATH.env" | while read -r my_new_path; do
echo " $(t_path "${my_new_path}")"
done
@@ -502,11 +577,13 @@ __bootstrap_webi() {
rm -f "$_webi_tmp/.PATH.env"
echo ">>> $(t_info 'ACTION REQUIRED') <<<"
echo ">>> $(t_attn 'ACTION REQUIRED') <<<"
echo ""
echo " Copy, paste & run the following command:"
echo " $(t_attn 'source ~/.config/envman/PATH.env')"
echo " (newly opened terminal windows will update automatically)"
echo ""
echo "^^^ $(t_attn 'ACTION REQUIRED') ^^^"
fi
fi
@@ -632,14 +709,9 @@ fn_wget() { (
a_url="${1}"
a_path="${2}"
cmd_wget="wget -c -q --user-agent"
cmd_wget="wget -q --user-agent"
if fn_is_tty; then
cmd_wget="wget -c -q --show-progress --user-agent"
fi
# busybox wget doesn't support --show-progress
# See <https://github.com/webinstall/webi-installers/pull/772>
if readlink "$(command -v wget)" | grep -q busybox; then
cmd_wget="wget --user-agent"
cmd_wget="wget -q --show-progress --user-agent"
fi
b_triple_ua="$(fn_get_target_triple_user_agent)"
@@ -648,9 +720,9 @@ fn_wget() { (
b_agent="webi/wget+curl ${b_triple_ua}"
fi
if ! $cmd_wget "${b_agent}" "${a_url}" -O "${a_path}"; then
if ! $cmd_wget "${b_agent}" -c "${a_url}" -O "${a_path}"; then
echo >&2 " $(t_err "failed to download (wget)") '$(t_url "${a_url}")'"
echo >&2 " $cmd_wget '${b_agent}' '${a_url}' -O '${a_path}'"
echo >&2 " $cmd_wget '${b_agent}' -c '${a_url}' -O '${a_path}'"
echo >&2 " $(wget -V)"
return 1
fi
@@ -693,10 +765,10 @@ fn_download_to_path() { (
a_path="${2}"
mkdir -p "$(dirname "${a_path}")"
if command -v curl > /dev/null; then
fn_curl "${a_url}" "${a_path}.part"
elif command -v wget > /dev/null; then
if command -v wget > /dev/null; then
fn_wget "${a_url}" "${a_path}.part"
elif command -v curl > /dev/null; then
fn_curl "${a_url}" "${a_path}.part"
else
echo >&2 " $(t_err "failed to detect HTTP client (curl, wget)")"
return 1
@@ -733,6 +805,11 @@ webi_upgrade() { (
echo "$(t_task 'Updating') $(t_pkg 'Webi')"
fi
b_download_dir="$(dirname "${a_path}")"
if ! test -w "${b_download_dir}"; then
echo " Creating $(t_path "${b_download_dir}/")"
mkdir -p "${b_download_dir}"
fi
echo " Downloading $(t_url "${b_webi_file_url}")"
echo " to $(t_path "${b_path_rel}")"
fn_download_to_path "${b_webi_file_url}" "${a_path}"
@@ -815,141 +892,4 @@ main() { (
__bootstrap_webi
); }
##############################################
# #
# envman helper functions #
# #
##############################################
fn_envman_init() { (
if ! test -r ~/.config/envman/; then
echo " Initializing ~/.config/envman/"
mkdir -p ~/.config/envman/
fi
# Note: the variables $BASH, $ZSH_NAME, etc are always empty
# because the active shell is always sh when this script runs
fn_envman_init_load_sh
fn_envman_init_shell sh '.profile' '.ash_history'
fn_envman_init_shell bash '.bashrc' '.bash_history'
fn_envman_init_shell zsh '.zshrc' '.zsh_sessions'
if command -v fish > /dev/null; then
fn_envman_init_load_fish
fn_envman_init_fish
fi
); }
fn_envman_init_load_sh() { (
touch -a ~/.config/envman/load.sh
if grep -q -F 'ENVMAN_LOAD' ~/.config/envman/load.sh; then
return 0
fi
cat << LOAD_SH > ~/.config/envman/load.sh
# Generated for envman. Do not edit.
# shellcheck disable=SC1090
touch -a ~/.config/envman/PATH.env
touch -a ~/.config/envman/ENV.env
touch -a ~/.config/envman/alias.env
touch -a ~/.config/envman/function.sh
# ENV first because we may use it in PATH
test -z "\${ENVMAN_LOAD:-}" && . ~/.config/envman/ENV.env
test -z "\${ENVMAN_LOAD:-}" && . ~/.config/envman/PATH.env
export ENVMAN_LOAD='loaded'
# function first because we may use it in alias
test -z "\${g_envman_load_sh:-}" && . ~/.config/envman/function.sh
test -z "\${g_envman_load_sh:-}" && . ~/.config/envman/alias.env
g_envman_load_sh='loaded'
LOAD_SH
); }
fn_envman_init_shell() { (
a_shell="${1}"
a_rc="${2}"
a_history="${3:-_history_file_doesnt_exist}"
a_login_shell="$(basename "${SHELL:-}")"
if ! command -v "${a_shell}" > /dev/null; then
return 0
fi
# .bashrc and .zshrc no longer exist by default on macOS
if ! test -e ~/"${a_rc}" && ! test -e ~/"${a_history}"; then
if test "${a_login_shell}" != "${a_shell}"; then
return 0
fi
fi
touch -a ~/"${a_rc}"
if grep -q -F '/.config/envman/load.sh' ~/"${a_rc}"; then
return 0
fi
# shellcheck disable=SC2088 # ~ should not expand here
echo >&2 " Edit $(t_path "~/${a_rc}") to $(t_cmd "source ~/.config/envman/load.sh")"
{
echo ''
echo '# Generated for envman. Do not edit.'
#shellcheck disable=SC2016 # vars should not expand here
echo '[ -s "$HOME/.config/envman/load.sh" ] && source "$HOME/.config/envman/load.sh"'
} >> ~/"${a_rc}"
); }
fn_envman_init_load_fish() { (
mkdir -p ~/.config/envman/
touch -a ~/.config/envman/load.fish
if grep -q -F 'ENVMAN_LOAD' ~/.config/envman/load.fish; then
return 0
fi
echo >&2 " Create ~/.config/envman/load.fish"
cat << EOF > ~/.config/envman/load.fish
# Generated for envman. Do not edit.
touch -a ~/.config/envman/PATH.env
touch -a ~/.config/envman/ENV.env
touch -a ~/.config/envman/alias.env
touch -a ~/.config/envman/function.fish
not set -q ENVMAN_LOAD; and source ~/.config/envman/ENV.env
not set -q ENVMAN_LOAD; and source ~/.config/envman/PATH.env
set -x ENVMAN_LOAD 'loaded'
not set -q g_envman_load_fish; and source ~/.config/envman/function.fish
not set -q g_envman_load_fish; and source ~/.config/envman/alias.env
set -g g_envman_load_fish 'loaded'
EOF
); }
fn_envman_init_fish() {
mkdir -p ~/.config/fish/
touch -a ~/.config/fish/config.fish
if grep -q -F '/.config/envman/load.fish' ~/.config/fish/config.fish; then
return 0
fi
# shellcheck disable=SC2088 # ~ should not expand here
echo >&2 " Edit $(t_path "~/.config/fish/config.fish") to $(t_cmd "source ~/.config/envman/load.fish")"
cat << EOF >> ~/.config/fish/config.fish
# Generated for envman. Do not edit.
test -s ~/.config/envman/load.fish; and source ~/.config/envman/load.fish
EOF
}
main

View File

@@ -1,45 +0,0 @@
'use strict';
var path = require('path');
var github = require('../_common/github.js');
var owner = 'eugeneware';
var repo = 'ffmpeg-static';
module.exports = function (request) {
return github(request, owner, repo).then(function (all) {
all.releases = all.releases
.filter(function (rel) {
let isFfmpeg = rel.name.includes('ffmpeg');
if (!isFfmpeg) {
return;
}
// remove README and LICENSE
return !['.README', '.LICENSE'].includes(path.extname(rel.name));
})
.map(function (rel) {
rel.version = rel.version.replace(/^b/, '');
if (/win32/.test(rel.name)) {
rel.os = 'windows';
rel.ext = 'exe';
}
if (/ia32/.test(rel.name)) {
rel.arch = '386';
} else if (/x64/.test(rel.name)) {
rel.arch = 'amd64';
}
return rel;
});
return all;
});
};
if (module === require.main) {
module.exports(require('@root/request')).then(function (all) {
all = require('../_webi/normalize.js')(all);
console.info(JSON.stringify(all));
});
}

View File

@@ -4,38 +4,16 @@ var github = require('../_common/github.js');
var owner = 'powershell';
var repo = 'powershell';
let ODDITIES = ['-fxdependent'];
function isOdd(build) {
for (let oddity of ODDITIES) {
let isOddity = build.name.includes(oddity);
if (isOddity) {
return true;
}
}
}
module.exports = function (request) {
return github(request, owner, repo).then(function (all) {
// remove checksums and .deb
all.releases = all.releases.filter(function (rel) {
let odd = isOdd(rel);
if (odd) {
return false;
}
let isPreview = rel.name.includes('-preview.');
if (isPreview) {
rel.channel = 'beta';
}
let isMusl = rel.download.match(/(\b|_)(musl|alpine)(\b|_)/i);
if (isMusl) {
// not a fully static build, not gnu-compatible
rel.arch = 'musl';
}
return true;
return !/(alpine)|(fxdependent)|(\.deb)|(\.pkg)|(\.rpm)$/i.test(rel.name);
});
all._names = ['PowerShell', 'powershell'];

View File

@@ -1,117 +0,0 @@
---
title: Webi Essentials
homepage: https://webinstall.dev/webi-essentials
tagline: |
The Webi Essentials are curl, git, tar, wget, xz, zip, & webi shell integrations
---
## Cheat Sheet
> Even Webi needs a place to start.
This installs the tools that are commonly needed to download and unpack various
packages, using the native package manager.
This requires **root or `sudo`** for `apt` or `apk` on Linux.
## Table of Contents
- Files
- Manual Install
- macOS
- Linux
- Alpine
- Windows
### Files
These are the files / directories that are created and/or modified with this
install:
```sh
# macOS
/Library/Developer/CommandLineTools/
/Library/Apple/usr/libexec/oah/libRosettaRuntime
# macOS & Windows
~/.local/bin/xz
~/.local/bin/unxz
# Windows
~/.local/opt/git/
# Linux
/usr/bin/git
```
### How to Install Manually
Most of these utilities are either built-in or provided by the native package
manager.
Webi makes it easy to automate across a wide variety of development systems, but
if you wanted to automate for a specific system, you could do so in a much
simpler way - because you don't have to worry about autodetection or fallbacks
😉.
So here's how to do that for each:
#### macOS
- Rosetta
```sh
softwareupdate --install-rosetta --agree-to-license
```
- Command Line Developer Tools (incl. `git`)
```sh
xcode-select --install
```
- `xz`
```sh
curl https://webi.sh/xz | sh
```
See <https://github.com/therootcompany/xz-static>.
#### Linux (Debian, Ubuntu)
```sh
sudo apt update
sudo apt install -y \
curl \
git \
tar \
wget \
xz \
zip
```
#### Alpine (Docker)
```sh
sudo apk add --no-cache \
curl \
git \
tar \
wget \
xz \
zip
```
#### Windows
`curl.exe` and BSD `tar.exe` are included as part of Windows 10+ - so there's no
needfor `wget` or `zip`.
- `git`
```sh
curl.exe -A MS https://webi.ms/git | powershell
```
See <https://github.com/git-for-windows/git/wiki/MinGit>
- `xz`
```sh
curl https://webi.sh/xz | sh
```
See <https://github.com/therootcompany/xz-static>.

View File

@@ -1,38 +0,0 @@
#!/usr/bin/env pwsh
function Install-WebiEssential() {
# wget: we only use curl.exe on Windows (Invoke-WebRequest is too slow)
# zip: BSD tar.exe handles .zip as well
# Fetch curl
Write-Output " Checking for Windows 10+ Built-in curl.exe ..."
IF (-Not (Get-Command -Name "curl.exe" -ErrorAction Silent)) {
Write-Error"error: curl.exe not found: something is very wrong"
Exit 1
}
# Fetch git
Write-Output " Checking for git ..."
IF (-Not (Get-Command -Name "git" -ErrorAction Silent)) {
& "$Env:USERPROFILE\.local\bin\webi-pwsh.ps1" git
$null = Sync-EnvPath
}
# Fetch tar
Write-Output " Checking for Windows 10+ Built-in BSD tar ..."
IF (-Not (Get-Command -Name "tar.exe" -ErrorAction Silent)) {
Write-Error"error: tar.exe not found: something is very wrong"
Exit 1
}
# Fetch xz
Write-Output " Checking for xz ..."
IF (-Not (Get-Command -Name "xz" -ErrorAction Silent)) {
& "$Env:USERPROFILE\.local\bin\webi-pwsh.ps1" xz
$null = Sync-EnvPath
}
Write-Output " OK"
}
Install-WebiEssential

View File

@@ -1,260 +0,0 @@
#!/bin/sh
set -e
set -u
#set -x
fn_install_webi_essentials_macos() { (
# streamline the output to be pretty
b_pkgs="$(
fn_check_pkg 'curl'
fn_check_pkg '/Library/Developer/CommandLineTools/usr/bin/git' 'git'
if test "$(uname -m)" = 'arm64'; then
fn_check_pkg '/Library/Apple/usr/libexec/oah/libRosettaRuntime' 'rosetta'
fi
fn_check_pkg 'tar'
# no wget because it requires brew
#fn_check_pkg 'wget'
fn_check_pkg 'xz'
fn_check_pkg 'zip'
)"
echo >&2 ""
#curl - built-in
# git
if ! xcode-select -p > /dev/null 2> /dev/null; then
cmd_xcode_cli_tools_install="xcode-select --install"
echo " Running $(t_cmd "${cmd_xcode_cli_tools_install}")"
$cmd_xcode_cli_tools_install 2> /dev/null
echo ""
echo ">>> $(t_attn 'ACTION REQUIRED') <<<"
echo ""
echo " $(t_attn "Click") '$(t_bold 'Install')' $(t_attn "in the pop-up")"
echo " (it may appear $(t_em 'under') this window)"
echo ""
echo "^^^ $(t_attn 'ACTION REQUIRED') ^^^"
echo ""
echo " waiting $(t_em 'for you') to finish installing Command Line Developer Tools ..."
while ! test -x /Library/Developer/CommandLineTools/usr/bin/git; do
sleep 0.25
done
echo " $(t_info 'OK')"
echo ""
sleep 1
fi
# rosetta
if test "$(uname -m)" = 'arm64'; then
# Also pkgutil --pkg-info com.apple.pkg.RosettaUpdateAuto
# See <https://apple.stackexchange.com/q/427970/27465>
cmd_install_rosetta="softwareupdate --install-rosetta --agree-to-license"
if ! arch -arch x86_64 uname -m > /dev/null 2>&1; then
echo " Running $(t_cmd "${cmd_install_rosetta}")"
$cmd_install_rosetta
fi
fi
#tar - built-in
#wget - skip
#xz
if ! command -v xz > /dev/null; then
echo " Running $(t_cmd "webi xz")"
~/.local/bin/webi xz
fi
#zip - built-in
# This should NEVER fail, but... Sanity check :)
{
b_pkgs_builtin="$(
fn_check_pkg 'curl'
fn_check_pkg 'tar'
fn_check_pkg 'zip'
)"
} 2> /dev/null
if test -n "${b_pkgs_builtin}"; then
echo ""
echo "error: expected these to be macOS built-ins:"
echo " ${b_pkgs}"
echo ""
exit 1
fi
echo " $(t_dim 'OK')"
); }
fn_check_pkg() { (
a_pkg="${1}"
a_pkgname="${2:-$a_pkg}"
printf >&2 ' %s %s %s' \
"$(t_dim "Checking for")" \
"$(t_pkg "${a_pkgname}")" \
"$(t_dim "...")"
if command -v "${a_pkg}" > /dev/null; then
echo >&2 " $(t_dim 'OK')"
return 0
fi
echo >&2 ' missing'
echo "${a_pkg}"
); }
###########
# mainish #
###########
fn_polite_sudo() { (
a_sudo="${1}"
a_cmds="${2}"
# no sudo needed, so don't ask
if test -z "${a_sudo}"; then
return 0
fi
# this is scripted, not user-interactive, continue
if test -z "${WEBI_TTY}"; then
return 0
fi
# this is user interactive, ask the user,defaulting to yes
echo ""
#shellcheck disable=SC2005 # echo for newline
echo "$(t_attn 'Use sudo for the following? [Y/n]')"
echo "${a_cmds}"
read -r b_yes < /dev/tty
b_yes="$(
echo "${b_yes}" |
tr '[:upper:]' '[:lower:]' |
tr -d '[:space:]'
)"
if test -z "${b_yes}" || test "${b_yes}" = "y" || test "${b_yes}" = "yes"; then
return 0
fi
echo " aborted"
return 1
); }
_install_webi_essentials() { (
if test "$(uname -s)" = 'Darwin'; then
fn_install_webi_essentials_macos
return 0
fi
b_pkgs="$(
fn_check_pkg 'curl'
fn_check_pkg 'git'
fn_check_pkg 'tar'
fn_check_pkg 'wget'
fn_check_pkg 'xz'
fn_check_pkg 'zip'
)"
b_pkgs="$(echo "${b_pkgs}" | tr '\n' ' ' | tr -s ' ')"
if test "${b_pkgs}" = " "; then
b_pkgs=''
fi
if test -z "${b_pkgs}"; then
echo " $(t_dim 'OK')"
return 0
fi
cmd_sudo=''
if test "$(id -u)" != "0"; then
echo " $(t_dim 'Checking for sudo ...')"
if command -v sudo > /dev/null; then
cmd_sudo='sudo '
else
if test "${b_pkgs}" = "xz"; then
_install_webi_essentials_webi "${cmd_sudo}" "${b_pkgs}"
echo " $(t_dim 'OK')"
return 0
fi
echo ""
echo ">>> $(t_warn 'WARNING') <<<"
echo ""
echo " You are not the 'root' user and there is no 'sudo'."
echo " (We'll try anyway but... this isn't going to work.)"
echo ""
echo "^^^ $(t_warn 'WARNING') ^^^"
echo ""
fi
fi
printf '%s' " $(t_dim 'Checking for apt/apk ...')"
if command -v apt > /dev/null; then
echo " $(t_pkg 'apt')"
_install_webi_essentials_apt "${cmd_sudo}" "${b_pkgs}"
echo " $(t_dim 'OK')"
return 0
fi
if command -v apk > /dev/null; then
echo " $(t_pkg 'apk')"
_install_webi_essentials_apk "${cmd_sudo}" "${b_pkgs}"
echo " $(t_dim 'OK')"
return 0
fi
echo " $(t_dim 'none')"
_install_webi_essentials_webi "${cmd_sudo}" "${b_pkgs}"
echo " $(t_dim 'OK')"
); }
_install_webi_essentials_apt() { (
cmd_sudo="${1}"
b_pkgs="${2}"
b_pkgs="$(echo "${b_pkgs}" | sed 's/xz/xz-utils/g')"
b_cmds="$(
printf ' %s\n %s' \
"$(t_cmd 'apt update')" \
"$(t_cmd "apt install -y ${b_pkgs}")"
)"
fn_polite_sudo "${cmd_sudo}" "${b_cmds}"
echo " $(t_dim 'Running') $(t_cmd "${cmd_sudo}apt update")"
${cmd_sudo} apt update
echo " $(t_dim 'Running') $(t_cmd "${cmd_sudo}apt install -y ${b_pkgs}")"
# shellcheck disable=SC2086
${cmd_sudo} apt install -y ${b_pkgs}
); }
_install_webi_essentials_apk() { (
cmd_sudo="${1}"
b_pkgs="${2}"
echo " $(t_dim 'Running') $(t_cmd "${cmd_sudo}apk add --no-cache")"
fn_polite_sudo "${cmd_sudo}" " $(t_cmd "apk add --no-cache ${b_pkgs}")"
# shellcheck disable=SC2086
${cmd_sudo} apk add --no-cache ${b_pkgs}
); }
_install_webi_essentials_webi() { (
cmd_sudo="${1}"
b_pkgs="${2}"
if test "${b_pkgs}" == 'xz'; then
~/.local/bin/webi xz
return 0
fi
if echo "${b_pkgs}" | grep -q xz; then
~/.local/bin/webi xz
b_pkgs="$(echo "${b_pkgs}" | sed 's/\sxz\s/ /')"
fi
echo ""
echo "error: unknown operating system:"
echo " $(uname -srm)"
if test -r /etc/issue; then
echo " $(cat /etc/issue)"
fi
echo ""
echo "$(t_err 'could not install'): $(t_warn "${b_pkgs}")"
echo ""
return 1
); }
_install_webi_essentials

View File

@@ -1,9 +1,219 @@
#!/bin/sh
set -e
set -u
#set -x
#########################################
# #
# Display Debug Info in Case of Failure #
# #
#########################################
fn_show_welcome_back() { (
if test -n "${WEBI_WELCOME:-}"; then
return 0
fi
echo ""
# invert t_task and t_pkg for top-level welcome message
printf -- ">>> %s %s <<<\n" \
"$(t_pkg 'Welcome to') $(t_task 'Webi')$(t_pkg '!')" \
"$(t_dim "- modern tools, instant installs.")"
echo " We expect your experience to be $(t_em 'absolutely perfect')!"
echo ""
echo " $(t_attn 'Success')? Star it! $(t_url 'https://github.com/webinstall/webi-installers')"
echo " $(t_attn 'Problem')? Report it: $(t_url 'https://github.com/webinstall/webi-installers/issues')"
echo " $(t_dim "(your system is") $(t_host "$(uname -s)")/$(t_host "$(uname -m)") $(t_dim "with") $(t_host "$(fn_get_libc)") $(t_dim "&") $(t_host "$(fn_get_http_client_name)")$(t_dim ")")"
sleep 0.2
); }
fn_get_libc() { (
# Ex:
# musl
# libc
if ldd /bin/ls 2> /dev/null | grep -q 'musl' 2> /dev/null; then
echo 'musl'
elif uname -o | grep -q 'GNU' || uname -s | grep -q 'Linux'; then
echo 'gnu'
else
echo 'libc'
fi
); }
fn_get_http_client_name() { (
# Ex:
# curl
# curl+wget
b_client=""
if command -v curl > /dev/null; then
b_client="curl"
fi
if command -v wget > /dev/null; then
if test -z "${b_client}"; then
b_client="wget"
else
b_client="curl+wget"
fi
fi
echo "${b_client}"
); }
#########################################
# #
# For Making the Display Nice #
# #
#########################################
# Term Types
t_cmd() { (fn_printf '\e[2m\e[35m%s\e[39m\e[22m' "${1}"); }
t_host() { (fn_printf '\e[2m\e[33m%s\e[39m\e[22m' "${1}"); }
t_link() { (fn_printf '\e[1m\e[36m%s\e[39m\e[22m' "${1}"); }
t_path() { (fn_printf '\e[2m\e[32m%s\e[39m\e[22m' "${1}"); }
t_pkg() { (fn_printf '\e[1m\e[32m%s\e[39m\e[22m' "${1}"); }
t_task() { (fn_printf '\e[36m%s\e[39m' "${1}"); }
t_url() { (fn_printf '\e[2m%s\e[22m' "${1}"); }
# Levels
t_info() { (fn_printf '\e[1m\e[36m%s\e[39m\e[22m' "${1}"); }
t_attn() { (fn_printf '\e[1m\e[33m%s\e[39m\e[22m' "${1}"); }
t_warn() { (fn_printf '\e[1m\e[33m%s\e[39m\e[22m' "${1}"); }
t_err() { (fn_printf '\e[31m%s\e[39m' "${1}"); }
# Styles
t_bold() { (fn_printf '\e[1m%s\e[22m' "${1}"); }
t_dim() { (fn_printf '\e[2m%s\e[22m' "${1}"); }
t_em() { (fn_printf '\e[3m%s\e[23m' "${1}"); }
t_under() { (fn_printf '\e[4m%s\e[24m' "${1}"); }
# FG Colors
t_cyan() { (fn_printf '\e[36m%s\e[39m' "${1}"); }
t_green() { (fn_printf '\e[32m%s\e[39m' "${1}"); }
t_magenta() { (fn_printf '\e[35m%s\e[39m' "${1}"); }
t_yellow() { (fn_printf '\e[33m%s\e[39m' "${1}"); }
fn_printf() { (
a_style="${1}"
a_text="${2}"
if fn_is_tty; then
#shellcheck disable=SC2059
printf -- "${a_style}" "${a_text}"
else
printf -- '%s' "${a_text}"
fi
); }
fn_sub_home() { (
my_rel=${HOME}
my_abs=${1}
echo "${my_abs}" | sed "s:^${my_rel}:~:"
); }
###################################
# #
# Detect HTTP Client #
# #
###################################
fn_wget() { (
# Doc:
# Downloads the file at the given url to the given path
a_url="${1}"
a_path="${2}"
cmd_wget="wget -q --user-agent"
if fn_is_tty; then
cmd_wget="wget -q --show-progress --user-agent"
fi
b_triple_ua="$(fn_get_target_triple_user_agent)"
b_agent="webi/wget ${b_triple_ua}"
if command -v curl > /dev/null; then
b_agent="webi/wget+curl ${b_triple_ua}"
fi
if ! $cmd_wget "${b_agent}" -c "${a_url}" -O "${a_path}"; then
echo >&2 " $(t_err "failed to download (wget)") '$(t_url "${a_url}")'"
echo >&2 " $cmd_wget '${b_agent}' -c '${a_url}' -O '${a_path}'"
echo >&2 " $(wget -V)"
return 1
fi
); }
fn_curl() { (
# Doc:
# Downloads the file at the given url to the given path
a_url="${1}"
a_path="${2}"
cmd_curl="curl -f -sSL -#"
if fn_is_tty; then
cmd_curl="curl -f -sSL"
fi
b_triple_ua="$(fn_get_target_triple_user_agent)"
b_agent="webi/curl ${b_triple_ua}"
if command -v wget > /dev/null; then
b_agent="webi/curl+wget ${b_triple_ua}"
fi
if ! $cmd_curl -A "${b_agent}" "${a_url}" -o "${a_path}"; then
echo >&2 " $(t_err "failed to download (curl)") '$(t_url "${a_url}")'"
echo >&2 " $cmd_curl -A '${b_agent}' '${a_url}' -o '${a_path}'"
echo >&2 " $(curl -V)"
return 1
fi
); }
fn_get_target_triple_user_agent() { (
# Ex:
# x86_64/unknown Linux/5.15.107-2-pve gnu
# arm64/unknown Darwin/22.6.0 libc
echo "$(uname -m)/unknown $(uname -s)/$(uname -r) $(fn_get_libc)"
); }
fn_download_to_path() { (
a_url="${1}"
a_path="${2}"
mkdir -p "$(dirname "${a_path}")"
if command -v wget > /dev/null; then
fn_wget "${a_url}" "${a_path}.part"
elif command -v curl > /dev/null; then
fn_curl "${a_url}" "${a_path}.part"
else
echo >&2 " $(t_err "failed to detect HTTP client (curl, wget)")"
return 1
fi
mv "${a_path}.part" "${a_path}"
); }
##############################################
# #
# Detect TTY and run main #
# #
##############################################
fn_is_tty() {
if test "${WEBI_TTY}" = 'tty'; then
return 0
fi
return 1
}
fn_detect_tty() { (
# stdin will NOT be a tty if it's being piped
# stdout & stderr WILL be a tty even when piped
# they are not a tty if being captured or redirected
# 'set -i' is NOT available in sh
if test -t 1 && test -t 2; then
return 0
fi
return 1
); }
__webi_main() {
my_date="$(date +%F_%H-%M-%S)"
@@ -61,6 +271,8 @@ __webi_main() {
set +e
WEBI_CURL="$(command -v curl)"
export WEBI_URL
WEBI_WGET="$(command -v wget)"
export WEBI_WGET
set -e
my_libc=''
@@ -152,7 +364,7 @@ __webi_main() {
my_checksum="$(
fn_checksum
)"
my_version=v1.2.8
my_version=v1.2.0
printf "\e[35mwebi\e[32m %s\e[0m Copyright 2020+ AJ ONeal\n" "${my_version} (${my_checksum})"
printf " \e[36mhttps://webinstall.dev/webi\e[0m\n"
}
@@ -188,8 +400,6 @@ __webi_main() {
echo " Output the version number of webi and exit."
echo ""
echo " Helper Utilities"
echo " --init Register command line completions with shell"
echo ""
echo " --list Show everything webi has to offer."
echo ""
echo " --info <package>"
@@ -214,18 +424,71 @@ __webi_main() {
exit 0
fi
if echo "$1" | grep -q -E '^(--list|list)$'; then
webi_list
if echo "$1" | grep -q -E '^(-l|--list|list)$'; then
echo >&2 "[warn] the format of --list output may change"
# because we don't have sitemap.xml for dev sites yet
my_host="https://webinstall.dev"
my_len="${#my_host}"
# 6 because the field will looks like "loc>WEBI_HOST/PKG_NAME"
# and the count is 1-indexed
my_count="$((my_len + 6))"
curl -fsS "${my_host}/sitemap.xml" |
grep -F "${my_host}" |
cut -d'<' -f2 |
cut -c "${my_count}"-
exit 0
fi
if echo "${1}" | grep -q -E '^(--info|info)$'; then
webi_info "$@"
exit 0
fi
if test -z "${2}"; then
echo >&2 "Usage: webi --info <package>"
exit 1
fi
echo >&2 "[warn] the output of --info is completely half-baked and will change"
my_pkg="${2}"
# TODO need a way to check that it exists at all (readme, win, lin)
echo ""
echo " Cheat Sheet: ${WEBI_HOST}/${my_pkg}"
echo " POSIX: curl -sS ${WEBI_HOST}/${my_pkg} | sh"
echo " Windows: curl.exe -A MS ${WEBI_HOST}/${my_pkg} | powershell"
echo "Releases (JSON): ${WEBI_HOST}/api/releases/${my_pkg}.json"
echo " Releases (tsv): ${WEBI_HOST}/api/releases/${my_pkg}.tab"
echo " (query params): ?channel=stable&limit=10"
echo " &os=${my_os}&arch=${my_arch}"
echo " Install Script: ${WEBI_HOST}/api/installers/${my_pkg}.sh?formats=tar,zip,xz,git,dmg,pkg"
echo " Static Assets: ${WEBI_HOST}/packages/${my_pkg}/README.md"
echo ""
# TODO os=linux,macos,windows (limit to tagged releases)
my_releases="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?channel=stable&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases}" | grep -q "error"; then
my_releases_beta="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases_beta}" | grep -q "error"; then
echo >&2 "'${my_pkg}' is a special case that does not have releases"
else
echo >&2 "ERROR no stable releases for '${my_pkg}'!"
fi
exit 0
fi
echo >&2 "Stable '${my_pkg}' releases:"
if command -v jq > /dev/null; then
printf '%s\n' "${my_releases}" |
jq
else
printf '%s\n' "${my_releases}"
fi
if echo "$1" | grep -q -E '^(--init|init)$'; then
webi_shell_init "$@"
exit 0
fi
@@ -238,271 +501,4 @@ __webi_main() {
}
webi_shell_init() { (
a_shell="${2:-}"
fn_shell_integrate_bash ""
fn_shell_integrate_zsh ""
fn_shell_integrate_fish ""
# update completions now
webi_list > /dev/null
if [ $# -eq 1 ]; then
exit 0
fi
case "${a_shell}" in
bash)
fn_shell_integrate_bash "force"
fn_shell_init_bash
;;
zsh)
fn_shell_integrate_zsh "force"
fn_shell_init_zsh
;;
fish)
fn_shell_integrate_fish "force"
fn_shell_init_fish
;;
*)
echo >&2 "Unsupported shell: $2"
exit 1
;;
esac
) }
fn_shell_integrate_bash() { (
a_force="${1}"
if test -z "${a_force}"; then
if ! command -v bash > /dev/null; then
return 0
fi
if ! test -e ~/.bashrc && ! test -e ~/.bash_history; then
return 0
fi
fi
touch -a ~/.bashrc
if grep -q 'webi --init' ~/.bashrc; then
return 0
fi
echo >&2 " Edit ~/.bashrc to add 'eval \"\$(webi --init bash)\"'"
# shellcheck disable=SC2016
{
echo ''
echo '# Generated by Webi. Do not edit.'
echo 'eval "$(webi --init bash)"'
} >> ~/.bashrc
); }
# shellcheck disable=SC2016
fn_shell_init_bash() { (
echo '_webi() {'
echo ' COMPREPLY=()'
echo ' local cur="${COMP_WORDS[COMP_CWORD]}"'
echo ' if [ "$COMP_CWORD" -eq 1 ]; then'
echo ' local completions=$(webi --list | cut -d" " -f1)'
echo ' COMPREPLY=( $(compgen -W "$completions" -- "$cur") )'
echo ' fi'
echo '}'
echo ''
echo 'complete -F _webi webi'
); }
fn_shell_integrate_zsh() { (
a_force="${1}"
if test -z "${a_force}"; then
if ! command -v zsh > /dev/null; then
return 0
fi
if ! test -e ~/.zshrc &&
! test -e ~/.zsh_sessions &&
! test -e ~/.zsh_history; then
return 0
fi
fi
touch -a ~/.zshrc
if grep -q 'webi --init' ~/.zshrc; then
return 0
fi
echo >&2 " Edit ~/.zshrc to add 'eval \"\$(webi --init zsh)\"'"
# shellcheck disable=SC2016
{
echo ''
echo '# Generated by Webi. Do not edit.'
echo 'eval "$(webi --init zsh)"'
} >> ~/.zshrc
); }
# shellcheck disable=SC2016
fn_shell_init_zsh() { (
echo '_webi() {'
echo ' local -a list completions'
echo ' list=$(webi --list | cut -d" " -f1)'
echo ' completions=(${(f)list})'
echo ' _describe -t commands "command" completions && ret=0'
echo '}'
echo ''
echo 'autoload -Uz compinit && compinit'
echo 'compdef _webi webi'
); }
fn_shell_integrate_fish() { (
a_force="${1}"
if test -z "${a_force}"; then
if ! command -v fish > /dev/null; then
return 0
fi
fi
mkdir -p ~/.config/fish
touch -a ~/.config/fish/config.fish
if grep -q 'webi --init' ~/.config/fish/config.fish; then
return 0
fi
echo >&2 " Edit ~/.config/fish/config.fish to add 'webi --init fish | source'"
# shellcheck disable=SC2016
{
echo ''
echo '# Generated by Webi. Do not edit.'
echo 'webi --init fish | source'
} >> ~/.config/fish/config.fish
); }
# shellcheck disable=SC2016
fn_shell_init_fish() { (
echo 'function __fish_webi_needs_command'
echo ' set cmd (commandline -opc)'
echo ' if [ (count $cmd) -eq 1 -a $cmd[1] = "webi" ]'
echo ' return 0'
echo ' end'
echo ' return 1'
echo 'end'
echo ''
echo 'set completions (webi --list | cut -d" " -f1)'
echo 'complete -f -c webi -n __fish_webi_needs_command -a "$completions"'
); }
webi_list() { (
# make sure there's always a cache dir and timestamp file
mkdir -p ~/.local/share/webi/var/
if ! test -r ~/.local/share/webi/var/list.txt; then
echo '0' > ~/.local/share/webi/var/last_update
elif ! test -r ~/.local/share/webi/var/last_update; then
echo '0' > ~/.local/share/webi/var/last_update
fi
# compare the timestamp in the timestamp file to now
# (in seconds since unix epoch)
my_stale_age=600
my_expire_age=900
my_now="$(date -u '+%s')"
my_then="$(cat ~/.local/share/webi/var/last_update)"
my_diff=$((my_now - my_then))
# show when the cache will update
my_stales_in=$((my_stale_age - my_diff))
my_expires_in=$((my_expire_age - my_diff))
# update if it's been longer than the staletime
if test "${my_stales_in}" -lt "0"; then
if test "${my_expires_in}" -lt "0"; then
fn_list_uncached
else
fn_list_uncached &
fi
fi
# give back the list
cat ~/.local/share/webi/var/list.txt
); }
fn_list_uncached() { (
# because we don't have sitemap.xml for dev sites yet
my_host="https://webinstall.dev"
my_len="${#my_host}"
# 6 because the field will looks like "loc>WEBI_HOST/PKG_NAME"
# and the count is 1-indexed
my_count="$((my_len + 6))"
my_now="$(date -u '+%s')"
echo "${my_now}" > ~/.local/share/webi/var/last_update
my_tmp="$(mktemp)"
{
echo "help"
echo "--help"
echo "version"
echo "-V"
echo "--version"
echo "--init" # <shell>
echo "--list"
echo "--info" # <package>
} > "${my_tmp}"
curl -fsS "${my_host}/sitemap.xml" |
grep -F "${my_host}" |
cut -d'<' -f2 |
cut -c "${my_count}"- >> "${my_tmp}"
mv "${my_tmp}" ~/.local/share/webi/var/list.txt
my_now="$(date -u '+%s')"
echo "${my_now}" > ~/.local/share/webi/var/last_update
); }
webi_info() { (
if test -z "${2}"; then
echo >&2 "Usage: webi --info <package>"
exit 1
fi
echo >&2 "[warn] the output of --info is completely half-baked and will change"
my_pkg="${2}"
# TODO need a way to check that it exists at all (readme, win, lin)
echo ""
echo " Cheat Sheet: ${WEBI_HOST}/${my_pkg}"
echo " POSIX: curl -sS ${WEBI_HOST}/${my_pkg} | sh"
echo " Windows: curl.exe -A MS ${WEBI_HOST}/${my_pkg} | powershell"
echo "Releases (JSON): ${WEBI_HOST}/api/releases/${my_pkg}.json"
echo " Releases (tsv): ${WEBI_HOST}/api/releases/${my_pkg}.tab"
echo " (query params): ?channel=stable&limit=10"
echo " &os=${my_os}&arch=${my_arch}"
echo " Install Script: ${WEBI_HOST}/api/installers/${my_pkg}.sh?formats=tar,zip,xz,git,dmg,pkg"
echo " Static Assets: ${WEBI_HOST}/packages/${my_pkg}/README.md"
echo ""
# TODO os=linux,macos,windows (limit to tagged releases)
my_releases="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?channel=stable&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases}" | grep -q "error"; then
my_releases_beta="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases_beta}" | grep -q "error"; then
echo >&2 "'${my_pkg}' is a special case that does not have releases"
else
echo >&2 "ERROR no stable releases for '${my_pkg}'!"
fi
exit 0
fi
echo >&2 "Stable '${my_pkg}' releases:"
if command -v jq > /dev/null; then
printf '%s\n' "${my_releases}" |
jq
else
printf '%s\n' "${my_releases}"
fi
); }
__webi_main "$@"