Create, Install, and Update a .NET CLI tool in C# - GUIDs Generator
This post outlines how to create, package, install, and later update a global .NET CLI tool. The example tool is called Guider. It generates GUIDs (Globally Unique Identifiers) and copies the generated GUID to the clipboard.
The project is available on GitHub at https://github.com/yamesant/guider
The tool can be run from anywhere on the computer and works as follows:
> guidgen
ef714860-a4d9-407e-a129-025c9547cdab
The same GUID is saved to the clipboard for quick pasting to elsewhere.
This tool behaves similarly to a standard uuidgen
tool:
> uuidgen
70AE6C87-D8B2-46E3-8BCF-5F02E292F8B8
According to this RFC, UUIDs (Universally Unique Identifiers) and GUIDs are two names for the same concept.
Implement
The implementation is straightforward because the project relies on pre-built functionality for both GUID generation and clipboard manipulation.
The C# language natively supports GUIDs in the System
namespace. The documentation for the GUID generation method can be found here. For clipboard manipulation, the project relies on the TextCopy library.
This commit implements the functionality. The code fits in five lines:
using TextCopy;
Guid guid = Guid.NewGuid();
Console.WriteLine($"Here is the GUID: {guid}");
ClipboardService.SetText(guid.ToString());
Now the project can be run with the dotnet run
command, and this is the result:
> dotnet run
Here is the GUID: 961786f3-739f-47d5-bd53-7e53a74467c4
Share
This commit adds the following properties to the .csproj
file in preparation for sharing the console project as a CLI tool. It also excludes the ./packages
directory from Git tracking.
<PackAsTool>true</PackAsTool>
<ToolCommandName>guidgen</ToolCommandName>
<PackageOutputPath>./packages</PackageOutputPath>
<PackageId>Guider</PackageId>
<Version>1.0.0</Version>
The project can then be packed with the dotnet pack
command. By default, this command uses the project name ("Guider.Console" in this case) as the package ID. The PackageId
property overrides this behaviour. Specifying the Version
property is also convenient. Without it, when the tool is updated, the new version has to be manually passed to the dotnet pack
command as an argument.
Once the dotnet pack
command is run, the directory structure looks like this:
The next command installs this tool for global use, i.e. it can be run in any directory.
dotnet tool install Guider --global --add-source ./packages
Use
The Guider tool is now installed and can be invoked from the terminal in any directory.
> guidgen
Here is the GUID: d6a0b682-6b09-4b0c-ab33-11ae757bbe84
All available .NET tools can be checked with the dotnet tool list --global
command. An example output is:
> dotnet tool list --global
Package Id Version Commands
--------------------------------------
dotnet-ef 8.0.8 dotnet-ef
guider 1.0.0 guidgen
Update
The preamble "Here is the GUID: " does not look nice. This commit removes it and updates the version.
Once the dotnet pack
command is rerun, a new package appears in the ./packages
folder:
The tool can be updated as follows:
> dotnet tool update Guider --global --add-source ./packages
Skipping NuGet package signature verification.
Tool 'guider' was successfully updated from version '1.0.0' to version '1.0.1'.
Now it outputs only the GUID:
> guidgen
d531b5e3-177a-42d0-a29e-f68dcd44366c
Next Steps
The Guider tool has been developed based on the Botsay tutorial, and it mirrors the Botsay tool in its simplicity.
Future enhancements for the Guider tool could include implementation of standard commands such as --help
, --version
, and potentially --no-copy
to prevent automatic copying to the clipboard. Command-line argument parsing can be implemented manually or by using a dedicated library, such as System.CommandLine.