Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Jan 11, 2024
1 parent b305b60 commit 82adfb7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 153 deletions.
89 changes: 37 additions & 52 deletions CliWrap/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,61 @@ namespace CliWrap;
/// <summary>
/// Instructions for running a process.
/// </summary>
public partial class Command : ICommandConfiguration
public partial class Command(
string targetFilePath,
string arguments,
string workingDirPath,
Credentials credentials,
IReadOnlyDictionary<string, string?> environmentVariables,
CommandResultValidation validation,
PipeSource standardInputPipe,
PipeTarget standardOutputPipe,
PipeTarget standardErrorPipe
) : ICommandConfiguration
{
/// <inheritdoc />
public string TargetFilePath { get; }
/// <summary>
/// Initializes an instance of <see cref="Command" />.
/// </summary>
public Command(string targetFilePath)
: this(
targetFilePath,
string.Empty,
Directory.GetCurrentDirectory(),
Credentials.Default,
new Dictionary<string, string?>(),
CommandResultValidation.ZeroExitCode,
PipeSource.Null,
PipeTarget.Null,
PipeTarget.Null
) { }

/// <inheritdoc />
public string Arguments { get; }
public string TargetFilePath { get; } = targetFilePath;

/// <inheritdoc />
public string WorkingDirPath { get; }
public string Arguments { get; } = arguments;

/// <inheritdoc />
public Credentials Credentials { get; }
public string WorkingDirPath { get; } = workingDirPath;

/// <inheritdoc />
public IReadOnlyDictionary<string, string?> EnvironmentVariables { get; }
public Credentials Credentials { get; } = credentials;

/// <inheritdoc />
public CommandResultValidation Validation { get; }
public IReadOnlyDictionary<string, string?> EnvironmentVariables { get; } =
environmentVariables;

/// <inheritdoc />
public PipeSource StandardInputPipe { get; }
public CommandResultValidation Validation { get; } = validation;

/// <inheritdoc />
public PipeTarget StandardOutputPipe { get; }
public PipeSource StandardInputPipe { get; } = standardInputPipe;

/// <inheritdoc />
public PipeTarget StandardErrorPipe { get; }

/// <summary>
/// Initializes an instance of <see cref="Command" />.
/// </summary>
public Command(
string targetFilePath,
string arguments,
string workingDirPath,
Credentials credentials,
IReadOnlyDictionary<string, string?> 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;

/// <summary>
/// Initializes an instance of <see cref="Command" />.
/// </summary>
public Command(string targetFilePath)
: this(
targetFilePath,
string.Empty,
Directory.GetCurrentDirectory(),
Credentials.Default,
new Dictionary<string, string?>(),
CommandResultValidation.ZeroExitCode,
PipeSource.Null,
PipeTarget.Null,
PipeTarget.Null
) { }
/// <inheritdoc />
public PipeTarget StandardErrorPipe { get; } = standardErrorPipe;

/// <summary>
/// Creates a copy of this command, setting the target file path to the specified value.
Expand Down
15 changes: 3 additions & 12 deletions CliWrap/CommandTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@ namespace CliWrap;
/// <summary>
/// Represents an asynchronous execution of a command.
/// </summary>
public partial class CommandTask<TResult> : IDisposable
public partial class CommandTask<TResult>(Task<TResult> task, int processId) : IDisposable
{
/// <summary>
/// Underlying task.
/// </summary>
public Task<TResult> Task { get; }
public Task<TResult> Task { get; } = task;

/// <summary>
/// Underlying process ID.
/// </summary>
public int ProcessId { get; }

/// <summary>
/// Initializes an instance of <see cref="CommandTask{TResult}" />.
/// </summary>
public CommandTask(Task<TResult> task, int processId)
{
Task = task;
ProcessId = processId;
}
public int ProcessId { get; } = processId;

internal CommandTask<T> Bind<T>(Func<Task<TResult>, Task<T>> transform) =>
new(transform(Task), ProcessId);
Expand Down
47 changes: 18 additions & 29 deletions CliWrap/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,49 @@ namespace CliWrap;
/// <summary>
/// User credentials used for starting a process.
/// </summary>
public partial class Credentials
public partial class Credentials(
string? domain = null,
string? userName = null,
string? password = null,
bool loadUserProfile = false
)
{
/// <summary>
/// Initializes an instance of <see cref="Credentials" />.
/// </summary>
// TODO: (breaking change) remove in favor of the other overload
[ExcludeFromCodeCoverage]
public Credentials(string? domain, string? username, string? password)
: this(domain, username, password, false) { }

/// <summary>
/// Active Directory domain used for starting the process.
/// </summary>
/// <remarks>
/// Only supported on Windows.
/// </remarks>
public string? Domain { get; }
public string? Domain { get; } = domain;

/// <summary>
/// Username used for starting the process.
/// </summary>
public string? UserName { get; }
public string? UserName { get; } = userName;

/// <summary>
/// Password used for starting the process.
/// </summary>
/// <remarks>
/// Only supported on Windows.
/// </remarks>
public string? Password { get; }
public string? Password { get; } = password;

/// <summary>
/// Whether to load the user profile when starting the process.
/// </summary>
/// <remarks>
/// Only supported on Windows.
/// </remarks>
public bool LoadUserProfile { get; }

/// <summary>
/// Initializes an instance of <see cref="Credentials" />.
/// </summary>
public Credentials(
string? domain = null,
string? userName = null,
string? password = null,
bool loadUserProfile = false
)
{
Domain = domain;
UserName = userName;
Password = password;
LoadUserProfile = loadUserProfile;
}

/// <summary>
/// Initializes an instance of <see cref="Credentials" />.
/// </summary>
// 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
Expand Down
36 changes: 8 additions & 28 deletions CliWrap/EventStream/CommandEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ public abstract class CommandEvent;
/// Event triggered when the command starts executing.
/// May only appear once in the event stream.
/// </summary>
public class StartedCommandEvent : CommandEvent
public class StartedCommandEvent(int processId) : CommandEvent
{
/// <summary>
/// Underlying process ID.
/// </summary>
public int ProcessId { get; }

/// <summary>
/// Initializes an instance of <see cref="StartedCommandEvent" />.
/// </summary>
public StartedCommandEvent(int processId) => ProcessId = processId;
public int ProcessId { get; } = processId;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
Expand All @@ -43,17 +38,12 @@ public class StartedCommandEvent : CommandEvent
/// <summary>
/// Event triggered when the underlying process writes a line of text to the standard output stream.
/// </summary>
public class StandardOutputCommandEvent : CommandEvent
public class StandardOutputCommandEvent(string text) : CommandEvent
{
/// <summary>
/// Line of text written to the standard output stream.
/// </summary>
public string Text { get; }

/// <summary>
/// Initializes an instance of <see cref="StandardOutputCommandEvent" />.
/// </summary>
public StandardOutputCommandEvent(string text) => Text = text;
public string Text { get; } = text;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
Expand All @@ -63,17 +53,12 @@ public class StandardOutputCommandEvent : CommandEvent
/// <summary>
/// Event triggered when the underlying process writes a line of text to the standard error stream.
/// </summary>
public class StandardErrorCommandEvent : CommandEvent
public class StandardErrorCommandEvent(string text) : CommandEvent
{
/// <summary>
/// Line of text written to the standard error stream.
/// </summary>
public string Text { get; }

/// <summary>
/// Initializes an instance of <see cref="StandardErrorCommandEvent" />.
/// </summary>
public StandardErrorCommandEvent(string text) => Text = text;
public string Text { get; } = text;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
Expand All @@ -84,17 +69,12 @@ public class StandardErrorCommandEvent : CommandEvent
/// Event triggered when the command finishes executing.
/// May only appear once in the event stream.
/// </summary>
public class ExitedCommandEvent : CommandEvent
public class ExitedCommandEvent(int exitCode) : CommandEvent
{
/// <summary>
/// Exit code set by the underlying process.
/// </summary>
public int ExitCode { get; }

/// <summary>
/// Initializes an instance of <see cref="ExitedCommandEvent" />.
/// </summary>
public ExitedCommandEvent(int exitCode) => ExitCode = exitCode;
public int ExitCode { get; } = exitCode;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
Expand Down
9 changes: 2 additions & 7 deletions CliWrap/Exceptions/CliWrapException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ namespace CliWrap.Exceptions;
/// <summary>
/// Parent class for exceptions thrown by <see cref="CliWrap" />.
/// </summary>
public abstract class CliWrapException : Exception
public abstract class CliWrapException(string message, Exception? innerException = null)
: Exception(message, innerException)
{
/// <summary>
/// Initializes an instance of <see cref="CliWrapException" />.
/// </summary>
protected CliWrapException(string message, Exception? innerException)
: base(message, innerException) { }

/// <summary>
/// Initializes an instance of <see cref="CliWrapException" />.
/// </summary>
Expand Down
40 changes: 15 additions & 25 deletions CliWrap/Exceptions/CommandExecutionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,28 @@ namespace CliWrap.Exceptions;
/// <summary>
/// Exception thrown when the command fails to execute correctly.
/// </summary>
public class CommandExecutionException : CliWrapException
public class CommandExecutionException(
ICommandConfiguration command,
int exitCode,
string message,
Exception? innerException = null
) : CliWrapException(message, innerException)
{
/// <summary>
/// Command that triggered the exception.
/// </summary>
public ICommandConfiguration Command { get; }

/// <summary>
/// Exit code returned by the process.
/// Initializes an instance of <see cref="CommandExecutionException" />.
/// </summary>
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) { }

/// <summary>
/// Initializes an instance of <see cref="CommandExecutionException" />.
/// Command that triggered the exception.
/// </summary>
public CommandExecutionException(
ICommandConfiguration command,
int exitCode,
string message,
Exception? innerException
)
: base(message, innerException)
{
Command = command;
ExitCode = exitCode;
}
public ICommandConfiguration Command { get; } = command;

/// <summary>
/// Initializes an instance of <see cref="CommandExecutionException" />.
/// Exit code returned by the process.
/// </summary>
// 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;
}

0 comments on commit 82adfb7

Please sign in to comment.