protected override bool RunInternal(Area ws, Versionr.Status status, IList <Versionr.Status.StatusEntry> targets, FileBaseCommandVerbOptions options) { BomTabOptions localOptions = options as BomTabOptions; try { List <Versionr.Status.StatusEntry> realTargets = new List <Status.StatusEntry>(); foreach (var x in targets) { if (x.VersionControlRecord != null && !x.IsDirectory && x.FilesystemEntry != null) { realTargets.Add(x); } } Printer.PrintMessage("Found {0} files for processing...", realTargets.Count); TabStats ts = new TabStats(); 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; } ProcessFile(AddTabs, localOptions.Trim, localOptions.TabSize, x, ts); })); if (System.Diagnostics.Debugger.IsAttached) { tasks[tasks.Count - 1].Wait(); } } Task.WaitAll(tasks.ToArray()); if (ts.Files > 0) { if (AddTabs) { Printer.PrintMessage("Updated {0} files and inserted {1} tabs.", ts.Files, ts.Insertions); } else { Printer.PrintMessage("Updated {0} files and inserted {1} spaces.", ts.Files, ts.Insertions * localOptions.TabSize); } } } finally { } return(true); }
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); }
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); }
protected override bool RunInternal(Area ws, Versionr.Status status, IList <Versionr.Status.StatusEntry> targets, FileBaseCommandVerbOptions options) { BomCleanOptions localOptions = options as BomCleanOptions; LocalOptions = localOptions; try { List <Versionr.Status.StatusEntry> realTargets = new List <Status.StatusEntry>(); foreach (var x in targets) { if (x.VersionControlRecord != null && !x.IsDirectory && x.FilesystemEntry != null && x.Code == StatusCode.Modified) { if (localOptions.Recorded && x.Staged == false) { continue; } realTargets.Add(x); } } Printer.PrintMessage("{0} files in initial list...", realTargets.Count); Dictionary <string, Versionr.Objects.Record> recordMap = new Dictionary <string, Versionr.Objects.Record>(); foreach (var x in ws.GetRecords(Workspace.Version)) { recordMap[x.CanonicalName] = x; } Printer.PrintMessage("Original version: #s#{0}##", Workspace.Version.ID); CleanStats cs = new CleanStats(); 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 string tmp = Versionr.Utilities.DiffTool.GetTempFilename(); Versionr.Objects.Record rec; if (recordMap.TryGetValue(x.CanonicalName, out rec)) { Encoding encoding = VSREncodingToEncoding(newFileType); string messages = string.Format("File #b#{0}##:", x.CanonicalName); ws.RestoreRecord(rec, DateTime.Now, tmp); var oldFileType = Versionr.Utilities.FileClassifier.Classify(new System.IO.FileInfo(tmp)); bool fixBOM = false; bool fixLines = false; if (oldFileType != newFileType && localOptions.BOM) { bool allowFix = true; if ((oldFileType == FileEncoding.ASCII || oldFileType == FileEncoding.Latin1) && (newFileType == FileEncoding.ASCII || newFileType == FileEncoding.Latin1)) { allowFix = false; } if (allowFix) { fixBOM = true; lock (cs) { cs.BOMFixes++; } messages += string.Format("\n - Fixing changed encoding: {0} -> {1}", oldFileType, newFileType); } } var fullText = new Lazy <string>(() => { using (var fs = x.FilesystemEntry.Info.OpenRead()) using (var sr = new System.IO.StreamReader(fs, encoding)) { return(sr.ReadToEnd()); } }); var oldFullText = new Lazy <string>(() => { using (var fs = new System.IO.FileInfo(tmp).OpenRead()) using (var sr = new System.IO.StreamReader(fs, VSREncodingToEncoding(oldFileType))) { return(sr.ReadToEnd()); } }); if (localOptions.Newlines) { // Fast version - all the same, except the newlines changed string resultString; bool lcheckResult = NewLineCheckFast(fullText.Value, oldFullText.Value, ref messages, out resultString); if (lcheckResult == true) { if (resultString != null) { lock (cs) { cs.LEFixes++; } fixLines = true; fullText = new Lazy <string>(() => { return(resultString); }); } } // Slow version - need to use diff engine } if (fixBOM || fixLines) { Encoding targetEncoding = VSREncodingToEncoding(oldFileType); x.FilesystemEntry.Info.IsReadOnly = false; using (var fs = x.FilesystemEntry.Info.Open(System.IO.FileMode.Create)) using (var sw = new System.IO.StreamWriter(fs, targetEncoding)) { sw.Write(fullText.Value); } Printer.PrintMessage(messages); } new System.IO.FileInfo(tmp).IsReadOnly = false; System.IO.File.Delete(tmp); } })); if (System.Diagnostics.Debugger.IsAttached) { tasks[tasks.Count - 1].Wait(); } } Task.WaitAll(tasks.ToArray()); if (cs.BOMFixes > 0) { Printer.PrintMessage("Updated encoding/BOM for {0} files.", cs.BOMFixes); } if (cs.LEFixes > 0) { Printer.PrintMessage("Updated line endings for {0} files.", cs.LEFixes); } } finally { } return(true); }