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
25 changes: 9 additions & 16 deletions src/PSTree/Commands/GetPSTreeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override void ProcessRecord()
private TreeFileSystemInfo[] Traverse(TreeDirectory directory)
{
_cache.Clear();
_stack.Push(directory);
directory.PushToStack(_stack);
string source = directory.FullName;

while (_stack.Count > 0)
Expand Down Expand Up @@ -105,12 +105,11 @@ private TreeFileSystemInfo[] Traverse(TreeDirectory directory)

if (keepProcessing)
{
TreeFile file = TreeFile
TreeFile
.Create(fileInfo, source, level)
.AddParent<TreeFile>(next)
.SetIncludeFlagIf(WithInclude);

_cache.Add(file);
.SetIncludeFlagIf(WithInclude)
.AddToCache(_cache);
}

continue;
Expand All @@ -121,16 +120,11 @@ private TreeFileSystemInfo[] Traverse(TreeDirectory directory)
continue;
}

TreeDirectory dir = TreeDirectory
TreeDirectory
.Create((DirectoryInfo)item, source, level)
.AddParent<TreeDirectory>(next);

if (keepProcessing && Directory || !WithInclude)
{
dir.Include = true;
}

_stack.Push(dir);
.AddParent<TreeDirectory>(next)
.SetIncludeFlagIf(keepProcessing && Directory || !WithInclude)
.PushToStack(_stack);
}

next.Length = totalLength;
Expand Down Expand Up @@ -160,8 +154,7 @@ private TreeFileSystemInfo[] Traverse(TreeDirectory directory)
return GetTree(WithInclude && !Directory);
}

private static bool IsHidden(FileSystemInfo item) =>
item.Attributes.HasFlag(FileAttributes.Hidden);
private static bool IsHidden(FileSystemInfo item) => item.Attributes.HasFlag(FileAttributes.Hidden);

private TreeFileSystemInfo[] GetTree(bool includeCondition)
{
Expand Down
39 changes: 20 additions & 19 deletions src/PSTree/Commands/GetPSTreeRegistryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@
}
}

