History/Inception
Despite
google, despite all of the other code sites, I cannot seem to find a
program that reliable counts the number of code lines in my projects.
Given my interesting in making small flexible tools, I decided it
was time to build my own program for counting lines of code.
Downloads
Binaries
Source Code
Documentation (word .doc)
VB Extension Example
Design
From
the start, I wanted to build a code line counter that was flexible.
So I separated the logic that tabulates the counts from the logic
that determines what is “countable”. I also made the logic that
determines what is countable, loaded at runtime. These two
decisions make for a dynamic and flexible utility program.
CodeCounter is easily extended to process other source code types,
such as fortran. It is as simple as implementing a few functions
defined in an interface.
What
does CodeCounter do
It
counts the lines of code in source files. Current source code type
supported are C#, Xaml and SQL. CodeCounter can easily be extended
to count source code from other file types. Read on to learn how.
Installation
If
you downloaded the binaries, you need to copy all of the files in the
ZIP into some location. I suggest some where in your path so that
the program is always accessible. In the binary zip file you
should have found the following files:
Big
Woo.NET.dll
CodeCounter.exe
CodeCounter.exe.config
CodeCounter.Library.dll
All
of the files need to be copied into the same location. For me, I
copied my files to
C:\Program Files\tools.
You should pick what works best for you.
Using
CodeCounter
From
the command line, just run the program with no inputs:
c:\src>CodeCounter.exe
CodeCounter
by matt raffel
(c)
2009 - matt.raffel@mindspring.com
-verbose
-recurse
-help
-supportedTypes
-about
{filename}
FileName
can be full filename or partial with wild card
As
you can see, there's not much. Pretty simple. Here's what the
commands mean and do:
-
-verbose
| by
default the only information shown on the screen is the results
of the line counting. Errors are also displayed, of course.
When including this option, detail information is listed, listing
each file found.
|
-recurse
| by
default CodeCounter ingores subdirectories. Recurse turns off
this behavior indicating searches to include subdirectories
looking for matching input.
|
-help
| shows
the above itty bitty help
|
{filename}
| this
is the important input. {filename} is the file or wildcard file
mask of the files you want counted. You can have more than one
{filename} input. {filename} can be relative or literal path to
a file and include any of the standard windows wildcards.
|
-about
| Opens
the webpage http://www.mattraffel.com/aboutme.aspx
|
-supportedTypes
| Lists
the file types that CodeCounter is configured to process, and it
shows the class responsible for the file type processing
|
The
command line arguments can be entered in any order. The only
command argument that is required is the filenames.
Examples
C:\src>CodeCounter
*.cs
This
example expects all C# files in the current directory to be counted.
C# files in subdirectories will not be counted.
C:\src>CodeCounter
test.cs
This
example says to count the code for test.cs in the current directory
and that's it.
C:\src>CodeCounter
test.cs *.sql -recurse
This
command expects test.cs and all .sql files, including those in
subdirectories to be counted. There is a little bit of a surprise
here, in that all test.cs files will be counted.
C:\src>CodeCounter
test.cs *.sql -verbose
Same
as the previous command, except the amount of output display is
significantly more.
Extending CodeCounter
One
of the key design features of my utility is the ease of extending
CodeCounter to include additional file types.
If
you are familiar with .NET, and particular C#, then you will find
extending codecounter is very simple.
Before
you get started, you might want to get the latest code first. You do
not need the code, but it might be more instructive to look at the C#
code counting implementation (class
CSharpCodeCounterLogic)
for understanding how to implement your own code counter.
In
summary:
To
start, create your own .NET project.
Add
a reference to the CodeCounter.Library.dll.
Create
a new class that implements the ICodeCounterLogic
interface.
Write
your code, build the assembly.
Copy
the assembly, preferrable to the same location as CodeCounter.exe.
Edit
CodeCounter.exe.config, updating the codeCounters section with your
new information.
Caution:
I would caution against adding your types to CodeCounter.Library.dll
as any updates from me would cause you to lose your changes. I
encourage you to put your ICodeCounterLogic
implementation in its own assembly.
The
ICodeCounterLogic
interface
-
CodeCounterLineType
LineType(string
line)
| This
is the logic method of the interface. The engine calls this
method for each line read in from the a file.
Return:
CodeCounterLineType
|
string[]
FileTypesHandled()
| Method
defines the types supported by the interface implementation.
At a minimum, a one element array should be returned. This
method is used by the help “engine”.
Return:string[]
Example:
new
string[]
{"cs"};
|
bool
CanProcessFile(string
file)
| Method
determines if the interface implementation is able to process the
input file. This method should examine the file to determine if
the file contains code it understands. In most cases,
evaluating file name extension might be sufficent.
Return:
false
or true
|
bool
EngineCanDetermineBlankLines()
| Currently
unused method.
|
void
PrefileProcessing(string
fileName)
| Called
by the engine prior to opening the file. This gives the
interface implementation an opportunity to perform any additional
work it might need prior to beginning line counting.
Caution:
if you open the file, make sure you close it or you open it in
shared mode, or the engine will not be able to open the file.
|
void
PostfileProcessing(string
fileName)
| Called
by the engine after the file is closed, and successfully counting
the lines of the file.
|
The
CodeCounterLineType
enum
-
EmptyLine
| Indicates
an empty Line
|
Code
| Indicates
the line contains code only
|
CodeAndComment
| Indicates
the line contains both code and comments
|
CommentOnly
| Indicates
the line is a comment
|
Statement
| Indicates a statement.
|
StatementAndComment
| Indicates a statement followed by a comment
|
The
codecounter.exe.config
file
Once
you have your assembly built (and assuming unit tested), you will
want to configure CodeCounter to include your assembly. This is
done by editing the CodeCounter config file, adding your assembly to
its collection of assembles.
<counterImplementations>
<logic
key="CS"
assemblyInformation=
"CSharpCodeCounterLogic,
CodeCounter.Library.CSharpCodeCounterLogic,
CodeCounter.Library.dll"/>
</counterImplementations>
Look
for the counterImplementations
section in the config file. You will want to duplicate one of the
existing lines in this section. Copy or create a new “logic”
line. For key,
you can put anything you like as long as it is unique. Since file
extension is unique, I have been using that.
For
assemblyInformation,
you enter the information about your assembly. The format of this
field is familiar, however I have to warn you it does not conform to
the standards. You need to enter, in comma delimited fashion, the
class name, the class name with complete namespace, and the assembly
name.
In
the example above, the class name is CSharpCodeCounterLogic.
The class name with namespace is
CodeCounter.Library.CSharpCodeCounterLogic.
And the assemby name is CodeCounter.Library.dll.
For
the assembly name, if you exclude a path, it will assume the assembly
is in the same folder as CodeCounter.exe.
VB Extension Example
If you download the VB extension example, you will find an updated CodeCounter able to count lines of code in VB.NET files (*.vb). Source code included! :)
I feel the VB example shows how simple it is to add new source code types to CodeCounter.
I hope you find my utility program useful. I look forward to hearing from you.
Update
2009.01.03 - I updated to the code (code only), to separate the reporting of lines and statements.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.