-
Notifications
You must be signed in to change notification settings - Fork 29
/
cloc.sh
executable file
·85 lines (71 loc) · 3.9 KB
/
cloc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# ------------------------------------------------------------------------------------------------ #
# This file is part of CosmoScout VR #
# ------------------------------------------------------------------------------------------------ #
# SPDX-FileCopyrightText: German Aerospace Center (DLR) <[email protected]>
# SPDX-License-Identifier: MIT
# This scripts counts the lines of code and comments in the src/ and plugins/ directories.
# The copyright-headers are substracted. It uses the commandline tool "cloc".
# All dumb comments like those /////////// or those // ------------ are also substracted.
# You can pass the --percentage-only flag to show only the percentage of code comments.
# Get the location of this script.
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
function countLines() {
# Run cloc - this counts code lines, blank lines and comment lines for the specified languages.
# We are only interested in the summary, therefore the tail -1
SUMMARY="$(cloc "$1" --fullpath --not-match-f="third-party" --include-lang="C++,C/C++ Header,GLSL,JavaScript" --md | tail -1)"
# The $SUMMARY is one line of a markdown table and looks like this:
# SUM:|101|3123|2238|10783
# We use the following command to split it into an array.
IFS='|' read -r -a TOKENS <<< "$SUMMARY"
# Store the individual tokens for better readability.
NUMBER_OF_FILES=${TOKENS[1]}
COMMENT_LINES=${TOKENS[3]}
LINES_OF_CODE=${TOKENS[4]}
# To make the estimate of commented lines more accurate, we have to substract the copyright header
# which is included in each file. This header has the length of five lines.
# All dumb comments like those /////////// or those // ------------ are also substracted. As cloc
# does not count inline comments, the overall estimate should be rather conservative.
DUMB_COMMENTS="$(grep -r -E '//////|// -----' "$1" | wc -l)"
COMMENT_LINES=$(($COMMENT_LINES - 3 * $NUMBER_OF_FILES - $DUMB_COMMENTS))
# Return the two values.
eval "$2=$LINES_OF_CODE"
eval "$3=$COMMENT_LINES"
}
# First count the source lines and comment lines in the src/ directory.
SOURCE_LINES_OF_CODE=""
SOURCE_LINES_OF_COMMENTS=""
countLines "${SCRIPT_DIR}/../src" SOURCE_LINES_OF_CODE SOURCE_LINES_OF_COMMENTS
# Then in the resources/gui/js directory.
JS_LINES_OF_CODE=""
JS_LINES_OF_COMMENTS=""
countLines "${SCRIPT_DIR}/../resources/gui/js" JS_LINES_OF_CODE JS_LINES_OF_COMMENTS
# Then in the resources/shaders directory.
GLSL_LINES_OF_CODE=""
GLSL_LINES_OF_COMMENTS=""
countLines "${SCRIPT_DIR}/../resources/shaders" GLSL_LINES_OF_CODE GLSL_LINES_OF_COMMENTS
# Then in the plugins/ directory.
PLUGINS_LINES_OF_CODE=""
PLUGINS_LINES_OF_COMMENTS=""
countLines "${SCRIPT_DIR}/../plugins" PLUGINS_LINES_OF_CODE PLUGINS_LINES_OF_COMMENTS
# Print results.
if [[ $* == *--percentage-only* ]]
then
awk -v a=$SOURCE_LINES_OF_COMMENTS -v b=$PLUGINS_LINES_OF_COMMENTS \
-v c=$JS_LINES_OF_COMMENTS -v d=$GLSL_LINES_OF_COMMENTS \
-v e=$SOURCE_LINES_OF_CODE -v f=$PLUGINS_LINES_OF_CODE \
-v g=$GLSL_LINES_OF_CODE -v h=$JS_LINES_OF_CODE \
'BEGIN {printf "%3.4f\n", 100*(a+b+c+d)/(a+b+c+d+e+f+g+h)}'
else
awk -v a=$SOURCE_LINES_OF_CODE -v b=$JS_LINES_OF_CODE -v c=$GLSL_LINES_OF_CODE\
'BEGIN {printf "Lines of source code: %6.1fk\n", (a+b+c)/1000}'
awk -v a=$PLUGINS_LINES_OF_CODE \
'BEGIN {printf "Lines of plugin code: %6.1fk\n", a/1000}'
awk -v a=$SOURCE_LINES_OF_COMMENTS -v b=$PLUGINS_LINES_OF_COMMENTS -v c=$JS_LINES_OF_COMMENTS \
'BEGIN {printf "Lines of comments: %6.1fk\n", (a+b+c)/1000}'
awk -v a=$SOURCE_LINES_OF_COMMENTS -v b=$PLUGINS_LINES_OF_COMMENTS \
-v c=$JS_LINES_OF_COMMENTS -v d=$GLSL_LINES_OF_COMMENTS \
-v e=$SOURCE_LINES_OF_CODE -v f=$PLUGINS_LINES_OF_CODE \
-v g=$GLSL_LINES_OF_CODE -v h=$JS_LINES_OF_CODE \
'BEGIN {printf "Comment Percentage: %3.4f\n", 100*(a+b+c+d)/(a+b+c+d+e+f+g+h)}'
fi