Skip to content

Commit 5db2985

Browse files
authored
[CleanRepo] Fix bugs with multiple docsets (#655)
1 parent 0267c38 commit 5db2985

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

cleanrepo/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Data;
22
using System.Diagnostics;
3-
using System.IO;
43
using System.Text;
54
using System.Text.Json;
65
using System.Text.RegularExpressions;
@@ -55,26 +54,26 @@ static async Task Main(string[] args)
5554

5655
static async Task RunOptions(Options options)
5756
{
58-
if (String.IsNullOrEmpty(options.Function))
57+
if (string.IsNullOrEmpty(options.Function))
5958
{
6059
Console.WriteLine($"\nYou didn't specify which function to perform, " +
6160
$"such as {s_functions[0]}, {s_functions[1]}, {s_functions[2]}, or {s_functions[3]}.");
6261
return;
6362
}
6463

65-
if (String.IsNullOrEmpty(options.DocFxDirectory))
64+
if (string.IsNullOrEmpty(options.DocFxDirectory))
6665
{
6766
Console.WriteLine("\nYou didn't specify the directory that contains the docfx.json file.");
6867
return;
6968
}
7069

71-
if (String.IsNullOrEmpty(options.TargetDirectory))
70+
if (string.IsNullOrEmpty(options.TargetDirectory))
7271
{
7372
Console.WriteLine("\nYou didn't specify the directory to search/clean.");
7473
return;
7574
}
7675

77-
if (String.IsNullOrEmpty(options.UrlBasePath))
76+
if (string.IsNullOrEmpty(options.UrlBasePath))
7877
{
7978
Console.WriteLine("\nYou didn't specify the URL base path, such as /dotnet or /windows/uwp.");
8079
return;
@@ -232,8 +231,7 @@ static async Task RunOptions(Options options)
232231
docFxRepo._imageLinkRegExes.Add($"social_image_url: ?\"?(?<path>{docFxRepo.UrlBasePath}.*?(\\.(png|jpg|gif|svg))+)");
233232

234233
// Gather media file names.
235-
if (docFxRepo._imageRefs is null)
236-
docFxRepo._imageRefs = HelperMethods.GetMediaFiles(options.TargetDirectory);
234+
docFxRepo._imageRefs ??= HelperMethods.GetMediaFiles(options.TargetDirectory);
237235

238236
Console.WriteLine($"\nCataloging '{docFxRepo._imageRefs.Count}' images (recursively) " +
239237
$"in the '{options.TargetDirectory}' directory...\n");
@@ -1668,7 +1666,11 @@ public static bool IsFileLinkedFromFile(FileInfo linkedFile, FileInfo linkingFil
16681666
public static List<FileInfo> GetRedirectionFiles(string directoryPath)
16691667
{
16701668
DirectoryInfo dir = new(directoryPath);
1671-
return dir.EnumerateFiles(".openpublishing.redirection.*.json", SearchOption.AllDirectories).ToList();
1669+
return
1670+
[
1671+
.. dir.EnumerateFiles(".openpublishing.redirection.json", SearchOption.AllDirectories),
1672+
.. dir.EnumerateFiles(".openpublishing.redirection.*.json", SearchOption.AllDirectories)
1673+
];
16721674
}
16731675

16741676
/// <summary>

cleanrepo/Repo.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void TryAddLinkingFile(string key, string linkingFile)
443443
}
444444

445445
/// <summary>
446-
/// Pulls docset information, including URL base path, from the OPS config and docfx.json files.
446+
/// Pulls docset information from the OPS config and docfx.json files.
447447
/// </summary>
448448
internal Dictionary<string, string>? GetDocsetInfo()
449449
{
@@ -464,7 +464,14 @@ void TryAddLinkingFile(string key, string linkingFile)
464464
if (sourceFolder.build_source_folder is null)
465465
continue;
466466

467-
string docfxFilePath = Path.Combine(OpsConfigFile.DirectoryName!, sourceFolder.build_source_folder, "docfx.json");
467+
string docfxFilePath = Path.GetFullPath(Path.Combine(OpsConfigFile.DirectoryName!, sourceFolder.build_source_folder, "docfx.json"));
468+
469+
if (!string.Equals(docfxFilePath, Path.Combine(DocFxDirectory!.FullName, "docfx.json"), StringComparison.InvariantCultureIgnoreCase))
470+
{
471+
// This is a different docset in the same repo. Ignore it.
472+
continue;
473+
}
474+
468475
DocFx? docfx = LoadDocfxFile(docfxFilePath);
469476
if (docfx == null)
470477
continue;
@@ -497,10 +504,10 @@ void TryAddLinkingFile(string key, string linkingFile)
497504
docsetFilePath = string.Concat(docsetFilePath, "/", item.src);
498505
}
499506
}
500-
501-
if (!mappingInfo.ContainsKey(docsetFilePath))
502-
mappingInfo.Add(docsetFilePath, UrlBasePath);
503507
}
508+
509+
if (!mappingInfo.ContainsKey(docsetFilePath))
510+
mappingInfo.Add(docsetFilePath, UrlBasePath);
504511
}
505512
}
506513
}
@@ -641,18 +648,28 @@ private static void RemoveRedirectHopsFromFile(FileInfo redirectsFile, Dictionar
641648
if (redirect.source_path != null)
642649
{
643650
// Construct the full path to the redirected file
644-
fullPath = Path.Combine(redirectsFile.DirectoryName!, redirect.source_path);
651+
fullPath = Path.GetFullPath(Path.Combine(redirectsFile.DirectoryName!, redirect.source_path));
645652
}
646653
else if (redirect.source_path_from_root != null)
647654
{
648655
// Construct the full path to the redirected file
649-
fullPath = Path.Combine(rootPath, redirect.source_path_from_root.Substring(1));
656+
fullPath = Path.GetFullPath(Path.Combine(rootPath, redirect.source_path_from_root.Substring(1)));
650657
}
651658

652659
// Path.GetFullPath doesn't require the file or directory to exist,
653660
// so this works on case-sensitive file systems too.
654661
if (redirect.redirect_url is not null)
655-
redirectsLookup.Add(Path.GetFullPath(fullPath!), redirect.redirect_url);
662+
{
663+
try
664+
{
665+
redirectsLookup.Add(fullPath!, redirect.redirect_url);
666+
}
667+
catch (ArgumentException)
668+
{
669+
Console.WriteLine($"WARNING: The source path '{fullPath}' appears more than once in the redirection file. " +
670+
$"Please remove duplicates to ensure that all hops are removed.\n");
671+
}
672+
}
656673
}
657674

658675
foreach (KeyValuePair<string, string> redirectPair in redirectsLookup)

0 commit comments

Comments
 (0)