Saturday, February 2, 2019

.NET Core Tools

.NET Core Tools was initially introduced with .NET Core 2.1. Basically .NET Core tools are .NET Core console apps that are packaged and distributed as NuGet packages and is similar to NPM global tools. Up to the release of .NET Core 3 Preview 1.0 back in December 2018, .NET Core tools are global meaning once installed, they are available from any location on the machine for the current user. But this did not allow the different tools version to be selected per location. Say for one repository you want the specific tool version of A, and for another repository, you want the version B, it wasn’t possible, but that’s until the release of .NET Core 3 Preview 2.0.

With .NET Core 3 Preview 2.0 which was released last week (29th January 2019), you can now maintain .NET Core tools locally as well as globally.

In this post, let’s have a look at how we can achieve that.

dotnetsay is a very basic .NET Core tool Microsoft has developed (you can find some nice set of tools here in this repo: natemcmaster/dotnet-tools). For this demo, let’s use the tool dotnetsay.

.NET Core Global Tools

To install a tool globally you can use the dotnet tool install command with --global/-g flag.
dotnet tool install -g dotnetsay --version 2.1.4
To run the tool you can use just say dotnetsay from the command prompt. Note: this is the ToolCommandName which is configured in the tool.
To list all of the tools, use dotnet tool list.
dotnet tool list -g

dotnet tool list -g
To update a tool, use dotnet tool update.
dotnet tool update -g dotnetsay
And to uninstall, dotnet tool uninstall.
dotnet tool uninstall -g dotnetsay
Basically --global/-g flag means the .NET Core tools at the machine level.

.NET Core Local Tools

To install a .NET Core tool to a specific location, you can use the --tool-path flag.
dotnet tool install dotnetsay --tool-path "C:\Users\Jaliya\Desktop\dotnet-tool-dotnetsay" 
The above command will install dotnetsay latest version to a folder named dotnet-tool-dotnetsay in my Desktop. With .NET Core Local tools, there comes this concept of a manifest file. You can define a manifest file which is a json file at your folder and define all the tools and their versions you like to have it there. To create the manifest file, you can run the following command.
dotnet new tool-manifest
Once you run this command, you can see a folder created named .config. There inside, you can see a json file created named dotnet-tools.json which has the following structure.
  "version": 1,
  "isRoot": true,
  "tools": {}
Now sitting inside your folder (dotnet-tool-dotnetsay here in my case) run the following command.
dotnet tool install dotnetsay
And now you should see the dotnet-tools.json file is updated as follows.
  "version": 1,
  "isRoot": true,
  "tools": {
    "dotnetsay": {
      "version": "2.1.4",
      "commands": [
To run local tools, you can’t execute ToolCommandName just like you did for global tools. For that, you need to use dotnet tool run command.
dotnet tool run dotnetsay
To see all your local tools, run dotnet tool list.
dotnet tool list
And you can see the following output.

dotnet tool list
There you can see which manifest is being used.

One of the important things to note is, if you don’t have a manifest file inside a folder, to install a local tool, you need to specify --tool-path flag.

Hope this helps.

Happy Coding.


No comments:

Post a Comment