build.sh 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/bash
  2. . "$MKIT_DIR/include/ini.sh" || die "cannot import ini.sh"
  3. . "$MKIT_DIR/include/facts.sh" || die "cannot import facts.sh"
  4. _build1() {
  5. #
  6. # Process one skeleton
  7. #
  8. local srcpath="$1"
  9. local dstpath="$2"
  10. local ftype="$3"
  11. test -n "$dstpath" || dstpath=${srcpath%.skel}
  12. test -n "$ftype" || ftype=$(_guess_ftype "$dstpath")
  13. debug_var srcpath dstpath ftype
  14. <"$srcpath" _build1_ftype "$ftype" >"$dstpath"
  15. mkdir -p "$MKIT_LOCAL"
  16. echo "$dstpath" >> "$MKIT_LOCAL/built.lst"
  17. }
  18. _build1_ftype() {
  19. #
  20. # Build a file of type $1
  21. #
  22. local ftype="$1"
  23. case $ftype in
  24. MKIT_COMMON) _expand_tokens "tokens" ;;
  25. markdown) _expand_includes | _expand_tokens "tokens" ;;
  26. rpmstuff) _expand_tokens "tokens" "rpmstuff:tokens" ;;
  27. *) die "unknown file type: $ftype" ;;
  28. esac
  29. }
  30. _expand_includes() {
  31. #
  32. # Expand include directives
  33. #
  34. # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
  35. #
  36. perl -we '
  37. use strict;
  38. my $text;
  39. while (<>) {
  40. chomp;
  41. if (m/<!-- include4: (\S+) -->/) {
  42. open my $fh, $1 or warn "cannot find: $1";
  43. my $text = do { local($/); <$fh> };
  44. close $fh;
  45. $text =~ s/^(.)/ $1/gm;
  46. chomp $text;
  47. print "$text\n";
  48. } else {
  49. print "$_\n";
  50. }
  51. }
  52. '
  53. }
  54. _expand_tokens() {
  55. #
  56. # Expand tokens from sections $@
  57. #
  58. local script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
  59. local section varname varvalue
  60. {
  61. for section in "$@";
  62. do
  63. debug_var section
  64. ini lskeys "$section" \
  65. | while read varname;
  66. do
  67. varvalue="$(ini 1value "$section:$varname" | _qfs )"
  68. echo "s|$varname|$varvalue|g;"
  69. debug_var varname varvalue
  70. done
  71. done
  72. echo "s|__MKIT_PROJ_NAME__|$(ini 1value project:name | _qfs)|g;"
  73. echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename | _qfs)|g;"
  74. echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname | _qfs)|g;"
  75. echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline | _qfs)|g;"
  76. echo "s|__MKIT_PROJ_MAINTAINER__|$(ini 1value project:maintainer | _qfs)|g;"
  77. echo "s|__MKIT_PROJ_VCS_BROWSER__|$(ini 1value project:vcs_browser | _qfs)|g;"
  78. echo "s|__MKIT_PROJ_GIT_LASTHASH__|$(git_lasthash | _qfs)|g;"
  79. echo "s|__MKIT_PROJ_VERSION__|$(semver | _qfs)|g;"
  80. echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|g;"
  81. } >> "$script"
  82. sed -f "$script" || die "_expand_tokens failed"
  83. rm "$script"
  84. }
  85. _guess_ftype() {
  86. #
  87. # Guess file type from destination path $1
  88. #
  89. local dstpath="$1"
  90. case $dstpath in
  91. *.md) echo markdown ;;
  92. *) echo MKIT_COMMON ;;
  93. esac
  94. }
  95. _qfs() {
  96. #
  97. # Quote for our sed scipt's RHS
  98. #
  99. sed '
  100. s:\\:\\\\:g
  101. s:|:\\|:g
  102. '
  103. }
  104. build() {
  105. #
  106. # Add meat to all skeletons
  107. #
  108. local srcpath
  109. find src -type f -name '*.skel' \
  110. | while read srcpath;
  111. do
  112. _build1 "$srcpath"
  113. done
  114. }
  115. build_manpages() {
  116. local manfile mdfile
  117. if command -v ronn >/dev/null;
  118. then
  119. ini lskeys "files:man" \
  120. | while read manfile;
  121. do
  122. mdfile="$manfile.md"
  123. ronn -r "$mdfile"
  124. mkdir -p "$MKIT_LOCAL"
  125. echo "$manfile" >> "$MKIT_LOCAL/built.lst"
  126. done
  127. else
  128. echo "ronn is not installed"
  129. return 1
  130. fi
  131. }
  132. clean() {
  133. #
  134. # Clean up tree after building
  135. #
  136. test -f "$MKIT_LOCAL/built.lst" && {
  137. <"$MKIT_LOCAL/built.lst" grep -v -e '\.\.' -e ^/ \
  138. | xargs -r rm -rf
  139. rm -f "$MKIT_LOCAL/built.lst"
  140. rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
  141. }
  142. true
  143. }
  144. debstuff() {
  145. #
  146. # Build Debian stuff (eamed tarball, debian dir)
  147. #
  148. local version="$(semver)"
  149. # tarball - we should already have by means of 'dist'
  150. #
  151. mv "${MKIT_PROJ_PKGNAME}-$version.tar.gz" \
  152. "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" \
  153. || die "could not rename tarball"
  154. echo "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" >> "$MKIT_LOCAL/built.lst"
  155. # read content of each mandatory file from debian_skel
  156. #
  157. local debian_skel=$(ini 1value dist:debstuff)
  158. test -n "$debian_skel" || die "dist:debstuff not specified"
  159. test -d "$debian_skel" || die "debian directory template found: $debian_skel"
  160. mkdir -p debian/source
  161. local dfsrc dftgt
  162. find "$debian_skel" -type f \
  163. | while read dfsrc;
  164. do
  165. dftgt="debian/${dfsrc#$debian_skel}"
  166. mkdir -p "$(dirname "$dftgt")"
  167. _build1 "$dfsrc" "$dftgt"
  168. done
  169. echo debian >> "$MKIT_LOCAL/built.lst"
  170. }
  171. dist() {
  172. #
  173. # Create distributable tarball
  174. #
  175. #FIXME: lacking Makefile skills, we do this step twice fot
  176. # rpmstuff, hence -f hack for gzip
  177. #
  178. local version=$(semver)
  179. local git_lasthash=$(git_lasthash)
  180. local dirname=$MKIT_PROJ_PKGNAME-$version
  181. mkdir -p "$dirname"
  182. ini values "dist:tarball" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
  183. update_version "$version" "$dirname/mkit.ini"
  184. mkdir -p "$dirname/.mkit"
  185. echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
  186. tar -cf "$dirname.tar" "$dirname"
  187. gzip -f "$dirname.tar" # see above FIXME
  188. mkdir -p "$MKIT_LOCAL"
  189. echo "$dirname.tar.gz" >> "$MKIT_LOCAL/built.lst"
  190. rm -rf "$dirname"
  191. }
  192. rpmstuff() {
  193. #
  194. # Build specfile
  195. #
  196. local specname="$MKIT_PROJ_PKGNAME.spec"
  197. local specsrc="$(ini 1value "dist:rpmstuff")"
  198. test -n "$specsrc" || die "dist:rpmstuff not specified"
  199. test -f "$specsrc" || die "specfile template not found: $specsrc"
  200. _build1 "$specsrc" "$specname"
  201. }