Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Last active December 7, 2018 13:31
Show Gist options
  • Save oldratlee/6d02a849b8b84a9aba26be7ee65f5560 to your computer and use it in GitHub Desktop.
Save oldratlee/6d02a849b8b84a9aba26be7ee65f5560 to your computer and use it in GitHub Desktop.
Generate comment coverage report for java of maven project, markdown table format
#!/bin/bash
# comment-coverage-report.sh
# generate comment coverage report, markdown table format
#
# NOTE:
# 1. run at project root dir
# 2. install ohcount: https://github.com/blackducksw/ohcount
# if you use mac install by brew: brew install ohcount
set -euo pipefail
readonly PROG="`basename $0`"
readonly run_timestamp="`date "+%Y-%m-%d_%H:%M:%S.%N"`"
readonly uuid="${PROG}_${run_timestamp}_pid$$_r${RANDOM}"
readonly tmp_dir="/tmp/$uuid"
################################################################################
# util functions
################################################################################
# NOTE: $'foo' is the escape sequence syntax of bash
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end
readonly cr=$'\r' # carriage return
# Getting console width using a bash script
# https://unix.stackexchange.com/questions/299067
readonly columns=$(stty size | awk '{print $2}')
printResponsiveMessage() {
if [ ! -t 1 ]; then
return
fi
local message="$*"
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
echo -n "${message:0:columns}"
}
clearResponsiveMessage() {
if [ ! -t 1 ]; then
return
fi
# How to delete line with echo?
# https://unix.stackexchange.com/questions/26576
#
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
# In particular, to clear from the cursor position to the beginning of the line:
# echo -e "\033[1K"
# Or everything on the line, regardless of cursor position:
# echo -e "\033[2K"
echo -n "$ec[2K$cr"
}
################################################################################
# biz logic
################################################################################
cleanupWhenExit() {
rm -rf "$tmp_dir" &> /dev/null
}
trap "cleanupWhenExit" EXIT
preprocessSrcDir() {
local dir="$1"
local tmp_process_dir="$tmp_dir/$(echo "$dir" | sed 's#[./]#__#g')_r$RANDOM"
mkdir -p "$tmp_process_dir"
if [ -d "$dir" ]; then
cp -r "$dir" "$tmp_process_dir"
# remove copyright comment at file beginning
#
# excluding first and last lines from sed /START/,/END/ - Stack Overflow
# https://stackoverflow.com/questions/1187354
# 4.24. How do I address all the lines between RE1 and RE2, excluding the lines themselves?
# http://sed.sourceforge.net/sedfaq4.html#s4.24
find "$tmp_process_dir" -iname \*.java -print0 |
xargs -0 -r -n64 sed -ri '
0, /^\s*package\s/ {
/^\s*package\s/b # keep end line(package line)
d
}
'
fi
echo "$tmp_process_dir"
}
getCommentCoverage() {
local dir="$1"
dir="$(preprocessSrcDir "$dir")"
local comment_coverage="$(ohcount "$dir" | awk '/^java /{printf "%s (%s/%s)", $5, $4, $3}')"
echo "${comment_coverage:--}"
}
listModulePoms() {
find . -name pom.xml
}
getCommentCoverageOfModule() {
local pom
local i=1
while read pom ; do
local module_dir="$(dirname "$pom")"
local module_name="$(basename "$module_dir")"
local module_product_code_dir="$module_dir/src/main/java"
local module_test_code_dir="$module_dir/src/test/java"
# no product code dir, skip
[ ! -d "$module_product_code_dir" ] && continue
printResponsiveMessage "prcocessing module $((i++)): $module_name at $module_dir" > /dev/stderr
# output comment coverage table line of module
echo "$module_name | $(getCommentCoverage "$module_product_code_dir") | $(getCommentCoverage "$module_test_code_dir")"
clearResponsiveMessage > /dev/stderr
done
}
prependNumColumn() {
cat -n | sed -r 's%^\s*([0-9]+)\s*%\1 | %'
}
genCommentCoverageReport() {
# output table header
echo "NO. | module | product code | test code"
echo "--- | ------ | ------------ | ---------"
# output table body
listModulePoms | getCommentCoverageOfModule | sort '-t|' -n -k2 | prependNumColumn
}
genCommentCoverageReport
@oldratlee
Copy link
Author

