123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/bash
  2. . "$MKIT_DIR/include/ini.sh" || die "cannot import ini.sh"
  3. build() {
  4. #
  5. # Add meat to all skeletons
  6. #
  7. local srcpath
  8. find src -type f -name '*.skel' \
  9. | while read srcpath;
  10. do
  11. build1 "$srcpath"
  12. done
  13. }
  14. build1() {
  15. #
  16. # Process one skeleton
  17. #
  18. local srcpath dstpath
  19. srcpath=$1
  20. dstpath=${srcpath%.skel}
  21. case $dstpath in
  22. *.md) <"$srcpath" expand_includes | expand_variables >"$dstpath" ;;
  23. *) <"$srcpath" expand_variables >"$dstpath" ;;
  24. esac
  25. mkdir -p "$MKIT_LOCAL"
  26. echo "$dstpath" >> "$MKIT_LOCAL/built.lst"
  27. }
  28. build_manpages() {
  29. local manfile mdfile
  30. if command -v ronn >/dev/null;
  31. then
  32. ini lskeys "files:man" \
  33. | while read manfile;
  34. do
  35. mdfile="$manfile.md"
  36. ronn -r "$mdfile"
  37. mkdir -p "$MKIT_LOCAL"
  38. echo "$manfile" >> "$MKIT_LOCAL/built.lst"
  39. done
  40. else
  41. echo "ronn is not installed"
  42. return 1
  43. fi
  44. }
  45. clean() {
  46. #
  47. # Clean up tree after building
  48. #
  49. test -f "$MKIT_LOCAL/built.lst" && {
  50. <"$MKIT_LOCAL/built.lst" xargs -r rm -f
  51. rm -f "$MKIT_LOCAL/built.lst"
  52. rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
  53. }
  54. true
  55. }
  56. dist() {
  57. #
  58. # Create distributable tarball
  59. #
  60. local version=$(get_version)
  61. local dirname=$MKIT_PKGNAME-$version
  62. mkdir -p "$dirname"
  63. ini values "lists:dist" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
  64. sed -i -e "s/^VERSION = .*/VERSION = $version/" "$dirname/config.mk"
  65. tar -cf "$dirname.tar" "$dirname"
  66. gzip "$dirname.tar"
  67. rm -rf "$dirname"
  68. }
  69. expand_includes() {
  70. #
  71. # Expand include directives
  72. #
  73. # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
  74. #
  75. perl -we '
  76. use strict;
  77. my $text;
  78. while (<>) {
  79. chomp;
  80. if (m/<!-- include4: (\S+) -->/) {
  81. open my $fh, $1 or warn "cannot find: $1";
  82. my $text = do { local($/); <$fh> };
  83. close $fh;
  84. $text =~ s/^(.)/ $1/gm;
  85. chomp $text;
  86. print "$text\n";
  87. } else {
  88. print "$_\n";
  89. }
  90. }
  91. '
  92. }
  93. expand_variables() {
  94. #
  95. # Expand variable values
  96. #
  97. local script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
  98. local varname varvalue
  99. ini lskeys "vars" \
  100. | while read varname;
  101. do
  102. varvalue="$(ini 1value "vars:$varname" | sed -e 's/\$/\\$/' )"
  103. echo "s|$varname|$varvalue|;" >> "$script"
  104. done
  105. echo "s|__CODENAME__|$CODENAME|;" >> "$script"
  106. echo "s|__VERSION__|$(get_version)|;" >> "$script"
  107. perl -wp "$script"
  108. rm "$script"
  109. }
  110. get_version() {
  111. #
  112. # Build semver version string with build metadata
  113. #
  114. # Build version string from available info using following
  115. # logic:
  116. #
  117. # 1. use VERSION (from config.mk)
  118. # 2. if we are in git, override the version with last tag
  119. # 3. if set, add PRERELEASE (from config.mk) as pre-release ID
  120. # (afer dash)
  121. # 4. if we are at a later commit than the last tag, add branch
  122. # name and commit sha1 to build metadata (after plus sign)
  123. # 5. if the tree is "dirty", i.e. has uncommited changes,
  124. # add "dirty" to build metadata
  125. #
  126. # The version is compatible with SemVer 2.0.0.
  127. #
  128. # Examples:
  129. #
  130. # myprog v1.0.7 # all clear
  131. # myprog v1.0.7-alpha # PRERELEASE="alpha"
  132. # myprog v1.0.7-alpha+g1aef811.master # ^^ + some commits after
  133. # myprog v1.0.7-alpha+gf14fc4f.api2 # ^^ + on a feature branch
  134. # myprog v1.0.7-alpha+gf14fc4f.api2.dirty # ^^ + tree edited
  135. # myprog v1.0.7-alpha+dirty # tag OK but tree edited
  136. # myprog v1.0.7+dirty # ^^ but no pre-release id
  137. #
  138. # Note that versions with "dirty" should be perceived as kind of
  139. # dangerous outside developer's own machine. Versions with sha1 are
  140. # safer but must not be released.
  141. #
  142. # I have considered decorating the git commit refs to make them
  143. # sort of sortable (e.g. "r1.g1aef811"), but on second thought,
  144. # I don't think it's good idea to give *any* semantics to meta-data
  145. # at all. First, there is no rule that r1<r2<r3; a commit can be
  146. # removing what other just added and one change can be split to
  147. # multiple commits. Also, the whole thing breaks anyway once you
  148. # rebase your branch (no, it's not a sin). The sole purpose of
  149. # meta-data is to *identify* the code, and provide safe path back
  150. # to tree; commit refs are already perfect for that.
  151. #
  152. # FIXME: Using PRERELEASE for release IDs may not be compatible with
  153. # release strategy implemented in release.sh
  154. #
  155. local version=$VERSION
  156. local prerl=$PRERELEASE
  157. grep ":" <<<"$prerl" && warn "colon in PRERELEASE may corrupt version data: $prerl"
  158. if git rev-parse HEAD >&/dev/null;
  159. then # we are in git repo... so we can get smart
  160. local lasttag=$(git tag | grep ^v | sort -V | tail -n1)
  161. if ! git describe --tags --exact-match HEAD >&/dev/null;
  162. then # we are at a later commit than the last tag
  163. local sha=g$(git log -1 --pretty=format:%h HEAD)
  164. local curbranch=$(git rev-parse --abbrev-ref HEAD)
  165. local commit="$curbranch.$sha"
  166. fi
  167. if test "$(git diff --shortstat 2>/dev/null)" != "";
  168. then # the tree is "dirty", i.e. has been edited
  169. local dirty=dirty
  170. fi
  171. test -n "$lasttag" && version=${lasttag:1}
  172. local suffix=""
  173. case "$commit:$dirty" in
  174. :) suffix="" ;;
  175. :dirty) suffix="+$dirty" ;;
  176. *:) suffix="+$commit" ;;
  177. *:dirty) suffix="+$commit.$dirty" ;;
  178. esac
  179. test -b "$prerl" && suffix="-$prerl$suffix"
  180. version="$version$suffix"
  181. fi
  182. echo "$version"
  183. }