mkit.sh 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. die() {
  5. #
  6. # Exit with message and non-zero exit status
  7. #
  8. echo "fatal: $*" >&2
  9. exit 4
  10. }
  11. mkit_import() {
  12. #
  13. # Import mkit module $1.sh
  14. #
  15. # Check for module, source it and die with reasonable message if needed.
  16. #
  17. local modname=$1
  18. local modpath
  19. modpath="$MKIT_DIR/include/$modname.sh"
  20. test -f "$modpath" || die "no such module: $modpath"
  21. bash -n "$modpath" || die "bad syntax: $modpath"
  22. #shellcheck disable=SC1090
  23. . "$modpath" || die "failed to import: $modname"
  24. }
  25. mkit_import build
  26. mkit_import deploy
  27. mkit_import release
  28. mkit_import ini
  29. __valid_targets() {
  30. #
  31. # List valid routes
  32. #
  33. echo _mkit_data
  34. echo build
  35. echo clean
  36. echo debstuff
  37. echo dist
  38. echo install
  39. echo release
  40. echo release_x
  41. echo release_y
  42. echo release_z
  43. echo rpmstuff
  44. echo uninstall
  45. echo vbump
  46. echo vbump_x
  47. echo vbump_y
  48. echo vbump_z
  49. }
  50. debug() {
  51. #
  52. # Print debug message
  53. #
  54. $MKIT_DEBUG || return 0
  55. echo "MKIT_DEBUG: ${FUNCNAME[1]}()" "$@" >&2
  56. }
  57. debug_var() {
  58. #
  59. # Print debug message
  60. #
  61. $MKIT_DEBUG || return 0
  62. local __mkit_debug_var_name__ # variable name to debug
  63. for __mkit_debug_var_name__ in "$@"; do
  64. {
  65. echo -n "MKIT_DEBUG: ${FUNCNAME[1]}():"
  66. echo -n " $__mkit_debug_var_name__"
  67. echo -n "='${!__mkit_debug_var_name__}'"
  68. echo
  69. } >&2
  70. done
  71. }
  72. __compver() {
  73. #
  74. # True if version $1 matches our version
  75. #
  76. # If our x is 0, check first two fragments, otherwise check just
  77. # the x. Fragments must equal.
  78. #
  79. local their_ver=$1 # their version
  80. local our_x # our X
  81. local our_y # our Y
  82. local their_x # their X
  83. local their_y # their Y
  84. their_x=${their_ver%%.*}
  85. their_y=${their_ver##$their_x.}
  86. their_y=${their_y%%.*}
  87. our_x=${MKIT_VERSION%%.*}
  88. our_y=${MKIT_VERSION##$our_x.}
  89. our_y=${our_y%%.*}
  90. debug_var MKIT_VERSION our_x our_y their_ver their_x their_y
  91. test "$their_x" -eq "$our_x" || return 1
  92. test "$our_x" -eq 0 && {
  93. test "$their_y" = "$our_y"
  94. return $?
  95. }
  96. return 0
  97. }
  98. __chkiniversion() {
  99. #
  100. # Check if ini version is supported
  101. #
  102. # Look for "#mkit version=0.0.0" or similar in first or last
  103. # 3 lines of the file; then check if the version is supported.
  104. #
  105. local ver_line # line declaring version
  106. local their_ver # the declared version
  107. ver_line=$(
  108. {
  109. head -3 "$MKIT_INI"
  110. tail -3 "$MKIT_INI"
  111. } | grep -m 1 -E '^# *mkit +version *= *v?[0-9]+\.[0-9]+\.[0-9]+'
  112. )
  113. test -n "$ver_line" \
  114. || die "version mark ('#mkit version=x.y.z') not found in: $MKIT_INI"
  115. their_ver="$(tr -d '[:blank:]v' <<<"${ver_line##*=}")"
  116. __compver "$their_ver" \
  117. || die "bad mkit.ini version: $their_ver does not match $MKIT_VERSION"
  118. }
  119. local_putb() {
  120. #
  121. # Make file $1 in $MKIT_LOCAL from stdin and mark as built
  122. #
  123. local fpath=$1
  124. local_put "$fpath" && rec_built "$MKIT_LOCAL/$fpath"
  125. }
  126. local_put() {
  127. #
  128. # Make file $1 in $MKIT_LOCAL from stdin
  129. #
  130. local fpath="$MKIT_LOCAL/$1"
  131. { mkdir -p "${fpath%/*}" && cat >"$fpath"; } \
  132. || die "cannot write to local cache: $fpath"
  133. }
  134. local_get() {
  135. #
  136. # Read file $1 in $MKIT_LOCAL
  137. #
  138. local fpath="$MKIT_LOCAL/$1"
  139. cat "$fpath" 2>/dev/null
  140. }
  141. mkit_init() {
  142. #
  143. # Do basic initialization
  144. #
  145. # Check for ini file and some variables
  146. #
  147. $MKIT_DRY && MKIT_DEBUG=true
  148. #shellcheck disable=SC2034
  149. MKIT_PROJ_PKGNAME=$(ini 1value "project:pkgname")
  150. test -f "$MKIT_INI" || die "cannot find mkit.ini: $MKIT_INI"
  151. __chkiniversion
  152. }
  153. rec_built() {
  154. #
  155. # Record file $1 for deletion on `clean`
  156. #
  157. local file=$1
  158. mkdir -p "$MKIT_LOCAL"
  159. echo "$file" >> "$MKIT_LOCAL/built.lst"
  160. }
  161. route() {
  162. #
  163. # Call correct function based on $1
  164. #
  165. if __valid_targets | grep -qwx "^$1"; then
  166. "$1"
  167. else
  168. {
  169. echo "usage: $(basename "$0") TARGET"
  170. echo
  171. echo "valid targets:"
  172. __valid_targets | sed 's/^/ /'
  173. } >&2
  174. fi
  175. }
  176. warn() {
  177. #
  178. # Print warning message
  179. #
  180. echo "$@" >&2
  181. }