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