feat: added optional path operations to ReadFiles and WriteFiles

This commit is contained in:
D. Moonfire 2023-09-03 18:33:40 -05:00
parent d2acd5a3c7
commit ddebc8e683
2 changed files with 50 additions and 3 deletions

View file

@ -5,6 +5,7 @@ using FluentValidation;
using MfGames.Gallium;
using MfGames.Nitride.Contents;
using MfGames.Nitride.Generators;
using MfGames.Nitride.IO.Paths;
using Zio;
@ -17,14 +18,18 @@ namespace MfGames.Nitride.IO.Contents;
[WithProperties]
public partial class ReadFiles : FileSystemOperationBase
{
private readonly Func<RemovePathPrefix> removePathPrefixFactory;
private readonly IValidator<ReadFiles> validator;
public ReadFiles(
IValidator<ReadFiles> validator,
IFileSystem fileSystem)
IFileSystem fileSystem,
Func<RemovePathPrefix> removePathPrefixFactory)
: base(fileSystem)
{
this.validator = validator;
this.removePathPrefixFactory = removePathPrefixFactory;
}
/// <summary>
@ -33,6 +38,13 @@ public partial class ReadFiles : FileSystemOperationBase
/// </summary>
public string Pattern { get; set; } = null!;
/// <summary>
/// Gets or sets the prefix to remove from the paths. This is convenience
/// method that is the same as calling ReadFiles, then RemovePathPrefix
/// with the parameter.
/// </summary>
public string? RemovePathPrefix { get; set; }
/// <summary>
/// Reads all files from the file system, filtering them out by the
/// minimatch pattern (as defined by DotNet.Blob).
@ -53,6 +65,7 @@ public partial class ReadFiles : FileSystemOperationBase
IEnumerable<Entity> input,
CancellationToken cancellationToken = default)
{
// Read the files from the file system.
this.validator.ValidateAndThrow(this);
var glob = Glob.Parse(this.Pattern);
@ -63,6 +76,15 @@ public partial class ReadFiles : FileSystemOperationBase
IEnumerable<Entity> entities = files.Select(this.ToEntity);
// If we have a remove prefix, then call that.
if (this.RemovePathPrefix != null)
{
RemovePathPrefix removePath = this.removePathPrefixFactory()
.WithPathPrefix(this.RemovePathPrefix);
entities = removePath.Run(entities, cancellationToken);
}
return entities;
}

View file

@ -5,6 +5,7 @@ using FluentValidation;
using MfGames.Gallium;
using MfGames.Nitride.Contents;
using MfGames.Nitride.Generators;
using MfGames.Nitride.IO.Paths;
using Serilog;
@ -18,6 +19,8 @@ namespace MfGames.Nitride.IO.Contents;
[WithProperties]
public partial class WriteFiles : FileSystemOperationBase, IOperation
{
private readonly Func<AddPathPrefix> addPathPrefixFactory;
private readonly IValidator<WriteFiles> validator;
private Dictionary<Type, Func<IContent, Stream>> factories;
@ -25,11 +28,13 @@ public partial class WriteFiles : FileSystemOperationBase, IOperation
public WriteFiles(
IValidator<WriteFiles> validator,
ILogger logger,
IFileSystem fileSystem)
IFileSystem fileSystem,
Func<AddPathPrefix> addPathPrefixFactory)
: base(fileSystem)
{
this.Logger = logger.ForContext<WriteFiles>();
this.validator = validator;
this.addPathPrefixFactory = addPathPrefixFactory;
this.TextEncoding = Encoding.UTF8;
this.factories = new Dictionary<Type, Func<IContent, Stream>>
@ -39,6 +44,13 @@ public partial class WriteFiles : FileSystemOperationBase, IOperation
};
}
/// <summary>
/// Gets or sets the optional prefix to add to the files before writing
/// out the files. This is the same as calling AddPathPrefix followed by
/// WriteFiles.
/// </summary>
public string? AddPathPrefix { get; set; }
public ILogger Logger { get; set; } = null!;
public Dictionary<Type, Func<IContent, Stream>> StreamFactories
@ -65,9 +77,22 @@ public partial class WriteFiles : FileSystemOperationBase, IOperation
IEnumerable<Entity> entities,
CancellationToken cancellationToken = default)
{
// Make sure everything is good.
this.validator.ValidateAndThrow(this);
IEnumerable<Entity> results = entities.SelectEntity<UPath>(this.Process)
// If we have the add prefix, then add it here. If we don't, then just
// pass it on without changes.
if (this.AddPathPrefix != null)
{
AddPathPrefix addPath = this.addPathPrefixFactory()
.WithPathPrefix(this.AddPathPrefix);
entities = addPath.Run(entities);
}
// Write out the files to the file system.
IEnumerable<Entity> results = entities
.SelectEntity<UPath>(this.Process)
.ToList();
return results;