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...
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.
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.
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.
"precompile": [ "dotnet bundle", "dotnet imgopt" ],
"prepublish": [ "bower install" ]
Happy Programming :)
Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.