Working Saturnin-based meta-command

ini.sh 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. __ini_cat() {
  5. #
  6. # A no-op for text stream
  7. #
  8. local line # each line
  9. while read -r line; do
  10. printf -- "%s\n" "$line"
  11. done
  12. }
  13. __ini_expand() {
  14. #
  15. # Expand reference value (prefix only)
  16. #
  17. local line # each input line
  18. local suffix # tail of the line
  19. local ref # reference
  20. local value # value if reference
  21. while read -r line; do # [foo:bar]/path
  22. suffix="${line#\[*\]}" # /path
  23. ref="${line%$suffix}" # [foo:bar]
  24. ref="${ref%\]}" # [foo:bar
  25. ref="${ref#\[}" # foo:bar
  26. value="$(ini 1value "$ref")" # foo_bar_value
  27. printf -- "%s\n" "$value$suffix" # foo_bar_value/path
  28. done
  29. }
  30. __ini_grepkey() {
  31. #
  32. # Read key from a section
  33. #
  34. local wnt=$1 # wanted key
  35. grep '.' \
  36. | grep -v '\s*#' \
  37. | sed -e 's/ *= */=/; s/ +$//; s/^//;' \
  38. | grep -e "^$wnt=" \
  39. | cut -d= -f2- \
  40. | __ini_maybe_expand
  41. }
  42. __ini_greppath() {
  43. #
  44. # Read key from the right section
  45. #
  46. # E.g. `files:share:my/lib.sh` should read
  47. #
  48. # [files:share]
  49. # my/lib.sh = proj/my/lib.sh
  50. #
  51. local wnt=$1 # wanted path
  52. local wntkey=${wnt##*:} # ^^ key part
  53. local wntsec=${wnt%:$wntkey} # ^^ section part
  54. local override # ENV override (only ENV section)
  55. if test "$wntsec" = 'ENV'; then
  56. override=${!wntkey}
  57. test -n "$override" \
  58. && echo "$override" \
  59. && return
  60. fi
  61. __ini_grepsec "$wntsec" | __ini_grepkey "$wntkey"
  62. }
  63. __ini_grepsec() {
  64. #
  65. # Read one INI section
  66. #
  67. local wnt=$1 # wanted section name
  68. local ok=false # are we in the section?
  69. local line # each input line
  70. grep '.' \
  71. | grep -v '\s*#' \
  72. | while read -r line; do
  73. case "$line" in
  74. \[$wnt\]) ok=true; continue ;;
  75. \[*\]) ok=false; continue ;;
  76. esac
  77. $ok || continue
  78. printf -- "%s\n" "$line"
  79. done \
  80. | sed -e 's/ *= */=/; s/ +$//; s/^//;'
  81. }
  82. __ini_lskeys() {
  83. #
  84. # List keys from a section
  85. #
  86. local sct=$1 # section of interest
  87. __ini_grepsec "$sct" | cut -d= -f1 | awk '!x[$0]++'
  88. }
  89. __ini_lssect() {
  90. #
  91. # List all section names
  92. #
  93. local arg=$1 # unused argument
  94. grep -x '\[.*\]' | sed 's/^.//; s/.$//'
  95. }
  96. __ini_maybe_expand() {
  97. #
  98. # Decide whether or not to expand
  99. #
  100. if test "$MKIT_INI_EXPAND" -gt 0; then
  101. MKIT_INI_EXPAND=$(( --MKIT_INI_EXPAND )) __ini_expand
  102. else
  103. __ini_cat
  104. fi
  105. }
  106. ini() {
  107. #
  108. # do ini operation
  109. #
  110. local op=$1 # operator
  111. local arg=$2 # argument
  112. local fn # internal function implementing $op
  113. local limit=__ini_cat # limiting internal function
  114. case $op in
  115. lskeys) fn=__ini_lskeys ;;
  116. lssect) fn=__ini_lssect ;;
  117. sec) fn=__ini_grepsec ;;
  118. values) fn=__ini_greppath ;;
  119. 1value) fn=__ini_greppath; limit="tail -1" ;;
  120. *) die "incorrect use of \`ini()\`"
  121. esac
  122. <"$MKIT_INI" $fn "$arg" | $limit
  123. }
  124. update_version() {
  125. #
  126. # Change project:version in mkit.ini at path $2 to value $1
  127. #
  128. local version=$1 # new version
  129. local inifile=$2 # mkit.ini path
  130. local tmp # mkit.ini cache
  131. tmp=$(mktemp -t mkit.update_version.XXXXXXXX)
  132. <"$inifile" perl -e '
  133. my $hit = 0;
  134. my $done = 0;
  135. foreach (<STDIN>) {
  136. if ($done) { print; next; }
  137. elsif (m/\[project\]/) { $hit++; print; next; }
  138. elsif (m/\[/) { $hit = 0; print; next; }
  139. elsif ($hit) { s/\bversion\b( *)=( *).*/version$1=$2$ARGV[0]/ and $done++; print; }
  140. else { print; next; }
  141. }
  142. ' "$version" >"$tmp" || die "failed to update version in mkit.ini"
  143. mv "$tmp" "$inifile"
  144. }