This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-nitride-cil/src/MfGames.Nitride.IO/NitrideIOEnumerableEntityEx...

91 lines
2.6 KiB
C#

using System.Collections.Generic;
using MfGames.Gallium;
using MAB.DotIgnore;
using Zio;
namespace MfGames.Nitride.IO;
/// <summary>
/// Extension methods for working with paths.
/// </summary>
public static class NitrideIOEnumerableEntityExtensions
{
/// <summary>
/// Retrieves an entity from the sequence of entities.
/// </summary>
/// <param name="input">The sequence of iterations to parse.</param>
/// <param name="path">The path to search for.</param>
/// <param name="foundEntity">The entity pulled out, if found. Otherwise null.</param>
/// <param name="removeFromResults">If true, then remove the entity from the list.</param>
/// <returns>The sequence of entities, optionally without one.</returns>
public static IEnumerable<Entity> GetEntityByPath(
this IEnumerable<Entity> input,
UPath path,
out Entity? foundEntity,
IfFoundOutput removeFromResults = IfFoundOutput.RemoveFromOutput)
{
List<Entity> output = new();
foundEntity = null;
foreach (Entity entity in input)
{
// If we don't have a path, it isn't it.
if (!entity.TryGet(out UPath entityPath))
{
output.Add(entity);
continue;
}
// See if the path matches. If it doesn't, then return it.
if (entityPath != path)
{
output.Add(entity);
continue;
}
// We found the entity, so optionally return it.
foundEntity = entity;
if (removeFromResults == IfFoundOutput.ReturnInOutput)
{
output.Add(entity);
}
}
// Return the resulting output.
return output;
}
/// <summary>
/// Filters out entities that match a .gitignore style list and returns
/// the remaining entities.
/// </summary>
public static IEnumerable<Entity> WhereNotIgnored(
this IEnumerable<Entity> input,
IgnoreList ignoreList)
{
foreach (Entity entity in input)
{
// If we don't have a path, nothing to do.
if (!entity.TryGet(out UPath path))
{
yield return entity;
}
// See if the path matches. We use the "path is directory" set to false
// because Entity represents files, not directories.
string text = path.ToString();
if (!ignoreList.IsIgnored(text, false))
{
yield return entity;
}
}
}
}