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

Improves how -Include works #39

Merged
merged 19 commits into from
Jan 19, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
code improvements. simplifies logic.
santisq committed Jan 16, 2025
commit 9a503b8cbd5abcbd60e0233707ea6f8978465f03
31 changes: 14 additions & 17 deletions src/PSTree/Commands/GetPSTreeCommand.cs
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@ public sealed class GetPSTreeCommand : CommandWithPathBase

private WildcardPattern[]? _includePatterns;

private readonly PSTreeIndexer _indexer = new();

private readonly Stack<PSTreeDirectory> _stack = new();

private readonly PSTreeCache _cache = new();
@@ -77,27 +75,23 @@ protected override void ProcessRecord()
{
foreach (string path in EnumerateResolvedPaths())
{
string source = path.TrimExcess();

if (File.Exists(source))
if (File.Exists(path))
{
WriteObject(PSTreeFile.Create(new FileInfo(source), source));
WriteObject(PSTreeFile.Create(path));
continue;
}

WriteObject(
Traverse(new DirectoryInfo(source), source),
Traverse(PSTreeDirectory.Create(path)),
enumerateCollection: true);
}
}

private PSTreeFileSystemInfo[] Traverse(
DirectoryInfo directory,
string source)
private PSTreeFileSystemInfo[] Traverse(PSTreeDirectory directory)
{
_indexer.Clear();
_cache.Clear();
_stack.Push(PSTreeDirectory.Create(directory, source));
_stack.Push(directory);
string source = directory.FullName;

while (_stack.Count > 0)
{
@@ -112,6 +106,7 @@ private PSTreeFileSystemInfo[] Traverse(
foreach (FileSystemInfo item in next.GetSortedEnumerable(_comparer))
{
childCount++;

if (!Force.IsPresent && item.IsHidden())
{
continue;
@@ -141,18 +136,20 @@ private PSTreeFileSystemInfo[] Traverse(

if (keepProcessing || RecursiveSize.IsPresent)
{
_stack.Push(PSTreeDirectory.Create(
(DirectoryInfo)item, source, level));
PSTreeDirectory dir = PSTreeDirectory
.Create((DirectoryInfo)item, source, level)
.WithParent(next);

_stack.Push(dir);
}
}

next.Length = size;
_indexer[next.FullName] = next;
_indexer.IndexItemCount(next, childCount);
next.IndexItemCount(childCount);

if (RecursiveSize.IsPresent)
{
_indexer.IndexLength(next, size);
next.IndexLength(size);
}

if (next.Depth <= Depth)
File renamed without changes.
19 changes: 0 additions & 19 deletions src/PSTree/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
@@ -6,8 +6,6 @@ namespace PSTree.Extensions;

internal static class PathExtensions
{
private static readonly char[] _dirSeparator = @"\/".ToCharArray();

internal static bool Validate(
this ProviderInfo provider,
string path,
@@ -29,23 +27,6 @@ internal static bool Exists(this string path) =>
internal static bool IsHidden(this FileSystemInfo item) =>
item.Attributes.HasFlag(FileAttributes.Hidden);

internal static string TrimExcess(this string path)
{
path = path.TrimEnd(_dirSeparator);

if (string.IsNullOrWhiteSpace(path))
{
return System.IO.Path.DirectorySeparatorChar.ToString();
}

if (System.IO.Path.GetPathRoot(path) == path)
{
return string.Concat(path.ToUpper(), System.IO.Path.DirectorySeparatorChar);
}

return path;
}

private static bool MatchAny(
FileSystemInfo item,
WildcardPattern[] patterns)
File renamed without changes.
44 changes: 28 additions & 16 deletions src/PSTree/PSTreeDirectory.cs
Original file line number Diff line number Diff line change
@@ -8,9 +8,7 @@ namespace PSTree;

public sealed class PSTreeDirectory : PSTreeFileSystemInfo<DirectoryInfo>
{
private string[]? _parents;

internal string[] Parents { get => _parents ??= GetParents(FullName); }
private PSTreeDirectory? _parent;

public DirectoryInfo Parent => Instance.Parent;

@@ -37,25 +35,14 @@ public IEnumerable<DirectoryInfo> EnumerateDirectories() =>
public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos() =>
Instance.EnumerateFileSystemInfos();

private static string[] GetParents(string path)
{
int index = -1;
List<string> parents = [];

while ((index = path.IndexOf(Path.DirectorySeparatorChar, index + 1)) != -1)
{
parents.Add(path.Substring(0, index));
}

return [.. parents];
}

internal IOrderedEnumerable<FileSystemInfo> GetSortedEnumerable(PSTreeComparer comparer) =>
Instance
.EnumerateFileSystemInfos()
.OrderBy(static e => e is DirectoryInfo)
.ThenBy(static e => e, comparer);

internal static PSTreeDirectory Create(string path) => Create(new DirectoryInfo(path), path);

internal static PSTreeDirectory Create(DirectoryInfo dir, string source)
{
string styled = TreeStyle.Instance.GetColoredName(dir);
@@ -67,4 +54,29 @@ internal static PSTreeDirectory Create(DirectoryInfo dir, string source, int dep
string styled = TreeStyle.Instance.GetColoredName(dir).Indent(depth);
return new PSTreeDirectory(dir, styled, source, depth);
}

internal PSTreeDirectory WithParent(PSTreeDirectory parent)
{
_parent = parent;
return this;
}

internal void IndexItemCount(int count)
{
ItemCount = count;
TotalItemCount = count;

for (PSTreeDirectory? parent = _parent; parent is not null; parent = parent._parent)
{
parent.TotalItemCount += count;
}
}

internal void IndexLength(long length)
{
for (PSTreeDirectory? parent = _parent; parent is not null; parent = parent._parent)
{
parent.Length += length;
}
}
}
2 changes: 2 additions & 0 deletions src/PSTree/PSTreeFile.cs
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ private PSTreeFile(
Length = file.Length;
}

internal static PSTreeFile Create(string path) => Create(new FileInfo(path), path);

internal static PSTreeFile Create(FileInfo file, string source)
{
string styled = TreeStyle.Instance.GetColoredName(file);
41 changes: 0 additions & 41 deletions src/PSTree/PSTreeIndexer.cs

This file was deleted.

6 changes: 3 additions & 3 deletions tools/requiredModules.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
InvokeBuild = '5.11.2'
InvokeBuild = '5.12.1'
platyPS = '0.14.2'
PSScriptAnalyzer = '1.22.0'
Pester = '5.6.0'
PSScriptAnalyzer = '1.23.0'
Pester = '5.7.1'
}