post_deps.bash 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. #------------------------------------------------------------------------------
  3. # Searches a directory passed as the first argument for any files containing the
  4. # M4 'include' macro call, then, if called with the '-d' flag, outputs a parsed
  5. # a dictionary in the format:
  6. #
  7. # file1:dependencies
  8. # file2:dependencies
  9. # file3:dependencies
  10. #
  11. # Or outputs only the files without any dependencies.
  12. #
  13. # Both of these outputs should then be parsed by Make to generate dynamic
  14. # recipes.
  15. #------------------------------------------------------------------------------
  16. # TODO: Implement proper argument handling
  17. print_with_deps_only=0
  18. [[ "$1" == '-d' ]] && print_with_deps_only=1 && shift
  19. postdir="$1"
  20. key=''
  21. declare -A posts_to_deps
  22. posts_with_deps="$(\
  23. grep 'include\(.*\)' -r $postdir | \
  24. sed -E -e 's/include\(//' -e 's/\)//' \
  25. )"
  26. # Get posts with dependencies
  27. for toparse in $posts_with_deps; do
  28. IFS=:
  29. for entry in $toparse ; do
  30. if [[ "$entry" == *post.* ]] ; then
  31. [[ "$entry" == "$key" ]] && continue
  32. key=$entry
  33. else
  34. [[ -n ${posts_to_deps["$key"]} ]] && entry=":$entry"
  35. posts_to_deps["$key"]+="$entry"
  36. fi
  37. done
  38. unset IFS
  39. done
  40. posts=''
  41. for post in "${!posts_to_deps[@]}" ; do
  42. ((print_with_deps_only)) && echo "$post:${posts_to_deps[$post]}"
  43. posts+="$post\n"
  44. done
  45. if ! ((print_with_deps_only)) ; then
  46. all_posts=$(find . -wholename './parts' -prune -o -name '*.post.html' -print)
  47. sort <<< $(printf "$all_posts\n$posts") | uniq -u
  48. fi