From 82adfb793753bddb88d2be35431e162dd98f79ac Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 11 Jan 2024 23:45:01 +0200 Subject: [PATCH] More cleanup --- CliWrap/Command.cs | 89 ++++++++----------- CliWrap/CommandTask.cs | 15 +--- CliWrap/Credentials.cs | 47 ++++------ CliWrap/EventStream/CommandEvent.cs | 36 ++------ CliWrap/Exceptions/CliWrapException.cs | 9 +- .../Exceptions/CommandExecutionException.cs | 40 ++++----- 6 files changed, 83 insertions(+), 153 deletions(-) diff --git a/CliWrap/Command.cs b/CliWrap/Command.cs index c4d5b5ec..27bed170 100644 --- a/CliWrap/Command.cs +++ b/CliWrap/Command.cs @@ -10,76 +10,61 @@ namespace CliWrap; /// /// Instructions for running a process. /// -public partial class Command : ICommandConfiguration +public partial class Command( + string targetFilePath, + string arguments, + string workingDirPath, + Credentials credentials, + IReadOnlyDictionary environmentVariables, + CommandResultValidation validation, + PipeSource standardInputPipe, + PipeTarget standardOutputPipe, + PipeTarget standardErrorPipe +) : ICommandConfiguration { - /// - public string TargetFilePath { get; } + /// + /// Initializes an instance of . + /// + public Command(string targetFilePath) + : this( + targetFilePath, + string.Empty, + Directory.GetCurrentDirectory(), + Credentials.Default, + new Dictionary(), + CommandResultValidation.ZeroExitCode, + PipeSource.Null, + PipeTarget.Null, + PipeTarget.Null + ) { } /// - public string Arguments { get; } + public string TargetFilePath { get; } = targetFilePath; /// - public string WorkingDirPath { get; } + public string Arguments { get; } = arguments; /// - public Credentials Credentials { get; } + public string WorkingDirPath { get; } = workingDirPath; /// - public IReadOnlyDictionary EnvironmentVariables { get; } + public Credentials Credentials { get; } = credentials; /// - public CommandResultValidation Validation { get; } + public IReadOnlyDictionary EnvironmentVariables { get; } = + environmentVariables; /// - public PipeSource StandardInputPipe { get; } + public CommandResultValidation Validation { get; } = validation; /// - public PipeTarget StandardOutputPipe { get; } + public PipeSource StandardInputPipe { get; } = standardInputPipe; /// - public PipeTarget StandardErrorPipe { get; } - - /// - /// Initializes an instance of . - /// - public Command( - string targetFilePath, - string arguments, - string workingDirPath, - Credentials credentials, - IReadOnlyDictionary environmentVariables, - CommandResultValidation validation, - PipeSource standardInputPipe, - PipeTarget standardOutputPipe, - PipeTarget standardErrorPipe - ) - { - TargetFilePath = targetFilePath; - Arguments = arguments; - WorkingDirPath = workingDirPath; - Credentials = credentials; - EnvironmentVariables = environmentVariables; - Validation = validation; - StandardInputPipe = standardInputPipe; - StandardOutputPipe = standardOutputPipe; - StandardErrorPipe = standardErrorPipe; - } + public PipeTarget StandardOutputPipe { get; } = standardOutputPipe; - /// - /// Initializes an instance of . - /// - public Command(string targetFilePath) - : this( - targetFilePath, - string.Empty, - Directory.GetCurrentDirectory(), - Credentials.Default, - new Dictionary(), - CommandResultValidation.ZeroExitCode, - PipeSource.Null, - PipeTarget.Null, - PipeTarget.Null - ) { } + /// + public PipeTarget StandardErrorPipe { get; } = standardErrorPipe; /// /// Creates a copy of this command, setting the target file path to the specified value. diff --git a/CliWrap/CommandTask.cs b/CliWrap/CommandTask.cs index 8515e5eb..a57ffd1e 100644 --- a/CliWrap/CommandTask.cs +++ b/CliWrap/CommandTask.cs @@ -8,26 +8,17 @@ namespace CliWrap; /// /// Represents an asynchronous execution of a command. /// -public partial class CommandTask : IDisposable +public partial class CommandTask(Task task, int processId) : IDisposable { /// /// Underlying task. /// - public Task Task { get; } + public Task Task { get; } = task; /// /// Underlying process ID. /// - public int ProcessId { get; } - - /// - /// Initializes an instance of . - /// - public CommandTask(Task task, int processId) - { - Task = task; - ProcessId = processId; - } + public int ProcessId { get; } = processId; internal CommandTask Bind(Func, Task> transform) => new(transform(Task), ProcessId); diff --git a/CliWrap/Credentials.cs b/CliWrap/Credentials.cs index c10a576c..b833fa0c 100644 --- a/CliWrap/Credentials.cs +++ b/CliWrap/Credentials.cs @@ -5,20 +5,33 @@ namespace CliWrap; /// /// User credentials used for starting a process. /// -public partial class Credentials +public partial class Credentials( + string? domain = null, + string? userName = null, + string? password = null, + bool loadUserProfile = false +) { + /// + /// Initializes an instance of . + /// + // TODO: (breaking change) remove in favor of the other overload + [ExcludeFromCodeCoverage] + public Credentials(string? domain, string? username, string? password) + : this(domain, username, password, false) { } + /// /// Active Directory domain used for starting the process. /// /// /// Only supported on Windows. /// - public string? Domain { get; } + public string? Domain { get; } = domain; /// /// Username used for starting the process. /// - public string? UserName { get; } + public string? UserName { get; } = userName; /// /// Password used for starting the process. @@ -26,7 +39,7 @@ public partial class Credentials /// /// Only supported on Windows. /// - public string? Password { get; } + public string? Password { get; } = password; /// /// Whether to load the user profile when starting the process. @@ -34,31 +47,7 @@ public partial class Credentials /// /// Only supported on Windows. /// - public bool LoadUserProfile { get; } - - /// - /// Initializes an instance of . - /// - public Credentials( - string? domain = null, - string? userName = null, - string? password = null, - bool loadUserProfile = false - ) - { - Domain = domain; - UserName = userName; - Password = password; - LoadUserProfile = loadUserProfile; - } - - /// - /// Initializes an instance of . - /// - // TODO: (breaking change) remove in favor of the other overload - [ExcludeFromCodeCoverage] - public Credentials(string? domain, string? username, string? password) - : this(domain, username, password, false) { } + public bool LoadUserProfile { get; } = loadUserProfile; } public partial class Credentials diff --git a/CliWrap/EventStream/CommandEvent.cs b/CliWrap/EventStream/CommandEvent.cs index 1bed8822..8d5c4a12 100644 --- a/CliWrap/EventStream/CommandEvent.cs +++ b/CliWrap/EventStream/CommandEvent.cs @@ -23,17 +23,12 @@ public abstract class CommandEvent; /// Event triggered when the command starts executing. /// May only appear once in the event stream. /// -public class StartedCommandEvent : CommandEvent +public class StartedCommandEvent(int processId) : CommandEvent { /// /// Underlying process ID. /// - public int ProcessId { get; } - - /// - /// Initializes an instance of . - /// - public StartedCommandEvent(int processId) => ProcessId = processId; + public int ProcessId { get; } = processId; /// [ExcludeFromCodeCoverage] @@ -43,17 +38,12 @@ public class StartedCommandEvent : CommandEvent /// /// Event triggered when the underlying process writes a line of text to the standard output stream. /// -public class StandardOutputCommandEvent : CommandEvent +public class StandardOutputCommandEvent(string text) : CommandEvent { /// /// Line of text written to the standard output stream. /// - public string Text { get; } - - /// - /// Initializes an instance of . - /// - public StandardOutputCommandEvent(string text) => Text = text; + public string Text { get; } = text; /// [ExcludeFromCodeCoverage] @@ -63,17 +53,12 @@ public class StandardOutputCommandEvent : CommandEvent /// /// Event triggered when the underlying process writes a line of text to the standard error stream. /// -public class StandardErrorCommandEvent : CommandEvent +public class StandardErrorCommandEvent(string text) : CommandEvent { /// /// Line of text written to the standard error stream. /// - public string Text { get; } - - /// - /// Initializes an instance of . - /// - public StandardErrorCommandEvent(string text) => Text = text; + public string Text { get; } = text; /// [ExcludeFromCodeCoverage] @@ -84,17 +69,12 @@ public class StandardErrorCommandEvent : CommandEvent /// Event triggered when the command finishes executing. /// May only appear once in the event stream. /// -public class ExitedCommandEvent : CommandEvent +public class ExitedCommandEvent(int exitCode) : CommandEvent { /// /// Exit code set by the underlying process. /// - public int ExitCode { get; } - - /// - /// Initializes an instance of . - /// - public ExitedCommandEvent(int exitCode) => ExitCode = exitCode; + public int ExitCode { get; } = exitCode; /// [ExcludeFromCodeCoverage] diff --git a/CliWrap/Exceptions/CliWrapException.cs b/CliWrap/Exceptions/CliWrapException.cs index d7b88bca..08199b54 100644 --- a/CliWrap/Exceptions/CliWrapException.cs +++ b/CliWrap/Exceptions/CliWrapException.cs @@ -6,14 +6,9 @@ namespace CliWrap.Exceptions; /// /// Parent class for exceptions thrown by . /// -public abstract class CliWrapException : Exception +public abstract class CliWrapException(string message, Exception? innerException = null) + : Exception(message, innerException) { - /// - /// Initializes an instance of . - /// - protected CliWrapException(string message, Exception? innerException) - : base(message, innerException) { } - /// /// Initializes an instance of . /// diff --git a/CliWrap/Exceptions/CommandExecutionException.cs b/CliWrap/Exceptions/CommandExecutionException.cs index b9eb74f5..dc76a564 100644 --- a/CliWrap/Exceptions/CommandExecutionException.cs +++ b/CliWrap/Exceptions/CommandExecutionException.cs @@ -6,38 +6,28 @@ namespace CliWrap.Exceptions; /// /// Exception thrown when the command fails to execute correctly. /// -public class CommandExecutionException : CliWrapException +public class CommandExecutionException( + ICommandConfiguration command, + int exitCode, + string message, + Exception? innerException = null +) : CliWrapException(message, innerException) { /// - /// Command that triggered the exception. - /// - public ICommandConfiguration Command { get; } - - /// - /// Exit code returned by the process. + /// Initializes an instance of . /// - public int ExitCode { get; } + // TODO: (breaking change) remove in favor of an optional parameter in the constructor above + [ExcludeFromCodeCoverage] + public CommandExecutionException(ICommandConfiguration command, int exitCode, string message) + : this(command, exitCode, message, null) { } /// - /// Initializes an instance of . + /// Command that triggered the exception. /// - public CommandExecutionException( - ICommandConfiguration command, - int exitCode, - string message, - Exception? innerException - ) - : base(message, innerException) - { - Command = command; - ExitCode = exitCode; - } + public ICommandConfiguration Command { get; } = command; /// - /// Initializes an instance of . + /// Exit code returned by the process. /// - // TODO: (breaking change) remove in favor of an optional parameter in the constructor above - [ExcludeFromCodeCoverage] - public CommandExecutionException(ICommandConfiguration command, int exitCode, string message) - : this(command, exitCode, message, null) { } + public int ExitCode { get; } = exitCode; }