Exploring Global Tools in .NET Core
March 17, 2018 by Anuraj
.NETCore GlobalTools
This post is about Global Tools in .NET Core, Global Tools is new feature in .NET Core. Global Tools helps you to write .NET Core console apps that can be packaged and delivered as NuGet packages. It is similar to npm global tools.
You can install the global tool using dotnet install
command line. And for un-installing you need to manually delete it from the installation location, in Windows it is %USERPROFILE%\.dotnet\tools
and in Linux systems ~/.dotnet/tools
.
Here is a sample .net global tool which helps to ascii art. So first you need to create a console application using dotnet new console -o banner
. Once it is created, you can open the project in VS Code, you need to modify the csproj
file and include following elements.
- PackAsTool - This specifies this is a tool.
- ToolCommandName - Specifies the command name - using this command users can invoke the tool.
Next you need to modify the main method and add the logic. I am using figgle library for the ascii art generation purposes. Here is the code.
Now you can build the tool using dotnet build
command and test it using dotnet .\bin\Debug\netcoreapp2.1\Banner.dll Hello World
command. It will print something like this.
Once it is done, you can run dotnet pack
command to build nuget package. You may see some warnings related to preview versions references, you can ignore them for time being.
Next you need to configure a local nuget repo to verify the tool installation and execution. For this, first create a nuget.config file. And inside this include the folder you created the nuget package. You can create nuget.config file using dotnet new nugetconfig
command. And add the following code. You need to change the value of the local key.
Next run the dotnet install tool -g Banner
command to install your tool.
Once installation is successful, you can run the banner
command to execute the tool.
You can find the source code in GitHub.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub