Skip to content

Instantly share code, notes, and snippets.

@dgolds
Last active October 11, 2015 18:37
Show Gist options
  • Save dgolds/3901863 to your computer and use it in GitHub Desktop.
Save dgolds/3901863 to your computer and use it in GitHub Desktop.
Bash Script to do quick counts of common source code types under the current or specified directory
#!/bin/bash
# Bash Script to do quick counts of common source code types under the current or specified directory
# https://gist.github.com/3901863
# To quickly grab it, just type
# wget https://gist.github.com/raw/3901863/dgSniffCode.sh && chmod +x dgSniffCode.sh
# Features to add:
# multiple params ...A hack until I support * arguments: ls | xargs -L 1 ~/dgSniffCode
# Colors for use with echo -e -- see http://tldp.org/LDP/abs/html/colorizing.html
cBlueHi='\E[37;44m'"\033[1m"
cNormal="\033[0m"
cGreenLo='\E[40;32m'
disableColors()
{
cBlueHi=""
cNormal=""
cGreenLo=""
}
summaryText=''
summaryOnly=0 # 1 if we only want the summary of non-test code
usage ()
{
echo $0:
echo Bash Script to do quick counts of common source code
echo types under the current or specified directory
echo " "
echo Usage:
echo " $0 -help Show this text"
echo " $0 [-[-s] Run the scan in the current directory"
echo " $0 [-s] [directory] Run the scan in the specified directory"
}
addToSummary()
{
# $1 = language, eg "Ruby"
# $2 = count, eg 12121
if [ ! $2 -eq 0 ]
then
printf -v summaryText2 "%s %'d lines of %s\n" "$summaryText" "$2" "$1"
summaryText=$summaryText2
fi
}
printSummary()
{
printf "${cBlueHi} Summary of non-test code lines: $cNormal\n"
sortedSummary=`echo "$summaryText" | sort -n -r`
printf "%s\n" "$sortedSummary"
}
countBadness ()
{
# $1 = suffix being checked
# $2 (option) = exclude files with this line
flag=0
for bad in TODO FIXME XXX '@todo' HACK hack 'sleep'
do
if [ -z $2 ]
then
badcount=`find . -name \*.$1 -exec cat '{}' \; | grep $bad | wc -l`
else
if [ $3 = "include" ]
then
badcount=$(cat $(find . -name \*.$1 -print | grep $2) | grep $bad | wc -l)
else
badcount=$(cat $(find . -name \*.$1 -print | grep -v $2) | grep $bad | wc -l)
fi
fi
if [ ! $badcount -eq 0 ]
then
echo -en " $cGreenLo $badcount $bad $cNormal"
flag=1
fi
done
if [ $flag -eq 1 ]
then
echo " "
fi
}
countLines ()
{
dgFiles=`find . -name \*.$2 -print | wc -l`
if [ ! $dgFiles -eq "0" ]
then
if [ $summaryOnly -eq 0 ]
then
echo -e $cBlueHi $1: $cNormal
fi
# $3 is unit/test/spec indicator, eg /spec/
if [ -z $3 ]
then
# "No spec"
dgAcrossDirs=`find . -name \*.$2 -execdir pwd \; | uniq | wc -l`
dgLines=`find . -name \*.$2 -exec cat '{}' \; | wc -l`
addToSummary $1 $dgLines
if [ $summaryOnly -eq 0 ]
then
printf " %'d dgFiles $1 files across %'d directories\n" $dgFiles $dgAcrossDirs
printf " %'d total lines of $1" $dgLines
printf " (Average lines per file = $(($dgLines/$dgFiles)))\n"
countBadness $2
printf "\n"
fi
else
# "spec" provided
# First the non-spec files...
dgFiles2=`find . -name \*.$2 -print | grep -v $3 | wc -l`
dgAcrossDirs=`find . -name \*.$2 -execdir pwd \; | grep -v $3 | uniq | wc -l`
if [ $dgFiles2 -eq 0 ]
then
dgLines2=0
else
dgLines2=$(cat $(find . -name \*.$2 -print | grep -v $3) | wc -l)
fi
addToSummary $1 $dgLines2
if [ $summaryOnly -eq 0 ]
then
printf " %'d Non-test $1 files across %'d directories\n" $dgFiles2 $dgAcrossDirs
printf " %'d total lines of Non-Test $1" $dgLines2
printf " (Average Lines per file = $(($dgLines2/$dgFiles2)))\n"
countBadness $2 $3 exclude
# Now the spec files
dgAcrossDirs=`find . -name \*.$2 -execdir pwd \; | grep $3 | uniq | wc -l`
dgFiles3=`find . -name \*.$2 -print | grep $3 | wc -l`
printf " + %'d TEST $1 files across $dgAcrossDirs directories\n" $dgFiles3
if [ ! $dgFiles3 -eq "0" ]
then
dgLines3=$(cat $(find . -name \*.$2 -print | grep $3) | wc -l)
printf " + %'d total lines of TEST $1" $dgLines3
printf " (Average Lines per file = $(($dgLines3/$dgFiles3)))\n"
printf " + $(((100*$dgLines3/($dgLines2+dgLines3)))) percent of $1 code is TEST $1\n"
countBadness $2 $3 include
fi
printf "\n"
fi
fi
fi
}
if [ "$1" = '-help' ]
then
usage
exit
fi
if [ "$1" = '-m' ]
then
disableColors
shift
fi
if [ "$1" = '-s' ]
then
summaryOnly=1
shift
fi
if [ ! -z $1 ]
then
pushd $1 > /dev/null
dgNeedToPopd="yes"
fi
echo "=== Quick Code sniff of $(pwd) ==="
tstfilter='/spec/\|/test/\|/tests/\|/Test/\|/Tests/\|/app-test/\|/darwin-test\|-test-'
countLines Ruby rb $tstfilter
countLines Yml yml $tstfilter
countLines eRuby erb $tstfilter
countLines Javascript js $tstfilter
countLines Flex mxml $tstfilter
countLines ActionScript as $tstfilter
countLines Java java $tstfilter
countLines JSP jsp $tstfilter
countLines HTML html $tstfilter
countLines CSS css $tstfilter
countLines Compass\(sCSS\) scss $tstfilter
countLines Scala scala $tstfilter
countLines Jade jade $tstfilter
countLines Shell sh $tstfilter
countLines C c $tstfilter
countLines Python py $tstfilter
countLines Go go _test.go
countLines Xml xml $tstfilter
countLines JSON json $tstfilter
if [ ! -z $dgNeedToPopd ]
then
popd > /dev/null
fi
printSummary
printf "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment