Shellfu/Bash/JAT data-driven testing mini-framework https://pagure.io/shellfu-bash-xcase

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_macros "macros" ;;
  26. rpmstuff) __expand_macros "macros" "rpmstuff:macros" ;;
  27. debstuff) __expand_macros "macros" "debstuff:macros" ;;
  28. *) die "unknown file type: $ftype" ;;
  29. esac
  30. }
  31. __expand_line() {
  32. #
  33. # Expand macro from $MacroMap in single line $1
  34. #
  35. # If macro value has multiple lines, repeat original line with
  36. # different substitution.
  37. #
  38. # E.g. if macro value is "foo\nbar" and macro name is __FOO__,
  39. # line `see: "__FOO__"` will expand to two lines: `see: "foo"`
  40. # and `see: "bar"`.
  41. #
  42. local line=$1 # line to process
  43. local mname # macro name
  44. local mvline # line of macro value
  45. local xline # expanded line
  46. xline=$line
  47. for mname in "${!MacroMap[@]}"; do
  48. if ! test "${line//$mname}" == "$line"; then
  49. xline=$(
  50. while IFS= read -r mvline; do
  51. echo "${line//$mname/$mvline}"
  52. done <<<"${MacroMap[$mname]}"
  53. )
  54. fi
  55. line=$xline
  56. done
  57. echo "$xline"
  58. return 1
  59. }
  60. __expand_macros() {
  61. #
  62. # Read stdin, expanding macros from sections $@
  63. #
  64. local section # each section to expand macros from
  65. local line # each line on stdin
  66. local mname # each macro name
  67. local -A MacroMap # macro value map
  68. MacroMap[__MKIT_PROJ_NAME__]=$(ini 1value project:name)
  69. MacroMap[__MKIT_PROJ_CODENAME__]=$(ini 1value project:codename)
  70. MacroMap[__MKIT_PROJ_LICENSE__]=$(ini 1value project:license)
  71. MacroMap[__MKIT_PROJ_PKGNAME__]=$(ini 1value project:pkgname)
  72. MacroMap[__MKIT_PROJ_TAGLINE__]=$(ini 1value project:tagline)
  73. MacroMap[__MKIT_PROJ_MAINTAINER__]=$(ini 1value project:maintainer)
  74. MacroMap[__MKIT_PROJ_VCS_BROWSER__]=$(ini 1value project:vcs_browser)
  75. MacroMap[__MKIT_PROJ_GIT_LASTHASH__]=$(__cached git_lasthash)
  76. MacroMap[__MKIT_PROJ_VERSION__]=$(__cached semver)
  77. MacroMap[__MKIT_SELF_VERSION__]=$MKIT_VERSION
  78. for section in "$@"; do
  79. for mname in $(ini lskeys "$section"); do
  80. MacroMap[$mname]=$(ini values "$section:$mname")
  81. done
  82. done
  83. debug_var MacroMap
  84. while IFS= read -r line; do
  85. __expand_line "$line"
  86. done
  87. }
  88. __guess_ftype() {
  89. #
  90. # Guess file type from destination path $1
  91. #
  92. local dstpath=$1 # destination path
  93. case $dstpath in
  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. __cached() {
  107. #
  108. # Cached value $1 of function $1()
  109. #
  110. # In order to support git-less builds, some values might be cached
  111. # in $MKIT_LOCAL. This function gets file $1 from that cache (cache
  112. # hit) or re-creates it (cache miss), but prints its body in either
  113. # case.
  114. #
  115. # The command to re-create file is the same as the key (ie. no
  116. # arguments).
  117. #
  118. local name=$1
  119. __local_get "$name" && return 0
  120. "$name" | __local_putb "$name"
  121. __local_get "$name"
  122. }
  123. __local_putb() {
  124. #
  125. # Make file $1 in $MKIT_LOCAL from stdin and mark as built
  126. #
  127. local fpath=$1
  128. __local_put "$fpath" && __rec_built "$MKIT_LOCAL/$fpath"
  129. }
  130. __local_put() {
  131. #
  132. # Make file $1 in $MKIT_LOCAL from stdin
  133. #
  134. local fpath="$MKIT_LOCAL/$1"
  135. { mkdir -p "${fpath%/*}" && cat >"$fpath"; } \
  136. || die "cannot write to local cache: $fpath"
  137. }
  138. __local_get() {
  139. #
  140. # Read file $1 in $MKIT_LOCAL
  141. #
  142. local fpath="$MKIT_LOCAL/$1"
  143. cat "$fpath" 2>/dev/null
  144. }
  145. __rec_built() {
  146. #
  147. # Record file $1 for deletion on `clean`
  148. #
  149. local file=$1
  150. mkdir -p "$MKIT_LOCAL"
  151. echo "$file" >> "$MKIT_LOCAL/built.lst"
  152. }
  153. _mkit_data() {
  154. #
  155. # Build sampler showing all macro values
  156. #
  157. local macro
  158. local section
  159. local sections
  160. sections=()
  161. ini lskeys macros | grep -q . && sections=(macros)
  162. sections+=( $(ini lssect | grep ':macros$') )
  163. {
  164. echo "(builtin):"
  165. echo " x_MKIT_PROJ_NAME__ => '__MKIT_PROJ_NAME__'"
  166. echo " x_MKIT_PROJ_CODENAME__ => '__MKIT_PROJ_CODENAME__'"
  167. echo " x_MKIT_PROJ_LICENSE__ => '__MKIT_PROJ_LICENSE__'"
  168. echo " x_MKIT_PROJ_PKGNAME__ => '__MKIT_PROJ_PKGNAME__'"
  169. echo " x_MKIT_PROJ_TAGLINE__ => '__MKIT_PROJ_TAGLINE__'"
  170. echo " x_MKIT_PROJ_MAINTAINER__ => '__MKIT_PROJ_MAINTAINER__'"
  171. echo " x_MKIT_PROJ_VCS_BROWSER__ => '__MKIT_PROJ_VCS_BROWSER__'"
  172. echo " x_MKIT_PROJ_GIT_LASTHASH__ => '__MKIT_PROJ_GIT_LASTHASH__'"
  173. echo " x_MKIT_PROJ_VERSION__ => '__MKIT_PROJ_VERSION__'"
  174. echo " x_MKIT_SELF_VERSION__ => '__MKIT_SELF_VERSION__'"
  175. for section in "${sections[@]}"; do
  176. echo "$section:"
  177. for macro in $(ini lskeys "$section"); do
  178. echo " x${macro:1} => '$macro'"
  179. done
  180. done
  181. } \
  182. | __expand_macros "MKIT_BUILTIN" "${sections[@]}" \
  183. | sed '/^ x/ s|x|_|'
  184. }
  185. build() {
  186. #
  187. # Add meat to all skeletons
  188. #
  189. local srcpath # each source path
  190. find . -type f -name '*.skel' \
  191. | while read -r srcpath; do
  192. __build1 "$srcpath"
  193. done
  194. }
  195. clean() {
  196. #
  197. # Clean up tree after building
  198. #
  199. test -f "$MKIT_LOCAL/built.lst" && {
  200. <"$MKIT_LOCAL/built.lst" grep -v -e '\.\.' -e ^/ \
  201. | xargs -r rm -rf
  202. rm -f "$MKIT_LOCAL/built.lst"
  203. rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
  204. }
  205. true
  206. }
  207. debstuff() {
  208. #
  209. # Build Debian stuff (eamed tarball, debian dir)
  210. #
  211. local version # package version
  212. local debian_skel # 'debian' folder skeleton
  213. local dfsrc # each source file from ^^
  214. local dftgt # each built packaging file
  215. version=$(__cached semver)
  216. # tarball - we should already have by means of 'dist'
  217. #
  218. mv "${MKIT_PROJ_PKGNAME}-$version.tar.gz" \
  219. "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" \
  220. || die "could not rename tarball"
  221. __rec_built "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz"
  222. # read content of each mandatory file from debian_skel
  223. #
  224. debian_skel=$(ini 1value dist:debstuff)
  225. test -n "$debian_skel" || die "dist:debstuff not specified"
  226. test -d "$debian_skel" || die "debian directory template found: $debian_skel"
  227. mkdir -p debian/source
  228. find "$debian_skel" -type f \
  229. | while read -r dfsrc; do
  230. dftgt="debian/${dfsrc#$debian_skel}"
  231. mkdir -p "$(dirname "$dftgt")"
  232. __build1 "$dfsrc" "$dftgt" debstuff
  233. done
  234. __rec_built debian
  235. }
  236. dist() {
  237. #
  238. # Create distributable tarball
  239. #
  240. #FIXME: lacking Makefile skills, we do this step twice for
  241. # rpmstuff, hence -f hack for gzip
  242. #
  243. local version # tarball version
  244. local git_lasthash # last git commit hash
  245. local dirname # directory and tarball name
  246. version=$(semver)
  247. dirname=$MKIT_PROJ_PKGNAME-$version
  248. git_lasthash=$(git_lasthash)
  249. mkdir -p "$dirname"
  250. ini values "dist:tarball" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
  251. mkdir -p "$dirname/.mkit"
  252. echo -n "$version" > "$dirname/.mkit/semver"
  253. echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
  254. tar -cf "$dirname.tar" "$dirname"
  255. gzip -f "$dirname.tar" # see above FIXME
  256. __rec_built "$dirname.tar.gz"
  257. rm -rf "$dirname"
  258. }
  259. rpmstuff() {
  260. #
  261. # Build specfile
  262. #
  263. local specname=$MKIT_PROJ_PKGNAME.spec # .spec filename
  264. local specsrc # source of specfile
  265. specsrc="$(ini 1value "dist:rpmstuff")"
  266. test -n "$specsrc" || die "dist:rpmstuff not specified"
  267. test -f "$specsrc" || die "specfile template not found: $specsrc"
  268. __build1 "$specsrc" "$specname" rpmstuff
  269. }