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

58 lines
1.3 KiB
C#
Raw Normal View History

2021-09-07 05:15:45 +00:00
using System.Collections.Generic;
using FluentValidation;
2022-09-06 05:53:22 +00:00
using MfGames.Gallium;
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)
2021-09-07 05:15:45 +00:00
{
this.validator.ValidateAndThrow(this);
2021-09-07 05:15:45 +00:00
2022-07-09 04:52:10 +00:00
return this.replacePath.WithReplacement(this.RunReplacement)
.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
}
}