tool 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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" ]
  39. then
  40. return 1
  41. fi
  42. return 0
  43. }
  44. tool_function_check() {
  45. local tool=$1
  46. local function=$2
  47. tool_include "$tool"
  48. if ! function_check "$function"
  49. then
  50. return 1
  51. fi
  52. return 0
  53. }
  54. tool_action() {
  55. local action=$1
  56. shift
  57. local tool=$1
  58. shift
  59. local arguments=$@
  60. (
  61. set +e
  62. if ! tool_check "$tool"
  63. then
  64. printf "Tool $tool check failed\n" >&2
  65. return 1
  66. fi
  67. tool_action_check "$action" "$tool" "$@"
  68. if [ $? -eq 0 ]
  69. then
  70. return 0
  71. fi
  72. tool_include "$tool"
  73. if ! function_check "$action"
  74. then
  75. return 0
  76. fi
  77. printf "Tool $tool $action (with ${arguments:-no argument})\n" >&2
  78. (
  79. set -e
  80. "$action" "$@"
  81. )
  82. if [ $? -ne 0 ]
  83. then
  84. printf "\nTool $tool $action (with ${arguments:-no argument}) failed\n" >&2
  85. return 1
  86. else
  87. printf "\nTool $tool $action (with ${arguments:-no argument}) completed\n" >&2
  88. fi
  89. )
  90. }
  91. tool_action_check() {
  92. local action=$1
  93. shift
  94. local tool=$1
  95. shift
  96. (
  97. set +e
  98. if ! tool_check "$tool"
  99. then
  100. printf "Tool $tool check failed\n" >&2
  101. return 1
  102. fi
  103. tool_include "$tool"
  104. if ! function_check "$action""_check"
  105. then
  106. return 1
  107. fi
  108. for tool_force in $TOOLS_FORCE
  109. do
  110. if [ "$tool_force" = "$tool" ]
  111. then
  112. return 1
  113. fi
  114. done
  115. (
  116. set -e
  117. "$action""_check" "$@"
  118. )
  119. )
  120. }
  121. tool_action_helper() {
  122. local helper=$1
  123. shift
  124. local tool=$1
  125. shift
  126. (
  127. set +e
  128. if ! tool_check "$tool"
  129. then
  130. printf "Tool $tool check failed\n" >&2
  131. return 1
  132. fi
  133. tool_include "$tool"
  134. if ! function_check "$helper"
  135. then
  136. return 0
  137. fi
  138. (
  139. set -e
  140. "$helper" "$@"
  141. )
  142. )
  143. }
  144. tool_action_arguments_recursive() {
  145. local action=$1
  146. shift
  147. local tool=$1
  148. shift
  149. local action_helper_arguments=$( tool_action_helper "arguments" "$tool" "$@" )
  150. local argument
  151. if [ $? -ne 0 ] || [ -z "$action_helper_arguments" ]
  152. then
  153. tool_action "$action" "$tool" "$@"
  154. else
  155. echo "$action_helper_arguments" | while read argument
  156. do
  157. tool_action_arguments_recursive "$action" "$tool" "$@" "$argument"
  158. done
  159. fi
  160. }
  161. tool_path() {
  162. local tool=$1
  163. local tool_path="$root/$TOOLS/$tool"
  164. echo "$tool_path"
  165. }
  166. tool_sources_path() {
  167. local tool=$1
  168. shift
  169. local path="$root/$TOOLS/$tool/$SOURCES"
  170. local argument
  171. for argument in "" "$@"
  172. do
  173. if ! [ -z "$argument" ]
  174. then
  175. path="$path/$argument"
  176. fi
  177. if directory_filled_check "$path"
  178. then
  179. echo "$path"
  180. return
  181. fi
  182. done
  183. }
  184. tool_usage_actions() {
  185. local tool=$1
  186. shift
  187. printf "\nGeneric actions:\n"
  188. for action in $TOOL_ACTIONS_GENERIC
  189. do
  190. if function_check "$action"
  191. then
  192. printf " $action\n"
  193. fi
  194. done
  195. if [ $# -gt 0 ]
  196. then
  197. printf "\nSpecific actions:\n"
  198. for action in "$@"
  199. do
  200. printf " $action\n"
  201. done
  202. fi
  203. }
  204. tool_usage_arguments() {
  205. local tool=$1
  206. shift
  207. printf "\nArguments:\n"
  208. tool_usage_arguments_recursive "$tool" " " "$@"
  209. }
  210. tool_usage_arguments_recursive() {
  211. local tool=$1
  212. shift
  213. local spacing=$1
  214. shift
  215. local action_helper_arguments=$( tool_action_helper "arguments" "$tool" "$@" )
  216. local argument
  217. if ! [ -z "$action_helper_arguments" ]
  218. then
  219. echo "$action_helper_arguments" | while read argument
  220. do
  221. printf "$spacing$argument\n"
  222. tool_usage_arguments_recursive "$tool" " $spacing" "$@" "$argument"
  223. done
  224. fi
  225. }
  226. tool_file_path() {
  227. local tool=$1
  228. shift
  229. local directory=$1
  230. shift
  231. local file=$1
  232. shift
  233. local tool_path=$( tool_path "$tool" )
  234. local path="$tool_path/$directory"
  235. local argument
  236. local file_path
  237. for argument in "" "$@"
  238. do
  239. if ! [ -z "$argument" ]
  240. then
  241. path="$path/$argument"
  242. fi
  243. if ! [ -f "$path/$file" ]
  244. then
  245. continue
  246. fi
  247. file_path="$path/$file"
  248. done
  249. if [ -z "$file_path" ]
  250. then
  251. return 1
  252. fi
  253. echo "$file_path"
  254. }
  255. tool_file_test() {
  256. local file_path=$( tool_file_path "$@" )
  257. test -f "$file_path"
  258. }
  259. tool_file_contents() {
  260. local file_path=$( tool_file_path "$@" )
  261. if [ -f "$file_path" ]
  262. then
  263. cat "$file_path"
  264. fi
  265. }
  266. tool_file_contents_herit() {
  267. local tool=$1
  268. shift
  269. local directory=$1
  270. shift
  271. local file=$1
  272. shift
  273. local tool_path=$( tool_path "$tool" )
  274. local path="$tool_path/$directory"
  275. local argument
  276. local file_path
  277. for argument in "" "$@"
  278. do
  279. if ! [ -z "$argument" ]
  280. then
  281. path="$path/$argument"
  282. fi
  283. file_path="$path/$file"
  284. if ! [ -f "$file_path" ]
  285. then
  286. continue
  287. fi
  288. cat "$file_path"
  289. done
  290. }