コード例 #1
0
ファイル: CsRewriter.cs プロジェクト: karthik-iyer/StingyJunk
        public static RewrittenFile CreateRewriteFile(RewrittenFile rewriteCandidate)
        {
            FileUtilities.RemoveIfPresentSoft(rewriteCandidate.RewrittenFilePath);

            var targetFileStream = new StreamWriter(rewriteCandidate.RewrittenFilePath);

            try
            {
                using (var sr = new StreamReader(rewriteCandidate.OriginalFilePath))
                {
                    //maybe there is a better way to do this aside from counting braces?
                    //http://stackoverflow.com/questions/32769630/how-to-compile-a-c-sharp-file-with-roslyn-programmatically
                    var braceDepth = 0;

                    var inBlockComment = false;
                    var inNamespace    = false;

                    while (sr.EndOfStream == false)
                    {
                        var line = sr.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        var trimStart = line.TrimStart();
                        var trimEnd   = line.TrimEnd();

                        if (trimStart.StartsWith("//"))
                        {
                            targetFileStream.WriteLine(line);
                            continue;
                        }

                        // while in block comments, dont count bracing
                        if (trimStart.StartsWith("/*"))
                        {
                            inBlockComment = true;
                            targetFileStream.WriteLine(line);
                            continue;
                        }

                        if (inBlockComment)
                        {
                            if (trimEnd.EndsWith("*/"))
                            {
                                inBlockComment = false;
                            }
                            targetFileStream.WriteLine(line);
                            continue;
                        }

                        var openingBraceCountForThisLine = trimStart.Length - trimStart.Replace("{", string.Empty).Length;
                        var closingBraceCountForThisLine = trimStart.Length - trimStart.Replace("}", string.Empty).Length;

                        // ReSharper disable StringIndexOfIsCultureSpecific.1
                        if (trimStart.IndexOf("namespace") >= 0)
                        // ReSharper restore StringIndexOfIsCultureSpecific.1
                        {
                            var stackedNamespaces = BuildStackedNamespacePaths(trimStart);
                            targetFileStream.WriteLine(stackedNamespaces.Select(n => $"using {n};"));
                            inNamespace = true;
                            braceDepth += openingBraceCountForThisLine;
                            braceDepth -= closingBraceCountForThisLine;
                            continue;
                        }

                        braceDepth += openingBraceCountForThisLine;
                        braceDepth -= closingBraceCountForThisLine;
                        //bool anythingBetweenBraces; //no nasty one liners

                        if (inNamespace && openingBraceCountForThisLine > 0 &&
                            braceDepth == 1 &&
                            closingBraceCountForThisLine < openingBraceCountForThisLine)
                        {
                            // "{", "{{", "{{ }", etc
                            targetFileStream.WriteLine();
                            continue;
                        }

                        if (inNamespace && closingBraceCountForThisLine > 0 &&
                            braceDepth == 0 &&
                            closingBraceCountForThisLine > openingBraceCountForThisLine)
                        {
                            // "}", "}}}", "{ }}", etc
                            //targetFileStream.WriteLine();
                            continue;
                        }

                        targetFileStream.WriteLine(line);
                    } //while reading stream
                }     //using streamreader
            }
            finally
            {
                targetFileStream.Flush();
                targetFileStream.Close();
            }

            return(rewriteCandidate);
        }
コード例 #2
0
 CreateDescriptor(RewrittenFile, PipFragmentType.VsoHash, f => f.GetFileValue(), (v, p) => p.AddVsoHash(v)),