Mac星球(软件、技巧)Mac优雅使用指南iOS

Mac OS 下统计代码行数

2017-07-24  本文已影响450人  jiaxiaolei

作为编程人员,项目中的代码行数很宇必要。
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 "^$"

大家都知道用 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
-------------------------------------------------------------------------------



## 统计某个类型的文件
命令:cloc *.c *.h
简介:该命令会统计当前文件夹下所有符合.c和.h的文件。
举例:
$ cloc *.py
       2 text files.
       2 unique files.
       0 files ignored.

http://cloc.sourceforge.net v 1.62  T=0.01 s (270.8 files/s, 9207.8 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Python                           2             15              8             45
-------------------------------------------------------------------------------
SUM:                             2             15              8             45
-------------------------------------------------------------------------------

#NOTE:  这里只会统计当前目录下的.py 文件,不会进行递归。

统计结果输出到指定文件:
使用 --report-file 选项。
举例:

 $ cloc ./  -report-file   a.txt
      69 text files.
      60 unique files.
      52 files ignored.
Wrote a.txt

## 统计每个文件的代码数:
使用 --by-file 选项
举例:
$ cloc --by-file my_project/
       2 text files.
       2 unique files.                              
       0 files ignored.

http://cloc.sourceforge.net v 1.60  T=0.01 s (149.5 files/s, 448.6 lines/s)
--------------------------------------------------------------------------------
File                              blank        comment           code
--------------------------------------------------------------------------------
my_project/perl.pl                    1              0              2
my_project/bash.sh                    1              0              2
--------------------------------------------------------------------------------
SUM:                                  2              0              4
--------------------------------------------------------------------------------

查看支持的语言:


$ cloc --show-lang
ABAP                       (abap)
ActionScript               (as)
Ada                        (ada, adb, ads, pad)
ADSO/IDSM                  (adso)
AMPLE                      (ample, dofile, startup)
Ant                        (build.xml, build.xml)
Apex Trigger               (trigger)
Arduino Sketch             (ino, pde)
ASP                        (asa, asp)
ASP.Net                    (asax, ascx, asmx, aspx, master, sitemap, webinfo)
AspectJ                    (aj)
Assembly                   (asm, S, s)
AutoHotkey                 (ahk)
awk                        (awk)
Blade                      (blade.php)
Bourne Again Shell         (bash)
Bourne Shell               (sh)
builder                    (xml.builder)
C                          (c, ec, pgc)
...


$ cloc --show-lang | wc
    195     629    7466


忽略某几个目录:

 --exclude-dir=<D1>[,D2,]  Exclude the given comma separated directories
                             D1, D2, D3, et cetera, from being scanned.  For
                             example  --exclude-dir=.cache,test  will skip
                             all files and subdirectories that have /.cache/
                             or /test/ as their parent directory.
                             Directories named .bzr, .cvs, .hg, .git, and
                             .svn are always excluded.
                             This option only works with individual directory
                             names so including file path separators is not
                             allowed.  Use --fullpath and --not-match-d=<regex>
                             to supply a regex matching multiple subdirectories.


举例:

$ cloc ./   --exclude-dir=docs,tests
 

扩展阅读

http://cloc.sourceforge.net/

https://sourceforge.net/p/cloc/code/HEAD/tree/trunk/cloc

简介:

官网的旧地址。
cloc is now being developed at https://github.com/AlDanial/cloc 

Legacy releases (1.64 and earlier) can still be found here. 

https://github.com/AlDanial/cloc
简介:

官网。

不同环境下的安装。

npm install -g cloc                    # https://www.npmjs.com/package/cloc
sudo apt-get install cloc              # Debian, Ubuntu
sudo yum install cloc                  # Red Hat, Fedora
sudo dnf install cloc                  # Fedora 22 or later
sudo pacman -S cloc                    # Arch
emerge -av dev-util/cloc               # Gentoo https://packages.gentoo.org/packages/dev-util/cloc
sudo pkg install cloc                  # FreeBSD
sudo port install cloc                 # Mac OS X with MacPorts
brew install cloc                      # Mac OS X with Homebrew
choco install cloc                     # Windows with Chocolatey


官网还推荐了其他代码统计工具:

Sonar
Ohcount
SLOCCount
sclc
USC's CODECOUNT
loc

利用Mac 终端命令统计代码行数
http://blog.csdn.net/colawjy/article/details/46425715
简介:

利用shell命令。

Mac OS 下统计代码行数的工具
http://www.cnblogs.com/wengzilin/p/4580646.html
一个转载:
http://blog.csdn.net/forwardto9/article/details/50389420
简介:

下载安装了cloc.
比较详细的介绍。

Counting lines of code with cloc
https://linuxconfig.org/counting-lines-of-code-with-cloc
简介:

linuxconfig.org  上的内容。

英文文章,解释还是比较全面的。 

有统计 .zip, .tar 的举例,有 `-by-file`的示例。 

http://www.jianshu.com/p/7c23265f0fed
PPRows for Mac
简介:

一作者自己开发的一个图形界面工具。可以体验一下。

github地址:https://github.com/jkpang/PPRows

上一篇下一篇

猜你喜欢

热点阅读