diff --git a/cleanrepo/Program.cs b/cleanrepo/Program.cs index 877bd11c..1aea950d 100644 --- a/cleanrepo/Program.cs +++ b/cleanrepo/Program.cs @@ -1,6 +1,5 @@ using System.Data; using System.Diagnostics; -using System.IO; using System.Text; using System.Text.Json; using System.Text.RegularExpressions; @@ -55,26 +54,26 @@ static async Task Main(string[] args) static async Task RunOptions(Options options) { - if (String.IsNullOrEmpty(options.Function)) + if (string.IsNullOrEmpty(options.Function)) { Console.WriteLine($"\nYou didn't specify which function to perform, " + $"such as {s_functions[0]}, {s_functions[1]}, {s_functions[2]}, or {s_functions[3]}."); return; } - if (String.IsNullOrEmpty(options.DocFxDirectory)) + if (string.IsNullOrEmpty(options.DocFxDirectory)) { Console.WriteLine("\nYou didn't specify the directory that contains the docfx.json file."); return; } - if (String.IsNullOrEmpty(options.TargetDirectory)) + if (string.IsNullOrEmpty(options.TargetDirectory)) { Console.WriteLine("\nYou didn't specify the directory to search/clean."); return; } - if (String.IsNullOrEmpty(options.UrlBasePath)) + if (string.IsNullOrEmpty(options.UrlBasePath)) { Console.WriteLine("\nYou didn't specify the URL base path, such as /dotnet or /windows/uwp."); return; @@ -232,8 +231,7 @@ static async Task RunOptions(Options options) docFxRepo._imageLinkRegExes.Add($"social_image_url: ?\"?(?{docFxRepo.UrlBasePath}.*?(\\.(png|jpg|gif|svg))+)"); // Gather media file names. - if (docFxRepo._imageRefs is null) - docFxRepo._imageRefs = HelperMethods.GetMediaFiles(options.TargetDirectory); + docFxRepo._imageRefs ??= HelperMethods.GetMediaFiles(options.TargetDirectory); Console.WriteLine($"\nCataloging '{docFxRepo._imageRefs.Count}' images (recursively) " + $"in the '{options.TargetDirectory}' directory...\n"); @@ -1668,7 +1666,11 @@ public static bool IsFileLinkedFromFile(FileInfo linkedFile, FileInfo linkingFil public static List GetRedirectionFiles(string directoryPath) { DirectoryInfo dir = new(directoryPath); - return dir.EnumerateFiles(".openpublishing.redirection.*.json", SearchOption.AllDirectories).ToList(); + return + [ + .. dir.EnumerateFiles(".openpublishing.redirection.json", SearchOption.AllDirectories), + .. dir.EnumerateFiles(".openpublishing.redirection.*.json", SearchOption.AllDirectories) + ]; } /// diff --git a/cleanrepo/Repo.cs b/cleanrepo/Repo.cs index 820d012e..a128e39a 100644 --- a/cleanrepo/Repo.cs +++ b/cleanrepo/Repo.cs @@ -443,7 +443,7 @@ void TryAddLinkingFile(string key, string linkingFile) } /// - /// Pulls docset information, including URL base path, from the OPS config and docfx.json files. + /// Pulls docset information from the OPS config and docfx.json files. /// internal Dictionary? GetDocsetInfo() { @@ -464,7 +464,14 @@ void TryAddLinkingFile(string key, string linkingFile) if (sourceFolder.build_source_folder is null) continue; - string docfxFilePath = Path.Combine(OpsConfigFile.DirectoryName!, sourceFolder.build_source_folder, "docfx.json"); + string docfxFilePath = Path.GetFullPath(Path.Combine(OpsConfigFile.DirectoryName!, sourceFolder.build_source_folder, "docfx.json")); + + if (!string.Equals(docfxFilePath, Path.Combine(DocFxDirectory!.FullName, "docfx.json"), StringComparison.InvariantCultureIgnoreCase)) + { + // This is a different docset in the same repo. Ignore it. + continue; + } + DocFx? docfx = LoadDocfxFile(docfxFilePath); if (docfx == null) continue; @@ -497,10 +504,10 @@ void TryAddLinkingFile(string key, string linkingFile) docsetFilePath = string.Concat(docsetFilePath, "/", item.src); } } - - if (!mappingInfo.ContainsKey(docsetFilePath)) - mappingInfo.Add(docsetFilePath, UrlBasePath); } + + if (!mappingInfo.ContainsKey(docsetFilePath)) + mappingInfo.Add(docsetFilePath, UrlBasePath); } } } @@ -641,18 +648,28 @@ private static void RemoveRedirectHopsFromFile(FileInfo redirectsFile, Dictionar if (redirect.source_path != null) { // Construct the full path to the redirected file - fullPath = Path.Combine(redirectsFile.DirectoryName!, redirect.source_path); + fullPath = Path.GetFullPath(Path.Combine(redirectsFile.DirectoryName!, redirect.source_path)); } else if (redirect.source_path_from_root != null) { // Construct the full path to the redirected file - fullPath = Path.Combine(rootPath, redirect.source_path_from_root.Substring(1)); + fullPath = Path.GetFullPath(Path.Combine(rootPath, redirect.source_path_from_root.Substring(1))); } // Path.GetFullPath doesn't require the file or directory to exist, // so this works on case-sensitive file systems too. if (redirect.redirect_url is not null) - redirectsLookup.Add(Path.GetFullPath(fullPath!), redirect.redirect_url); + { + try + { + redirectsLookup.Add(fullPath!, redirect.redirect_url); + } + catch (ArgumentException) + { + Console.WriteLine($"WARNING: The source path '{fullPath}' appears more than once in the redirection file. " + + $"Please remove duplicates to ensure that all hops are removed.\n"); + } + } } foreach (KeyValuePair redirectPair in redirectsLookup)