Welcome to little lamb

Code » comain » release » tree

[release] / getdeps

#!/bin/sh

. "$(dirname $0)"/libcomain

usage()
{
    local r=$1
    shift

    cat <<EOF
usage: $0 [-n] ACTION

      list                          List all components and dependencies

      cpnt                          Clone repo of components
      deps                          Clone repo of dependencies
      dep NAME [..]                 Clone repo of dependencies NAME [..]

      help                          Show this help screen and exit
EOF
    exit $r
}

showdep()
{
    local dep=$2
    local depname="${dep##*/}"
    local needed
    eval needed=\"\$need$depname\"
    local rec="$3"
    local cpnt=0
    test -z "$rec" && test -e "$dep/cpnt" && cpnt=1
    echon "$rec- $depname version $(cat $dep/version)"
    if test -z "$rec" && test "$needed" != "1"; then
        echon " [optional: --with-$(echo $needed | sed "s/ /, --with-/g")]"
    fi
    echo
    if test -f "$dep/site"; then echo "$rec  site : $(head -1 $dep/site)"; fi
    if test -f "$dep/git" ; then echo "$rec  repo : $(head -1 $dep/git)" ; fi
    if test -f "$dep/ref" ; then echo "$rec  ref  : $(cat $dep/ref)" ; fi

    # recursive lila dependency
    if test $cpnt -eq 1; then return; fi
    if ! test -d "deps/$depname/meta/deps"; then return; fi
    for d in "deps/$depname"/meta/deps/*; do
        if ! test -d "$d"; then continue; fi
        showdep 0 "$d" "$rec  "
    done
}

list()
{
    if test $nb_cpnt -ge 1; then echo "Components :"; fi
    foreach cpnt $nb_cpnt showdep
    if test $nb_cpnt -ge 1; then echo; fi
    if test $nb_deps -ge 1; then echo "Dependencies :"; fi
    foreach deps $nb_deps showdep
}

mbrun()
{
    if test $dryrun -eq 0; then
        run "$@"
    else
        echo "dry-run: $@"
    fi
}

getdep()
{
    local dep=$2
    local depname="${dep##*/}"
    local rec=$3
    local cpnt=0
    test -e "$dep/cpnt" && cpnt=1
    if test $cpnt -eq 1; then
        if test "$(expr substr "$dep" 1 5)" = "deps/"; then
            dst="${dep%/meta/deps/*}"
        else
            dst="."
        fi
    else
        dst="deps"
        local libname="$(cat "$dep/library" 2>/dev/null)"
        if test -z "$libname"; then libname="$depname"; fi
        if test -e "$LIBDIR/lib$libname.so" || test -e "$LIBDIR/lib$libname.a"; then
            echo :: $depname found in system, skipping
            return
        fi
    fi

    if ! test -e "deps"; then run mkdir "deps"; fi
    echo ":: Getting $depname..."

    if test -e "$dst/$depname"; then
        warn "cannot clone: '$dst/$depname' already exists"
    else
        mbrun git clone "$(head -1 $dep/git)" "$dst/$depname"
        local ref="$(cat $dep/ref 2>/dev/null)"
        if test -z "$ref"; then ref="$(cat $dep/version)"; fi
        mbrun git -C "$dst/$depname" checkout -b "for-$prjname-$version" "$ref"
    fi
    if test $cpnt -eq 1 && ! test -e "$dst/deps/$depname"; then
        mbrun ln -s "../$depname" "$dst/deps/$depname"
    fi

    # recursive lila dependency
    if test $cpnt -eq 1; then return; fi
    if ! test -d "deps/$depname/meta/deps"; then return; fi
    if ! test -d "deps/$depname/deps"; then run mkdir "deps/$depname/deps"; fi
    for d in "deps/$depname/meta/deps/"*; do
        if ! test -d "$d"; then continue; fi
        getdep 0 "$d" "../$rec"
        local n="${d##*/}"
        local p="$(realpath "deps/$n")"
        if test -e "$d/cpnt"; then
            n="deps/$depname/$n"
        else
            n="deps/$depname/deps/$n"
        fi
        if ! test -e "$n"; then
            mbrun ln -s "$p" "$n"
        fi
    done
}

loadoptions
loaddeps

if test $# -eq 0; then usage 0; fi

if test -z "$LIBDIR"; then LIBDIR=/lib; fi
isdep=0
dryrun=0
for arg ; do
    if test $isdep -eq 1; then
        if test ! -d meta/deps/$arg; then
            warn "unknown dependency '$arg'"
        else
            getdep 0 meta/deps/$arg
        fi
        continue
    fi
    case "$arg" in
        list) list ;;

        cpnt) foreach cpnt $nb_cpnt getdep;;
        deps) foreach deps $nb_deps getdep;;
        dep) isdep=1 ;;

        -n|--dry-run) dryrun=1 ;;
        -h|--help|help) usage 0  ;;
        *) dief 1 "unknown action '$arg'" ;;
    esac
done

echo "done."