Do you dotnet cli? Here's how you can add your own tools...

Is the dotnet cli old hat to you now? Or are you just jumping on board?

You might think it's an all or nothing kind of tool, that what's in it is all that's in it. Well nope! Anuraj has written up a nice post on how you can extend it, adding your own commands/tools to it...

Building a custom dotnet cli tool

This post is about building a custom dotnet cli tool, using this you can extend the dotnet cli for various operations like minifing images, scripts, css etc. The tools used by the .NET CLI are just console applications, so you can create a dotnet core console application and use it. In this post I am building a tool to optimize images in the web application. I am using the ImageProcessorCore package.

Using the ImageProcessorCore, I am fetching all the image files under the current directory and sub directories, and loading the image file in ImageProcessorCore image class and setting the quality to 10, which will reduce the size of the image. The one thing that is different from the normal dotnet core console app, you need to specify “outputName” in the “buildOptions” of project.json file.

"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"outputName": "dotnet-imgopt"
}

In this “dotnet-imgopt” will make the tool callable from the CLI using “dotnet imgopt”. And here is the code which minify the images. This code is added inside your main method.

Here is the code, which will set the quality of the code.

...

Now you can run the command “dotnet imgopt”, which will invoke the tool and which will compress the images. Here is the screenshot of the tool running on my application.

dotnet cli tool running

Similar to bundling and minification tool, you can run this tool along with the publish events, like this, which will compress the images as part of pre-compile step.

"scripts": {
"precompile": [ "dotnet bundle", "dotnet imgopt" ],
"prepublish": [ "bower install" ]
}

Happy Programming :)

... [Click through to see the entire post]



Tags:

Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
    Unsubscribing
    Subscribing

No comments

Sorry, no comments have been submitted yet.

Please make a comment below or check back later.

Comment on the post

Already have a Channel 9 account? Please sign in