From 415996a57943fe3aad3dfe4d5979ffc67f8ba3dc Mon Sep 17 00:00:00 2001 From: "dylan.araps@gmail.com" Date: Fri, 13 Sep 2019 21:19:19 +0000 Subject: kiss: add helper function to remove duplicate code FossilOrigin-Name: e3aeedfacdab1fc8e663d01650a3e07816ebdacc7b972dbabf168216a14c6e6c --- kiss | 148 +++++++++++++++++++++++++++++-------------------------------------- 1 file changed, 65 insertions(+), 83 deletions(-) (limited to 'kiss') diff --git a/kiss b/kiss index 066e089..6c5ebac 100755 --- a/kiss +++ b/kiss @@ -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,24 +179,19 @@ 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 "*) ;; - - *) - # Recurse through the dependencies of the child - # packages. Keep doing this. - [ -f "$repo_dir/depends" ] && - while read -r dep _; do - [ "${dep##\#*}" ] || continue - pkg_depends "$dep" ||: - done < "$repo_dir/depends" - - # After child dependencies are added to the list, - # add the package which depends on them. - [ "$2" ] || deps="$deps $1 " - ;; - esac + contains "$deps" "$1" || { + # Recurse through the dependencies of the child + # packages. Keep doing this. + [ -f "$repo_dir/depends" ] && + while read -r dep _; do + [ "${dep##\#*}" ] || continue + pkg_depends "$dep" ||: + done < "$repo_dir/depends" + + # After child dependencies are added to the list, + # add the package which depends on them. + [ "$2" ] || deps="$deps $1 " + } } pkg_verify() { @@ -343,17 +345,13 @@ pkg_build() { log "Resolving dependencies" for pkg; do - case $explicit in - *" $pkg "*) ;; - - *) - pkg_depends "$pkg" explicit + contains "$explicit" "$pkg" || { + pkg_depends "$pkg" explicit - # Mark packages passed on the command-line - # separately from those detected as dependencies. - explicit="$explicit $pkg " - ;; - esac + # Mark packages passed on the command-line + # separately from those detected as dependencies. + explicit="$explicit $pkg " + } 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 + # There's no better way to remove a word from a string in + # POSIX 'sh' sadly. + 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 "*) - shift - set -- "$@" "$pkg" - continue - ;; - esac + contains "$explicit_build" "$pkg" && { + shift + set -- "$@" "$pkg" + continue + } # 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,22 +781,18 @@ pkg_updates() { continue } - case $repos in - # If the repository has already been updated, skip it. - *" $PWD "*) ;; - *) - repos="$repos $PWD " + contains "$repos" "$PWD" || { + repos="$repos $PWD " - log "[$PWD] Updating repository" + log "[$PWD] Updating repository" - if [ -w "$PWD" ]; then - git pull - else - log "[$PWD] Need root to update" - sudo git pull - fi - ;; - esac + if [ -w "$PWD" ]; then + git pull + else + log "[$PWD] Need root to update" + sudo git pull + fi + } done log "Checking for new package versions" @@ -828,26 +816,24 @@ pkg_updates() { done # If the package manager has an update, handle it first. - case $outdated in - *" kiss "*) - log "Detected package manager update" \ - "The package manager will be updated first" \ - "Continue?: Press Enter to continue or Ctrl+C to abort here" + 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" - # POSIX 'read' has none of the "nice" options like '-n', '-p' - # etc etc. This is the most basic usage of 'read'. - # '_' is used as 'dash' errors when no variable is given to 'read'. - read -r _ || exit + # POSIX 'read' has none of the "nice" options like '-n', '-p' + # etc etc. This is the most basic usage of 'read'. + # '_' is used as 'dash' errors when no variable is given to 'read'. + read -r _ || exit - pkg_build kiss - args i kiss + pkg_build kiss + args i kiss - log "Updated the package manager" \ - "Re-run 'kiss update' to update your system" + log "Updated the package manager" \ + "Re-run 'kiss update' to update your system" - exit 0 - ;; - esac + exit 0 + } # 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 -- cgit v1.2.3