using System.Text; namespace Geekeey.Extensions.Process; /// /// Represents a pipe for the process's standard input stream. /// public abstract partial class PipeSource { /// /// Reads the binary content pushed into the pipe and writes it to the destination stream. /// Destination stream represents the process's standard input stream. /// public abstract Task CopyToAsync(Stream destination, CancellationToken cancellationToken = default); } public partial class PipeSource { private class AnonymousPipeSource(Func func) : PipeSource { public override async Task CopyToAsync(Stream destination, CancellationToken cancellationToken = default) => await func(destination, cancellationToken); } } public partial class PipeSource { /// /// Pipe source that does not provide any data. /// Functionally equivalent to a null device. /// public static PipeSource Null { get; } = Create((_, cancellationToken) => !cancellationToken.IsCancellationRequested ? Task.CompletedTask : Task.FromCanceled(cancellationToken)); /// /// Creates an anonymous pipe source with the method /// implemented by the specified asynchronous delegate. /// public static PipeSource Create(Func func) => new AnonymousPipeSource(func); /// /// Creates an anonymous pipe source with the method /// implemented by the specified synchronous delegate. /// public static PipeSource Create(Action action) => Create( (destination, _) => { action(destination); return Task.CompletedTask; }); /// /// Creates a pipe source that reads from the specified stream. /// public static PipeSource FromStream(Stream stream) => Create( async (destination, cancellationToken) => await stream.CopyToAsync(destination, cancellationToken)); /// /// Creates a pipe source that reads from the specified file. /// public static PipeSource FromFile(string filePath) => Create( async (destination, cancellationToken) => { await using var source = File.OpenRead(filePath); await source.CopyToAsync(destination, cancellationToken); }); /// /// Creates a pipe source that reads from the specified memory buffer. /// public static PipeSource FromBytes(ReadOnlyMemory data) => Create( async (destination, cancellationToken) => await destination.WriteAsync(data, cancellationToken)); /// /// Creates a pipe source that reads from the specified byte array. /// public static PipeSource FromBytes(byte[] data) => FromBytes((ReadOnlyMemory)data); /// /// Creates a pipe source that reads from the specified string. /// public static PipeSource FromString(string str, Encoding encoding) => FromBytes(encoding.GetBytes(str)); /// /// Creates a pipe source that reads from the specified string. /// Uses for encoding. /// public static PipeSource FromString(string str) => FromString(str, Console.InputEncoding); /// /// Creates a pipe source that reads from the standard output of the specified command. /// public static PipeSource FromCommand(Command command) => Create( async (destination, cancellationToken) => await command.WithStandardOutputPipe(PipeTarget.ToStream(destination)).ExecuteAsync(cancellationToken)); }