当前位置: 代码迷 >> Web前端 >> 代码规模统计――CLOC.pl工具运用介绍
  详细解决方案

代码规模统计――CLOC.pl工具运用介绍

热度:417   发布时间:2013-01-20 10:22:41.0
代码规模统计――CLOC.pl工具使用介绍
  作者:zhanhailiang 日期:2013-01-09

因项目汇报,需要统计代码规模。

自然的想法是用Shell来统计各类文件(.php|js|css|html|xml|yaml)的代码规模

> find . -name "*.php" -exec grep -Ev "^$" {} \;|wc -l
75450
> find . -name "*.js" -exec grep -Ev "^$" {} \;|wc -l   
36725
> find . -name "*.html" -exec grep -Ev "^$" {} \;|wc -l  
17830
> find . -name "*.css" -exec grep -Ev "^$" {} \;|wc -l    
4146
> find . -name "*.xml" -exec grep -Ev "^$" {} \;|wc -l   
4349
> find . -name "*.yaml" -exec grep -Ev "^$" {} \;|wc -l 
0

在sourceforge发现统计代码规模的好工具 cloc.pl。

引用下官方的简介

cloc counts blank lines, comment lines, and physical lines of source code in many
programming languages. Given two versions of a code base, cloc can compute differences
in blank, comment, and source lines. It is written entirely in Perl with no dependencies
outside the standard distribution of Perl v5.6 and higher (code from some external modules
is embedded within cloc) and so is quite portable. cloc is known to run on many flavors of
Linux, Mac OS X, AIX, Solaris, IRIX, z/OS, and Windows. (To run the Perl source version of
cloc on Windows one needs ActiveState Perl 5.6.1 or higher, Cygwin, or MobaXTerm with the
Perl plug-in installed. Alternatively one can use the Windows binary of cloc generated with
perl2exe to run on Windows computers that have neither Perl nor Cygwin.)

接下来介绍下如何使用cloc.pl来统计代码规模:

1).下载脚本

> wget http://nchc.dl.sourceforge.net/project/cloc/cloc/v1.56/cloc-1.56.pl

2).给脚本添加执行权限+x

> chmod +x cloc-1.56.pl

3).对项目源码打开

> tar zcvf itravel2.tar.gz itravel2

4).统计代码规模

> ./cloc-1.56.pl itravel2.tar.gz 
     595 text files.
     594 unique files.
    2507 files ignored.
 
http://cloc.sourceforge.net v 1.56  T=4.0 s (148.0 files/s, 35279.8 lines/s)
-------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------
PHP                            224           4018           9848          62685
Javascript                     118           3570           3083          31469
HTML                           215            574           2557          14820
XML                             19              0             57           4292
CSS                             16            160            473           3513
-------------------------------------------
SUM:                           592           8322          16018         116779
-------------------------------------------

欲知关于cloc.pl脚本详细的信息,请参见 http://cloc.sourceforge.net/