tool 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/usr/bin/env 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_GENERIC_IGNORE_CHECK=(usage update)
  18. TOOL_ACTIONS_HELPERS=(arguments)
  19. tool_include() {
  20. local tool=$1
  21. local tool_path=$( tool_path "$tool" )
  22. unset -f "${TOOL_ACTIONS[@]}"
  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 '%s\n' "Tool $tool check failed" >&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 '%s\n' "Tool $tool $action (with ${arguments:-no argument})" >&2
  76. (
  77. set -e
  78. "$action" "$@"
  79. )
  80. if [ $? -ne 0 ]
  81. then
  82. printf '\n%s\n' "Tool $tool $action (with ${arguments:-no argument}) failed" >&2
  83. return 1
  84. else
  85. printf '\n%s\n' "Tool $tool $action (with ${arguments:-no argument}) completed" >&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 '%s\n' "Tool $tool check failed" >&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 '%s\n' "Tool $tool check failed" >&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. printf '%s\n' "$action_helper_arguments" | while read argument
  154. do
  155. tool_action_arguments_recursive "$action" "$tool" "$@" "$argument"
  156. done
  157. fi
  158. }
  159. tool_arguments_targets() {
  160. local tool="$1"
  161. shift
  162. local tool_path="$(tool_path "$tool")"
  163. local targets_path="$tool_path/$CONFIGS"
  164. local argument
  165. for argument in "$@"; do
  166. targets_path="$targets_path/$argument"
  167. done
  168. targets_path="$targets_path/$TARGETS"
  169. if [[ -f "$targets_path" ]]; then
  170. cat "$targets_path"
  171. fi
  172. }
  173. tool_path() {
  174. local tool=$1
  175. local tool_path="$root/$TOOLS/$tool"
  176. printf '%s\n' "$tool_path"
  177. }
  178. tool_sources_path() {
  179. local tool=$1
  180. shift
  181. local path="$root/$TOOLS/$tool/$SOURCES"
  182. local argument
  183. for argument in "" "$@"
  184. do
  185. if ! [ -z "$argument" ]
  186. then
  187. path="$path/$argument"
  188. fi
  189. if directory_filled_check "$path"
  190. then
  191. printf '%s\n' "$path"
  192. return
  193. fi
  194. done
  195. }
  196. tool_usage_actions() {
  197. local tool=$1
  198. shift
  199. printf '\n%s\n' 'Generic actions:'
  200. for action in "${TOOL_ACTIONS_GENERIC[@]}"
  201. do
  202. if function_check "$action"
  203. then
  204. printf '%s\n' " $action"
  205. fi
  206. done
  207. if [ $# -gt 0 ]
  208. then
  209. printf '\n%s\n' 'Specific actions:'
  210. for action in "$@"
  211. do
  212. printf '%s\n' " $action"
  213. done
  214. fi
  215. }
  216. tool_usage_arguments() {
  217. local tool=$1
  218. shift
  219. printf '\n%s\n' 'Arguments:'
  220. tool_usage_arguments_recursive "$tool" " " "$@"
  221. }
  222. tool_usage_arguments_recursive() {
  223. local tool=$1
  224. shift
  225. local spacing=$1
  226. shift
  227. local action_helper_arguments=$( tool_action_helper "arguments" "$tool" "$@" )
  228. local argument
  229. if ! [ -z "$action_helper_arguments" ]
  230. then
  231. printf '%s\n' "$action_helper_arguments" | while read argument
  232. do
  233. printf '%s\n' "$spacing$argument"
  234. tool_usage_arguments_recursive "$tool" " $spacing" "$@" "$argument"
  235. done
  236. fi
  237. }
  238. tool_file_path() {
  239. local tool=$1
  240. shift
  241. local directory=$1
  242. shift
  243. local file=$1
  244. shift
  245. local tool_path=$( tool_path "$tool" )
  246. local path="$tool_path/$directory"
  247. local argument
  248. local file_path
  249. for argument in "" "$@"
  250. do
  251. if ! [ -z "$argument" ]
  252. then
  253. path="$path/$argument"
  254. fi
  255. if ! [ -f "$path/$file" ]
  256. then
  257. continue
  258. fi
  259. file_path="$path/$file"
  260. done
  261. if [ -z "$file_path" ]
  262. then
  263. return 1
  264. fi
  265. printf '%s\n' "$file_path"
  266. }
  267. tool_file_test() {
  268. local file_path=$( tool_file_path "$@" )
  269. test -f "$file_path"
  270. }
  271. tool_file_contents() {
  272. local file_path=$( tool_file_path "$@" )
  273. if [ -f "$file_path" ]
  274. then
  275. cat "$file_path"
  276. fi
  277. }
  278. tool_file_contents_herit() {
  279. local tool=$1
  280. shift
  281. local directory=$1
  282. shift
  283. local file=$1
  284. shift
  285. local tool_path=$( tool_path "$tool" )
  286. local path="$tool_path/$directory"
  287. local argument
  288. local file_path
  289. for argument in "" "$@"
  290. do
  291. if ! [ -z "$argument" ]
  292. then
  293. path="$path/$argument"
  294. fi
  295. file_path="$path/$file"
  296. if ! [ -f "$file_path" ]
  297. then
  298. continue
  299. fi
  300. cat "$file_path"
  301. done
  302. }