oldratlee commented Dec 5, 2018

comment coverage report for apache/dubbo@1f751a6 :

NO. module product code test code
1 dubbo-demo-api 0.0% (0/4) -
2 dubbo-serialization-fastjson 0.0% (0/182) -
3 dubbo-serialization-fst 0.0% (0/196) -
4 dubbo-serialization-protostuff 1.0% (3/311) -
5 dubbo-rpc-http 1.6% (3/186) 4.3% (9/202)
6 dubbo-registry-redis 1.7% (10/574) 0.0% (0/81)
7 dubbo-rpc-redis 1.9% (3/152) 1.7% (3/177)
8 dubbo-remoting-zookeeper 2.4% (12/489) 0.0% (0/269)
9 dubbo-rpc-webservice 2.4% (3/123) 12.4% (16/113)
10 dubbo-registry-zookeeper 2.9% (8/270) 3.3% (4/118)
11 dubbo-serialization-jdk 3.0% (12/385) -
12 dubbo-rpc-memcached 3.1% (3/93) 0.0% (0/3)
13 dubbo-rpc-hessian 3.3% (9/261) 3.8% (9/228)
14 dubbo-serialization-hessian2 3.6% (6/161) -
15 dubbo-registry-multicast 3.8% (15/384) 23.0% (44/147)
16 dubbo-remoting-mina 4.0% (23/555) 19.5% (34/140)
17 dubbo-container-log4j 4.1% (3/70) 20.0% (3/12)
18 dubbo-monitor-default 4.5% (17/357) 1.9% (6/317)
19 dubbo-remoting-netty 4.9% (52/1016) 10.9% (68/558)
20 dubbo-remoting-grizzly 5.0% (27/512) -
21 dubbo-rpc-dubbo 5.9% (173/2740) 5.3% (111/1996)
22 dubbo-rpc-rest 7.2% (58/753) 3.5% (11/303)
23 dubbo-serialization-kryo 7.3% (34/432) -
24 dubbo-rpc-thrift 7.4% (93/1162) 8.2% (677/7612)
25 dubbo-container-spring 7.7% (3/36) 17.6% (3/14)
26 dubbo-filter-validation 8.5% (33/356) 0.0% (0/163)
27 dubbo-rpc-injvm 9.9% (15/136) 5.9% (12/192)
28 dubbo-compatible 10.0% (245/2214) 0.7% (11/1575)
29 dubbo-rpc-rmi 10.4% (13/112) 33.3% (80/160)
30 dubbo-registry-default 11.3% (23/180) 10.1% (172/1538)
31 dubbo-remoting-p2p 11.8% (117/872) 0.0% (0/155)
32 dubbo-remoting-netty4 12.1% (168/1223) 7.4% (20/250)
33 dubbo-container-api 13.2% (12/79) -
34 dubbo-cluster 13.8% (408/2544) 7.5% (278/3421)
35 dubbo-registry-api 14.8% (350/2010) 13.9% (129/796)
36 dubbo-config-api 16.1% (762/3984) 4.8% (212/4167)
37 dubbo-filter-cache 16.4% (96/490) 0.0% (0/202)
38 dubbo-qos 17.0% (289/1412) 0.0% (0/728)
39 dubbo-common 18.5% (3414/15045) 1.8% (159/8846)
40 dubbo-monitor-api 18.8% (52/225) 2.8% (6/208)
41 dubbo-container-logback 19.2% (14/59) 13.0% (3/20)
42 dubbo-demo-provider 19.2% (5/21) -
43 dubbo-config-spring 19.4% (683/2843) 7.5% (238/2945)
44 dubbo-remoting-http 19.8% (79/319) 0.0% (0/60)
45 dubbo-remoting-api 20.1% (1545/6148) 6.0% (255/4016)
46 dubbo-test-spring3 20.2% (20/79) -
47 dubbo-demo-consumer 20.8% (5/19) -
48 dubbo-rpc-api 20.9% (1036/3924) 4.6% (76/1591)
49 dubbo-metrics-api 39.9% (405/609) 0.0% (0/129)
50 dubbo-serialization-api 69.5% (194/85) -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment