Add self-update command to refresh miaur from Git
Introduce `miaur self-update` / `--self-update` that clones RepoUrl@Branch, detects user vs system install prefix, and reinstalls in place. Configurable via conf/env; supports --noconfirm, --repo-url, and --branch. Co-authored-by: Frank Vitrano <WHC2006@users.noreply.github.com>
This commit is contained in:
co-authored by
Frank Vitrano
parent
bba71040d6
commit
a4f4767c47
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env bash
|
||||
# Self-update miaur from its Git repository
|
||||
|
||||
# Detect install prefix from the running binary location.
|
||||
# Prints: user|/path/prefix or system|/path/prefix
|
||||
miaur_detect_prefix() {
|
||||
case "$MIAUR_HOME" in
|
||||
"${HOME}/.local/share/miaur")
|
||||
printf 'user|%s\n' "${HOME}/.local"
|
||||
;;
|
||||
/usr/local/share/miaur)
|
||||
printf 'system|%s\n' /usr/local
|
||||
;;
|
||||
*)
|
||||
# Dev checkout or custom layout — default to a user install
|
||||
printf 'user|%s\n' "${HOME}/.local"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Read MIAUR_VERSION=... from a common.sh path
|
||||
_miaur_read_version_file() {
|
||||
local file=$1
|
||||
[[ -f $file ]] || return 1
|
||||
local line
|
||||
line=$(grep -E '^MIAUR_VERSION=' "$file" | head -n1) || return 1
|
||||
line=${line#MIAUR_VERSION=}
|
||||
line=${line#\"}
|
||||
line=${line%\"}
|
||||
line=${line#\'}
|
||||
line=${line%\'}
|
||||
printf '%s\n' "$line"
|
||||
}
|
||||
|
||||
# Install tree from SRC into PREFIX (user|system via $1 mode)
|
||||
_miaur_install_tree() {
|
||||
local mode=$1
|
||||
local prefix=$2
|
||||
local src=$3
|
||||
local share="${prefix}/share/miaur"
|
||||
local binlink="${prefix}/bin/miaur"
|
||||
|
||||
[[ -f $src/miaur && -d $src/lib ]] || die "incomplete source tree: $src"
|
||||
|
||||
_run() {
|
||||
if [[ $mode == system ]]; then
|
||||
as_root "$@"
|
||||
else
|
||||
"$@"
|
||||
fi
|
||||
}
|
||||
|
||||
msg "Installing to ${share}"
|
||||
_run mkdir -p "$share" "${prefix}/bin"
|
||||
_run rm -rf "$share"
|
||||
_run mkdir -p "$share"
|
||||
_run cp -a "$src/miaur" "$src/lib" "$share/"
|
||||
if [[ -d $src/completions ]]; then
|
||||
_run cp -a "$src/completions" "$share/"
|
||||
fi
|
||||
if [[ -d $src/config ]]; then
|
||||
_run cp -a "$src/config" "$share/"
|
||||
fi
|
||||
_run chmod 755 "$share/miaur"
|
||||
_run ln -sfn "$share/miaur" "$binlink"
|
||||
|
||||
if [[ -d $src/completions ]]; then
|
||||
if [[ $mode == user ]]; then
|
||||
mkdir -p "${HOME}/.local/share/bash-completion/completions"
|
||||
cp -f "$src/completions/miaur.bash" \
|
||||
"${HOME}/.local/share/bash-completion/completions/miaur" 2>/dev/null || true
|
||||
else
|
||||
if [[ -d /usr/share/bash-completion/completions ]]; then
|
||||
as_root cp -f "$src/completions/miaur.bash" \
|
||||
/usr/share/bash-completion/completions/miaur 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
printf '%s\n' "$binlink"
|
||||
}
|
||||
|
||||
# Fetch latest sources into a temp dir; prints path to clone
|
||||
_miaur_fetch_latest() {
|
||||
local repo=$1
|
||||
local branch=$2
|
||||
local tmp
|
||||
tmp=$(mktemp -d)
|
||||
# Caller cleans up via SELF_UPDATE_TMP
|
||||
SELF_UPDATE_TMP=$tmp
|
||||
msg2 "Cloning ${repo} (${branch})..."
|
||||
${MIAUR_GIT} clone --depth 1 --branch "$branch" "$repo" "$tmp/miaur" \
|
||||
|| die "failed to clone ${repo} (branch ${branch}); set RepoUrl / MIAUR_REPO if you forked"
|
||||
[[ -f $tmp/miaur/miaur && -d $tmp/miaur/lib ]] || die "cloned tree looks incomplete"
|
||||
printf '%s\n' "$tmp/miaur"
|
||||
}
|
||||
|
||||
cmd_self_update() {
|
||||
need_cmd git
|
||||
|
||||
local repo=${MIAUR_REPO:-https://github.com/WHC2006/miaur.git}
|
||||
local branch=${MIAUR_BRANCH:-main}
|
||||
local info mode prefix
|
||||
info=$(miaur_detect_prefix)
|
||||
mode=${info%%|*}
|
||||
prefix=${info#*|}
|
||||
|
||||
local old_ver=${MIAUR_VERSION}
|
||||
msg "Current version: v${old_ver}"
|
||||
msg2 "Install prefix: ${prefix} (${mode})"
|
||||
msg2 "Source: ${repo} @ ${branch}"
|
||||
|
||||
if [[ $MIAUR_HOME != "${prefix}/share/miaur" ]]; then
|
||||
warn "Running from ${MIAUR_HOME}; update will install into ${prefix}/share/miaur"
|
||||
fi
|
||||
|
||||
confirm_ask "Update miaur from Git?" || {
|
||||
warn "Cancelled by user"
|
||||
return 1
|
||||
}
|
||||
|
||||
SELF_UPDATE_TMP=
|
||||
# shellcheck disable=SC2064
|
||||
trap '[[ -n ${SELF_UPDATE_TMP:-} ]] && rm -rf "$SELF_UPDATE_TMP"; sudoloop_stop' RETURN
|
||||
|
||||
local src new_ver binlink
|
||||
src=$(_miaur_fetch_latest "$repo" "$branch")
|
||||
new_ver=$(_miaur_read_version_file "$src/lib/common.sh") || new_ver=unknown
|
||||
|
||||
if [[ $new_ver == "$old_ver" ]]; then
|
||||
msg "Remote reports the same version (v${new_ver}); reinstalling anyway"
|
||||
else
|
||||
msg "Updating: v${old_ver} -> v${new_ver}"
|
||||
fi
|
||||
|
||||
if [[ $mode == system ]]; then
|
||||
sudoloop_start
|
||||
fi
|
||||
|
||||
binlink=$(_miaur_install_tree "$mode" "$prefix" "$src")
|
||||
|
||||
msg "Done: ${binlink}"
|
||||
if [[ -x $binlink ]]; then
|
||||
"$binlink" -V
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user