Skip to content

deinsoftware/toolbox

Repository files navigation

dein ToolBox [ Win+Mac+Linux ]

github-actions-build github-actions-pack nuget-version nuget-downloads sonar-reliability sonar-security sonar-maintainability sonar-coverage license

ToolBox

ToolBox was created to simplify and automate tasks related to the .Net console. Was born in HardHat project as a Class. Now grown up as a library and can be used by other console applications.

The Code is Dark and Full of Errors! Console is your friend ... don't be afraid!

Menu


Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

What things you need to install?

ToolBox supports netstandard2.1, netcoreapp3.1, net5.0, net6.0 and net7.0 target frameworks.

Installing

ToolBox is available as project or package. We strong recommend add as a NuGet package if don't need make modifications directly on the source code library.

Follow these instructions to add ToolBox in your project.

Add As Package

In your project folder, where is located .csproj file run this command on terminal:

dotnet add package dein.ToolBox

Official documentation: dotnet add package

Add As Reference

Clone ToolBox from GitHub on recommended path. Using this command on terminal:

OS Command
win git clone https://github.com/deinsoftware/toolbox.git "D:\Developer\DEIN\Projects\_devTB"
mac git clone https://github.com/deinsoftware/toolbox.git ~/Developer/DEIN/Projects/_devTB

In your project folder, where is located .csproj file run this command on terminal:

OS Command
win dotnet add reference "D:\Developer\DEIN\Projects\_devCC\ToolBox\ToolBox.csproj"
mac dotnet add reference ~/Developer/DEIN/Projects/_devCC/ToolBox/ToolBox.csproj

Copy Command Bridge files on the path:

  • Bat (Windows)
  • Bash (MacOS / Linux)

Inside your .csproj add Command Bridge files on build:

<Project Sdk="Microsoft.NET.Sdk">
    <ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
        <!-- Command Bridge -->
        <None Update="cmd.sh" CopyToOutputDirectory="PreserveNewest" />
        <None Update="cmd.bat" CopyToOutputDirectory="PreserveNewest" />
        <!-- Projects -->
        <ProjectReference Include="..\..\_devTB\ToolBox\ToolBox.csproj" />
    </ItemGroup>
</Project>

Official documentation: dotnet add reference

Back to menu


Usage

Keep calm, you are almost done. Review this usage steps and enjoy life.

To understand how this library works, take a look inside Sample folder. Better easy to use a guide than words.

Just go to Sample project folder and run this command on terminal:

cd Sample
dotnet run

Files

Include operations relative to File System and implements IFileSystem and ICommandSystem with specific actions and commands per Operative System.

using ToolBox.Files;

On the main class Program, add static properties DiskConfigurator and PathsConfigurator and inside the Main method create an instance of the library according the Operative System.

class Program
{
    public static DiskConfigurator _disk {get; set;}
    public static PathsConfigurator _path {get; set;}

    static void Main(string[] args)
    {
        _disk = new DiskConfigurator(FileSystem.Default);
        switch (OS.GetCurrent())
        {
            case "win":
                _path = new PathsConfigurator(CommandSystem.Win, FileSystem.Default);
                break;
            case "mac":
                _path = new PathsConfigurator(CommandSystem.Mac, FileSystem.Default);
                break;
        }
        //Foo()
        //Bar()
    }
}

If you want to use _path and/or _disk in other class, add a static using to Program class:

using static Namesapace.Program;

replace Namespace with a defined namespace in your project.

Disk

_disk.FilterCreator(extension[]); //Create a Regex with accepted extensions.
_disk.CopyAll(source, destination, overwrite, filter[]); //Copy all files and folder from source to destination
_disk.CopyDirectories(source, destination); //Copy all folder from source to destination
_disk.CopyFiles(source, destination, overwrite, filter[]); //Copy all files from source to destination
_disk.DeleteAll(source, recursive); //Delete all files and folders from source

If you want get Notifications about copy or delete process, need implement the INotificationSystem interface.

public sealed class ConsoleNotificationSystem : INotificationSystem
{
    public void ShowAction(string action, string message)
    {
        _colorify.Wrap($" [{action}] {message}", txtPrimary);
    }
}

And send it as a parameter on DiskConfiguration definition.

_disk = new DiskConfigurator(FileSystem.Default, new ConsoleNotificationSystem());

Paths

_path.Combine(values[]); //Return combined path. Automatic detect `~` as user folder
_path.GetDirectories(path, filter); //Return Folders inside path
_path.GetFiles(path, filter); //Return Files inside path

Log

Include operations relative to Logs and implements the IFileSystem with specific actions and commands per Operative System.

using ToolBox.Log;

On the main class Program, add static properties ILogSystem and inside the Main method create an instance of the class according a value (maybe in your config system).

class Program
{
    private static Config _conf { get; set; }
    private static ILogSystem _log {get; set;}

    static void Main(string[] args)
    {
        _conf = Settings.Read();
        switch (_conf.log.system)
        {
            case "csv":
                _log = new FileLogCsv(FileSystem.Default, _path.Combine("~"), ".application.log");
                break;
            case "txt":
                _log = new FileLogTxt(FileSystem.Default, _path.Combine("~"), ".application.log");
                break;
        }
        //Foo()
        //Bar()
    }
}

Save

_log.Save(exception, logLevel); //Save exception on file

Platform

Platform namespace for Operative System detection and commands.

using ToolBox.Platform;

OS.IsWin();       //Return true on Windows
OS.IsMac();       //Return true on MacOS
OS.IsGnu();       //Return true on Linux

