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
1 change: 1 addition & 0 deletions cleanrepo/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class Options
public string? FilterTextJsonFile { get; set; }
public List<string>? ReferencingDirectories { get; set; }
public List<string>? ExcludeSnippetDescendantsOfFolders { get; set; }
public List<string>? ExcludeOrphanedIncludeFiles { get; set; }
}
38 changes: 36 additions & 2 deletions cleanrepo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ static async Task RunOptions(Options options)
else
Console.WriteLine($"\nChecking {includeFiles.Count} include files.");

ListOrphanedIncludes(options.TargetDirectory, includeFiles, options.Delete);
ListOrphanedIncludes(options.TargetDirectory, includeFiles, options.Delete, options.ExcludeOrphanedIncludeFiles);
break;
}
case "FindOrphanedSnippets":
Expand Down Expand Up @@ -618,10 +618,38 @@ string originalLink
/// For each markdown file
/// Do a RegEx search for the include file
/// If found, BREAK to the next include file
private static void ListOrphanedIncludes(string inputDirectory, Dictionary<string, int> includeFiles, bool deleteOrphanedIncludes)
private static void ListOrphanedIncludes(string inputDirectory, Dictionary<string, int> includeFiles, bool deleteOrphanedIncludes, List<string>? excludedIncludeFiles)
{
DirectoryInfo? rootDirectory = null;

HashSet<string> excludedFileNames = new(StringComparer.InvariantCultureIgnoreCase);
HashSet<string> excludedFullPaths = new(StringComparer.InvariantCultureIgnoreCase);

if (excludedIncludeFiles is { Count: > 0 })
{
foreach (string excludedIncludeFile in excludedIncludeFiles)
{
if (string.IsNullOrWhiteSpace(excludedIncludeFile))
continue;

string candidate = excludedIncludeFile.Trim();

if (candidate.IndexOfAny([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]) >= 0 ||
Path.IsPathRooted(candidate))
{
string fullPath = Path.IsPathRooted(candidate)
? Path.GetFullPath(candidate)
: Path.GetFullPath(Path.Combine(inputDirectory, candidate));

excludedFullPaths.Add(fullPath);
}
else
{
excludedFileNames.Add(candidate);
}
}
}

// Get all files that could possibly link to the include files
List<FileInfo>? mdfiles = HelperMethods.GetAllReferencingFiles("*.md", inputDirectory, ref rootDirectory);
List<FileInfo>? ymlfiles = HelperMethods.GetAllReferencingFiles("*.yml", inputDirectory, ref rootDirectory);
Expand Down Expand Up @@ -691,6 +719,12 @@ private static void ListOrphanedIncludes(string inputDirectory, Dictionary<strin
StringBuilder output = new();
foreach (KeyValuePair<string, int> includeFile in includeFiles)
{
if (excludedFullPaths.Contains(Path.GetFullPath(includeFile.Key)) ||
excludedFileNames.Contains(Path.GetFileName(includeFile.Key)))
{
includeFiles[includeFile.Key] = 1;
}

// Special case - consider license.md and readme.md as "referenced"
// even if they aren't explicitly referenced in any other file.
if (string.Equals(Path.GetFileName(includeFile.Key), "LICENSE.md", StringComparison.InvariantCultureIgnoreCase) ||
Expand Down
Loading