Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter '--max-depth' #44

Merged
merged 17 commits into from
Aug 19, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add command `regex create` ([#45](https://github.com/josefpihrt/roslynator/pull/45)).
- Add parameter `--max-depth` for maximum directory depth ([#44](https://github.com/josefpihrt/roslynator/pull/44)).
- Add property `Orang.FileSystem.DirectoryMatcher.MaxDepth`

## [0.4.0] - 2023-08-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ internal abstract class FileSystemCommandLineOptions : CommonRegexCommandLineOpt
HelpText = "Include line number.")]
public bool LineNumber { get; set; }

[Option(
longName: OptionNames.MaxDepth,
HelpText = "Maximum directory depth.",
MetaValue = MetaValues.Num,
Default = int.MaxValue)]
public int MaxDepth { get; set; }

[HideFromConsoleHelp]
[Option(
shortName: OptionShortNames.NoContent,
Expand Down Expand Up @@ -287,6 +294,7 @@ public bool TryParse(FileSystemCommandOptions options, ParseContext context)
options.ModifiedTimePredicate = modifiedTimePredicate;
options.SizePredicate = sizePredicate;
options.AlignColumns = AlignColumns;
options.MaxDirectoryDepth = MaxDepth;

options.FilePropertyOptions = new FilePropertyOptions(
includeCreationTime: creationTime,
Expand Down
2 changes: 2 additions & 0 deletions src/CommandLine/CommandOptions/FileSystemCommandOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ internal FileSystemCommandOptions()

public FilterPredicate<long>? SizePredicate { get; internal set; }

public int MaxDirectoryDepth { get; internal set; }

public ContentDisplayStyle ContentDisplayStyle => Format.ContentDisplayStyle;

public bool OmitContent => ContentDisplayStyle == ContentDisplayStyle.Omit;
Expand Down
1 change: 1 addition & 0 deletions src/CommandLine/Commands/FileSystemCommand`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private SearchState CreateSearch()
LogProgress = (ProgressReporter is not null) ? p => ProgressReporter.Report(p) : null,
RecurseSubdirectories = Options.RecurseSubdirectories,
DefaultEncoding = Options.DefaultEncoding,
MaxDirectoryDepth = Options.MaxDirectoryDepth,
};

OnSearchCreating(search);
Expand Down
1 change: 1 addition & 0 deletions src/CommandLine/Names/OptionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal static class OptionNames
public const string Literal = "literal";
public const string Manual = "manual";
public const string MaxCount = "max-count";
public const string MaxDepth = "max-depth";
public const string MaxMatchesInFile = "max-matches-in-file";
public const string MaxMatchingFiles = "max-matching-files";
public const string MaxWordLength = "max-word-length";
Expand Down
2 changes: 2 additions & 0 deletions src/FileSystem/FileSystem/DirectoryMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class DirectoryMatcher

public FileEmptyOption EmptyOption { get; set; }

public int MaxDepth { get; set; } = int.MaxValue;

internal Func<DirectoryInfo, bool>? MatchDirectoryInfo { get; set; }

internal (FileMatch? FileMatch, Exception? Exception) Match(string path)
Expand Down
9 changes: 9 additions & 0 deletions src/FileSystem/FileSystem/Fluent/DirectoryMatcherBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class DirectoryMatcherBuilder
private FileAttributes? _attributes;
private FileAttributes? _attributesToSkip;
private FileEmptyOption? _emptyOption;
private int _maxDepth = int.MaxValue;

internal DirectoryMatcherBuilder()
{
Expand Down Expand Up @@ -67,6 +68,13 @@ public DirectoryMatcherBuilder NonEmpty()
return this;
}

public DirectoryMatcherBuilder MaxDepth(int maxDepth)
{
_maxDepth = maxDepth;

return this;
}

internal DirectoryMatcher Build()
{
return new DirectoryMatcher()
Expand All @@ -76,6 +84,7 @@ internal DirectoryMatcher Build()
WithAttributes = _attributes ?? 0,
WithoutAttributes = _attributesToSkip ?? 0,
EmptyOption = _emptyOption ?? FileEmptyOption.None,
MaxDepth = _maxDepth,
};
}
}
31 changes: 15 additions & 16 deletions src/FileSystem/FileSystem/SearchState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public SearchState(FileMatcher? matcher, DirectoryMatcher? directoryMatcher)

public SearchTelemetry? Telemetry { get; set; }

public int MinDirectoryDepth { get; set; }

public int MaxDirectoryDepth { get; set; } = int.MaxValue;

public IEnumerable<FileMatch> Find(
string directoryPath,
CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -89,14 +93,15 @@ internal IEnumerable<FileMatch> Find(
: MatchStatus.FailFromPositive;
}

var directory = new Directory(directoryPath, matchStatus);
var directory = new Directory(directoryPath, 0, matchStatus);

while (true)
{
Report(directory.Path, SearchProgressKind.SearchDirectory, isDirectory: true);

if (FileMatcher is not null
&& !directory.IsFail)
&& !directory.IsFail
&& directory.Depth >= MinDirectoryDepth)
{
IEnumerator<string> fi = null!;

Expand Down Expand Up @@ -157,7 +162,8 @@ internal IEnumerable<FileMatch> Find(

if (!directory.IsFail
&& DirectoryMatcher is not null
&& DirectoryMatcher?.NamePart != FileNamePart.Extension)
&& DirectoryMatcher?.NamePart != FileNamePart.Extension
&& directory.Depth >= MinDirectoryDepth)
{
if (matchStatus == MatchStatus.Unknown
&& (IncludeDirectory is not null || ExcludeDirectory is not null))
Expand Down Expand Up @@ -189,8 +195,11 @@ internal IEnumerable<FileMatch> Find(
matchStatus = IncludeOrExcludeDirectory(currentDirectory);
}

if (matchStatus != MatchStatus.FailFromNegative)
subdirectories!.Enqueue(new Directory(currentDirectory, matchStatus));
if (matchStatus != MatchStatus.FailFromNegative
&& directory.Depth < MaxDirectoryDepth)
{
subdirectories!.Enqueue(new Directory(currentDirectory, directory.Depth + 1, matchStatus));
}
}

cancellationToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -287,18 +296,8 @@ private static bool IsWellKnownException(Exception ex)
}

[DebuggerDisplay("{DebuggerDisplay,nq}")]
private readonly struct Directory
private readonly record struct Directory(string Path, int Depth, MatchStatus Status)
{
public Directory(string path, MatchStatus status)
{
Path = path;
Status = status;
}

public string Path { get; }

public MatchStatus Status { get; }

public bool IsSuccess => Status == MatchStatus.Success;

public bool IsFail => Status == MatchStatus.FailFromPositive || Status == MatchStatus.FailFromNegative;
Expand Down