.Net Core,  Microsoft

Play with dotnet tool and a BOT

I was poking about with cleaning up my DotNet Core sdks and in my travels a found this thing call dotnet tools… Let the experiment begin… To credit the source of an article on Microsoft go here: dotnet tool 

Do a console project with dotnet:

dotnet new console -o botsay

Then, of course, cd into the botsay directory… and edit the Program.cs (yeap you can do this in VS 2017)

Here’s the main method:

using System.Reflection;

static void Main(string[] args)
{
if (args.Length == 0)
{
var versionString = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion
.ToString();

Console.WriteLine($"botsay v{versionString}");
Console.WriteLine("-------------");
Console.WriteLine("\nUsage:");
Console.WriteLine(" botsay <message>");
return;
}

ShowBot(string.Join(' ', args));
}

The ShowBot method is this:

static void ShowBot(string message)
{
    string bot = $"\n        {message}";
    bot += @"
    __________________
                      \
                       \
                          ....
                          ....'
                           ....
                        ..........
                    .............'..'..
                 ................'..'.....
               .......'..........'..'..'....
              ........'..........'..'..'.....
             .'....'..'..........'..'.......'.
             .'..................'...   ......
             .  ......'.........         .....
             .    _            __        ......
            ..    #            ##        ......
           ....       .                 .......
           ......  .......          ............
            ................  ......................
            ........................'................
           ......................'..'......    .......
        .........................'..'.....       .......
     ........    ..'.............'..'....      ..........
   ..'..'...      ...............'.......      ..........
  ...'......     ...... ..........  ......         .......
 ...........   .......              ........        ......
.......        '...'.'.              '.'.'.'         ....
.......       .....'..               ..'.....
   ..       ..........               ..'........
          ............               ..............
         .............               '..............
        ...........'..              .'.'............
       ...............              .'.'.............
      .............'..               ..'..'...........
      ...............                 .'..............
       .........                        ..............
        .....
";
    Console.WriteLine(bot);
}

It’s a simple core console app that takes a text phrase as input and shows the bot saying it.

Now the fun part… Of course, you should make sure that the code compiles. 
Go back to your command window and do the folllowing:

 

dotnet run
dotnet run -- "Hello from the bot"
dotnet run -- hello from the bot

It’s a Bot!

Now… let’s make a dotnet tool…

Edit your botsay.csproj file. You can do this in VS 2017 or your favorite notepad… 

You need to add these lines:

  <PackAsTool>true</PackAsTool>
<ToolCommandName>botsay</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
 

In the end the csproj file show look like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>

    <PackAsTool>true</PackAsTool>
    <ToolCommandName>botsay</ToolCommandName>
    <PackageOutputPath>./nupkg</PackageOutputPath>

  </PropertyGroup>

</Project>

Now the tool making part:

dotnet pack

dotnet tool install --global --add-source ./nupkg botsay

You can invoke the tool using the following command: botsay
Tool 'botsay' (version '1.0.0') was successfully installed.

Once you get the message back from the “dotnet tool”… command you now have a dotnet tool…
** You may have to close the command window and open a new one… depending on your OS, etc.

You can type botsay –You’re Message Here…

Now, as you experiment don’t forget to clean up 

dotnet tool uninstall -g botsay

You should read more about dotnet tool… start here: dotnet tool on Mircosoft docs

 

 

Leave a Reply