是否有可能找到整个解决方案中的代码行数?我听说过MZ-Tools,但是是否有一个开源的工具?
当前回答
下面是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计数,所以我不得不忍受一段时间。我希望其他人能发现这是有用的,如果你有任何问题或需要帮助,请告诉我。
其他回答
在Visual Studio Team System 2008中,你可以在Analyze- >菜单中执行“计算解决方案的代码度量”,它将为你提供整个解决方案的行数(其中包括g)。
找到了这个技巧: LOC与VS查找和替换
不是一个插件,但如果这是你正在寻找的。
你可以在Visual Studio 2010中使用Project Line Counter插件。通常它不能与Visual Studio 2010一起工作,但是它可以通过一个有用的.reg文件从这里:http://www.onemanmmo.com/index.php?cmd=newsitem&comment=news.1.41.0
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}
推荐文章
- csproj文件中的“Service Include”是干什么用的?
- 如何在Visual Studio中找到堆栈跟踪?
- 使用不同的Web。在开发和生产环境中进行配置
- 是否有一种方法可以在Visual Studio中删除一行而不剪切它?
- Microsoft Visual Studio的推荐插件
- Visual Studio: ContextSwitchDeadlock
- Visual Studio:如何打破处理异常?
- VS 2017 Git本地提交数据库。每次提交时锁定错误
- 无法启动IIS Express Web服务器,注册URL失败,访问被拒绝
- 如何下载Visual Studio社区版2015(不是2017)
- 查找和替换-添加回车符或换行符
- Visual Studio Community和其他付费版本的区别是什么?
- "输出类型为类库的项目不能直接启动"
- “无法在证书存储区中找到清单签名证书”-即使添加了新密钥
- 目标文件中无法解析的外部符号