build.sh 5.8KB

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