Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions cleanrepo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -232,8 +231,7 @@ static async Task RunOptions(Options options)
docFxRepo._imageLinkRegExes.Add($"social_image_url: ?\"?(?<path>{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");
Expand Down Expand Up @@ -1668,7 +1666,11 @@ public static bool IsFileLinkedFromFile(FileInfo linkedFile, FileInfo linkingFil
public static List<FileInfo> 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)
];
}

/// <summary>
Expand Down
33 changes: 25 additions & 8 deletions cleanrepo/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ void TryAddLinkingFile(string key, string linkingFile)
}

/// <summary>
/// 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.
/// </summary>
internal Dictionary<string, string>? GetDocsetInfo()
{
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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<string, string> redirectPair in redirectsLookup)
Expand Down
Loading