2022-06-05 18:44:51 +00:00
|
|
|
using System;
|
2021-09-07 05:15:45 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using DotNet.Globbing;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Gallium;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Nitride.Contents;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using Zio;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.IO.Contents;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A module that reads files from the file system and wraps them in an
|
|
|
|
/// entity with the following components: UPath, IContent.
|
|
|
|
/// </summary>
|
|
|
|
[WithProperties]
|
|
|
|
public partial class ReadFiles : FileSystemOperationBase
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
private readonly IValidator<ReadFiles> validator;
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
public ReadFiles(
|
|
|
|
IValidator<ReadFiles> validator,
|
|
|
|
IFileSystem fileSystem)
|
2022-06-05 18:44:51 +00:00
|
|
|
: base(fileSystem)
|
|
|
|
{
|
|
|
|
this.validator = validator;
|
|
|
|
}
|
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
/// <summary>
|
2022-06-05 18:44:51 +00:00
|
|
|
/// Gets or sets the file pattern to retrieve fields. This supports
|
|
|
|
/// globbing both "*" and "**" patterns such as "**/*".
|
2021-09-07 05:15:45 +00:00
|
|
|
/// </summary>
|
2022-06-05 18:44:51 +00:00
|
|
|
public string Pattern { get; set; } = null!;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads all files from the file system, filtering them out by the
|
|
|
|
/// minimatch pattern (as defined by DotNet.Blob).
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A populated collection of entities.</returns>
|
|
|
|
public IEnumerable<Entity> Run()
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
return this.Run(Array.Empty<Entity>());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads all files from the file system, filtering them out by the
|
|
|
|
/// minimatch pattern (as defined by DotNet.Blob).
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A populated collection of entities.</returns>
|
|
|
|
public override IEnumerable<Entity> Run(IEnumerable<Entity> input)
|
|
|
|
{
|
|
|
|
this.validator.ValidateAndThrow(this);
|
|
|
|
|
|
|
|
var glob = Glob.Parse(this.Pattern);
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
IEnumerable<FileEntry> files = this.FileSystem.EnumerateFileEntries("/", "*", SearchOption.AllDirectories)
|
|
|
|
.Where(x => glob.IsMatch(x.Path.ToString()));
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
IEnumerable<Entity> entities = files.Select(this.ToEntity);
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ReadFiles WithFileSystem(IFileSystem value)
|
|
|
|
{
|
|
|
|
this.FileSystem = value;
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an entity with the standard components for all Zio-based
|
|
|
|
/// files. This attaches the file's path relative to the file system
|
|
|
|
/// and a way of accessing the content from the file system.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="file">The Zio file entry.</param>
|
|
|
|
/// <returns>An Entity with appropriate content.</returns>
|
|
|
|
private Entity ToEntity(FileEntry file)
|
|
|
|
{
|
2022-07-09 04:52:10 +00:00
|
|
|
Entity entity = new Entity().Set(file.Path)
|
|
|
|
.SetBinaryContent(new FileEntryBinaryContent(file));
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
return entity;
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
|
|
|
}
|