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
1 changed files with 64 additions and 82 deletions

68
kiss
View File

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