开发者

Lines-of-code counting for many C# solutions

开发者 https://www.devze.com 2022-12-29 00:33 出处:网络
I am currently researching a solution for counting lines of code in C#. I pretty much need a combination of the following two tools:

I am currently researching a solution for counting lines of code in C#.

I pretty much need a combination of the following two tools:

http://richnewman.wordpress.com/2007/07/01/c-and-vbnet-line-count-utility/

http://www.locmetrics.com/index.html

My problem is that I need to recursively scan a folder containing a lot of visual studio solutions. So can't really use the first tool without any major work on its code, as it's only able to scan a single solution at a time.

But I also need to split the results for each solution, preferably even the contained projects. This disqualifies the second tool I found. I also found NDepend which suffers from the same problem.

Do you know of any free tools that do what I need? I am unable to find anything suitable开发者_Go百科.


NDepend is a great tool designed for measuring and visualising code metrics and complexity.

Powershell would do it:

(dir -Include *.cs -Recurse | select-string .).Count

Counting Lines of Source Code in PowerShell:

Line count per path:

   gci . *.cs -Recurse | select-string . | Group Path

Min / Max / Averages:

   gci . *.cs -Recurse | select-string . | Group Filename | Measure-Object Count -Min -Max -Average

Comment ratio:

   $items = gci . *.cs -rec; ($items | select-string "//").Count / ($items | select-string .).Count


## Count the number of lines in all C# files in (and below) 
## the current directory. 

function CountLines($directory) 
{ 
    $pattern = "*.cs" 
    $directories = [System.IO.Directory]::GetDirectories($directory) 
    $files = [System.IO.Directory]::GetFiles($directory, $pattern) 

    $lineCount = 0 

    foreach($file in $files) 
    { 
        $lineCount += [System.IO.File]::ReadAllText($file).Split("`n").Count 
    } 

    foreach($subdirectory in $directories) 
    { 
        $lineCount += CountLines $subdirectory 
    } 

    $lineCount 
} 

CountLines (Get-Location) 

Also, Line Counter


I think LOCcode is an interesting free tool for counting number of Lines Of Code. It allows to choose which of files must be processed. It counts LOC in all enabled tasks.

Lines-of-code counting for many C# solutions

Unfortunately, it seems that development of LOCCode is over.


What you need is logical lines of code counting as defined here: How do you count your number of Lines Of Code (LOC)

If you use NDepend to count your number of lines of code you can still append all your VS sln in a NDepend project. However logical lines of code is a metric inferred from PDB files so make sure that all your assemblies have corresponding PDB files associated.

Also you might be interested by:Why is it useful to count the number of Lines Of Code (LOC) ?


I liked what Mitch Wheat has said but I don't like some useless information is calculated as 'line of code'. I've written a C# code to find total number of REAL lines in the code here: http://rajputyh.blogspot.in/2014/02/counting-number-of-real-lines-in-your-c.html

You need to build a small utility out of that code to provide path of your root folder where all "*.cs" files are kept. Good thing about that code is that it is not dependent upon the project file. I generally check-out my code and delete auto generated files and the use the tool to count number of lines.


In the end I went with LocMetrics, unfortunately this didn't really solve my per-solution problem.

But the folder structure of the reposiotry maps well enough to solutions so I was decided to use the tool above.

Thanks everybody for helping

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号