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