Working Saturnin-based meta-command

build.sh 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. rpmstuff) __expand_tokens "tokens" "rpmstuff:tokens" ;;
  27. debstuff) __expand_tokens "tokens" "debstuff:tokens" ;;
  28. *) die "unknown file type: $ftype" ;;
  29. esac
  30. }
  31. __expand_tokens() {
  32. #
  33. # Read stdin, expanding tokens from sections $@
  34. #
  35. local script # sed script cache
  36. local section # each section to expand tokens from
  37. local varname # each token name
  38. local varvalue # each token value
  39. script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
  40. {
  41. for section in "$@"; do
  42. debug_var section
  43. ini lskeys "$section" \
  44. | while read -r varname; do
  45. varvalue="$(ini 1value "$section:$varname" | __qfs )"
  46. echo "s|$varname|$varvalue|g;"
  47. debug_var varname varvalue
  48. done
  49. done
  50. echo "s|__MKIT_PROJ_NAME__|$(ini 1value project:name | __qfs)|g;"
  51. echo "s|__MKIT_PROJ_CODENAME__|$(ini 1value project:codename | __qfs)|g;"
  52. echo "s|__MKIT_PROJ_LICENSE__|$(ini 1value project:license | __qfs)|g;"
  53. echo "s|__MKIT_PROJ_PKGNAME__|$(ini 1value project:pkgname | __qfs)|g;"
  54. echo "s|__MKIT_PROJ_TAGLINE__|$(ini 1value project:tagline | __qfs)|g;"
  55. echo "s|__MKIT_PROJ_MAINTAINER__|$(ini 1value project:maintainer | __qfs)|g;"
  56. echo "s|__MKIT_PROJ_VCS_BROWSER__|$(ini 1value project:vcs_browser | __qfs)|g;"
  57. echo "s|__MKIT_PROJ_GIT_LASTHASH__|$(git_lasthash | __qfs)|g;"
  58. echo "s|__MKIT_PROJ_VERSION__|$(semver | __qfs)|g;"
  59. echo "s|__MKIT_SELF_VERSION__|$MKIT_VERSION|g;"
  60. } >> "$script"
  61. sed -f "$script" || die "__expand_tokens failed"
  62. rm "$script"
  63. }
  64. __guess_ftype() {
  65. #
  66. # Guess file type from destination path $1
  67. #
  68. local dstpath=$1 # destination path
  69. case $dstpath in
  70. *) echo MKIT_COMMON ;;
  71. esac
  72. }
  73. __qfs() {
  74. #
  75. # Quote for our sed scipt's RHS
  76. #
  77. sed '
  78. s:\\:\\\\:g
  79. s:|:\\|:g
  80. '
  81. }
  82. _mkit_data() {
  83. #
  84. # Build sampler showing all token values
  85. #
  86. local token
  87. local section
  88. local sections
  89. sections=()
  90. ini lskeys tokens | grep -q . && sections=(tokens)
  91. sections+=( $(ini lssect | grep ':tokens$') )
  92. {
  93. echo "(builtin):"
  94. echo " x_MKIT_PROJ_NAME__ => '__MKIT_PROJ_NAME__'"
  95. echo " x_MKIT_PROJ_CODENAME__ => '__MKIT_PROJ_CODENAME__'"
  96. echo " x_MKIT_PROJ_LICENSE__ => '__MKIT_PROJ_LICENSE__'"
  97. echo " x_MKIT_PROJ_PKGNAME__ => '__MKIT_PROJ_PKGNAME__'"
  98. echo " x_MKIT_PROJ_TAGLINE__ => '__MKIT_PROJ_TAGLINE__'"
  99. echo " x_MKIT_PROJ_MAINTAINER__ => '__MKIT_PROJ_MAINTAINER__'"
  100. echo " x_MKIT_PROJ_VCS_BROWSER__ => '__MKIT_PROJ_VCS_BROWSER__'"
  101. echo " x_MKIT_PROJ_GIT_LASTHASH__ => '__MKIT_PROJ_GIT_LASTHASH__'"
  102. echo " x_MKIT_PROJ_VERSION__ => '__MKIT_PROJ_VERSION__'"
  103. echo " x_MKIT_SELF_VERSION__ => '__MKIT_SELF_VERSION__'"
  104. for section in "${sections[@]}"; do
  105. echo "$section:"
  106. for token in $(ini lskeys "$section"); do
  107. echo " x${token:1} => '$token'"
  108. done
  109. done
  110. } \
  111. | __expand_tokens "MKIT_BUILTIN" "${sections[@]}" \
  112. | sed '/^ x/ s|x|_|'
  113. }
  114. build() {
  115. #
  116. # Add meat to all skeletons
  117. #
  118. local srcpath # each source path
  119. semver >/dev/null
  120. find . -type f -name '*.skel' \
  121. | while read -r srcpath; do
  122. __build1 "$srcpath"
  123. done
  124. }
  125. clean() {
  126. #
  127. # Clean up tree after building
  128. #
  129. test -f "$MKIT_LOCAL/built.lst" && {
  130. <"$MKIT_LOCAL/built.lst" grep -v -e '\.\.' -e ^/ \
  131. | xargs -r rm -rf
  132. rm -f "$MKIT_LOCAL/built.lst"
  133. rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
  134. }
  135. true
  136. }
  137. debstuff() {
  138. #
  139. # Build Debian stuff (eamed tarball, debian dir)
  140. #
  141. local version # package version
  142. local debian_skel # 'debian' folder skeleton
  143. local dfsrc # each source file from ^^
  144. local dftgt # each built packaging file
  145. version=$(semver)
  146. # tarball - we should already have by means of 'dist'
  147. #
  148. mv "${MKIT_PROJ_PKGNAME}-$version.tar.gz" \
  149. "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" \
  150. || die "could not rename tarball"
  151. rec_built "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz"
  152. # read content of each mandatory file from debian_skel
  153. #
  154. debian_skel=$(ini 1value dist:debstuff)
  155. test -n "$debian_skel" || die "dist:debstuff not specified"
  156. test -d "$debian_skel" || die "debian directory template found: $debian_skel"
  157. mkdir -p debian/source
  158. find "$debian_skel" -type f \
  159. | while read -r dfsrc; do
  160. dftgt="debian/${dfsrc#$debian_skel}"
  161. mkdir -p "$(dirname "$dftgt")"
  162. __build1 "$dfsrc" "$dftgt" debstuff
  163. done
  164. rec_built debian
  165. }
  166. dist() {
  167. #
  168. # Create distributable tarball
  169. #
  170. #FIXME: lacking Makefile skills, we do this step twice for
  171. # rpmstuff, hence -f hack for gzip
  172. #
  173. local version # tarball version
  174. local git_lasthash # last git commit hash
  175. local dirname # directory and tarball name
  176. version=$(semver)
  177. dirname=$MKIT_PROJ_PKGNAME-$version
  178. git_lasthash=$(git_lasthash)
  179. mkdir -p "$dirname"
  180. ini values "dist:tarball" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
  181. update_version "$version" "$dirname/mkit.ini"
  182. mkdir -p "$dirname/.mkit"
  183. echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
  184. tar -cf "$dirname.tar" "$dirname"
  185. gzip -f "$dirname.tar" # see above FIXME
  186. rec_built "$dirname.tar.gz"
  187. rm -rf "$dirname"
  188. }
  189. rpmstuff() {
  190. #
  191. # Build specfile
  192. #
  193. local specname=$MKIT_PROJ_PKGNAME.spec # .spec filename
  194. local specsrc # source of specfile
  195. specsrc="$(ini 1value "dist:rpmstuff")"
  196. test -n "$specsrc" || die "dist:rpmstuff not specified"
  197. test -f "$specsrc" || die "specfile template not found: $specsrc"
  198. __build1 "$specsrc" "$specname" rpmstuff
  199. }