cem-utils

Random utilities
git clone git://git.ckyln.com/~cem/cem-utils.git
Log | Files | Refs | README

kf (2302B)


      1 #!/bin/sh
      2 
      3 # KISS functions that I use. Mostly
      4 # for repository management.
      5 
      6 out() { printf '\033[1;36m-> \033[m%s\n' "$@" ;}
      7 error() { printf '\033[1;31m!> error: \033[m%s\n' "$@" >&2 ;}
      8 die() { error "$1" ; exit 1 ;}
      9 
     10 usage() {
     11 out \
     12 "${0##*/} [a|b|n|h] [options]" \
     13 "add   Commit the current directory as a new package" \
     14 "bump  Commit current directory with a 'package: bump to version message'" \
     15 "help  Print help information per function" \
     16 "new   Create a template package with package-name, version, and source"
     17 exit 1
     18 }
     19 
     20 help() {
     21     case "$1" in
     22         a|add) out "Resets git, adds current directory and commits" "with a 'package: add new package at version to repository' message" ; exit ;;
     23         b|bump) out "Resets git, adds current directory and commits" "with a 'package: bump to version' message"; exit ;;
     24         n|new) out "kf new <package> [version] [sources]"; exit ;;
     25         h|help) out "kf help <function>" ; exit ;;
     26         *) usage ;; esac
     27 }
     28 
     29 add() {
     30     [ "$1" ] && help add
     31     [ -f version ] || die "Could not find version directory, are you sure that you are on a package directory?"
     32     git reset
     33     git add .
     34     git commit -m "$(basename "$PWD"): add new package at $(cut -d ' ' -f 1 version) to $(basename "$(dirname "$PWD")")"
     35 }
     36 
     37 bump() {
     38     [ "$1" ] && help bump
     39     [ -f version ] || die "Could not find version directory, are you sure that you are on a package directory?"
     40     git reset
     41     git add .
     42     git commit -m "$(basename "$PWD"): bump to $(cut -d ' ' -f 1 version)"
     43 }
     44 
     45 new() {
     46     # 1: Package name
     47     # 2: Package version
     48     # 3: Package source
     49     [ "$1" ] || help new
     50     [ -d "$1" ] && die "Package $1 already exists"
     51     mkdir -p "$1" || die "Couldn't create directory to ${PWD}/${1}"
     52     out "Populating build file"; {
     53         printf "#!/bin/sh -e\n" > "$1/build"
     54 	chmod +x "$1/build"
     55     }
     56     [ "$2" ] && {
     57         out "Populating version file with '${2%% *} 1'"
     58         printf '%s 1\n' "${2%% *}" > "$1/version"
     59     } || printf " 1\n" > "$1/version"
     60 
     61     [ "$3" ] && {
     62         out "Populating sources file with '$3'"
     63         printf '%s\n' "$3" > "$1/sources"
     64     } || :> "$1/sources"
     65 }
     66 
     67 mode="$1"
     68 shift
     69 
     70 case "$mode" in
     71     a|add)  add "$@" ;;
     72     b|bump) bump "$@" ;;
     73     n|new) new "$@" ;;
     74     h|help) help "$1" ;;
     75     *) usage ;;
     76 esac