#!/bin/bash # MKit - simple install helper # See LICENSE file for copyright and license details. mkit_import ini mkit_import facts __build1() { # # Process one skeleton $1 of type $3 (or guessed) to path $2 # local srcpath=$1 # skeleton path local dstpath=$2 # destination meaty animal path local ftype=$3 # file/builder type test -n "$dstpath" || dstpath=${srcpath%.skel} test -n "$ftype" || ftype=$(__guess_ftype "$dstpath") debug_var srcpath dstpath ftype <"$srcpath" __build1_ftype "$ftype" >"$dstpath" rec_built "$dstpath" } __build1_ftype() { # # Build a file of type $1; fom stdin to stdout # local ftype=$1 # file/builder type case $ftype in MKIT_COMMON) __expand_tokens "tokens" ;; rpmstuff) __expand_tokens "tokens" "rpmstuff:tokens" ;; debstuff) __expand_tokens "tokens" "debstuff:tokens" ;; *) die "unknown file type: $ftype" ;; esac } __expand_tokens() { # # Read stdin, expanding tokens from sections $@ # local script # sed script cache local section # each section to expand tokens from local varname # each token name local varvalue # each token value script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX) { for section in "$@"; do debug_var section ini lskeys "$section" \ | while read -r varname; do varvalue="$(ini 1value "$section:$varname" | __qfs )" echo "s|$varname|$varvalue|g;" debug_var varname varvalue done done echo "s|__MKIT_PROJ_NAME__|$(ini 1value project:name | __qfs)|g;" echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename | __qfs)|g;" echo "s|__MKIT_PROJ_LICENSE__|$(ini 1value project:license | __qfs)|g;" echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname | __qfs)|g;" echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline | __qfs)|g;" echo "s|__MKIT_PROJ_MAINTAINER__|$(ini 1value project:maintainer | __qfs)|g;" echo "s|__MKIT_PROJ_VCS_BROWSER__|$(ini 1value project:vcs_browser | __qfs)|g;" echo "s|__MKIT_PROJ_GIT_LASTHASH__|$(git_lasthash | __qfs)|g;" echo "s|__MKIT_PROJ_VERSION__|$(semver | __qfs)|g;" echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|g;" } >> "$script" sed -f "$script" || die "__expand_tokens failed" rm "$script" } __guess_ftype() { # # Guess file type from destination path $1 # local dstpath=$1 # destination path case $dstpath in *) echo MKIT_COMMON ;; esac } __qfs() { # # Quote for our sed scipt's RHS # sed ' s:\\:\\\\:g s:|:\\|:g ' } _mkit_data() { # # Build sampler showing all token values # local token local section local sections sections=() ini lskeys tokens | grep -q . && sections=(tokens) sections+=( $(ini lssect | grep ':tokens$') ) { echo "(builtin):" echo " x_MKIT_PROJ_NAME__ => '__MKIT_PROJ_NAME__'" echo " x_MKIT_PROJ_CODENAME__ => '__MKIT_PROJ_CODENAME__'" echo " x_MKIT_PROJ_LICENSE__ => '__MKIT_PROJ_LICENSE__'" echo " x_MKIT_PROJ_PKGNAME__ => '__MKIT_PROJ_PKGNAME__'" echo " x_MKIT_PROJ_TAGLINE__ => '__MKIT_PROJ_TAGLINE__'" echo " x_MKIT_PROJ_MAINTAINER__ => '__MKIT_PROJ_MAINTAINER__'" echo " x_MKIT_PROJ_VCS_BROWSER__ => '__MKIT_PROJ_VCS_BROWSER__'" echo " x_MKIT_PROJ_GIT_LASTHASH__ => '__MKIT_PROJ_GIT_LASTHASH__'" echo " x_MKIT_PROJ_VERSION__ => '__MKIT_PROJ_VERSION__'" echo " x_MKIT_SELF_VERSION__ => '__MKIT_SELF_VERSION__'" for section in "${sections[@]}"; do echo "$section:" for token in $(ini lskeys "$section"); do echo " x${token:1} => '$token'" done done } \ | __expand_tokens "MKIT_BUILTIN" "${sections[@]}" \ | sed '/^ x/ s|x|_|' } build() { # # Add meat to all skeletons # local srcpath # each source path semver >/dev/null find . -type f -name '*.skel' \ | while read -r srcpath; do __build1 "$srcpath" done } clean() { # # Clean up tree after building # test -f "$MKIT_LOCAL/built.lst" && { <"$MKIT_LOCAL/built.lst" grep -v -e '\.\.' -e ^/ \ | xargs -r rm -rf rm -f "$MKIT_LOCAL/built.lst" rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL" } true } debstuff() { # # Build Debian stuff (eamed tarball, debian dir) # local version # package version local debian_skel # 'debian' folder skeleton local dfsrc # each source file from ^^ local dftgt # each built packaging file version=$(semver) # tarball - we should already have by means of 'dist' # mv "${MKIT_PROJ_PKGNAME}-$version.tar.gz" \ "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" \ || die "could not rename tarball" rec_built "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" # read content of each mandatory file from debian_skel # debian_skel=$(ini 1value dist:debstuff) test -n "$debian_skel" || die "dist:debstuff not specified" test -d "$debian_skel" || die "debian directory template found: $debian_skel" mkdir -p debian/source find "$debian_skel" -type f \ | while read -r dfsrc; do dftgt="debian/${dfsrc#$debian_skel}" mkdir -p "$(dirname "$dftgt")" __build1 "$dfsrc" "$dftgt" debstuff done rec_built debian } dist() { # # Create distributable tarball # #FIXME: lacking Makefile skills, we do this step twice for # rpmstuff, hence -f hack for gzip # local version # tarball version local git_lasthash # last git commit hash local dirname # directory and tarball name version=$(semver) dirname=$MKIT_PROJ_PKGNAME-$version git_lasthash=$(git_lasthash) mkdir -p "$dirname" ini values "dist:tarball" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname" update_version "$version" "$dirname/mkit.ini" mkdir -p "$dirname/.mkit" echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash" tar -cf "$dirname.tar" "$dirname" gzip -f "$dirname.tar" # see above FIXME rec_built "$dirname.tar.gz" rm -rf "$dirname" } rpmstuff() { # # Build specfile # local specname=$MKIT_PROJ_PKGNAME.spec # .spec filename local specsrc # source of specfile specsrc="$(ini 1value "dist:rpmstuff")" test -n "$specsrc" || die "dist:rpmstuff not specified" test -f "$specsrc" || die "specfile template not found: $specsrc" __build1 "$specsrc" "$specname" rpmstuff }