summaryrefslogtreecommitdiff
path: root/BCT/Samples/CodeCounter/console/Program.cs
blob: 1815170c13a2c848dda8a800213447fe06c8e036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
 ******************************************************************************
 This file is part of BigWoo.NET.

    BigWoo.NET is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    BigWoo.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with BigWoo.NET; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    architected and written by 
    matt raffel 
    matt.raffel@mindspring.com

   copyright (c) 2008 by matt raffel unless noted otherwise

 ******************************************************************************
*/
#region using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BigWoo.NET.ApplicationSupport;
using BigWoo.NET.Utility;
using System.IO;
using BigWoo.Utility;
using CodeCounter.Library; 
#endregion

namespace CodeCounter
{
    /// <summary>
    /// The main control loop for the code counter program.  Evaulates command line arguments
    /// spins up file searches and code counting implmentations to count the code in the file
    /// </summary>
    class Program
    {
        private class PrintHelpException : Exception { }

        private class ShowAboutWebPageException : Exception { }

        #region private data

        private const string COMMAND_VERBOSE = "-verbose";
        private const string COMMAND_RECURSE = "-recurse";
        private const string COMMAND_SUPPORTED = "-supportedTypes";
        private const string COMMAND_SHOWABOUTWEBPAGE = "-about";

        private const string WEBSITE = "www.mattraffel.com/aboutme.aspx";

        private char[] _spinner = new char[] { '|', '/', '-', '\\', '|', '/', '-', '\\' };
        private char[] _osWildCards = new char[] { '*', '?' };
        private object _lockObject = new object();

        private List<string> _fileArgs = new List<string>();
        private ScanResultsList _listOfFiles = new ScanResultsList();
        private ScanForFiles _fileScanner = new ScanForFiles();

        private bool _quiet = true;
        private bool _recursive = false;
        private bool _detailedHelp = false;

        private AutoResetEvent _eventReset = null;
        private int _erasableTextLengh = 0;
        private int _spinnerPosition = 0;
        private string _lastDirectoryProcessed = string.Empty;

        private long _filesCounted = 0L;
        private long _totalLines = 0L;
        private long _codeLines = 0L;
        private long _commentLines = 0L;
        private long _statementLines = 0L;
        private long _errors = 0L;
        #endregion

        #region private methods
        /// <summary>
        /// Draws what might appear to some as a spinner in the console window to 
        /// show that the program is functioning and not hung
        /// </summary>
        private void DrawSpinner()
        {
            // move cursor back to starting position, ignoring if we muck up
            ConsoleBackspace(_erasableTextLengh);

            if (_spinnerPosition >= _spinner.Length)
                _spinnerPosition = 0;

            string outputText = string.Format("{0}", _spinner[_spinnerPosition]);
            Console.Write(outputText);

            _erasableTextLengh = outputText.Length;

            _spinnerPosition++;

        }

