Skip to content

Commit

Permalink
Add IPathComparer to IFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Jul 1, 2024
1 parent 02e1069 commit e1bde83
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Spectre.IO.Testing/FakeDirectoryContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class FakeDirectoryContent

public IReadOnlyDictionary<FilePath, FakeFile> Files => _files;

public FakeDirectoryContent(FakeDirectory owner, PathComparer comparer)
public FakeDirectoryContent(FakeDirectory owner, IPathComparer comparer)
{
Owner = owner;
_directories = new Dictionary<DirectoryPath, FakeDirectory>(comparer);
Expand Down
7 changes: 6 additions & 1 deletion src/Spectre.IO.Testing/FakeFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public sealed class FakeFileSystem : IFileSystem
private readonly FakeFileProvider _fileProvider;
private readonly FakeDirectoryProvider _directoryProvider;
private readonly IEnvironment _environment;
private readonly IPathComparer _comparer;

/// <inheritdoc/>
public IPathComparer Comparer => _comparer;

/// <inheritdoc/>
public IFileProvider File => _fileProvider;
Expand All @@ -28,7 +32,8 @@ public FakeFileSystem(IEnvironment environment)

_fileProvider = new FakeFileProvider(tree);
_directoryProvider = new FakeDirectoryProvider(tree);
_environment = environment;
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_comparer = new PathComparer(_environment.Platform.IsUnix());
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.IO.Testing/FakeFileSystemTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class FakeFileSystemTree
{
private readonly FakeDirectory _root;

public PathComparer Comparer { get; }
public IPathComparer Comparer { get; }

public FakeFileSystemTree(IEnvironment environment)
{
Expand Down
15 changes: 8 additions & 7 deletions src/Spectre.IO/DirectoryPathCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public sealed class DirectoryPathCollection : IEnumerable<DirectoryPath>
/// <value>The number of directories in the collection.</value>
public int Count => _paths.Count;

internal PathComparer Comparer { get; }
internal IPathComparer Comparer { get; }

/// <summary>
/// Initializes a new instance of the <see cref="DirectoryPathCollection"/> class.
/// </summary>
public DirectoryPathCollection()
: this(PathComparer.Default)
: this(Enumerable.Empty<DirectoryPath>(), null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DirectoryPathCollection"/> class.
/// </summary>
/// <param name="comparer">The comparer.</param>
public DirectoryPathCollection(PathComparer comparer)
public DirectoryPathCollection(IPathComparer comparer)
: this(Enumerable.Empty<DirectoryPath>(), comparer)
{
}
Expand All @@ -43,7 +43,7 @@ public DirectoryPathCollection(PathComparer comparer)
/// </summary>
/// <param name="paths">The paths.</param>
public DirectoryPathCollection(IEnumerable<DirectoryPath> paths)
: this(paths, PathComparer.Default)
: this(paths, null)
{
}

Expand All @@ -52,10 +52,11 @@ public DirectoryPathCollection(IEnumerable<DirectoryPath> paths)
/// </summary>
/// <param name="paths">The paths.</param>
/// <param name="comparer">The comparer.</param>
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <c>null</c>.</exception>
public DirectoryPathCollection(IEnumerable<DirectoryPath> paths, PathComparer comparer)
public DirectoryPathCollection(IEnumerable<DirectoryPath> paths, IPathComparer? comparer = null)
{
Comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
ArgumentNullException.ThrowIfNull(paths);

Comparer = comparer ?? PathComparer.Default;
_paths = new HashSet<DirectoryPath>(paths, comparer);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Spectre.IO/Extensions/DirectoryPathExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Spectre.IO;

Expand Down Expand Up @@ -34,4 +35,15 @@ public static DirectoryPath ExpandEnvironmentVariables(this DirectoryPath path,
var result = environment.ExpandEnvironmentVariables(path.FullPath);
return new DirectoryPath(result);
}

/// <summary>
/// Converts an <see cref="IEnumerable{DirectoryPath}"/> to a <see cref="DirectoryPathCollection"/>.
/// </summary>
/// <param name="source">The paths to add to the collection.</param>
/// <param name="comparer">The comparer to use. If <c>null</c>, the default one is used.</param>
/// <returns>A new <see cref="DirectoryPathCollection"/>.</returns>
public static DirectoryPathCollection ToPathCollection(this IEnumerable<DirectoryPath> source, IPathComparer? comparer = null)
{
return new DirectoryPathCollection(source, comparer);
}
}
12 changes: 12 additions & 0 deletions src/Spectre.IO/Extensions/FilePathExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Spectre.IO;

Expand Down Expand Up @@ -34,4 +35,15 @@ public static FilePath ExpandEnvironmentVariables(this FilePath path, IEnvironme
var result = environment.ExpandEnvironmentVariables(path.FullPath);
return new FilePath(result);
}

/// <summary>
/// Converts an <see cref="IEnumerable{FilePath}"/> to a <see cref="FilePathCollection"/>.
/// </summary>
/// <param name="source">The paths to add to the collection.</param>
/// <param name="comparer">The comparer to use. If <c>null</c>, the default one is used.</param>
/// <returns>A new <see cref="FilePathCollection"/>.</returns>
public static FilePathCollection ToPathCollection(this IEnumerable<FilePath> source, IPathComparer? comparer = null)
{
return new FilePathCollection(source, comparer);
}
}
20 changes: 20 additions & 0 deletions src/Spectre.IO/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace Spectre.IO;

/// <summary>
/// Contains extensions for <see cref="Path"/>.
/// </summary>
public static class PathExtensions
{
/// <summary>
/// Converts an <see cref="IEnumerable{Path}"/> to a <see cref="PathCollection"/>.
/// </summary>
/// <param name="source">The paths to add to the collection.</param>
/// <param name="comparer">The comparer to use. If <c>null</c>, the default one is used.</param>
/// <returns>A new <see cref="PathCollection"/>.</returns>
public static PathCollection ToPathCollection(this IEnumerable<Path> source, IPathComparer? comparer = null)
{
return new PathCollection(source, comparer);
}
}
16 changes: 9 additions & 7 deletions src/Spectre.IO/FilePathCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public sealed class FilePathCollection : IEnumerable<FilePath>
/// <value>The number of files in the collection.</value>
public int Count => _paths.Count;

internal PathComparer Comparer { get; }
internal IPathComparer Comparer { get; }

/// <summary>
/// Initializes a new instance of the <see cref="FilePathCollection"/> class.
/// </summary>
public FilePathCollection()
: this(PathComparer.Default)
: this(Enumerable.Empty<FilePath>(), null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="FilePathCollection"/> class.
/// </summary>
/// <param name="comparer">The comparer.</param>
public FilePathCollection(PathComparer comparer)
public FilePathCollection(IPathComparer comparer)
: this(Enumerable.Empty<FilePath>(), comparer)
{
}
Expand All @@ -43,7 +43,7 @@ public FilePathCollection(PathComparer comparer)
/// </summary>
/// <param name="paths">The paths.</param>
public FilePathCollection(IEnumerable<FilePath> paths)
: this(paths, PathComparer.Default)
: this(paths, null)
{
}

Expand All @@ -53,10 +53,12 @@ public FilePathCollection(IEnumerable<FilePath> paths)
/// <param name="paths">The paths.</param>
/// <param name="comparer">The comparer.</param>
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <c>null</c>.</exception>
public FilePathCollection(IEnumerable<FilePath> paths, PathComparer comparer)
public FilePathCollection(IEnumerable<FilePath> paths, IPathComparer? comparer = null)
{
Comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
_paths = new HashSet<FilePath>(paths, comparer);
ArgumentNullException.ThrowIfNull(paths);

Comparer = comparer ?? PathComparer.Default;
_paths = new HashSet<FilePath>(paths, Comparer);
}

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions src/Spectre.IO/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Spectre.IO;
using System;

namespace Spectre.IO;

/// <summary>
/// A physical file system implementation.
Expand All @@ -10,6 +12,9 @@ public sealed class FileSystem : IFileSystem
/// </summary>
public static FileSystem Shared { get; } = new();

/// <inheritdoc/>
public IPathComparer Comparer { get; }

/// <inheritdoc/>
public IFileProvider File { get; }

Expand All @@ -19,8 +24,10 @@ public sealed class FileSystem : IFileSystem
/// <summary>
/// Initializes a new instance of the <see cref="FileSystem"/> class.
/// </summary>
public FileSystem()
/// <param name="comparer">The path comparer to use.</param>
public FileSystem(IPathComparer? comparer = null)
{
Comparer = comparer ?? PathComparer.Default;
File = new FileProvider();
Directory = new DirectoryProvider();
}
Expand Down
27 changes: 9 additions & 18 deletions src/Spectre.IO/Globber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Spectre.IO;
/// </summary>
public sealed class Globber : IGlobber
{
private readonly IPathComparer _comparer;
private readonly GlobParser _parser;
private readonly GlobVisitor _visitor;

Expand All @@ -25,42 +26,32 @@ public sealed class Globber : IGlobber
/// <param name="environment">The environment.</param>
public Globber(IFileSystem fileSystem, IEnvironment environment)
{
if (fileSystem == null)
{
throw new ArgumentNullException(nameof(fileSystem));
}

if (environment is null)
{
throw new ArgumentNullException(nameof(environment));
}
ArgumentNullException.ThrowIfNull(fileSystem);
ArgumentNullException.ThrowIfNull(environment);

_comparer = fileSystem.Comparer;
_parser = new GlobParser(environment);
_visitor = new GlobVisitor(fileSystem, environment);
}

/// <inheritdoc/>
public IEnumerable<Path> Match(string pattern, GlobberSettings settings)
{
if (pattern == null)
{
throw new ArgumentNullException(nameof(pattern));
}
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(pattern);

if (string.IsNullOrWhiteSpace(pattern))
{
return Enumerable.Empty<Path>();
}

// Make sure we got some settings.
settings ??= new GlobberSettings();

// Parse the pattern into an AST.
var root = _parser.Parse(pattern, settings.Comparer ?? PathComparer.Default);
var comparer = settings.Comparer ?? _comparer;
var root = _parser.Parse(pattern, comparer);

// Visit all nodes in the parsed patterns and filter the result.
return _visitor.Walk(root, settings)
.Select(x => x.Path)
.Distinct(settings.Comparer ?? PathComparer.Default);
.Distinct(comparer);
}
}
2 changes: 1 addition & 1 deletion src/Spectre.IO/GlobberSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public sealed class GlobberSettings
/// <summary>
/// Gets or sets the comparer to use.
/// </summary>
public PathComparer? Comparer { get; set; }
public IPathComparer? Comparer { get; set; }
}
5 changes: 5 additions & 0 deletions src/Spectre.IO/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
/// </summary>
public interface IFileSystem
{
/// <summary>
/// Gets the file system's <see cref="IPathComparer"/>.
/// </summary>
IPathComparer Comparer { get; }

/// <summary>
/// Gets the <see cref="IFileProvider"/> belonging to this <see cref="IFileSystem"/>>.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Spectre.IO/IPathComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;

namespace Spectre.IO;

/// <summary>
/// Compares <see cref="Path"/> instances.
/// </summary>
public interface IPathComparer : IEqualityComparer<Path?>, IComparer<Path?>
{
/// <summary>
/// Gets a value indicating whether comparison is case sensitive.
/// </summary>
/// <value>
/// <c>true</c> if comparison is case sensitive; otherwise, <c>false</c>.
/// </value>
bool IsCaseSensitive { get; }
}
2 changes: 1 addition & 1 deletion src/Spectre.IO/Internal/Globbing/GlobParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public GlobParser(IEnvironment environment)
_environment = environment;
}

public GlobNode Parse(string pattern, PathComparer comparer)
public GlobNode Parse(string pattern, IPathComparer comparer)
{
return Parse(new GlobParserContext(pattern, comparer));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.IO/Internal/Globbing/GlobParserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class GlobParserContext
public GlobToken? CurrentToken { get; private set; }
public RegexOptions Options { get; }

public GlobParserContext(string pattern, PathComparer comparer)
public GlobParserContext(string pattern, IPathComparer comparer)
{
_buffer = GlobTokenizer.Tokenize(pattern);

Expand Down
Loading

0 comments on commit e1bde83

Please sign in to comment.