Hmb.ProcessRunner
1.0.5
dotnet add package Hmb.ProcessRunner --version 1.0.5
NuGet\Install-Package Hmb.ProcessRunner -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hmb.ProcessRunner" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hmb.ProcessRunner" Version="1.0.5" />
<PackageReference Include="Hmb.ProcessRunner" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hmb.ProcessRunner --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hmb.ProcessRunner, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hmb.ProcessRunner@1.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hmb.ProcessRunner&version=1.0.5
#tool nuget:?package=Hmb.ProcessRunner&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hmb.ProcessRunner
Repository
Summary
Hmb.ProcessRunner is a library for executing shell commands. The class ProcessService exposes two methods:
ExecuteAsyncto execute a command thru a OS Shell.Whichreturns a list of file paths from PATH env var where the given file has been found.
Why?
- Do not worry about OS. Default shells will be used in major OS (Windows, OSX, Linux).
- Run inline scripts, command is not limited to executable files. inline commands such as
echo $env:PATHorecho $PATHcan be used. - Long running command with a lot of output can take advantage of
System.Threading.Channel - Use
awaitto continue execution once command finishes.
Examples
Following examples uses the sample application Hmb.ProcessRunner.TestApp.exe, it supports following subcommands:
Description:
Usage:
Hmb.ProcessRunner.TestApp [command] [options]
Options:
--version Show version information
-?, -h, --help Show help and usage information
Commands:
echo <message> Echos the first argument []
exit <exitCode> Exits the application [default: 0]
sleep <seconds> Sleeps for the specified number of seconds [default: 1]
env Command to related to environmental variables
counter <number> Command to count to the specified number [default: 2147483647]
csv Command to generate a CSV file
Execute command without error
// arrange
var command = $"dotnet {TestAppFilePath} echo \"Hello World\"";
// act
var exitCode = await _processService.ExecuteAsync(command);
// assert
Assert.That(exitCode, Is.EqualTo(0));
Capture unicode in standard output writer
// arrange
var unicodeMessage = "Hello World π γδΈηγγγγ«γ‘γ―γγ";
var command = $@"dotnet {TestAppFilePath} echo ""{unicodeMessage}""";
// act
var sb = new StringBuilder();
var exitCode = await _processService.ExecuteAsync(
command,
standardOutputWriter: new StringWriter(sb),
standardOutputEncoding: Encoding.UTF8
);
// assert
Assert.That(exitCode, Is.EqualTo(0));
Assert.That(sb.ToString(), Is.EqualTo(unicodeMessage));
Overrides environment variables
// arrange
string envVarName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "HOMEPATH" : @"HOME";
string envVarOldValue = Environment.GetEnvironmentVariable(envVarName) ?? string.Empty;
string envVarNewValue = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
string command = $@"dotnet {TestAppFilePath} env print {envVarName}";
// act
var sb = new StringBuilder();
using var sw = new StringWriter(sb);
var exitCode = await _processService.ExecuteAsync(
command,
standardOutputWriter: sw,
surrogateEnvironmentalVariables: new Dictionary<string, string?> {
{ envVarName, envVarNewValue }
});
// assert
string standardOutput = sb.ToString().Trim();
Assert.That(exitCode, Is.EqualTo(0));
Assert.That(envVarOldValue, Is.Not.Empty);
Assert.That(envVarNewValue, Is.Not.Empty);
Assert.That(envVarOldValue, Is.Not.EqualTo(envVarNewValue));
Assert.That(standardOutput, Is.EqualTo(envVarNewValue));
Which finds dotnet command
// arrange
var command = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet";
// act
var foundFilePaths = _processService.Which(command);
// assert
Assert.That(foundFilePaths, Is.Not.Null);
Assert.That(foundFilePaths.Any(), Is.True);
Using System.Threading.Channel to capture large outputs
// arrange
const int countLimit = 20_000;
string command = $@"dotnet {TestAppFilePath} csv --count {countLimit}";
Channel<string> stdOutputChannel = Channel.CreateUnbounded<string>();
// act
Task producer = Task.Run(async () =>
{
await _processService.ExecuteAsync(
command: command,
standardOutputChannel: stdOutputChannel
);
});
int counter = 0;
Task consumer = Task.Run(async () =>
{
await foreach (var standardOutputLine in stdOutputChannel.Reader.ReadAllAsync())
{
counter++;
}
});
await Task.WhenAll(producer, consumer);
// assert
Assert.That(counter, Is.EqualTo(countLimit));
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.