kiss: add helper function to remove duplicate code

This commit is contained in:
Dylan Araps 2019-09-14 00:19:20 +03:00
parent 3e04556a57
commit 08c0d1eed8

68
kiss
View File

@ -24,6 +24,13 @@ log() {
printf '\033[1;32m->\033[m %s.\n' "$@"
}
contains() {
# Check if a "string list" contains a word.
case " $1 " in *" $2 "*) return 0; esac
return 1
}
pkg_lint() {
# Check that each mandatory file in the package entry exists.
log "[$1] Checking repository files"
@ -172,11 +179,7 @@ pkg_depends() {
# This does a depth-first search. The deepest dependencies are
# listed first and then the parents in reverse order.
case $deps in
# Dependency is already in list, skip it.
*" $1 "*) ;;
*)
contains "$deps" "$1" || {
# Recurse through the dependencies of the child
# packages. Keep doing this.
[ -f "$repo_dir/depends" ] &&
@ -188,8 +191,7 @@ pkg_depends() {
# After child dependencies are added to the list,
# add the package which depends on them.
[ "$2" ] || deps="$deps $1 "
;;
esac
}
}
pkg_verify() {
@ -343,17 +345,13 @@ pkg_build() {
log "Resolving dependencies"
for pkg; do
case $explicit in
*" $pkg "*) ;;
*)
contains "$explicit" "$pkg" || {
pkg_depends "$pkg" explicit
# Mark packages passed on the command-line
# separately from those detected as dependencies.
explicit="$explicit $pkg "
;;
esac
}
done
explicit_build="$explicit"
@ -362,11 +360,10 @@ pkg_build() {
# package, remove it from the explicit list as it needs to be
# installed as a dependency.
for pkg; do
case $deps in
# There's no better way to remove a word from a string in
# POSIX 'sh' sadly.
*" $pkg "*) explicit=$(echo "$explicit" | sed "s/ $pkg / /g")
esac
contains "$deps" "$pkg" &&
explicit=$(echo "$explicit" | sed "s/ $pkg / /g")
done
# Set the resolved dependency list as the function's arguments.
@ -375,10 +372,9 @@ pkg_build() {
# The dependency solver always lists all dependencies regardless of
# whether or not they are installed. Filter out installed dependencies.
for pkg; do
case $explicit_build in
*" $pkg "*) ;;
*) pkg_list "$pkg" >/dev/null && continue ;;
esac
contains "$explicit_build" "$pkg" || {
pkg_list "$pkg" >/dev/null && continue
}
build_packages="$build_packages$pkg "
done
@ -406,13 +402,11 @@ pkg_build() {
for pkg; do
# Don't check for a pre-built package if it was passed to KISS
# directly.
case $explicit_build in
*" $pkg "*)
contains "$explicit_build" "$pkg" && {
shift
set -- "$@" "$pkg"
continue
;;
esac
}
# Figure out the version and release.
read -r version release < "$(pkg_find "$pkg")/version"
@ -492,9 +486,7 @@ pkg_build() {
# Install only dependencies of passed packages.
# Skip this check if this is a package update.
case $explicit in
*" $pkg "*) [ "$pkg_update" ] || continue
esac
contains "$explicit" "$pkg" && [ -z "$pkg_update" ] && continue
log "[$pkg] Needed as a dependency or has an update, installing"
args i "$pkg"
@ -789,10 +781,7 @@ pkg_updates() {
continue
}
case $repos in
# If the repository has already been updated, skip it.
*" $PWD "*) ;;
*)
contains "$repos" "$PWD" || {
repos="$repos $PWD "
log "[$PWD] Updating repository"
@ -803,8 +792,7 @@ pkg_updates() {
log "[$PWD] Need root to update"
sudo git pull
fi
;;
esac
}
done
log "Checking for new package versions"
@ -828,8 +816,7 @@ pkg_updates() {
done
# If the package manager has an update, handle it first.
case $outdated in
*" kiss "*)
contains "$outdated" kiss && {
log "Detected package manager update" \
"The package manager will be updated first" \
"Continue?: Press Enter to continue or Ctrl+C to abort here"
@ -846,8 +833,7 @@ pkg_updates() {
"Re-run 'kiss update' to update your system"
exit 0
;;
esac
}
# Disable globbing.
set -f
@ -951,9 +937,7 @@ args() {
# The purpose of these two loops is to order the
# argument list based on dependence.
for pkg in $deps; do
case " $* " in
*" $pkg "*) pkg_install "$pkg" ;;
esac
contains "$*" "$pkg" && pkg_install "$pkg"
done
;;
@ -966,9 +950,7 @@ args() {
# Reverse the list of dependencies filtering out anything
# not explicitly set for removal.
for pkg in $deps; do
case " $* " in
*" $pkg "*) remove_pkgs="$pkg $remove_pkgs"
esac
contains "$*" "$pkg" && remove_pkgs="$pkg $remove_pkgs"
done
for pkg in $remove_pkgs; do