        /// <summary>
        /// Erases "spaces" by moving the cursor left by "spaces"
        /// Exceptions are ignored
        /// </summary>
        /// <param name="spaces">int, the number of spaces to backspace</param>
        private void ConsoleBackspace(int spaces)
        {
            try
            {
                if (0 < spaces)
                {
                    int move = spaces;

                    if (spaces > Console.CursorLeft)
                        move = Console.CursorLeft;

                    Console.CursorLeft -= move;
                }
            }
            catch(Exception ex) 
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// processes the command line arguments.  We expect no more than
        ///  -quiet
        ///  -recurse
        ///  -help
        ///  -supportedTypes
        ///  {filename}
        ///  
        /// FileName can be full filename or partial with wild card
        /// </summary>
        /// <param name="args">string[]</param>
        private void ProcessCommandLineArgs(string[] args)
        {
            foreach (string arg in args)
            {
                if (('-' == arg[0]) || (('/' == arg[0])))
                {
                    if (0 == arg.CompareTo(COMMAND_VERBOSE))
                        _quiet = false;
                    else if (0 == arg.CompareTo(COMMAND_RECURSE))
                        _recursive = true;
                    else if (0 == arg.CompareTo(COMMAND_SUPPORTED))
                    {
                        _detailedHelp = true;
                        throw new PrintHelpException();
                    }
                    else if (0 == arg.CompareTo(COMMAND_SHOWABOUTWEBPAGE))
                        throw new ShowAboutWebPageException();
                    else
                        throw new PrintHelpException();
                }
                else
                    _fileArgs.Add(arg);
            }

            if (0 == _fileArgs.Count)
                throw new PrintHelpException();
        }

        /// <summary>
        /// Using a mask, look for files matching the mask, digging recursively
        /// into sub-directories if recursive is true.  While this code can work
        /// for files without a wildcard, for performance reasons the expectation 
        /// is this method is only called when fileMask has a wildcard in it, 
        /// or recursive is true.
        /// </summary>
        /// <param name="fileMask">string</param>
        private void BuildFileListForMask(string fileMask)
        {
            Console.Write(string.Format("scanning for {0}.", fileMask));

            _fileScanner.FilesList = new ScanResultsList();
            _fileScanner.IsRecursive = _recursive;
            _fileScanner.RootDir = Environment.CurrentDirectory;
            _fileScanner.FileMask = fileMask;
            _fileScanner.Scan();

            _listOfFiles.AddRange(_fileScanner.FilesList.ToArray());

            ConsoleBackspace(_erasableTextLengh);
            _erasableTextLengh = 0;

            if (false == _quiet)
                Console.WriteLine("..done");
            else
                Console.WriteLine("");

        }

        /// <summary>
        /// Called when recursive is true, scan for all matches in fileMask
        /// input received from the commandline
        /// </summary>
        private void BuildFileListViaScanner()
        {
            // for each input that "appears" to be a file mask (either a file name or a file wildcard)
            // gather the file names)
            foreach (string fileMask in _fileArgs)
            {
                BuildFileListForMask(fileMask);
            }
        }

        /// <summary>
        /// Called when recursive is false, evaluates the input to see if search is really
        /// required or not, only using the file scanner when a wildcard is present in the
        /// input
        /// </summary>
        private void BuildFileListIntelligently()
        {
            // for each input that "appears" to be a file mask (either a file name or a file wildcard)
            // gather the file names)
            foreach (string fileMask in _fileArgs)
            {
                if (-1 < fileMask.IndexOfAny(_osWildCards))
                {
                    BuildFileListForMask(fileMask);
                }
                else
                {
                    DefaultScanForFilesData nextFile = new DefaultScanForFilesData();
                    nextFile.FileInfo = new FileInfo(fileMask);
                    _listOfFiles.Add(nextFile);
                }
            }
        }

        /// <summary>
        /// optimized building of the file list....if recursive is not true
        /// additional code checks the input and determines how to add files
        /// to the list
        /// </summary>
        private void BuildFileList()
        {
            _listOfFiles.Clear();

            // go ahead and hook up the event handler for file found
            // in case we do end up using the scanner
            _fileScanner.OnFileFound += new FileFoundEvent(OnFileFound);
            _fileScanner.OnDirectoryFound += new ScanningDirectoryEvent(OnDirectoryFound);

            // since recursive is true, it really doesnt matter if 
            // a wild card is in the input, just start scanning
            if (true == _recursive)
            {
                BuildFileListViaScanner();
            }
            else
            {
                BuildFileListIntelligently();
            }
        }

        /// <summary>
        /// File list has been built, now its time to crack open each file
        /// and count the code
        /// </summary>
        private void ProcessFileList()
        {
            // now we are ready to count the files
            CodeCounterEngine codeCounterEngine = new CodeCounterEngine();
            _eventReset = new AutoResetEvent(false);
            object lockObject = new object();

            // hook up finished even handler (required)
            codeCounterEngine.OnFinish += new CodeCounterFinishedEvent(OnCodeCountingFinished);

            if (0 < _listOfFiles.Count)
                Console.WriteLine(string.Format("counting {0} possible files", _listOfFiles.Count));

            // only add these event handlers if the user wanted output
            if (false == _quiet)
            {
                codeCounterEngine.OnStart += new CodeCounterUpdateEvent(OnCodeCountingStart);
                codeCounterEngine.OnUpdate += new CodeCounterUpdateEvent(OnCodeCountingUpdate);
            }

            // for each file in the array
            foreach (IScanForFilesData file in _listOfFiles)
            {
                try
                {
                    codeCounterEngine.File = file.FileInfo;

                    // clean everything up
                    while (true == Console.KeyAvailable)
                        Console.Read();

                    _spinnerPosition = 0;
                    _erasableTextLengh = 0;
                    _eventReset.Reset();

                    // make erasure execute on a separate thread
                    ThreadStart codeCountThreadStart = new ThreadStart(codeCounterEngine.Count);
                    Thread codeCountThread = new Thread(codeCountThreadStart);
                    codeCountThread.Name = "codeCountingThread worker";
                    codeCountThread.Start();

                    _eventReset.WaitOne();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// Prints help to console
        /// </summary>
        private void PrintHelp()
        {
            Console.WriteLine("");
            Console.WriteLine("\t-verbose");
            Console.WriteLine("\t-recurse");
            Console.WriteLine("\t-supportedTypes");
            Console.WriteLine("\t-help");
            Console.WriteLine("\t-about (opens www.mattraffel.com)");
            Console.WriteLine("\t{filename}");
            Console.WriteLine("");
            Console.WriteLine("FileName can be full filename or partial with wild card");

            // _detailedHelp is set to true when -supportedTypes is found in the command line
            // this means we show information from each of the code counter implmentations
            if (true == _detailedHelp)
            {
                Console.WriteLine("");
                CodeCounterLogicImplementerList list = CodeCounterLogicImplementerList.LoadFromConfigFile();
                if (0 == list.Count)
                {
                    Console.WriteLine("No code types supported.  Check the config file.");
                    return;
                }

                Console.WriteLine("File formats supported:");
                
                foreach (CodeCounterLogicImplementer implementor in list)
                {
                    string[] fileTypeList = implementor.Implementer.FileTypesHandled();
                    foreach (string fileType in fileTypeList)
                    {
                        // most file types are short so we will make a fair assumption
                        // that it is ok to right justify fileType by 10 chars....
                        string details = string.Format("{0,10} by {1}", fileType, implementor.ImplementerType.Name);
                        Console.WriteLine(details);
                    }
                }

            }
        }

        /// <summary>
        /// Prints the results of counting code lines
        /// </summary>
        private void PrintSummaryReport()
        {
            if (false == _quiet)
                Console.WriteLine("");

            Console.WriteLine(string.Format("Files          {0,10:#,##0}", _filesCounted));
            if (0 < _codeLines)
                Console.WriteLine(string.Format("Code Lines     {0,10:#,##0}", _codeLines));

            if (0 < _statementLines)
                Console.WriteLine(string.Format("Statment Lines {0,10:#,##0}", _statementLines));

            Console.WriteLine(string.Format("Comment Lines  {0,10:#,##0}", _commentLines));

            if (0 < _filesCounted)
                Console.WriteLine(string.Format("Avg Lines/File {0,10:#,##0}", _totalLines / _filesCounted));

            if (0 < _errors)
            {
                Console.WriteLine(string.Format("Error Files {0,10:#,##0}", _errors));
                if (true == _quiet)
                    Console.WriteLine(" ==> to see details about the errors run with -verbose");
            }

            Console.WriteLine("");
        }

        /// <summary>
        /// A little extra self boasting.....
        /// </summary>
        private void ShowAboutWebPage()
        {
            try
            {
                Console.WriteLine("");
                Console.WriteLine(string.Format("ok to open {0} in your browser (Y/n)?", WEBSITE));
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                char input = keyInfo.KeyChar;

                if (('y' == input) || ('Y' == input) || ('\r' == input))
                    System.Diagnostics.Process.Start(string.Format("http://{0}", WEBSITE));

                //if ('y' != input)
                //  System.Diagnostics.Process.Start(string.Format("http://{0}", WEBSITE));
            }
            catch
            {
                Console.WriteLine(string.Format("Error trying to open url.  Please visit http://{0} when you have a moment.", WEBSITE));
            }
        }

        /// <summary>
        /// Prints a standard bit of header information every time
        /// </summary>
        private void PrintHeader()
        {
            Console.WriteLine("CodeCounter by matt raffel");
            Console.WriteLine("(c) 2009 - matt.raffel@mindspring.com");
        }

        /// <summary>
        /// This is the main function of this class
        /// it runs through the command line parameters
        /// sets up the FileErasure object and lets it do
        /// its business
        /// </summary>
        /// <param name="args">string[], command line arguments</param>
        private void Run(string[] args)
        {
            PrintHeader();

            try
            {
                // process input
                ProcessCommandLineArgs(args);

                // now we should know what files to count or what mask to use
                // to get the files to count so build a definitive list 
                // of files
                BuildFileList();

                // now count the code
                ProcessFileList();

                // and print out the results
                PrintSummaryReport();
            }
            catch (PrintHelpException)
            {
                PrintHelp();
            }
            catch (ShowAboutWebPageException)
            {
                ShowAboutWebPage();
            }
            catch (Exception error)
            {
                Console.WriteLine("");
                Console.WriteLine("Application Exception:");
                Console.WriteLine(error.Message);
                Console.WriteLine(error.StackTrace);
            }
            finally
            {
            }
        }
        #endregion

        #region event methods
        /// <summary>
        /// Provide simple UI updates on scanning
        /// </summary>
        /// <param name="directoryName">string</param>
        private void OnDirectoryFound(string directoryName)
        {
            if (false == _quiet)
                DrawSpinner();
        }

        /// <summary>
        /// Provide simple UI updates on scanning
        /// </summary>
        /// <param name="fileData">string</param>
        private void OnFileFound(IScanForFilesData fileData)
        {
            if (false == _quiet)
                DrawSpinner();
        }

        /// <summary>
        /// Finished counting lines in a file, now its time to note it
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="counterFinishedArgs">CodeCounterFinishedEventArgs</param>
        private void OnCodeCountingFinished(object sender, CodeCounterFinishedEventArgs counterFinishedArgs)
        {
            lock (_lockObject)
            {
                try
                {

                    if (CodeCounterFunctionTypes.Summarizing == counterFinishedArgs.Function)
                    {
                        if (false == _quiet)
                        {
                            // move cursor back to starting position, ignoring if we muck up
                            ConsoleBackspace(_erasableTextLengh);
                            string spaces = new string(' ', _erasableTextLengh);
                            Console.Write(spaces);
                            Console.WriteLine("");
                        }

                        // only files we succeeding in counting are part of the file count 
                        // error files are not included in the value
                        _filesCounted++;

                        _codeLines += counterFinishedArgs.CodeLines;
                        _totalLines += counterFinishedArgs.Lines;
                        _commentLines += counterFinishedArgs.CommentLines;
                        _statementLines += counterFinishedArgs.StatementLines;

                    }
                    else
                    {
                        _errors++;

                        if ((counterFinishedArgs.AdditionalData is ConfigurationFileException) ||
                            (counterFinishedArgs.AdditionalData is FileTypeNotSupportedException)) 
                        {
                            if (false == _quiet)
                            {
                                // move cursor back to starting position, ignoring if we muck up
                                ConsoleBackspace(_erasableTextLengh);
                                Console.Write("...file type not supported");
                                Console.WriteLine("");
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
                finally
                {
                    _spinnerPosition = 0;
                    _erasableTextLengh = 0;
                    _eventReset.Set();

                }
            }
        }

        /// <summary>
        /// Updates from the code counter as it evaluates a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="erasureArgs">CodeCounterUpdateEventArgs</param>
        private void OnCodeCountingUpdate(object sender, CodeCounterUpdateEventArgs erasureArgs)
        {
            lock (_lockObject)
            {
                try
                {
                    // move cursor back to starting position, ignoring if we muck up
                    DrawSpinner();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }
        
        /// <summary>
        /// Indicates a file has been found that matches our criteria for counting the lines
        /// of code in the file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="startArgs">CodeCounterUpdateEventArgs</param>
        private void OnCodeCountingStart(object sender, CodeCounterUpdateEventArgs startArgs)
        {
            try
            {
                System.Diagnostics.Debug.Assert(0 == _erasableTextLengh);

                if (0 != _lastDirectoryProcessed.CompareTo(startArgs.FileInfo.DirectoryName))
                {
                    _lastDirectoryProcessed = startArgs.FileInfo.DirectoryName;
                    string directory = string.Format("{0}:", _lastDirectoryProcessed);
                    Console.WriteLine(directory);
                }

                _spinnerPosition = 0;
                _erasableTextLengh = 0;
                string fileName = string.Format("\t{0}: ", startArgs.FileInfo.Name);
                Console.Write(fileName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        internal Program()
        {
        }
        #endregion

        #region startup entry method
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args">string[]</param>
        static void Main(string[] args)
        {
            Program go = new Program();
            go.Run(args);

            if (true == System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("press a key....");
                Console.Read();
            }
        } 
        #endregion
    }
}