#!/bin/bash . "$MKIT_DIR/include/ini.sh" || die "cannot import ini.sh" build() { # # Add meat to all skeletons # local srcpath find src -type f -name '*.skel' \ | while read srcpath; do build1 "$srcpath" done } build1() { # # Process one skeleton # local srcpath="$1" local dstpath="$2" local ftype="$3" test -n "$dstpath" || dstpath=${srcpath%.skel} test -n "$ftype" || ftype=$(guess_ftype "$dstpath") debug_var srcpath dstpath ftype <"$srcpath" build1_ftype "$ftype" >"$dstpath" mkdir -p "$MKIT_LOCAL" echo "$dstpath" >> "$MKIT_LOCAL/built.lst" } guess_ftype() { # # Guess file type from destination path $1 # local dstpath="$1" case $dstpath in *.md) echo markdown ;; *) echo MKIT_COMMON ;; esac } build1_ftype() { # # Build a file of type $1 # local ftype="$1" case $ftype in MKIT_COMMON) expand_variables "vars" ;; markdown) expand_includes | expand_variables "vars" ;; rpmstuff) expand_variables "vars" "rpmstuff:vars" ;; *) die "unknown file type: $ftype" ;; esac } build_manpages() { local manfile mdfile if command -v ronn >/dev/null; then ini lskeys "files:man" \ | while read manfile; do mdfile="$manfile.md" ronn -r "$mdfile" mkdir -p "$MKIT_LOCAL" echo "$manfile" >> "$MKIT_LOCAL/built.lst" done else echo "ronn is not installed" return 1 fi } 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 } dist() { # # Create distributable tarball # #FIXME: lacking Makefile skills, we do this step twice fot # rpmstuff, hence -f hack for gzip # local version=$(get_version) local dirname=$MKIT_PROJ_PKGNAME-$version mkdir -p "$dirname" ini values "lists:dist" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname" update_version "$version" "$dirname/mkit.ini" tar -cf "$dirname.tar" "$dirname" gzip -f "$dirname.tar" # see above FIXME mkdir -p "$MKIT_LOCAL" echo "$dirname.tar.gz" >> "$MKIT_LOCAL/built.lst" rm -rf "$dirname" } debstuff() { # # Build Debian stuff (eamed tarball, debian dir) # local version="$(get_version)" # 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" echo "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" >> "$MKIT_LOCAL/built.lst" # read content of each mandatory file from debian_skel # local debian_skel=$(ini 1value debstuff:debian_skel) test -n "$debian_skel" || die "debstuff:debian_skel not specified" test -d "$debian_skel" || die "debian directory template found: $debian_skel" mkdir -p debian/source local dfsrc dftgt find "$debian_skel" -type f \ | while read dfsrc; do dftgt="debian/${dfsrc#$debian_skel}" mkdir -p "$(dirname "$dftgt")" build1 "$dfsrc" "$dftgt" done echo debian >> "$MKIT_LOCAL/built.lst" } rpmstuff() { # # Build specfile # local specname="$MKIT_PROJ_PKGNAME.spec" local specsrc="$(ini 1value "rpmstuff:spec_skel")" test -n "$specsrc" || die "rpmstuff:spec_skel not specified" test -f "$specsrc" || die "specfile template not found: $specsrc" build1 "$specsrc" "$specname" } expand_includes() { # # Expand include directives # # Expand e.g. `` to include code of foo.sh # perl -we ' use strict; my $text; while (<>) { chomp; if (m//) { open my $fh, $1 or warn "cannot find: $1"; my $text = do { local($/); <$fh> }; close $fh; $text =~ s/^(.)/ $1/gm; chomp $text; print "$text\n"; } else { print "$_\n"; } } ' } expand_variables() { # # Expand variables from sections $@ # local script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX) local section varname varvalue { for section in "$@"; do debug_var section ini lskeys "$section" \ | while read varname; do varvalue="$(ini 1value "$section:$varname" | sed -e 's/\$/\\$/' )" echo "s|$varname|$varvalue|;" debug_var varname varvalue done done echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename)|;" echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname)|;" echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline)|;" echo "s|__MKIT_PROJ_VERSION__|$(get_version)|;" echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|;" } >> "$script" perl -wp "$script" || die "expand_variables failed" rm "$script" } get_version() { # # Build semver version string with build metadata # # Build version string from available info using following # logic: # # 1. use project.version (from mkit.ini) # 2. if we are in git, override the version with last tag # 3. if set, add project:prerl (from mkit.ini) as pre-release ID # (afer dash) # 4. if we are at a later commit than the last tag, add branch # name and commit sha1 to build metadata (after plus sign) # 5. if the tree is "dirty", i.e. has uncommited changes, # add "dirty" to build metadata # # The version is compatible with SemVer 2.0.0. # # Examples: # # myprog v1.0.7 # all clear # myprog v1.0.7-alpha # mkit.ini: project:prerl="alpha" # myprog v1.0.7-alpha+g1aef811.master # ^^ + some commits after # myprog v1.0.7-alpha+gf14fc4f.api2 # ^^ + on a feature branch # myprog v1.0.7-alpha+gf14fc4f.api2.dirty # ^^ + tree edited # myprog v1.0.7-alpha+dirty # tag OK but tree edited # myprog v1.0.7+dirty # ^^ but no pre-release id # # Note that versions with "dirty" should be perceived as kind of # dangerous outside developer's own machine. Versions with sha1 are # safer but must not be released. # # I have considered decorating the git commit refs to make them # sort of sortable (e.g. "r1.g1aef811"), but on second thought, # I don't think it's good idea to give *any* semantics to meta-data # at all. First, there is no rule that r1&/dev/null; then # we are in git repo... so we can get smart local latest_tag=$( git log --format="%D" \ | sed 's/,/\n/g' \ | sed 's/^[[:blank:]]*//; ' \ | grep -E '^tag: v[[:digit:]]+\.' \ | cut -d' ' -f2 \ | head -1 ) if ! git describe --tags --exact-match HEAD >&/dev/null; then # we are at a later commit than the last tag local sha=g$(git log -1 --pretty=format:%h HEAD) local curbranch=$(git rev-parse --abbrev-ref HEAD) local commit="$curbranch.$sha" fi if test "$(git diff --shortstat 2>/dev/null)" != ""; then # the tree is "dirty", i.e. has been edited local dirty=dirty fi test -n "$latest_tag" && version=${latest_tag:1} local suffix="" case "$commit:$dirty" in :) suffix="" ;; :dirty) suffix="+$dirty" ;; *:) suffix="+$commit" ;; *:dirty) suffix="+$commit.$dirty" ;; esac test -b "$prerl" && suffix="-$prerl$suffix" version="$version$suffix" fi echo "$version" }