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

146
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,24 +179,19 @@ 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. # Recurse through the dependencies of the child
*" $1 "*) ;; # 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,
# Recurse through the dependencies of the child # add the package which depends on them.
# packages. Keep doing this. [ "$2" ] || deps="$deps $1 "
[ -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
} }
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
*) # Mark packages passed on the command-line
pkg_depends "$pkg" explicit # separately from those detected as dependencies.
explicit="$explicit $pkg "
# Mark packages passed on the command-line }
# separately from those detected as dependencies.
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. contains "$deps" "$pkg" &&
*" $pkg "*) explicit=$(echo "$explicit" | sed "s/ $pkg / /g") explicit=$(echo "$explicit" | sed "s/ $pkg / /g")
esac
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,22 +781,18 @@ pkg_updates() {
continue continue
} }
case $repos in contains "$repos" "$PWD" || {
# If the repository has already been updated, skip it. repos="$repos $PWD "
*" $PWD "*) ;;
*)
repos="$repos $PWD "
log "[$PWD] Updating repository" log "[$PWD] Updating repository"
if [ -w "$PWD" ]; then if [ -w "$PWD" ]; then
git pull git pull
else else
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,26 +816,24 @@ 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"
# POSIX 'read' has none of the "nice" options like '-n', '-p' # POSIX 'read' has none of the "nice" options like '-n', '-p'
# etc etc. This is the most basic usage of 'read'. # etc etc. This is the most basic usage of 'read'.
# '_' is used as 'dash' errors when no variable is given to 'read'. # '_' is used as 'dash' errors when no variable is given to 'read'.
read -r _ || exit read -r _ || exit
pkg_build kiss pkg_build kiss
args i kiss args i kiss
log "Updated the package manager" \ log "Updated the package manager" \
"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