You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

165 rivejä
3.7 KiB

  1. #!/bin/bash
  2. ScriptFile=$(readlink -f "${BASH_SOURCE[0]}")
  3. ScriptDir=$(dirname "${ScriptFile}")
  4. SearchDir="${ScriptDir}"
  5. FileExts="h;in;hpp;inl;c;cpp;make;cmake;txt;json;js;jsx;ts;tsx;html;tpl;css"
  6. Verbose=0
  7. function Log()
  8. {
  9. printf "$@\n"
  10. }
  11. function Error()
  12. {
  13. >&2 printf "$@\n"
  14. }
  15. function Panic()
  16. {
  17. Error "$@"
  18. exit 1
  19. }
  20. function PrintHelp()
  21. {
  22. printf "
  23. This is a tool to calculate the number of code lines within a certain folder.
  24. The tool looks for a 'CMakeLists.txt' or a 'Makefile' in the subfolders.
  25. If such a file exists, the folder is treated as subproject.
  26. Subprojects have their own counters.
  27. Parameters:
  28. -d|--dir <directory>
  29. Directory to search in
  30. (default ${SearchDir}).
  31. -e|--ext <file extension>
  32. File extension to look for
  33. (default: ${FileExts})
  34. -v|--verbose
  35. Verbose output (Print lines of code for each file extension)
  36. (default: false)
  37. -?|-h|--help
  38. Print this help.
  39. "
  40. }
  41. # Parse arguments
  42. while [ $# -gt 0 ]; do
  43. case $1 in
  44. "-d" | "--dir" )
  45. if [ $# -lt 2 ]; then
  46. Panic "Parameter $1 expects exactly one parameter!"
  47. fi
  48. SearchDir=$(readlink -m "$2")
  49. shift
  50. ;;
  51. "-e" | "--ext" )
  52. if [ $# -lt 2 ]; then
  53. Panic "Parameter $1 expects exactly one parameter!"
  54. fi
  55. FileExts="$2"
  56. shift
  57. ;;
  58. "-v" | "--verbose" )
  59. Verbose=1
  60. ;;
  61. "-h" | "-?" | "--help" )
  62. PrintHelp
  63. exit 0
  64. ;;
  65. * )
  66. Panic "Invalid or unknown parameter: $1"
  67. ;;
  68. esac
  69. shift
  70. done
  71. OLD_IFS=${IFS}
  72. IFS=';'
  73. read -ra Exts <<< "${FileExts}"
  74. IFS=${OLD_IFS}
  75. Projects="$(find ${SearchDir} -type d -exec sh -c 'test -f "$1/CMakeLists.txt" || test -f "$1/Makefile"' -- {} \; -print -prune)"
  76. First=1
  77. declare -A ExtLines
  78. TotalLines=0
  79. echo "======================================="
  80. echo "= Lines of Code ="
  81. echo "======================================="
  82. for Project in ${Projects[@]}; do
  83. Name=${Project#"$SearchDir"}
  84. if [ "$Verbose" -eq 1 ]; then
  85. if [ "$First" -eq 1 ]; then
  86. echo "_______________________________________"
  87. fi
  88. printf "%-30s\n" "${Name:-root}:"
  89. echo " \`+-----------------------------------"
  90. fi
  91. ProjectLines=0
  92. for ext in "${Exts[@]}"; do
  93. Lines=$(find ${Project} \
  94. -type f \
  95. -iname *.${ext} \
  96. 2>/dev/null \
  97. | xargs cat 2>/dev/null \
  98. | wc -l)
  99. if [ "$Verbose" -eq 1 ] && [ "$Lines" -gt 0 ]; then
  100. printf " |%-26s %8d\n" "${ext}" ${Lines}
  101. fi
  102. if [ "$First" -eq 1 ]; then
  103. ExtLines[$ext]=$Lines
  104. else
  105. let ExtLines[$ext]=${ExtLines[$ext]}+${Lines}
  106. fi
  107. let ProjectLines=${ProjectLines}+${Lines}
  108. done
  109. if [ "$Verbose" -eq 1 ]; then
  110. echo " +-----------------------------------"
  111. printf " |%-26s %8d\n" "total:" ${ProjectLines}
  112. else
  113. printf "%-30s %8d\n" "${Name:-root}:" ${ProjectLines}
  114. fi
  115. First=0
  116. let TotalLines=${TotalLines}+${ProjectLines}
  117. done
  118. echo "======================================="
  119. if [ "$Verbose" -eq 1 ]; then
  120. printf "%-30s\n" "total:"
  121. echo " \`+-----------------------------------"
  122. for ext in "${Exts[@]}"; do
  123. if [ "${ExtLines[$ext]}" -gt 0 ]; then
  124. printf " |%-26s %8d\n" "${ext}" ${ExtLines[$ext]}
  125. fi
  126. done
  127. echo " +-----------------------------------"
  128. printf " |%-26s %8d\n" "total:" ${TotalLines}
  129. else
  130. printf "%-30s %8d\n" "total:" ${TotalLines}
  131. fi
  132. echo "======================================="