Example #1
0
        public override void Populate(TextWriter trapFile)
        {
            trapFile.files(this, TransformedPath.Value, TransformedPath.NameWithoutExtension, TransformedPath.Extension);

            if (TransformedPath.ParentDirectory is PathTransformer.ITransformedPath dir)
            {
                trapFile.containerparent(Folder.Create(Context, dir), this);
            }

            var fromSource = TransformedPath.Extension.ToLowerInvariant().Equals("cs");

            if (fromSource)
            {
                foreach (var text in Context.Compilation.SyntaxTrees
                         .Where(t => t.FilePath == originalPath)
                         .Select(tree => tree.GetText()))
                {
                    var rawText    = text.ToString() ?? "";
                    var lineCounts = LineCounter.ComputeLineCounts(rawText);
                    if (rawText.Length > 0 && rawText[rawText.Length - 1] != '\n')
                    {
                        lineCounts.Total++;
                    }

                    trapFile.numlines(this, lineCounts);
                    Context.TrapWriter.Archive(originalPath, TransformedPath, text.Encoding ?? System.Text.Encoding.Default);
                }
            }

            trapFile.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0);
        }
Example #2
0
        public void ComputeLineCountsTest6()
        {
            var input =
                @"
/*
There once was a counter of lines,
Which worked (if one trusted the signs) -
But best to be sure,
For in old days of yore
Dodgy coders were sent down the mines.
*/

using System;   // always useful

class Program
{
    static void Main(string[] args)
    {
        // Print out something inane.
        Console.WriteLine(""Something inane!"");
    }
}
";

            Assert.Equal(new LineCounts {
                Total = 20, Code = 8, Comment = 9
            }, LineCounter.ComputeLineCounts(input));
        }
Example #3
0
        public void ComputeLineCountsTest5()
        {
            var input = "\nConsole.WriteLine();\nConsole.WriteLine();   // Foo\n";

            Assert.Equal(new LineCounts {
                Total = 4, Code = 2, Comment = 1
            }, LineCounter.ComputeLineCounts(input));
        }
Example #4
0
        public void ComputeLineCountsTest4()
        {
            var input = "\nConsole.WriteLine();";

            Assert.Equal(new LineCounts {
                Total = 2, Code = 1, Comment = 0
            }, LineCounter.ComputeLineCounts(input));
        }
Example #5
0
        public void ComputeLineCountsTest2()
        {
            var input = "Console.WriteLine();   // Wibble";

            Assert.Equal(new LineCounts {
                Total = 1, Code = 1, Comment = 1
            }, LineCounter.ComputeLineCounts(input));
        }
Example #6
0
        public override void Populate(TextWriter trapFile)
        {
            trapFile.files(this, TransformedPath.Value, TransformedPath.NameWithoutExtension, TransformedPath.Extension);

            if (TransformedPath.ParentDirectory is PathTransformer.ITransformedPath dir)
            {
                trapFile.containerparent(Extraction.Entities.Folder.Create(Context, dir), this);
            }

            var trees = Context.Compilation.SyntaxTrees.Where(t => t.FilePath == originalPath);

            if (trees.Any())
            {
                foreach (var text in trees.Select(tree => tree.GetText()))
                {
                    var rawText    = text.ToString() ?? "";
                    var lineCounts = LineCounter.ComputeLineCounts(rawText);
                    if (rawText.Length > 0 && rawText[rawText.Length - 1] != '\n')
                    {
                        lineCounts.Total++;
                    }

                    trapFile.numlines(this, lineCounts);
                    Context.TrapWriter.Archive(originalPath, TransformedPath, text.Encoding ?? System.Text.Encoding.Default);
                }
            }
            else if (IsPossiblyTextFile())
            {
                try
                {
                    System.Text.Encoding encoding;
                    var lineCount = 0;
                    using (var sr = new StreamReader(originalPath, detectEncodingFromByteOrderMarks: true))
                    {
                        while (sr.ReadLine() is not null)
                        {
                            lineCount++;
                        }
                        encoding = sr.CurrentEncoding;
                    }

                    trapFile.numlines(this, new LineCounts()
                    {
                        Total = lineCount, Code = 0, Comment = 0
                    });
                    Context.TrapWriter.Archive(originalPath, TransformedPath, encoding ?? System.Text.Encoding.Default);
                }
                catch (Exception exc)
                {
                    Context.ExtractionError($"Couldn't read file: {originalPath}. {exc.Message}", null, null, exc.StackTrace);
                }
            }

            trapFile.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0);
        }
Example #7
0
        public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body)
        {
            var start = identifier.GetLocation().SourceSpan.Start;
            var end   = body.GetLocation().SourceSpan.End - 1;

            var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start);

            var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n";

            return(LineCounter.ComputeLineCounts(text));
        }
Example #8
0
        public override void Populate(TextWriter trapFile)
        {
            if (Path == null)
            {
                trapFile.files(this, "", "", "");
            }
            else
            {
                var fi = new FileInfo(Path);

                string extension = fi.Extension ?? "";
                string name      = fi.Name;
                name = name.Substring(0, name.Length - extension.Length);
                int fromSource = extension.ToLowerInvariant().Equals(".cs") ? 1 : 2;

                // remove the dot from the extension
                if (extension.Length > 0)
                {
                    extension = extension.Substring(1);
                }
                trapFile.files(this, PathAsDatabaseString(Path), name, extension);

                trapFile.containerparent(Folder.Create(Context, fi.Directory), this);
                if (fromSource == 1)
                {
                    foreach (var text in Context.Compilation.SyntaxTrees.
                             Where(t => t.FilePath == Path).
                             Select(tree => tree.GetText()))
                    {
                        var rawText    = text.ToString() ?? "";
                        var lineCounts = LineCounter.ComputeLineCounts(rawText);
                        if (rawText.Length > 0 && rawText[rawText.Length - 1] != '\n')
                        {
                            lineCounts.Total++;
                        }

                        trapFile.numlines(this, lineCounts);
                        Context.TrapWriter.Archive(fi.FullName, text.Encoding ?? System.Text.Encoding.Default);
                    }
                }

                trapFile.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0);
            }
        }
Example #9
0
        public override LineCounts DefaultVisit(SyntaxNode node)
        {
            var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString();

            return(LineCounter.ComputeLineCounts(text));
        }