Esempio n. 1
0
        private void IdentifyLineEndings(string canonicalName, string fullText, BomCheckEOLOptions localOptions, CheckEOLStats cs)
        {
            StringBuilder sb = new StringBuilder();

            int[] leTypes = new int[3];

            BomClean.LineEndingCursor cursor = new BomClean.LineEndingCursor(fullText);

            bool specific = (localOptions.CRLFOnly || localOptions.CROnly || localOptions.LFOnly);

            while (cursor.NextLine())
            {
                var leType = cursor.LineEndingType;

                if (leType != BomClean.LineEndingType.EOF)
                {
                    leTypes[(int)leType]++;
                }
            }
            if (leTypes.Where(x => x > 0).Count() > 1)
            {
                if (!specific)
                {
                    Printer.PrintMessage("#b#{0}##: #w#Mixed## #q#({1})##", canonicalName, BomClean.LETypesToString(leTypes));
                }
                lock (cs)
                    cs.MixedFiles++;
            }
            else if (leTypes[0] > 0)
            {
                if (localOptions.CRLFOnly || (!specific && !localOptions.Ambigious))
                {
                    Printer.PrintMessage("#b#{0}##: #s#CRLF##", canonicalName);
                }
                lock (cs)
                    cs.CRLFFiles++;
            }
            else if (leTypes[1] > 0)
            {
                if (localOptions.LFOnly || (!specific && !localOptions.Ambigious))
                {
                    Printer.PrintMessage("#b#{0}##: #s#LF##", canonicalName);
                }
                lock (cs)
                    cs.LFFiles++;
            }
            else if (leTypes[2] > 0)
            {
                if (localOptions.CROnly || (!specific && !localOptions.Ambigious))
                {
                    Printer.PrintMessage("#b#{0}##: #w#CR##", canonicalName);
                }
                lock (cs)
                    cs.CRFiles++;
            }
            else if (!specific && !localOptions.Ambigious)
            {
                Printer.PrintMessage("#b#{0}##: #c#(No line endings)##", canonicalName);
            }
            return;
        }
Esempio n. 2
0
        protected override bool RunInternal(Area ws, Versionr.Status status, IList <Versionr.Status.StatusEntry> targets, FileBaseCommandVerbOptions options)
        {
            BomCheckEOLOptions localOptions = options as BomCheckEOLOptions;

            LocalOptions = localOptions;
            try
            {
                List <Versionr.Status.StatusEntry> realTargets = new List <Status.StatusEntry>();
                foreach (var x in targets)
                {
                    if (!x.IsDirectory && x.FilesystemEntry != null)
                    {
                        if (localOptions.Recorded && x.Staged == false)
                        {
                            continue;
                        }
                        realTargets.Add(x);
                    }
                }
                Printer.PrintMessage("{0} files in initial list...", realTargets.Count);

                CheckEOLStats cs = new CheckEOLStats();

                List <Task> tasks = new List <Task>();
                foreach (var x in realTargets)
                {
                    tasks.Add(GetTaskFactory(options).StartNew(() =>
                    {
                        var newFileType = Versionr.Utilities.FileClassifier.Classify(x.FilesystemEntry.Info);
                        if (newFileType == Versionr.Utilities.FileEncoding.Binary)
                        {
                            return;
                        }
                        // Displaying local modifications
                        Encoding encoding = BomClean.VSREncodingToEncoding(newFileType);
                        string fullText;
                        using (var fs = x.FilesystemEntry.Info.OpenRead())
                            using (var sr = new System.IO.StreamReader(fs, encoding))
                            {
                                fullText = sr.ReadToEnd();
                            }
                        IdentifyLineEndings(x.CanonicalName, fullText, localOptions, cs);
                    }));
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        tasks[tasks.Count - 1].Wait();
                    }
                }
                Task.WaitAll(tasks.ToArray());
                if ((cs.CRLFFiles + cs.CRFiles + cs.LFFiles + cs.MixedFiles) > 1)
                {
                    Printer.PrintMessage("Final Count:");
                    if (cs.CRLFFiles > 0)
                    {
                        Printer.PrintMessage("\t#b#{0}## CRLF", cs.CRLFFiles);
                    }
                    if (cs.LFFiles > 0)
                    {
                        Printer.PrintMessage("\t#b#{0}## LF", cs.LFFiles);
                    }
                    if (cs.CRFiles > 0)
                    {
                        Printer.PrintMessage("\t#b#{0}## CR", cs.CRFiles);
                    }
                    if (cs.MixedFiles > 0)
                    {
                        Printer.PrintMessage("\t#w#{0}## Mixed", cs.MixedFiles);
                    }
                }
            }
            finally
            {
            }
            return(true);
        }
Esempio n. 3
0
        protected override bool RunInternal(Area ws, Versionr.Status status, IList <Versionr.Status.StatusEntry> targets, FileBaseCommandVerbOptions options)
        {
            BomSetEOLOptions localOptions = options as BomSetEOLOptions;

            LocalOptions = localOptions;
            try
            {
                List <Versionr.Status.StatusEntry> realTargets = new List <Status.StatusEntry>();
                foreach (var x in targets)
                {
                    if (!x.IsDirectory && x.FilesystemEntry != null)
                    {
                        if (localOptions.Recorded && x.Staged == false)
                        {
                            continue;
                        }
                        realTargets.Add(x);
                    }
                }
                Printer.PrintMessage("{0} files in initial list...", realTargets.Count);

                EOLStats cs = new EOLStats();

                List <Task> tasks = new List <Task>();
                foreach (var x in realTargets)
                {
                    tasks.Add(GetTaskFactory(options).StartNew(() =>
                    {
                        var newFileType = Versionr.Utilities.FileClassifier.Classify(x.FilesystemEntry.Info);
                        if (newFileType == Versionr.Utilities.FileEncoding.Binary)
                        {
                            return;
                        }
                        // Displaying local modifications
                        Encoding encoding = BomClean.VSREncodingToEncoding(newFileType);
                        string fullText;
                        using (var fs = x.FilesystemEntry.Info.OpenRead())
                            using (var sr = new System.IO.StreamReader(fs, encoding))
                            {
                                fullText = sr.ReadToEnd();
                            }
                        string resultString;
                        BomClean.LineEndingType let;
                        if (UnifyLineEndings(fullText, localOptions, out resultString, out let))
                        {
                            x.FilesystemEntry.Info.IsReadOnly = false;
                            using (var fs = x.FilesystemEntry.Info.Open(System.IO.FileMode.Create))
                                using (var sw = new System.IO.StreamWriter(fs, encoding))
                                {
                                    sw.Write(resultString);
                                }
                            cs.LEFixes++;
                            Printer.PrintMessage("#b#{0}##: => #s#{1}", x.CanonicalName, let == BomClean.LineEndingType.CR ? "CR" : let == BomClean.LineEndingType.CRLF ? "CRLF" : "LF");
                        }
                    }));
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        tasks[tasks.Count - 1].Wait();
                    }
                }
                Task.WaitAll(tasks.ToArray());
                if (cs.LEFixes > 0)
                {
                    Printer.PrintMessage("Updated line endings for {0} files.", cs.LEFixes);
                }
            }
            finally
            {
            }
            return(true);
        }