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/Nitride.IO/Paths/RemovePathPrefix.cs
Dylan R. E. Moonfire 78054ee2a7 feat: initial release
2021-09-07 00:15:45 -05:00

59 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using Gallium;
using Zio;
namespace Nitride.IO.Paths
{
/// <summary>
/// An operation that removes a path prefix from the input.
/// </summary>
[WithProperties]
public partial class RemovePathPrefix : INitrideOperation
{
public RemovePathPrefix()
{
}
public RemovePathPrefix(UPath pathPrefix)
{
this.PathPrefix = pathPrefix;
}
/// <summary>
/// Gets or sets the prefix for the path operations.
/// </summary>
public UPath? PathPrefix { get; set; }
public IEnumerable<Entity> Run(IEnumerable<Entity> input)
{
if (this.PathPrefix == null)
{
throw new InvalidOperationException(
nameof(RemovePathPrefix)
+ ".Prefix was not set "
+ "(via constructor, property, or SetPrefix) before "
+ "Replace() was called.");
}
ReplacePaths replacePaths = new ReplacePaths()
.WithReplacement(this.RunReplacement);
return replacePaths.Run(input);
}
private UPath RunReplacement(Entity _, UPath path)
{
string normalized = path.ToString();
string prefix = this.PathPrefix.ToString()!;
if (normalized.StartsWith(prefix))
{
return (UPath)path.ToString().Substring(prefix.Length);
}
return path;
}
}
}