作為編程人員,項目中的代碼行數很有必要。
Windows 下比較成熟的工具較多,而Mac下工具比較少。
這里舉幾個Mac 下統計代碼行數的方法:
自定義腳本:
腳本1:
終端進入目錄,輸入:
$ find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.h" -or -name "*.rss" ")" -print | xargs wc -l
缺點:
需要自定義腳本:不同的編程語言,有不同的文件后綴名,需要自行配置;
不能過濾掉注釋;
不能過濾掉空行
腳本2:
$ find . -name "*.m" -or -name "*.h" -or -name "*.xib" -or -name "*.c" |xargs grep -v "^$"|wc -l
改進:
去掉空行
xargs grep -v "^$"
CLOC:
大家都知道用 wc -l
命令進行代碼行數統計,但是它會將代碼中的注釋、空行所占用的文本行都統計在內。如果想查看一個 tar 包或一個項目目錄中“實際”的代碼行數並且不願意自己去寫一個腳本來做此類工作,那么可以考慮使用 cloc。
cloc 是一個 perl 腳本,它可以統計很多種編程語言的代碼文件中的空行、注釋以及實際的代 碼行數。
CLOC是Count Lines of Code的意思,可以計算空行數、注釋行數、各種語言的有效行數,還可以比較兩個代碼庫在各種行數之間的不同。CLOC是完全由Perl實現的,不依賴第三方組件,移植性強。
下載安裝 cloc.
$ brew install cloc
==> Downloading https://downloads.sourceforge.net/project/cloc/cloc/v1.62/cloc-1.62.pl ######################################################################## 100.0% 🍺 /usr/local/Cellar/cloc/1.62: 2 files, 372K, built in 13 seconds
對當前目錄進行統計
$ cloc ./ 105 text files. 89 unique files. 27 files ignored. http://cloc.sourceforge.net v 1.62 T=0.37 s (174.4 files/s, 13545.8 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Python 38 480 507 2632 Bourne Shell 17 82 183 674 HTML 2 21 1 124 JSON 1 0 0 99 YAML 3 18 7 79 Javascript 3 0 98 43 CSS 1 0 1 1 ------------------------------------------------------------------------------- SUM: 65 601 797 3652 -------------------------------------------------------------------------------
優點:
過濾了空行;
區分了注釋和代理;
自動識別不同語言;
編寫一個java的文件。
filename: test.java
/************************************************************************* > File Name: test.java > Author: > Mail: > Created Time: 日 7/23 23:06:09 2017 ************************************************************************/ public class test public static void main(String[] args){} System.out.println("come into main"); }
重新統計:
$ cloc ./ 106 text files. 90 unique files. 27 files ignored. http://cloc.sourceforge.net v 1.62 T=0.36 s (181.4 files/s, 13940.1 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Python 38 486 508 2632 Bourne Shell 17 82 183 674 HTML 2 21 1 124 JSON 1 0 0 99 YAML 3 18 7 79 Javascript 3 0 98 43 Java 1 4 6 4 CSS 1 0 1 1 ------------------------------------------------------------------------------- SUM: 66 611 804 3656 -------------------------------------------------------------------------------
專題
$ cloc --help Usage: cloc [options] <file(s)/dir(s)> | <set 1> <set 2> | <report files> Count, or compute differences of, physical lines of source code in the given files (may be archives such as compressed tarballs or zip files) and/or recursively below the given directories. ## 統計一個 tar 包中的代碼行 $ cloc small-2.0.tar.gz 42 text files. 41 unique files. 4 files ignored. http://cloc.sourceforge.net v 1.50 T=1.0 s (38.0 files/s, 3451.0 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C 21 435 482 1695 C/C++ Header 8 102 161 275 m4 4 18 4 136 make 4 18 72 29 Bourne Shell 1 2 20 2 ------------------------------------------------------------------------------- SUM: 38 575 739 2137 --------------------------------------------------------