Working Saturnin-based meta-command

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