private void Build(string path, Direction direction) { path = Path.GetFullPath(path); foreach (var file in Directory.GetFiles(path)) { var key = GetKey(file); Files.TryGetValue(key, out var model); Console.WriteLine($"Checking Path {file}"); if (model == null) { model = new FileCompareModel(); Files.Add(key, model); } if (direction == Direction.Left) { model.LeftPath = file; } else { model.RightPath = file; } Files[key] = model; } var dirs = Directory.GetDirectories(path); foreach (var dir in dirs) { Build(dir, direction); } }
public async Task <string> GenerateDiffFile(string tempPath, string fileName, FileCompareModel diff) { var tempDiffFile = Path.Join(tempPath, fileName + ".html"); // get the directory var p = Path.GetDirectoryName(tempDiffFile); Directory.CreateDirectory(p); using (var stream = File.CreateText(tempDiffFile)) { // html header await stream.WriteAsync(GetHtmlStartBodyBlock()); // content await DiffTwoFilesAsync(fileName, diff, stream); // footer of html await stream.WriteAsync(GetHtmlEndBodyBlock()); } return(tempDiffFile); }
private async Task DiffTwoFilesAsync(string file, FileCompareModel diffViewModel, StreamWriter streamWriter) { var title = GetSectionTitle("Diff Comparison"); var builder = new SideBySideDiffBuilder(new Differ()); if (!File.Exists(diffViewModel.RightPath) && !File.Exists(diffViewModel.LeftPath)) { // use the repository item path in the message var html = title + WrapTableDefinition(BuildRow($"Could not locate file {diffViewModel.RightPath}. A Diff Comparison was not generated")); streamWriter.Write(html); return; } if (!File.Exists(diffViewModel.RightPath) || !File.Exists(diffViewModel.LeftPath)) { var html = title + WrapTableDefinition(BuildRow("Only One Version of this file is available. A Diff Comparison was not generated")); streamWriter.Write(html); return; } // add the repository items path for the sub title. title += GetSubSectionTitle(file); var leftText = ""; var rightText = ""; var sw = new System.Diagnostics.Stopwatch(); sw.Start(); using (var reader = File.OpenText(diffViewModel.LeftPath)) { Log($"[AuditReports]:[Reading Left]:[Started]"); leftText = await reader.ReadToEndAsync(); Log($"[AuditReports]:[Reading Left]:[Completed]"); } using (var reader = File.OpenText(diffViewModel.RightPath)) { Log($"[AuditReports]:[Reading Right]:[Started]"); rightText = await reader.ReadToEndAsync(); Log($"[AuditReports]:[Reading Right]:[Completed]"); } Log($"[AuditReports]:[BuildDiffModel]:[Started]"); var diff = await Task.Run(() => { return(builder.BuildDiffModel(leftText, rightText)); }); Log($"[AuditReports]:[BuildDiffModel]:[Completed]"); var htmlBuilder = new Html.DiffHtml(); var include = IncludeUnChangedText(diffViewModel.RightPath, diffViewModel.LeftPath); if (!include) { title += GetSubSectionTitle("Due to the size of the original files, the comparison will only show modified lines."); } await streamWriter.WriteLineAsync(title); Log($"[AuditReports]:[Build]:[Started]"); await htmlBuilder.BuildAsync(diff, streamWriter, include); Log($"[AuditReports]:[Build]:[Completed]"); Console.ForegroundColor = ConsoleColor.Green; sw.Stop(); Log($"[AuditReports]:[{nameof(DiffTwoFilesAsync)}]:[Completed In]:[{ElapsedTime(sw.Elapsed)}]"); Console.ResetColor(); }