是否有可能找到整个解决方案中的代码行数?我听说过MZ-Tools,但是是否有一个开源的工具?
当前回答
您可以使用Visual Studio Code Metrics PowerTool 10.0。它是一个命令行实用程序,为您计算托管代码(包括代码行)上的一些指标。你可以获得一个VS 2010插件,它可以将工具带入Visual Studio,并使它像选择菜单项并单击“Analyze Solution”一样快速。
其他回答
我发现powershell在这方面很有用。我认为LoC是一个非常虚假的指标,所以我不认为需要任何更正式的东西。
从一个较小的解决方案目录:
PS C:\Path> (gci -include *.cs,*.xaml -recurse | select-string .).Count
8396
PS C:\Path>
这将计算所有解决方案的.cs和.xaml文件中的非空行。对于一个较大的项目,我只是使用了不同的扩展列表:
PS C:\Other> (gci -include *.cs,*.cpp,*.h,*.idl,*.asmx -recurse | select-string .).Count
909402
PS C:\Other>
如果一个命令行就可以做到,为什么要使用整个应用程序呢?:)
下面是Visual Studio 2012/2013/2015的更新,供那些想要做“查找”选项的人使用(我认为这是最简单的):这个RegEx将找到所有非空行,并排除一些内容,以给出最准确的结果。
在“查找”框中输入以下正则表达式。请确保选择“使用正则表达式”选项。根据您的需要,将搜索选项更改为“当前项目”或“整个解决方案”。现在选择“查找全部”。在“查找结果”窗口的底部,您将看到“匹配行”,这是代码的行数。
^(?!(\s*\*))(?!(\s*\-\-\>))(?!(\s*\<\!\-\-))(?!(\s*\n))(?!(\s*\*\/))(?!(\s*\/\*))(?!(\s*\/\/\/))(?!(\s*\/\/))(?!(\s*\}))(?!(\s*\{))(?!(\s(using))).*$
该RegEx不包括以下项:
评论
// This is a comment
多行注释(假设每一行都正确地注释了,每行前面都有一个*)
/* I am a
* multi-line
* comment */
XML智能感知
/// <summary>
/// I'm a class description for Intellisense
/// </summary>
HTML注释:
<!-- I am a HTML Comment -->
使用语句:
using System;
using System.Web;
左大括号:
{
右大括号:
}
注意:括号之间的任何内容都将包含在搜索中,但在这个例子中,只有4行代码会被计算在内,而不是实际的18行非空行:
public class Test
{
/// <summary>
/// Do Stuff
/// </summary>
public Test()
{
TestMe();
}
public void TestMe()
{
//Do Stuff Here
/* And
* Do
* Stuff
* Here */
}
}
我创建这个给我一个更准确的LOC计数比以前的一些选项,并认为我将分享。老板们喜欢LOC计数,所以我不得不忍受一段时间。我希望其他人能发现这是有用的,如果你有任何问题或需要帮助,请告诉我。
你可以用:
SCLOCCount http://www.dwheeler.com/sloccount/-开源 Loc度量,http://www.locmetrics.com/ -不是开源的,但是很容易使用
cloc是一个优秀的命令行、基于perl的windows可执行文件,它将按文件格式分组,分解空行、注释行和源代码行。
现在它不会专门运行在VS解决方案文件上,但它可以递归遍历目录,并且您可以根据需要设置文件名过滤器。
以下是他们网页上的输出示例:
prompt> cloc perl-5.10.0.tar.gz 4076 text files. 3883 unique files. 1521 files ignored. http://cloc.sourceforge.net v 1.07 T=10.0 s (251.0 files/s, 84566.5 lines/s) ------------------------------------------------------------------------------- Language files blank comment code scale 3rd gen. equiv ------------------------------------------------------------------------------- Perl 2052 110356 112521 309778 x 4.00 = 1239112.00 C 135 18718 22862 140483 x 0.77 = 108171.91 C/C++ Header 147 7650 12093 44042 x 1.00 = 44042.00 Bourne Shell 116 3402 5789 36882 x 3.81 = 140520.42 Lisp 1 684 2242 7515 x 1.25 = 9393.75 make 7 498 473 2044 x 2.50 = 5110.00 C++ 10 312 277 2000 x 1.51 = 3020.00 XML 26 231 0 1972 x 1.90 = 3746.80 yacc 2 128 97 1549 x 1.51 = 2338.99 YAML 2 2 0 489 x 0.90 = 440.10 DOS Batch 11 85 50 322 x 0.63 = 202.86 HTML 1 19 2 98 x 1.90 = 186.20 ------------------------------------------------------------------------------- SUM: 2510 142085 156406 547174 x 2.84 = 1556285.03 -------------------------------------------------------------------------------
第三代等效规模是对第三代语言需要多少代码的粗略估计。不是特别有用,但是很有趣。
显然工具更容易,但我觉得在powershell这样做很酷:)
该脚本查找.sln文件中的所有.csproj引用,然后在每个csproj文件中查找包含用于编译的文件。对于包含用于编译的每个文件,它都会创建一个具有以下属性的对象:解决方案、项目、文件、行。它将所有这些对象存储在一个列表中,然后根据需要对数据进行分组和投影。
#path to the solution file e.g. "D:\Code\Test.sln"
$slnFile = "D:\Code\Test.sln"
#results
$results = @()
#iterate through .csproj references in solution file
foreach($projLines in get-item $slnFile | Get-Content | Select-String '".*csproj')
{
$projFile = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($slnFile), [regex]::Match($projLines,'[^"]*csproj').Value)
$projFolder = [System.IO.Path]::GetDirectoryName($projFile)
#from csproj file: get lines for files to compile <Compile Include="..."/>
$includeLines = get-item $projFile | Get-Content | Select-String '<Compile Include'
#count of all files lines in project
$linesInProject = 0;
foreach($fileLine in $includeLines)
{
$includedFilePath = [System.IO.Path]::Combine($projFolder, [Regex]::Match($fileLine, '"(?<file>.*)"').Groups["file"].Value)
$lineCountInFile = (Get-Content $includedFilePath).Count
$results+=New-Object PSObject -Property @{ Solution=$slnFile ;Project=$projFile; File=$includedFilePath; Lines=$lineCountInFile }
}
}
#filter out any files we dont need
$results = $results | ?{!($_.File -match "Designer")}
#print out:
"---------------lines per solution--------------"
$results | group Solution | %{$_.Name + ": " + ($_.Group | Measure-Object Lines -Sum).Sum}
"---------------lines per peoject--------------"
$results | group Project | %{$_.Name + ": " + ($_.Group | Measure-Object Lines -Sum).Sum}
推荐文章
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- Visual Studio复制项目
- 如何在iis7应用程序池中设置。net Framework 4.5版本
- Visual Studio - Resx文件默认“内部”为“公共”
- 在Visual Studio 2010中有快速查找文件的方法吗?
- Nuget连接尝试失败“无法为源加载服务索引”
- 在Visual Studio中查看多个项目/解决方案
- Visual Studio打开默认浏览器,而不是Internet Explorer
- csproj文件中的“Service Include”是干什么用的?
- 如何在Visual Studio中找到堆栈跟踪?
- 使用不同的Web。在开发和生产环境中进行配置
- 是否有一种方法可以在Visual Studio中删除一行而不剪切它?
- Microsoft Visual Studio的推荐插件
- Visual Studio: ContextSwitchDeadlock