private TreeRegistryBase[] Traverse(RegistryKey key)
private TreeRegistryBase[] Traverse(RegistryKey registryKey)
{
_cache.Clear();
_stack.Push(key.CreateTreeKey(System.IO.Path.GetFileName(key.Name)));
string source = key.Name;

registryKey.
CreateTreeKey(System.IO.Path.GetFileName(registryKey.Name)).
PushToStack(_stack);

Check warning on line 61 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L59-L61

Added lines #L59 - L61 were not covered by tests

string source = registryKey.Name;

Check warning on line 63 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L63

Added line #L63 was not covered by tests

while (_stack.Count > 0)
{
(TreeRegistryKey tree, key) = _stack.Pop();
(TreeRegistryKey tree, registryKey) = _stack.Pop();

Check warning on line 67 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L67

Added line #L67 was not covered by tests
int depth = tree.Depth + 1;

using (key)
using (registryKey)

Check warning on line 70 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L70

Added line #L70 was not covered by tests
{
if (depth <= Depth)
{
Expand All @@ -72,22 +76,21 @@
goto PushKeys;
}

foreach (string value in key.GetValueNames())
foreach (string value in registryKey.GetValueNames())
{
if (ShouldSkipValue(value))
{
continue;
}

TreeRegistryValue treevalue = new(key, value, source, depth);

_cache.Add(treevalue
new TreeRegistryValue(registryKey, value, source, depth)

Check warning on line 86 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L86

Added line #L86 was not covered by tests
.AddParent<TreeRegistryValue>(tree)
.SetIncludeFlagIf(WithInclude));
.SetIncludeFlagIf(WithInclude)
.AddToCache(_cache);

Check warning on line 89 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L88-L89

Added lines #L88 - L89 were not covered by tests
}

PushKeys:
foreach (string keyname in key.GetSubKeyNames())
foreach (string keyname in registryKey.GetSubKeyNames())
{
if (ShouldExclude(keyname))
{
Expand All @@ -96,22 +99,21 @@

try
{
RegistryKey? subkey = key.OpenSubKey(keyname);
RegistryKey? subkey = registryKey.OpenSubKey(keyname);

Check warning on line 102 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L102

Added line #L102 was not covered by tests

if (subkey is null)
{
continue;
}

(TreeRegistryKey, RegistryKey) treekey = subkey
subkey

Check warning on line 109 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L109

Added line #L109 was not covered by tests
.CreateTreeKey(keyname, source, depth)
.AddParent(tree);

_stack.Push(treekey);
.AddParent(tree)
.PushToStack(_stack);

Check warning on line 112 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L111-L112

Added lines #L111 - L112 were not covered by tests
}
catch (SecurityException exception)
{
string path = System.IO.Path.Combine(key.Name, keyname);
string path = System.IO.Path.Combine(registryKey.Name, keyname);

Check warning on line 116 in src/PSTree/Commands/GetPSTreeRegistryCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Commands/GetPSTreeRegistryCommand.cs#L116

Added line #L116 was not covered by tests
WriteError(exception.ToSecurityError(path));
}
}
Expand All @@ -125,8 +127,7 @@
return _cache.GetResult(WithInclude && !KeysOnly).Format();
}

private bool ShouldSkipValue(string value) =>
string.IsNullOrEmpty(value) || ShouldExclude(value) || !ShouldInclude(value);
private bool ShouldSkipValue(string value) => ShouldExclude(value) || !ShouldInclude(value);

private bool TryGetKey(string path, [NotNullWhen(true)] out RegistryKey? key)
{
Expand Down
19 changes: 19 additions & 0 deletions src/PSTree/Extensions/TreeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@
return tree;
}

internal static void AddToCache<TBase, TLeaf>(this TLeaf leaf, Cache<TBase, TLeaf> cache)
where TLeaf : TBase
where TBase : ITree
{
cache.Add(leaf);
}

internal static void PushToStack(this TreeDirectory directory, Stack<TreeDirectory> stack)
{
stack.Push(directory);
}

#if NETCOREAPP
[SkipLocalsInit]
#endif
Expand Down Expand Up @@ -188,6 +200,13 @@
return treeKey;
}

internal static void PushToStack(
this (TreeRegistryKey, RegistryKey) treeKey,
Stack<(TreeRegistryKey, RegistryKey)> stack)
{
stack.Push(treeKey);
}

Check warning on line 208 in src/PSTree/Extensions/TreeExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/Extensions/TreeExtensions.cs#L206-L208

Added lines #L206 - L208 were not covered by tests

internal static void Deconstruct(
this string[] strings,
out string baseKey,
Expand Down
10 changes: 10 additions & 0 deletions src/PSTree/TreeDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ internal void IndexLength(long length)
i.Length += length;
}
}

internal TreeDirectory SetIncludeFlagIf(bool condition)
{
if (condition)
{
Include = true;
}

return this;
}
}
14 changes: 10 additions & 4 deletions src/PSTree/TreeRegistryValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
{
private readonly string _parentPath;

private readonly string _valueName;

internal override bool Include { get; set; } = true;

public RegistryValueKind Kind { get; }
Expand All @@ -22,14 +24,18 @@
base(string.Empty, source)
{
_parentPath = key.Name;
Kind = key.GetValueKind(value);
Hierarchy = GetColoredName(value, Kind).Indent(depth);
_valueName = value;

Check warning on line 27 in src/PSTree/TreeRegistryValue.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/TreeRegistryValue.cs#L27

Added line #L27 was not covered by tests
Depth = depth;
Name = value;
Name = GetNameOrDefault(value);
Kind = key.GetValueKind(value);
Hierarchy = GetColoredName(Name, Kind).Indent(depth);

Check warning on line 31 in src/PSTree/TreeRegistryValue.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/TreeRegistryValue.cs#L29-L31

Added lines #L29 - L31 were not covered by tests
PSParentPath = $"{_providerPath}{_parentPath}";
}

public object? GetValue() => Registry.GetValue(_parentPath, Name, null);
private static string GetNameOrDefault(string value) =>
string.IsNullOrEmpty(value) ? "(Default)" : value;

public object? GetValue() => Registry.GetValue(_parentPath, _valueName, null);

Check warning on line 38 in src/PSTree/TreeRegistryValue.cs

View check run for this annotation

Codecov / codecov/patch

src/PSTree/TreeRegistryValue.cs#L38

Added line #L38 was not covered by tests

private static string GetColoredName(string name, RegistryValueKind kind) =>
TreeStyle.Instance.Registry.GetColoredValue(name, kind);
Expand Down