123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #!/bin/bash
-
- . "$MKIT_DIR/include/ini.sh" || die "cannot import ini.sh"
- . "$MKIT_DIR/include/facts.sh" || die "cannot import facts.sh"
-
-
- _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"
- }
-
- _build1_ftype() {
- #
- # Build a file of type $1
- #
- local ftype="$1"
- case $ftype in
- MKIT_COMMON) _expand_tokens "tokens" ;;
- markdown) _expand_includes | _expand_tokens "tokens" ;;
- rpmstuff) _expand_tokens "tokens" "rpmstuff:tokens" ;;
- *) die "unknown file type: $ftype" ;;
- esac
- }
-
- _expand_includes() {
- #
- # Expand include directives
- #
- # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
- #
- perl -we '
- use strict;
- my $text;
- while (<>) {
- chomp;
- if (m/<!-- include4: (\S+) -->/) {
- 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_tokens() {
- #
- # Expand tokens 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" | _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_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"
- case $dstpath in
- *.md) echo markdown ;;
- *) echo MKIT_COMMON ;;
- esac
- }
-
- _qfs() {
- #
- # Quote for our sed scipt's RHS
- #
- sed '
- s:\\:\\\\:g
- s:|:\\|:g
- '
- }
-
- build() {
- #
- # Add meat to all skeletons
- #
- local srcpath
- find src -type f -name '*.skel' \
- | while read srcpath;
- do
- _build1 "$srcpath"
- done
- }
-
- 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
- }
-
- debstuff() {
- #
- # Build Debian stuff (eamed tarball, debian dir)
- #
- local 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"
- 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"
- }
-
- dist() {
- #
- # Create distributable tarball
- #
- #FIXME: lacking Makefile skills, we do this step twice fot
- # rpmstuff, hence -f hack for gzip
- #
- local version=$(semver)
- local git_lasthash=$(git_lasthash)
- 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"
- mkdir -p "$dirname/.mkit"
- echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
- 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"
- }
-
- 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"
- }
|