Skip to content

Commit

Permalink
Refactor with C# 12 features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Dec 10, 2023
1 parent 2d2cb35 commit da19cc6
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 113 deletions.
6 changes: 2 additions & 4 deletions CliWrap.Tests/Utils/TempDir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace CliWrap.Tests.Utils;

internal partial class TempDir : IDisposable
internal partial class TempDir(string path) : IDisposable
{
public string Path { get; }

public TempDir(string path) => Path = path;
public string Path { get; } = path;

public void Dispose()
{
Expand Down
6 changes: 2 additions & 4 deletions CliWrap.Tests/Utils/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace CliWrap.Tests.Utils;

internal partial class TempFile : IDisposable
internal partial class TempFile(string path) : IDisposable
{
public string Path { get; }

public TempFile(string path) => Path = path;
public string Path { get; } = path;

public void Dispose()
{
Expand Down
10 changes: 3 additions & 7 deletions CliWrap.Tests/ValidationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@

namespace CliWrap.Tests;

public class ValidationSpecs
public class ValidationSpecs(ITestOutputHelper testOutput)
{
private readonly ITestOutputHelper _testOutput;

public ValidationSpecs(ITestOutputHelper testOutput) => _testOutput = testOutput;

[Fact(Timeout = 15000)]
public async Task I_can_try_to_execute_a_command_and_get_an_error_if_it_returns_a_non_zero_exit_code()
{
Expand All @@ -27,7 +23,7 @@ public async Task I_can_try_to_execute_a_command_and_get_an_error_if_it_returns_
ex.ExitCode.Should().Be(1);
ex.Command.Should().BeEquivalentTo(cmd);

_testOutput.WriteLine(ex.ToString());
testOutput.WriteLine(ex.ToString());
}

[Fact(Timeout = 15000)]
Expand All @@ -45,7 +41,7 @@ public async Task I_can_try_to_execute_a_command_with_buffering_and_get_a_detail
ex.ExitCode.Should().Be(1);
ex.Command.Should().BeEquivalentTo(cmd);

_testOutput.WriteLine(ex.ToString());
testOutput.WriteLine(ex.ToString());
}

[Fact(Timeout = 15000)]
Expand Down
28 changes: 9 additions & 19 deletions CliWrap/Buffered/BufferedCommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,21 @@ namespace CliWrap.Buffered;
/// <summary>
/// Result of a command execution, with buffered text data from standard output and standard error streams.
/// </summary>
public class BufferedCommandResult : CommandResult
public class BufferedCommandResult(
int exitCode,
DateTimeOffset startTime,
DateTimeOffset exitTime,
string standardOutput,
string standardError
) : CommandResult(exitCode, startTime, exitTime)
{
/// <summary>
/// Standard output data produced by the underlying process.
/// </summary>
public string StandardOutput { get; }
public string StandardOutput { get; } = standardOutput;

/// <summary>
/// Standard error data produced by the underlying process.
/// </summary>
public string StandardError { get; }

/// <summary>
/// Initializes an instance of <see cref="BufferedCommandResult" />.
/// </summary>
public BufferedCommandResult(
int exitCode,
DateTimeOffset startTime,
DateTimeOffset exitTime,
string standardOutput,
string standardError
)
: base(exitCode, startTime, exitTime)
{
StandardOutput = standardOutput;
StandardError = standardError;
}
public string StandardError { get; } = standardError;
}
18 changes: 4 additions & 14 deletions CliWrap/CommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace CliWrap;
/// <summary>
/// Result of a command execution.
/// </summary>
public class CommandResult
public class CommandResult(int exitCode, DateTimeOffset startTime, DateTimeOffset exitTime)
{
/// <summary>
/// Exit code set by the underlying process.
/// </summary>
public int ExitCode { get; }
public int ExitCode { get; } = exitCode;

/// <summary>
/// Whether the command execution was successful (i.e. exit code is zero).
Expand All @@ -20,25 +20,15 @@ public class CommandResult
/// <summary>
/// Time at which the command started executing.
/// </summary>
public DateTimeOffset StartTime { get; }
public DateTimeOffset StartTime { get; } = startTime;

/// <summary>
/// Time at which the command finished executing.
/// </summary>
public DateTimeOffset ExitTime { get; }
public DateTimeOffset ExitTime { get; } = exitTime;

/// <summary>
/// Total duration of the command execution.
/// </summary>
public TimeSpan RunTime => ExitTime - StartTime;

/// <summary>
/// Initializes an instance of <see cref="CommandResult" />.
/// </summary>
public CommandResult(int exitCode, DateTimeOffset startTime, DateTimeOffset exitTime)
{
ExitCode = exitCode;
StartTime = startTime;
ExitTime = exitTime;
}
}
10 changes: 3 additions & 7 deletions CliWrap/PipeSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ public abstract partial class PipeSource

public partial class PipeSource
{
private class AnonymousPipeSource : PipeSource
private class AnonymousPipeSource(Func<Stream, CancellationToken, Task> copyToAsync)
: PipeSource
{
private readonly Func<Stream, CancellationToken, Task> _copyToAsync;

public AnonymousPipeSource(Func<Stream, CancellationToken, Task> copyToAsync) =>
_copyToAsync = copyToAsync;

public override async Task CopyToAsync(
Stream destination,
CancellationToken cancellationToken = default
) => await _copyToAsync(destination, cancellationToken).ConfigureAwait(false);
) => await copyToAsync(destination, cancellationToken).ConfigureAwait(false);
}
}

Expand Down
16 changes: 5 additions & 11 deletions CliWrap/PipeTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,18 @@ public abstract partial class PipeTarget

public partial class PipeTarget
{
private class AnonymousPipeTarget : PipeTarget
private class AnonymousPipeTarget(Func<Stream, CancellationToken, Task> copyFromAsync)
: PipeTarget
{
private readonly Func<Stream, CancellationToken, Task> _copyFromAsync;

public AnonymousPipeTarget(Func<Stream, CancellationToken, Task> copyFromAsync) =>
_copyFromAsync = copyFromAsync;

public override async Task CopyFromAsync(
Stream origin,
CancellationToken cancellationToken = default
) => await _copyFromAsync(origin, cancellationToken).ConfigureAwait(false);
) => await copyFromAsync(origin, cancellationToken).ConfigureAwait(false);
}

private class AggregatePipeTarget : PipeTarget
private class AggregatePipeTarget(IReadOnlyList<PipeTarget> targets) : PipeTarget
{
public IReadOnlyList<PipeTarget> Targets { get; }

public AggregatePipeTarget(IReadOnlyList<PipeTarget> targets) => Targets = targets;
public IReadOnlyList<PipeTarget> Targets { get; } = targets;

public override async Task CopyFromAsync(
Stream origin,
Expand Down
13 changes: 3 additions & 10 deletions CliWrap/Utils/Disposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

namespace CliWrap.Utils;

internal partial class Disposable : IDisposable
{
private readonly Action _dispose;

public Disposable(Action dispose) => _dispose = dispose;

public void Dispose() => _dispose();
}

internal partial class Disposable
internal class Disposable(Action dispose) : IDisposable
{
public static IDisposable Null { get; } = Create(() => { });

public static IDisposable Create(Action dispose) => new Disposable(dispose);

public void Dispose() => dispose();
}
10 changes: 3 additions & 7 deletions CliWrap/Utils/Extensions/AsyncDisposableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ internal static partial class AsyncDisposableExtensions
// - Stream class on .NET Framework 4.6.1 -> calls Dispose()
// - Stream class on .NET Core 3.0 -> calls DisposeAsync()
// - Stream class on .NET Standard 2.0 -> calls DisposeAsync() or Dispose(), depending on the runtime
private readonly struct AsyncDisposableAdapter : IAsyncDisposable
private readonly struct AsyncDisposableAdapter(IDisposable target) : IAsyncDisposable
{
private readonly IDisposable _target;

public AsyncDisposableAdapter(IDisposable target) => _target = target;

public async ValueTask DisposeAsync()
{
if (_target is IAsyncDisposable asyncDisposable)
if (target is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
}
else
{
_target.Dispose();
target.Dispose();
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions CliWrap/Utils/Observable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace CliWrap.Utils;

internal class Observable<T> : IObservable<T>
internal class Observable<T>(Func<IObserver<T>, IDisposable> subscribe) : IObservable<T>
{
private readonly Func<IObserver<T>, IDisposable> _subscribe;

public Observable(Func<IObserver<T>, IDisposable> subscribe) => _subscribe = subscribe;

public IDisposable Subscribe(IObserver<T> observer) => _subscribe(observer);
public IDisposable Subscribe(IObserver<T> observer) => subscribe(observer);
}

internal static class Observable
Expand Down
7 changes: 2 additions & 5 deletions CliWrap/Utils/ProcessEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace CliWrap.Utils;

internal class ProcessEx : IDisposable
internal class ProcessEx(ProcessStartInfo startInfo) : IDisposable
{
private readonly Process _nativeProcess;
private readonly Process _nativeProcess = new() { StartInfo = startInfo };
private readonly TaskCompletionSource<object?> _exitTcs =
new(TaskCreationOptions.RunContinuationsAsynchronously);

Expand Down Expand Up @@ -40,9 +40,6 @@ internal class ProcessEx : IDisposable

public int ExitCode => _nativeProcess.ExitCode;

public ProcessEx(ProcessStartInfo startInfo) =>
_nativeProcess = new Process { StartInfo = startInfo };

public void Start()
{
// Hook up events
Expand Down
18 changes: 6 additions & 12 deletions CliWrap/Utils/SynchronizedObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@

namespace CliWrap.Utils;

internal class SynchronizedObserver<T> : IObserver<T>
internal class SynchronizedObserver<T>(IObserver<T> observer, object? syncRoot = null)
: IObserver<T>
{
private readonly IObserver<T> _observer;
private readonly object _syncRoot;

public SynchronizedObserver(IObserver<T> observer, object? syncRoot = null)
{
_observer = observer;
_syncRoot = syncRoot ?? new object();
}
private readonly object _syncRoot = syncRoot ?? new object();

public void OnCompleted()
{
lock (_syncRoot)
{
_observer.OnCompleted();
observer.OnCompleted();
}
}

public void OnError(Exception error)
{
lock (_syncRoot)
{
_observer.OnError(error);
observer.OnError(error);
}
}

public void OnNext(T value)
{
lock (_syncRoot)
{
_observer.OnNext(value);
observer.OnNext(value);
}
}
}
10 changes: 3 additions & 7 deletions CliWrap/Utils/WindowsSignaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@

namespace CliWrap.Utils;

internal partial class WindowsSignaler : IDisposable
internal partial class WindowsSignaler(string filePath) : IDisposable
{
private readonly string _filePath;

public WindowsSignaler(string filePath) => _filePath = filePath;

public bool TrySend(int processId, int signalId)
{
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = _filePath,
FileName = filePath,
Arguments =
processId.ToString(CultureInfo.InvariantCulture)
+ ' '
Expand Down Expand Up @@ -49,7 +45,7 @@ public void Dispose()
{
try
{
File.Delete(_filePath);
File.Delete(filePath);
}
catch
{
Expand Down

0 comments on commit da19cc6

Please sign in to comment.