jjm.one.CommandLineToolWrapper 2.0.0-alpha.1

This is a prerelease version of jjm.one.CommandLineToolWrapper.
dotnet add package jjm.one.CommandLineToolWrapper --version 2.0.0-alpha.1                
NuGet\Install-Package jjm.one.CommandLineToolWrapper -Version 2.0.0-alpha.1                
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="jjm.one.CommandLineToolWrapper" Version="2.0.0-alpha.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add jjm.one.CommandLineToolWrapper --version 2.0.0-alpha.1                
#r "nuget: jjm.one.CommandLineToolWrapper, 2.0.0-alpha.1"                
#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.
// Install jjm.one.CommandLineToolWrapper as a Cake Addin
#addin nuget:?package=jjm.one.CommandLineToolWrapper&version=2.0.0-alpha.1&prerelease

// Install jjm.one.CommandLineToolWrapper as a Cake Tool
#tool nuget:?package=jjm.one.CommandLineToolWrapper&version=2.0.0-alpha.1&prerelease                

jjm.one.CommandLineToolWrapper

A C# library that provides a wrapper for command line tools.

Status

Build & Test Status (main) Build&Test
Nuget Package Version Nuget Version
SonarCloudQuality Gate Status Quality Gate Status

Table of contents

Nuget Package

You can get the latest version of this software as a nuget package form nuget.org

Full Documentation

The full documentation for this package can be found here.

Repo

The associated repo for this package can be found here.

Brief overview of the interfaces and classes

All interfaces and classes provided by this package are briefly presented below.

IToolWrapper Interface

This interface defines a contract for a wrapper around a command line tool. It has a single method, RunCommandAsync, which takes a command and its arguments, and returns a Task that completes with a ProcessResult.

public interface IToolWrapper
{
    Task<ProcessResult> RunCommandAsync(string command, string arguments);
}

ToolWrapper Class

This class is the default implementation of the IToolWrapper interface. It uses an instance of IProcessRunner to execute commands, and logs information about the tool's operations. It also has settings for the command line tool and the wrapper itself, which are provided through the constructor.

public class ToolWrapper : IToolWrapper
{
    public ToolWrapper(ToolSettings toolSettings, WrapperSettings wrapperSettings, IProcessRunner? processRunner = null, ILogger<ToolWrapper>? logger = null)
    {
        // ...
    }

    public Task<ProcessResult> RunCommandAsync(string command, string arguments)
    {
        // ...
    }
}

You can use the ToolWrapper class to run a command like this:

var toolWrapper = new ToolWrapper(new ToolSettings(), new WrapperSettings());
var result = await toolWrapper.RunCommandAsync("echo", "Hello, world!");
Console.WriteLine(result.Output);  // Outputs: Hello, world!

ProcessResult Class

This class represents the result of a process run by the ToolWrapper. It contains the exit code of the process and optionally, the output and error messages.

public class ProcessResult
{
    public int ExitCode { get; init; }
    public string? Output { get; init; }
    public string? Error { get; init; }
}

When you run a command with the ToolWrapper, you get a ProcessResult:

var result = await toolWrapper.RunCommandAsync("echo", "Hello, world!");
Console.WriteLine(result.ExitCode);  // Outputs: 0
Console.WriteLine(result.Output);  // Outputs: Hello, world!
Console.WriteLine(result.Error);  // Outputs: null

ToolWrapper Constructor

The constructor of the ToolWrapper class takes in ToolSettings and WrapperSettings for the command line tool and the wrapper respectively. It also takes an optional IProcessRunner and ILogger<ToolWrapper>. If no IProcessRunner is provided, a new ProcessRunner is created. If no ILogger<ToolWrapper> is provided, logging is disabled.

public ToolWrapper(ToolSettings toolSettings, WrapperSettings wrapperSettings, IProcessRunner? processRunner = null, ILogger<ToolWrapper>? logger = null)
{
    // ...
}

ToolWrapper.RunCommandAsync Method

This method is the implementation of the RunCommandAsync method from the IToolWrapper interface. It runs a command asynchronously and returns a ProcessResult.

public Task<ProcessResult> RunCommandAsync(string command, string arguments)
{
    // ...
}

You can use this method to run a command like this:

var result = await toolWrapper.RunCommandAsync("echo", "Hello, world!");
Console.WriteLine(result.Output);  // Outputs: Hello, world!

Dependency Injection

The AddToolWrapper method is an extension method for IServiceCollection that you can use to register the ToolWrapper class and its dependencies in the dependency injection container.

Here's an example of how you can use it in the ConfigureServices method of your Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // Create instances of ToolSettings and WrapperSettings
    // You might want to populate these from your configuration
    var toolSettings = new ToolSettings();
    var wrapperSettings = new WrapperSettings();

    // Use the AddToolWrapper method to add ToolWrapper and its dependencies
    services.AddToolWrapper(toolSettings, wrapperSettings);

    // Add other services...
}

In this example, ToolSettings and WrapperSettings are being created as new instances. You might want to populate these from your application's configuration (e.g., from an appsettings.json file) instead.

Please replace YourNamespace with the actual namespace where the AddToolWrapper method is defined.

After you've registered ToolWrapper with the IServiceCollection, you can have it automatically injected into your classes by adding a parameter of type IToolWrapper to the constructor of your class:

public class MyClass
{
    private readonly IToolWrapper _toolWrapper;

    public MyClass(IToolWrapper toolWrapper)
    {
        _toolWrapper = toolWrapper;
    }

    // Use _toolWrapper in your methods...
}

In this example, MyClass has a dependency on IToolWrapper, which will be automatically injected by the .NET Core dependency injection framework when MyClass is created.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0-alpha.1 76 4/9/2024
2.0.0-alpha.0 63 3/11/2024
1.0.0-alpha.3 113 1/8/2024
1.0.0-alpha.2 74 1/2/2024