feat: add system.buffers support for pipe target
All checks were successful
default / dotnet-default-workflow (pull_request) Successful in 1m12s
default / dotnet-default-workflow (push) Successful in 1m19s

This commit is contained in:
Louis Seubert 2026-08-25 22:29:06 +02:00
commit d45574645d
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
3 changed files with 39 additions and 2 deletions

View file

@ -1,8 +1,9 @@
// Copyright (c) The Geekeey Authors // Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2 // SPDX-License-Identifier: EUPL-1.2
using System.Text; using System.Buffers;
using System.IO.Pipelines; using System.IO.Pipelines;
using System.Text;
using Geekeey.Process.Buffered; using Geekeey.Process.Buffered;
@ -619,6 +620,19 @@ internal sealed class PipingTests
await Assert.That(output.Length).IsEqualTo(100_000); await Assert.That(output.Length).IsEqualTo(100_000);
} }
[Test]
public async Task I_can_execute_a_command_and_pipe_the_stdout_to_a_buffer_writer()
{
var output = new ArrayBufferWriter<byte>();
var cmd = new Command(Testing.Fixture.Program.FilePath)
.WithArguments(["generate", "blob", "--length", "100000"]) |
PipeTarget.ToBufferWriter(output);
await cmd.ExecuteAsync();
await Assert.That(output.WrittenCount).IsEqualTo(100_000);
}
[Test] [Test]
public async Task I_can_execute_a_command_and_pipe_the_stdout_into_multiple_hierarchical_targets() public async Task I_can_execute_a_command_and_pipe_the_stdout_into_multiple_hierarchical_targets()
{ {

View file

@ -1,8 +1,8 @@
// Copyright (c) The Geekeey Authors // Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2 // SPDX-License-Identifier: EUPL-1.2
using System.Text;
using System.IO.Pipelines; using System.IO.Pipelines;
using System.Text;
namespace Geekeey.Process; namespace Geekeey.Process;

View file

@ -195,6 +195,29 @@ public partial class PipeTarget
}); });
} }
/// <summary>
/// Creates a pipe target that writes to the specified buffer writer.
/// </summary>
public static PipeTarget ToBufferWriter(IBufferWriter<byte> writer)
{
return Create(async (origin, cancellationToken) =>
{
using var buffer = MemoryPool<byte>.Shared.Rent(MemoryBufferStream.DefaultBufferSize);
while (true)
{
var read = await origin.ReadAsync(buffer.Memory, cancellationToken);
if (read <= 0)
{
break;
}
buffer.Memory[..read].Span.CopyTo(writer.GetSpan(read));
writer.Advance(read);
}
});
}
/// <summary> /// <summary>
/// Creates a pipe target that writes to the specified file. /// Creates a pipe target that writes to the specified file.
/// </summary> /// </summary>