Last active
December 7, 2018 13:31
-
-
Save oldratlee/6d02a849b8b84a9aba26be7ee65f5560 to your computer and use it in GitHub Desktop.
Generate comment coverage report for java of maven project, markdown table format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
comment coverage report for apache/dubbo@1f751a6 :