OS.GetCurrent();
//Return "win" on Windows
//Return "mac" on MacOS
//Return "gnu" on Linux

Shell

On the main class Program, add static properties ShellConfigurator and inside the Main method create an instance of the library according the Operative System.

using ToolBox.Bridge;

On the main class Program, add static properties ILogSystem and inside the Main method create an instance of the class according a value (maybe in your config system).

using static ToolBox.Notification;
class Program
{
    public static INotificationSystem _notificationSystem { get; set; }
    public static IBridgeSystem _bridgeSystem { get; set; }
    public static ShellConfigurator _shell { get; set; }

    static void Main(string[] args)
    {
        _notificationSystem = new ConsoleNotificationSystem(); //Or _notificationSystem = NotificationSystem.Default;
        switch (OS.GetCurrent())
        {
            case "win":
                _bridgeSystem = BridgeSystem.Bat;
                break;
            case "mac":
            case "gnu":
                _bridgeSystem = BridgeSystem.Bash;
                break;
        }
        _shell = new ShellConfigurator(_bridgeSystem, _notificationSystem);
        //Foo()
        //Bar()
    }
}

If you want to use _shell in other class, add a static using to Program class:

using static Namesapace.Program;

replace Namespace with a defined namespace in your project.

Notification

If you want customize shell output need implement the INotificationSystem interface or can use default implementation with NotificationSystem.Default static class.

using ToolBox.Notification;
public sealed class ConsoleNotificationSystem : INotificationSystem
{
    private string _pastMessage { get; set; } = "";

    public void StandardOutput(string message)
    {
        var diff = message.Except(_pastMessage).ToArray();
            if (diff.Length <= 2 && message.Contains("%")) //Control Progress Messages
            {
                Console.SetCursorPosition(0, Console.CursorTop - 1);
            }
            _colorify.Wrap($" {message}", txtPrimary);
            _pastMessage = message;
    }

    public void StandardWarning(string message)
    {
        _colorify.Wrap($" {message}", txtWarning);
    }

    public void StandardError(string message)
    {
        _colorify.Wrap($" {message}", txtDanger);
    }

    public void StandardLine()
    {
        _colorify.BlankLines();
    }
}

Browse

_shell.Browse(url); //Open and URL in default browser

Term

Run a command in a shell terminal.

_shell.Term(command);                           //Run a command in hidden mode
_shell.Term(command, Output.Hidden);            //Run a command in hidden mode
_shell.Term(command, Output.Internal);          //Run a command in internal mode, showing his results in same terminal with INotificationSystem implementation
_shell.Term(command, Output.External);          //Run a command in a new terminal window
_shell.Term(command, Output.Internal, path);    //Path parameter define a path where the command needs to be executed

Using Response to receive command result with: code, stdout and stderr

Response result = _shell.Term("dotnet --version", Output.Hidden);
_shell.Result(result.stdout, "Not Installed");
_colorify.WriteLine(result.code.ToString(), txtInfo);
if (result.code == 0){
    _colorify.WriteLine($"Command Works :D", txtSuccess);
} else {
    _colorify.WriteLine(result.stderr, txtDanger);
}

Result

_shell.Result(value);                   //Clean special characters from value and print in terminal.
_shell.Result(value, warningMessage);   //Clean special characters from value and print in terminal or it's empty show the warningMessage.

System

Include operations relative to System.

using ToolBox.System;

Environment Variables

Env.GetValue(key);        //Return value from key
Env.SetValue(key, value); //Set value to key (only for program session, not permanent)
Env.IsNullOrEmpty(key);   //Return true when value from key is defined

Network

Network.GetLocalIPv4();             //Return current ip address
Network.GetOctetsIPv4(ip, number);  //Return ip address with octets defined on number

User

User.GetUserName(); //Return logged username
User.GetMachine();  //Return machine name
User.GetDomain();   //Return domain name

Transform

Include operations relative to Transform Text.

using ToolBox.Transform;

Strings

Strings.CleanSpecialCharacters(value); //Receive an string and clean \r (carriage return) and \n (new line) characters.
Strings.RemoveWords(value, wordsToRemove[]);  //Return value without removed words
Strings.GetWord(value, wordPosition); //Search in value the wordPosition and return the word.
Strings.SplitLines[](value, wordPosition); //Receive a value string and split on array when found \n (New Line) character and return an array with all lines.
Strings.ExtractLine(value, search); //Receive a value string and split on array when found \n (New Line) character and return first line with search value.
Strings.ExtractLine(value, search, wordsToRemove[]); //Receive a value string and split on array when found \n (New Line) character and return first line with search value and removeWords defined.

Validations

Include operations relative to Validations.

using ToolBox.Validations;

Boolean

Bool.SomeFalse(values[]);  //Return true if there is false value

Number

Number.IsNumber(value);             //Return true if value is number
Number.IsOnRange(min, value, max);  //Return true if number is between min and max

Strings

Strings.SomeNullOrEmpty(values[]);  //Return true if there is an empty or null value

Web

Web.IsUrl(value); //Return true if value is an http or https valid address

Back to menu


About

Built With

  • .Net - .Net is a free and open-source web framework, developed by Microsoft and the community.
  • VS Code - Code editing redefined.
  • SonarQube - Continuous code quality.

Contributing

Please read CONTRIBUTING for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the ToolBox on GitHub.

Authors

See also the list of contributors who participated in this project.

Sponsors

If this project help you reduce time to develop, you can give me a cup of coffee.

GitHub Sponsors paypal

No sponsors yet! Will you be the first?

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Back to menu