(编辑:请参阅底部的正确用法部分.)
主要问题
你如何cloc使用它的--exclude-list-file=<file>选择?基本上,我正在尝试将其提供给.clocignore文件.
预期的行为
cloc 文档说明如下:
--exclude-list-file=<file> Ignore files and/or directories whose names
appear in <file>. <file> should have one entry
per line. Relative path names will be resolved
starting from the directory where cloc is
invoked. See also --list-file.
Run Code Online (Sandbox Code Playgroud)
尝试
以下命令按预期工作:
cloc --exclude-dir=node_modules .
Run Code Online (Sandbox Code Playgroud)
但是这个命令不排除任何东西:
cloc --exclude-list-file=myignorefile .
Run Code Online (Sandbox Code Playgroud)
这是以下内容myignorefile:
node_modules
node_modules/
node_modules/*
node_modules/**
./node_modules
./node_modules/
./node_modules/*
./node_modules/**
/full/path/to/current/directory/node_modules
/full/path/to/current/directory/node_modules/
/full/path/to/current/directory/node_modules/*
/full/path/to/current/directory/node_modules/**
Run Code Online (Sandbox Code Playgroud)
cloc如果不存在则不会出错myignorefile,所以我对它正在做的事情没有任何反馈.
(我正在运行OS X并cloc通过Homebrew 安装了v1.60.)
tl; dr - @ Raman的答案中指定的方法都需要较少的指定.clocignore并且运行速度要快得多.
通过@拉曼的回答带动下,我调查的源代码:cloc 确实事实上方面--exclude-list-file,但不是过程不同的看法--exclude-dir在两个重要方面.
首先,while --exclude-dir将忽略其路径包含指定字符串的任何文件,但--exclude-list-file只会排除指定的确切文件或目录.clocignore.
如果您有这样的目录结构:
.clocignore
node_modules/foo/first.js
app/node_modules/bar/second.js
Run Code Online (Sandbox Code Playgroud)
而内容.clocignore就是
node_modules
Run Code Online (Sandbox Code Playgroud)
然后cloc --exclude-list-file=.clocignore .将成功忽略first.js但计数second.js.而cloc --exclude-dir=node_modules .将忽略两者.
要解决这个问题,.clocignore需要包含这个:
node_modules
app/node_modules
Run Code Online (Sandbox Code Playgroud)
其次,源代码cloc似乎将指定的目录添加--exlude-dir到列表中,该列表在计算文件之前会被查阅.而在计算文件后--exclude-list-file查阅发现的目录列表.
意思--exclude-list-file是,在忽略最终报告中的结果之前,仍会处理文件,这些文件可能很慢.这是通过实验证实了:在一个示例中的代码库,它采取半秒钟运行cloc与--exclude-dir,11秒到具有等效运行--exclude-list-file.
Ram*_*man 21
我发现最好的解决方法是.clocignore直接提供内容--exclude-dir.例如,如果您正在使用bash并且tr可用:
cloc --exclude-dir=$(tr '\n' ',' < .clocignore) .
Run Code Online (Sandbox Code Playgroud)
接受的答案对我不起作用,因为我也想指定子目录,这只能通过使用--not-match-d=""regex 参数来实现。所以我创建了一个 PHP 文件,它使用 .clocignore 文件生成整个 CLOC 命令(示例输出)
$ php cloc.php
cloc --fullpath --not-match-d="(node_modules|App/ios|App/android)" --not-match-f="(yarn\.lock|package\.json|package\-lock\.json)" .
Run Code Online (Sandbox Code Playgroud)
该脚本基本上将目录路径内爆为单个正则表达式字符串,并输出完整的 cloc 命令以方便复制。如果有人觉得它有用,我会把它放在要点上:)
https://gist.github.com/Lukakva/a2ef7626724a809ff2859e7203accf53