是否有可能找到整个解决方案中的代码行数?我听说过MZ-Tools,但是是否有一个开源的工具?


当前回答

您可以使用免费工具SourceMonitor

给出了很多衡量标准:代码行数、语句计数、复杂度、块深度

图形输出通过图表

其他回答

我发现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>

如果一个命令行就可以做到,为什么要使用整个应用程序呢?:)

我更喜欢OxyProject Metrics VS插件。

在Visual Studio 2015中,进入分析菜单并选择“计算代码度量”。

显然工具更容易,但我觉得在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 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计数,所以我不得不忍受一段时间。我希望其他人能发现这是有用的,如果你有任何问题或需要帮助,请告诉我。