using System.Collections.Generic;
using Gallium;
using MAB.DotIgnore;
using Zio;
namespace Nitride.IO;
///
/// Extension methods for working with paths.
///
public static class NitrideIOEnumerableEntityExtensions
{
///
/// Retrieves an entity from the sequence of entities.
///
/// The sequence of iterations to parse.
/// The path to search for.
/// The entity pulled out, if found. Otherwise null.
/// If true, then remove the entity from the list.
/// The sequence of entities, optionally without one.
public static IEnumerable GetEntityByPath(
this IEnumerable input,
UPath path,
out Entity? foundEntity,
IfFoundOutput removeFromResults = IfFoundOutput.RemoveFromOutput)
{
List 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;
}
///
/// Filters out entities that match a .gitignore style list and returns
/// the remaining entities.
///
public static IEnumerable WhereNotIgnored(
this IEnumerable 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;
}
}
}
}