本文介紹了 3 種統(tǒng)計(jì)項(xiàng)目代碼的方式,分別為 Cloc 庫(kù)、VSCode 代碼統(tǒng)計(jì)插件 —— VS Code Counter、IDEA 代碼統(tǒng)計(jì)插件 —— Statistic。
一、 使用 Cloc 統(tǒng)計(jì)代碼行數(shù)
Cloc (opens new window)是一款使用 Perl 語(yǔ)言開發(fā)的開源代碼統(tǒng)計(jì)工具,支持多平臺(tái)使用、多語(yǔ)言識(shí)別,能夠計(jì)算指定目標(biāo)文件或文件夾中的文件數(shù)(files)、空白行數(shù)(blank)、注釋行數(shù)(comment)和代碼行數(shù)(code)。
1.1、安裝 cloc
Windows 系統(tǒng)可前往下載 exe 安裝程序 (opens new window)。Mac 系統(tǒng)可使用 Homwbrew 安裝。
# Mac 系統(tǒng)安裝
$ brew install cloc
1.2、執(zhí)行命令
# Vue 工程代碼統(tǒng)計(jì)
$ cloc ./ --exclude-dir=node_modules
1.3、 常用命令:
-
cloc ./統(tǒng)計(jì)當(dāng)前文件夾代碼量 -
cloc ./ --exclude-dir=目錄名統(tǒng)計(jì)排除當(dāng)前目錄下的某個(gè)文件夾的代碼量,多個(gè)文件使用,分隔 -
cloc -by-file 文件后綴名統(tǒng)計(jì)特定文件的代碼量
輸出參考:
cloc ./ --exclude-dir=node_modules
70 text files.
70 unique files.
9 files ignored.
github.com/AlDanial/cloc v 1.86 T=0.13 s (482.5 files/s, 47444.8 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Vuejs Component 22 408 269 3371
JavaScript 24 185 209 1067
Sass 10 88 34 299
SVG 3 0 0 64
JSON 1 0 0 41
Markdown 1 11 0 29
HTML 1 3 1 18
-------------------------------------------------------------------------------
SUM: 62 695 513 4889
-------------------------------------------------------------------------------
二、VSCode 代碼統(tǒng)計(jì)插件 —— VS Code Counter
VS Code Counter 是一款輕量、清新的代碼統(tǒng)計(jì)插件,他可以統(tǒng)計(jì)詳細(xì)的代碼信息;
2.1、安裝插件
搜索并安裝 VS Code Counter 插件。

2.2、使用
在需要統(tǒng)計(jì)的目錄上點(diǎn)擊右鍵,選擇【Count lines in directory】菜單;

執(zhí)行成功將會(huì)在項(xiàng)目目錄下生成一個(gè)統(tǒng)計(jì)目錄,其中包含多種格式的統(tǒng)計(jì)數(shù)據(jù)。

另外,使用命令操作,還支持其他方式的代碼統(tǒng)計(jì)。

三、IDEA 代碼統(tǒng)計(jì)插件 —— Statistic
在 IDEA 編輯器中,我們可以使用 Statistic (opens new window)統(tǒng)計(jì)項(xiàng)目信息,該插件可以按擴(kuò)展名、大小、行數(shù)位置等排序方式統(tǒng)計(jì)項(xiàng)目文件。
3.1 安裝插件
打開 IDEA 的設(shè)置面板,在 Plugins 插件市場(chǎng)搜索 Statistic 并安裝。

3.2 展示統(tǒng)計(jì)信息
插件安裝完成后,在 IDEA 的底部??繖跁?huì)多出一個(gè) Statistic 選項(xiàng)卡。

點(diǎn)擊該選項(xiàng)卡,并點(diǎn)擊左上角的刷新按鈕,即可統(tǒng)計(jì)出詳細(xì)信息。

3.3 配置排除的目錄
點(diǎn)擊 Statistic 選項(xiàng)卡頂部的 Settings 按鈕,在配置面板可以配置需要排除統(tǒng)計(jì)的目錄。
