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