-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-sloc
More file actions
executable file
·245 lines (189 loc) · 9.07 KB
/
script-sloc
File metadata and controls
executable file
·245 lines (189 loc) · 9.07 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env bash
#
# Report SLOC (source lines of code) of one or more shell scripts.
#
# Accepts one or more script files as parameters.
# A script is considered valid if the first line begins
# with exactly the characters #!/
#
# The shebang is not included in calculations.
#
# NOTE Multi-line herestrings are currently not supported and will be
# categorized according to the content similar to any other normal
# line (unlike heredocs). This means a comment in a multi-line herestring
# will be counted as a comment line and empty lines will be counted
# towards the empty line counter.
#
# When iDEBUG is set to 1, .sloc* files will be created in the same path
# as the input file(s). Each .sloc* file will contain a copy of lines
# from the source file based on what the script detected the line to be.
#
# The output file format is:
# [source_line]line_content
#
# Examples:
# *.sloc (valid code including heredoc contents)
# *.sloc.empty (empty lines that are not inside a heredoc)
# *.sloc.heredoc (heredoc declaration, contents, and end delimiter)
# *.sloc.only_comments (lines that contain only comments and are not part of heredoc contents)
# *.sloc.inline_comments (lines that contain comments preceded by non-whitespace characters that are also not part of heredoc contents)
iDEBUG=0
# When iINCLUDE_SCRIPT is set to 1, the script itself can be included for processing (eg. ./script-sloc *)
iINCLUDE_SCRIPT=0
# Requires at least one parameter, the input file
[[ -z $1 || ! -f $1 || ! -r $1 ]] && {
echo "$0 <script>"
exit 1
}
# Requires bc to calculate SLOC/comment and comment/SLOC ratios
if ! command -v bc >/dev/null
then
echo "bc is required for ratio calculations"
fi
# Support multiple file inputs via shift'ing positional parameters as needed (eg. ./script-sloc *)
while [[ -n $1 ]]
do
# If not a file, skip
[[ ! -f $1 ]] && { shift; continue; }
# Reset variables for each file
line=''
total_lines=0
comment_inline_lines=0
comment_standalone_lines=0
empty_lines=0
sloc_total_lines=0
heredoc=0
eohd=''
# If iINCLUDE_SCRIPT is set to 0 and the current file is the script, skip this file
[[ $iINCLUDE_SCRIPT -eq 0 && $1 -ef "$0" ]] && { shift; continue; }
# If debug is enabled, remove existing output files for the current file
[[ $iDEBUG -eq 1 ]] && {
for slocfile in "$1.sloc"*
do
[[ -f $slocfile ]] && rm -v "$slocfile"
done
}
# Begin reading the current file, one line at a time
while IFS= read -r line
do
# Increment the total lines counter
total_lines=$(( total_lines + 1 ))
# If the first line doesn't begin with exactly #!/ then skip this file
if [[ $total_lines -eq 1 ]]
then
if [[ $line != '#!/'* ]]
then
echo "$1: Shebang not found"
shift
break 1
else
# Don't count the shebang as a comment line
continue
fi
fi
# If the current line is not empty and it is equal to the string indicating the end of a heredoc, stop ignoring lines
[[ -n $line && $line = "$eohd" ]] && {
# "Boolean" flag used to ignore contents of heredocs
heredoc=0
# If debug is enabled, log lines considered to be part of a heredoc
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc.heredoc
}
# If we're not inside a heredoc
if [[ $heredoc -eq 0 ]]
then
[[ -z $line ]] && {
# Increment the empty line counter
empty_lines=$(( empty_lines + 1 ))
# If debug is enabled, log lines considered to be empty
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc.empty
# Don't count this line as anything else
continue
}
# Comment only lines are lines that begin with # and also lines that have only space and/or tab preceding #
[[ $line =~ ^[[:blank:]]*#+ ]] && {
# Increment the comment only counter
comment_standalone_lines=$(( comment_standalone_lines + 1 ))
# If debug is enabled, log lines considered containing only a comment/comments
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc.only_comments
# Don't count this line as anything else
continue
}
# Detect lines with comments and code (inline comments) that are not preceded by only space(s) and/or tab(s)
[[ $line =~ [[:print:]]+[^[:blank:]][[:blank:]]+#.*$ ]] && {
# Increment the inline comment counter
comment_inline_lines=$(( comment_inline_lines + 1 ))
# If debug is enabled, log lines considered to contain inline comments
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc.inline_comments
}
# If we are inside a heredoc
elif [[ $heredoc -eq 1 ]]
then
# If debug is enabled, log lines considered to be part of a heredoc
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc.heredoc
fi
# NOTE Match valid herestrings
#.*[[:blank:]]+<<<[[:blank:]]*
# Detect heredocs and then get the end delimeter
# NOTE A variable is used to store the heredoc regex to avoid unecessary
# complexities and potential issues with escaping characters, namely <
heredoc_regex='^[[:blank:]]*.*[[:blank:]]+<<[^<].*$'
[[ $line =~ $heredoc_regex ]] && {
# If debug is enabled, log lines considered to be part of a heredoc
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc.heredoc
# "Boolean" flag used to ignore contents of heredocs
heredoc=1
# Anchored left, remove *<< (remove the command and the redirect operator)
eohd=${line#*<<}
# If the first character is " (part of a quoted heredoc)
[[ ${eohd:0:1} = "\"" ]] && {
# Remove the leading "
eohd=${eohd:1:${#eohd}}
# Anchored right, remove all instances of "*
eohd=${eohd%%\"*}
# End of heredoc delimiter remains
}
# Anchored right remove >* (part of unquoted heredoc)
eohd=${eohd%>*}
# If the last character is >, remove it
[[ ${eohd:$(( ${#eohd} - 1 )):1} = '>' ]] && eohd=${eohd:0:$(( ${#eohd} - 1 ))}
# Remove any remaining [[:blank:]] (space and tab)
eohd=${eohd//[[:blank:]]}
# Remove any potential comments
eohd=${eohd%%#*}
# End of heredoc delimiter remains
}
# Increment the sloc lines counter
sloc_total_lines=$(( sloc_total_lines + 1 ))
# If debug is enabled, log lines considered to be LOC
[[ $iDEBUG -eq 1 ]] && printf '%s\n' "[$total_lines]$line" >> "$1".sloc
done < "$1"
# This particular script can't process one line scripts
if [[ $total_lines -gt 1 ]]
then
# Exclude the shebang line from calculations
total_lines=$(( total_lines - 1 ))
# Calculate
sloc_total_lines_percent=$(( ( sloc_total_lines * 100 ) / total_lines ))
comment_total_lines=$(( comment_standalone_lines + comment_inline_lines ))
comment_total_lines_percent=$(( ( comment_total_lines * 100 ) / total_lines ))
comment_standalone_lines_percent=$(( ( comment_standalone_lines * 100 ) / comment_total_lines ))
#comment_standalone_lines_percent=$(bc -l <<< "scale=2; ( $comment_standalone_lines * 100 ) / $comment_total_lines")
comment_inline_lines_percent=$(( ( comment_inline_lines * 100 ) / comment_total_lines ))
#comment_inline_lines_percent=$(bc -l <<< "scale=2; ( $comment_inline_lines * 100 ) / $comment_total_lines")
empty_lines_percent=$(( ( empty_lines * 100 ) / total_lines ))
#sloc_to_comment_ratio=$(( sloc_total_lines / comment_total_lines ))
sloc_to_comment_ratio=$(bc -l <<< "scale=3; $sloc_total_lines / $comment_total_lines")
comment_to_sloc_ratio=$(bc -l <<< "scale=3; $comment_total_lines / $sloc_total_lines")
# Report
printf '%s%4.2f%s' "$1: $sloc_total_lines/$total_lines SLOC ($sloc_total_lines_percent%) [SLOC/Comment:" "$sloc_to_comment_ratio" "], "
printf '%s%4.2f%s' "$comment_total_lines comments ($comment_total_lines_percent%) [S:$comment_standalone_lines_percent%,I:$comment_inline_lines_percent%] [Comment/SLOC:" "$comment_to_sloc_ratio" "], "
printf '%s\n' "$empty_lines empty ($empty_lines_percent%)"
# If the current file has a shebang and also contains zero or one newlines
# this script can't process it
elif [[ $total_lines -lt 1 ]]
then
echo "$1: Unable to process"
fi
# Shift the positional parameters (process the next file, if any)
shift 1
done