123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #!/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 dstpath
- srcpath=$1
- dstpath=${srcpath%.skel}
- case $dstpath in
- *.md) cat $srcpath \
- | expand_includes \
- | expand_variables \
- > $dstpath ;;
- *) cat $srcpath \
- | expand_variables \
- > $dstpath ;;
- esac
- echo $dstpath >> built.list
- }
-
- 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
- echo $manfile >> built.list
- done
- else
- echo "ronn is not installed"
- return 1
- fi
- }
-
- clean() {
- #
- # Clean up tree after building
- #
- test -f built.list && {
- cat built.list | xargs rm -f
- rm -f built.list
- } || :
- }
-
- dist() {
- #
- # Create distributable tarball
- #
- local version=$(get_version)
- local dirname=$MKIT_PKGNAME-$version
- mkdir -p $dirname
- local item
- cp -R $(ini values "lists:dist") $dirname
- sed -i -e "s/^VERSION = .*/VERSION = $version/" $dirname/config.mk
- tar -cf $dirname.tar $dirname
- gzip $dirname.tar
- rm -rf $dirname
- }
-
- 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_variables() {
- #
- # Expand variable values
- #
- local script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
- local varname varvalue
- ini lskeys "vars" \
- | while read varname;
- do
- varvalue="$(ini 1value "vars:$varname" | sed -e 's/\$/\\$/' )"
- echo "s|$varname|$varvalue|;" >> $script
- done
- echo "s|__CODENAME__|$CODENAME|;" >> $script
- echo "s|__VERSION__|$(get_version)|;" >> $script
- perl -wp $script
- rm $script
- }
-
- get_version() {
- #
- # Build semver version string with build metadata
- #
- # Build version string from available info using following
- # logic:
- #
- # 1. use VERSION (from config.mk)
- # 2. if we are in git, override the version with last tag
- # 3. if set, add PRERELEASE (from config.mk) 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 # PRERELEASE="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<r2<r3; a commit can be
- # removing what other just added and one change can be split to
- # multiple commits. Also, the whole thing breaks anyway once you
- # rebase your branch (no, it's not a sin). The sole purpose of
- # meta-data is to *identify* the code, and provide safe path back
- # to tree; commit refs are already perfect for that.
- #
- # FIXME: The assumpton "if we are in git repo, we can read version
- # from tags" fails if we are in shallow clone made from
- # other than a tagged commit.
- #
- # FIXME: Using PRERELEASE for release IDs may not be compatible with
- # release strategy implemented in release.sh
- #
- local version=$VERSION
- local prerl=$PRERELEASE
- grep ":" <<<"$prerl" && warn "colon in PRERELEASE may corrupt version data: $prerl"
- if git rev-parse HEAD >&/dev/null;
- then # we are in git repo... so we can get smart
- local lasttag=$(git tag | grep ^v | sort -V | tail -n1)
- 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
- version=${lasttag:1}
- local suffix=""
- case $prerl:$commit:$dirty in
- ::) suffix="" ;;
- ::dirty) suffix="+$dirty" ;;
- :*:) suffix="+$commit" ;;
- :*:dirty) suffix="+$commit" ;;
- *::) suffix="-$prerl" ;;
- *::dirty) suffix="-$prerl+$dirty" ;;
- *:*:) suffix="-$prerl+$commit" ;;
- *:*:dirty) suffix="-$prerl+$commit.$dirty" ;;
- esac
- version=$version$suffix
- fi
- echo $version
- }
|