Sunday, April 16, 2017

How to make a dotnet CLI Tool

Good news, everyone! It is remarkably easy to make a new dotnet CLI (Command Line Interface) tool! I recently created a CLI tool for one of my new projects, Tact.NET RPC, and in this post I will be referencing that project as my example.

The Basics

Step 1) Create your CLI Console App

All you have to do is...

  1. Create a normal .NET Core Console App.
    • NOTE: Currently, the dotnet CLI only supports netcoreapp1.0
  2. Rename the assembly to be prefixed with "dotnet-"
  3. dotnet pack the project and put the package in your local NuGet package source

...that is it! It is literally that easy to create your CLI tool!

Recommendation: use Microsoft.Extensions.Configuration to parse your command line arguments in a standard way.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <AssemblyName>dotnet-tactrpcgen</AssemblyName>
    <PackageId>Tact.Rpc.Generator</PackageId>
    <Version>1.0.3</Version>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />
  </ItemGroup>
</Project>

Step 2) Consume your CLI Tool NuGet Package

Edit your csproj file and add a DotNetCliToolReference element that references your package.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Tact.Rpc.Generator" Version="1.0.3" />
  </ItemGroup>
</Project>

Now you are able to invoke your console app via the dotnet command line whenever it is executed in the same path as the csproj.

That's it; it really is that easy!

Development Tips

Here is a simple way to automate the creation and consumption of your CLI tool NuGet package during development.

Create a local package directory

I recommend that you create a packages folder at the root of your solution along with a NuGet.config file; this will allow you to automatically build and consume NuGet packages from this location.

<configuration>
  <packageSources>
    <add key="LocalPackages" value="packages" />
  </packageSources>
</configuration>

Pack on post build

Go back and update your CLI tool's csproj to include a post build step that packs your project and places the output in your package folder. (There is a csproj GeneratePackageOnBuild setting, however at this time I do not believe that you can customize the output directory, so instead I recommend using a post build step.)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <AssemblyName>dotnet-tactrpcgen</AssemblyName>
    <PackageId>Tact.Rpc.Generator</PackageId>
    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
    <PostBuildEvent>
      dotnet pack $(SolutionDir)rpc\src\Tact.Rpc.Generator -o $(SolutionDir)packages --no-build
    </PostBuildEvent>
    <Version>1.0.3</Version>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />
  </ItemGroup>
</Project>

Execute on pre build

Obviously this is a completely optional step that should only be used when necessary; in the case of Tact.NET RPC, the active code generation is powered by the CLI tool, and thus it needs to run every build.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <PreBuildEvent>
      cd $(SolutionDir)rpc\demo\Demo.Rpc
      dotnet tactrpcgen
    </PreBuildEvent>
  </PropertyGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Tact.Rpc.Generator" Version="1.0.3" />
  </ItemGroup>
</Project>

Increment version to update

NOTE: Despite updating the your local package directory each build, the package will actually be cached in your global NuGet cache, and thus updates will not be picked up unless you increment the version number.

Enjoy,
Tom

14 comments:

  1. The code generator module utilizes the "parse information" to build the test saddle source code.
    mélybölcsős fuvarozás Europa-Road Kft.

    ReplyDelete
  2. Pakistan no.1 Article in my softcrackersstore.com site Now just 1 click to download!
    GoodSync Crack
    Brave Browser Crack
    VSDC Video Editor Pro Crack

    ReplyDelete
  3. Thanks for the great post you posted. I like the way you describe the unique content. The points you raise are valid and reasonable. I am a tech support expert telling you about.
    best cryptocurrency exchange|
    kraken exchange|
    uniswap exchange |
    crypto.com exchange|

    ReplyDelete
  4. Cryptocurrencies have evolved and managed to leave a mark in the next chapter of the financial industry along with tech improvisations. It has been quite the journey for the digital currencies from no financial worth to hundreds of dollars’ worth. robinhood wallet | robinhood crypto wallet

    ReplyDelete
  5. Finances in our world were given an online existence and backed with digital cryptography, it is now known as cryptocurrency. To make crypto easily accessible to the entire world, crypto exchange platforms came to life along with crypto wallets to provide a safe space for your crypto funds.
    Coinbase.com login |
    Pro.coinbase.com Login |
    Coinbase Wallet

    ReplyDelete
  6. Binance is a cryptocurrency exchange that provides a platform for trading various cryptocurrencies. In addition, it provides a crypto wallet for its traders, where they can store their electronic funds.
    binance us login
    The exchange also has supporting programs that help traders make investment decisions. In addition, Binance has its blockchain-based token, Binance Coin.
    binance.com login

    ReplyDelete
  7. These are genuinely fantastic ideas about blogging really. You have touched some very nice points here. Please keep up this good writing.

    B Sc 1st NC Result

    ReplyDelete
  8. If you have the details that you need to fill up your Uphold sign-up form then you need to jump to the next section to start the sign-up and login process.
    uphold login
    Kraken is considered one of the most preferred crypto exchanges for Bitcoin trading. Trading Bitcoin kraken is safer on this exchange.
    kraken login

    ReplyDelete
  9. The principal justification behind these is to get the youngster's advantage up and get the inventive energies siphoning and capability commendably all through the juvenile years. Clearly, PC games entered the picture sooner or later after the young person sorted out some way to press a catch and will continue into adulthood in an astonishing number of cases. toys remote control toys

    ReplyDelete
  10. Uphold is available on both desktop computers and smartphones, so to get started with this cryptocurrency exchange platform, you must either visit Uphold.com or find the uphold app in your phone’s app store.
    uphold wallet|uphold exchange|blockfi wallet|block fi wallet | blockfi app |

    ReplyDelete
  11. Thanks for giving such a wonderful informative information. The detailed read above has been fully-equipped to first, introduce you to the Kucoin App and then take you through exclusive details on the subject. Reading through the data piece above, you are now aware of all the pros and cons that an easily-created KuCoin login account could bring to you, along with significant details on the United States’ jurisdiction that doesn’t allow the exchange to function there and also the list of major crypto available on it.
    For More About:- Binance smart chain wallet$Kucoin.com$Kucoin Wallet

    ReplyDelete

Real Time Web Analytics