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


当前回答

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
-------------------------------------------------------------------------------

第三代等效规模是对第三代语言需要多少代码的粗略估计。不是特别有用,但是很有趣。

其他回答

你可以用:

SCLOCCount http://www.dwheeler.com/sloccount/-开源 Loc度量,http://www.locmetrics.com/ -不是开源的,但是很容易使用

我更喜欢OxyProject Metrics VS插件。

VS2005, 2003和2002的开源行计数器可以在这里找到:

http://www.wndtabs.com/

这里还讨论了如何创建行计数VS插件,并在Codeproject中提供了完整的代码

http://www.codeproject.com/KB/macros/LineCounterAddin.aspx

另外,Slick Edit Gadgets有一个不错的行计数器,这里:

http://www.slickedit.com/products/slickedit

和Microsoft Visual Studio Team System 2008包括一个很好的行计数器。

不过要记住:

用代码行数来衡量编程进度就像用重量来衡量飞机制造进度。 比尔盖茨

一个简单的解决方案是在所有文件中进行搜索。在使用通配符时输入“*”。这将匹配所有的行。在查找结果窗口的最后,你应该看到一行排序:

匹配行数:563匹配文件数:17搜索文件数:17

当然,这对于大型项目来说不是很好,因为所有行都被加工并加载到内存中,以便在查找结果窗口中显示。

参考:

先进的例子

显然工具更容易,但我觉得在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}