tool 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/bin/bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. TOOL_ACTIONS_GENERIC="usage update execute"
  17. TOOL_ACTIONS_HELPERS="arguments"
  18. TOOL_FUNCTIONS=$( for action in $TOOL_ACTIONS_GENERIC ; do echo "$action" "$action""_check" ; done ; echo "$TOOL_ACTIONS_HELPERS" )
  19. tool_include() {
  20. local tool=$1
  21. local tool_path=$( tool_path "$tool" )
  22. unset -f $TOOL_FUNCTIONS
  23. . "$tool_path/$tool"
  24. tool_helper_include "$tool"
  25. }
  26. tool_helper_include() {
  27. local tool=$1
  28. local tool_path=$( tool_path "$tool" )
  29. local include="$tool_path/$tool-helper"
  30. if [ -f "$include" ]
  31. then
  32. . "$include"
  33. fi
  34. }
  35. tool_check() {
  36. local tool="${1##*/}"
  37. local tool_path="$(tool_path "${tool}")"
  38. if ! [[ -f "${tool_path}/${tool}" ]]; then
  39. return 1
  40. fi
  41. }
  42. tool_function_check() {
  43. local tool=$1
  44. local function=$2
  45. tool_include "$tool"
  46. if ! function_check "$function"
  47. then
  48. return 1
  49. fi
  50. return 0
  51. }
  52. tool_action() {
  53. local action=$1
  54. shift
  55. local tool=$1
  56. shift
  57. local arguments=$@
  58. (
  59. set +e
  60. if ! tool_check "$tool"
  61. then
  62. printf "Tool $tool check failed\n" >&2
  63. return 1
  64. fi
  65. tool_action_check "$action" "$tool" "$@"
  66. if [ $? -eq 0 ]
  67. then
  68. return 0
  69. fi
  70. tool_include "$tool"
  71. if ! function_check "$action"
  72. then
  73. return 0
  74. fi
  75. printf "Tool $tool $action (with ${arguments:-no argument})\n" >&2
  76. (
  77. set -e
  78. "$action" "$@"
  79. )
  80. if [ $? -ne 0 ]
  81. then
  82. printf "\nTool $tool $action (with ${arguments:-no argument}) failed\n" >&2
  83. return 1
  84. else
  85. printf "\nTool $tool $action (with ${arguments:-no argument}) completed\n" >&2
  86. fi
  87. )
  88. }
  89. tool_action_check() {
  90. local action=$1
  91. shift
  92. local tool=$1
  93. shift
  94. (
  95. set +e
  96. if ! tool_check "$tool"
  97. then
  98. printf "Tool $tool check failed\n" >&2
  99. return 1
  100. fi
  101. tool_include "$tool"
  102. if ! function_check "$action""_check"
  103. then
  104. return 1
  105. fi
  106. for tool_force in $TOOLS_FORCE
  107. do
  108. if [ "$tool_force" = "$tool" ]
  109. then
  110. return 1
  111. fi
  112. done
  113. (
  114. set -e
  115. "$action""_check" "$@"
  116. )
  117. )
  118. }
  119. tool_action_helper() {
  120. local helper=$1
  121. shift
  122. local tool=$1
  123. shift
  124. (
  125. set +e
  126. if ! tool_check "$tool"
  127. then
  128. printf "Tool $tool check failed\n" >&2
  129. return 1
  130. fi
  131. tool_include "$tool"
  132. if ! function_check "$helper"
  133. then
  134. return 0
  135. fi
  136. (
  137. set -e
  138. "$helper" "$@"
  139. )
  140. )
  141. }
  142. tool_action_arguments_recursive() {
  143. local action=$1
  144. shift
  145. local tool=$1
  146. shift
  147. local action_helper_arguments=$( tool_action_helper "arguments" "$tool" "$@" )
  148. local argument
  149. if [ $? -ne 0 ] || [ -z "$action_helper_arguments" ]
  150. then
  151. tool_action "$action" "$tool" "$@"
  152. else
  153. echo "$action_helper_arguments" | while read argument
  154. do
  155. tool_action_arguments_recursive "$action" "$tool" "$@" "$argument"
  156. done
  157. fi
  158. }
  159. tool_path() {
  160. local tool=$1
  161. local tool_path="$root/$TOOLS/$tool"
  162. echo "$tool_path"
  163. }
  164. tool_sources_path() {
  165. local tool=$1
  166. shift
  167. local path="$root/$TOOLS/$tool/$SOURCES"
  168. local argument
  169. for argument in "" "$@"
  170. do
  171. if ! [ -z "$argument" ]
  172. then
  173. path="$path/$argument"
  174. fi
  175. if directory_filled_check "$path"
  176. then
  177. echo "$path"
  178. return
  179. fi
  180. done
  181. }
  182. tool_usage_actions() {
  183. local tool=$1
  184. shift
  185. printf "\nGeneric actions:\n"
  186. for action in $TOOL_ACTIONS_GENERIC
  187. do
  188. if function_check "$action"
  189. then
  190. printf " $action\n"
  191. fi
  192. done
  193. if [ $# -gt 0 ]
  194. then
  195. printf "\nSpecific actions:\n"
  196. for action in "$@"
  197. do
  198. printf " $action\n"
  199. done
  200. fi
  201. }
  202. tool_usage_arguments() {
  203. local tool=$1
  204. shift
  205. printf "\nArguments:\n"
  206. tool_usage_arguments_recursive "$tool" " " "$@"
  207. }
  208. tool_usage_arguments_recursive() {
  209. local tool=$1
  210. shift
  211. local spacing=$1
  212. shift
  213. local action_helper_arguments=$( tool_action_helper "arguments" "$tool" "$@" )
  214. local argument
  215. if ! [ -z "$action_helper_arguments" ]
  216. then
  217. echo "$action_helper_arguments" | while read argument
  218. do
  219. printf "$spacing$argument\n"
  220. tool_usage_arguments_recursive "$tool" " $spacing" "$@" "$argument"
  221. done
  222. fi
  223. }
  224. tool_file_path() {
  225. local tool=$1
  226. shift
  227. local directory=$1
  228. shift
  229. local file=$1
  230. shift
  231. local tool_path=$( tool_path "$tool" )
  232. local path="$tool_path/$directory"
  233. local argument
  234. local file_path
  235. for argument in "" "$@"
  236. do
  237. if ! [ -z "$argument" ]
  238. then
  239. path="$path/$argument"
  240. fi
  241. if ! [ -f "$path/$file" ]
  242. then
  243. continue
  244. fi
  245. file_path="$path/$file"
  246. done
  247. if [ -z "$file_path" ]
  248. then
  249. return 1
  250. fi
  251. echo "$file_path"
  252. }
  253. tool_file_test() {
  254. local file_path=$( tool_file_path "$@" )
  255. test -f "$file_path"
  256. }
  257. tool_file_contents() {
  258. local file_path=$( tool_file_path "$@" )
  259. if [ -f "$file_path" ]
  260. then
  261. cat "$file_path"
  262. fi
  263. }
  264. tool_file_contents_herit() {
  265. local tool=$1
  266. shift
  267. local directory=$1
  268. shift
  269. local file=$1
  270. shift
  271. local tool_path=$( tool_path "$tool" )
  272. local path="$tool_path/$directory"
  273. local argument
  274. local file_path
  275. for argument in "" "$@"
  276. do
  277. if ! [ -z "$argument" ]
  278. then
  279. path="$path/$argument"
  280. fi
  281. file_path="$path/$file"
  282. if ! [ -f "$file_path" ]
  283. then
  284. continue
  285. fi
  286. cat "$file_path"
  287. done
  288. }