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/Paths/RemovePathPrefix.cs

63 lines
1.4 KiB
C#
Raw Normal View History

2021-09-07 05:15:45 +00:00
using System.Collections.Generic;
using System.Threading;
using FluentValidation;
2022-09-06 05:53:22 +00:00
using MfGames.Gallium;
using MfGames.Nitride.Generators;
2021-09-07 05:15:45 +00:00
using Zio;
2022-09-06 05:53:22 +00:00
namespace MfGames.Nitride.IO.Paths;
/// <summary>
/// An operation that removes a path prefix from the input.
/// </summary>
[WithProperties]
public partial class RemovePathPrefix : IOperation
2021-09-07 05:15:45 +00:00
{
private readonly ReplacePath replacePath;
private readonly IValidator<RemovePathPrefix> validator;
2022-07-09 04:52:10 +00:00
public RemovePathPrefix(
IValidator<RemovePathPrefix> validator,
ReplacePath replacePath)
{
this.validator = validator;
this.replacePath = replacePath;
}
2021-09-07 05:15:45 +00:00
/// <summary>
/// Gets or sets the prefix for the path operations.
2021-09-07 05:15:45 +00:00
/// </summary>
public UPath PathPrefix { get; set; }
public IEnumerable<Entity> Run(
IEnumerable<Entity> input,
CancellationToken cancellationToken = default)
2021-09-07 05:15:45 +00:00
{
this.validator.ValidateAndThrow(this);
2021-09-07 05:15:45 +00:00
2022-11-03 01:46:15 +00:00
return this.replacePath
.WithReplacement(this.RunReplacement)
2022-07-09 04:52:10 +00:00
.Run(input);
}
2021-09-07 05:15:45 +00:00
2022-07-09 04:52:10 +00:00
private UPath RunReplacement(
Entity _,
UPath path)
{
string normalized = path.ToString();
string prefix = this.PathPrefix.ToString()!;
2021-09-07 05:15:45 +00:00
if (normalized.StartsWith(prefix))
2021-09-07 05:15:45 +00:00
{
2022-07-09 04:52:10 +00:00
return (UPath)path.ToString()
.Substring(prefix.Length);
2021-09-07 05:15:45 +00:00
}
return path;
2021-09-07 05:15:45 +00:00
}
}