計算專案的程式碼行數與使用的程式語言

最近有一個需求是要計算整個 project 的程式碼行數,為了解決這個問題我找到一個程式叫做 cloc 可以用很快的方法計算整個 project 的程式含量與這個 project 使用多少語言來撰寫。

安裝 cloc

$ sudo apt install cloc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libb-hooks-op-check-perl libclass-method-modifiers-perl libclass-xsaccessor-perl libdevel-callchecker-perl libdynaloader-functions-perl
  libimport-into-perl libmodule-runtime-perl libmoo-perl libparallel-forkmanager-perl libparams-classify-perl libregexp-common-perl librole-tiny-perl libstrictures-perl libsub-name-perl
  libsub-quote-perl
Suggested packages:
  libscalar-number-perl libbareword-filehandles-perl libindirect-perl libmultidimensional-perl
The following NEW packages will be installed:
  cloc libalgorithm-diff-perl libalgorithm-diff-xs-perl libb-hooks-op-check-perl libclass-method-modifiers-perl libclass-xsaccessor-perl libdevel-callchecker-perl libdynaloader-functions-perl
  libimport-into-perl libmodule-runtime-perl libmoo-perl libparallel-forkmanager-perl libparams-classify-perl libregexp-common-perl librole-tiny-perl libstrictures-perl libsub-name-perl
  libsub-quote-perl
0 upgraded, 18 newly installed, 0 to remove and 4 not upgraded.
Need to get 591 kB of archives.
After this operation, 2,070 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

使用 cloc 計算程式碼行數

計算完之後可以看到這隻程式可以計算出多種語言包誇 JavaScript、C/C++ 與 SQL 多種語言的檔案數量跟程式數量等等的資料

$ cloc .
   13352 text files.
   11248 unique files.                                          
    3398 files ignored.

github.com/AlDanial/cloc v 1.82  T=15.84 s (631.0 files/s, 90710.0 lines/s)
---------------------------------------------------------------------------------------
Language                             files          blank        comment           code
---------------------------------------------------------------------------------------
JavaScript                            7055         104040         155910         727519
Markdown                               885          42864              4         110280
JSON                                   769            128              0         100825
TypeScript                             960           4613          65476          48644
HTML                                    56            389              3          21232
Python                                  52           4198           7606          18687
C/C++ Header                            10           1372            821           7421
CoffeeScript                            24            804            582           2557
C++                                      6            382             95           1785
YAML                                   108            110             41           1649
SVG                                      3              2              3           1264
Bourne Shell                            11            201            212            948
XML                                      8              0              0            883
CSS                                      6             28             15            688
SQL                                      2            133              0            455
Windows Module Definition                5             83              0            451
Bourne Again Shell                       4             76             78            281
Lisp                                     3             42             38            264
C#                                       1             55              7            211
Dockerfile                               3             26             67            152
make                                    14             58             30            140
Smarty                                   2              7              0             71
DOS Batch                                7             19              0             41
Nix                                      1              1              0             19
EJS                                      2              0              0             14
---------------------------------------------------------------------------------------
SUM:                                  9997         159631         230988        1046481
---------------------------------------------------------------------------------------

參考資料