dotnet command line app host is one of the important and nice things about dotnet core. As of now it has a very few commands built in to it. Some of the available commands (as of this writing) are
- new – to scaffold a new project
- restore – to restore the nuget packages
- build – to build the dotnet core app
- run – to run the dotnet core app
- test – to execute the unit tests
- publish – to publish the aspnet core apps
- pack – to create a nuget package for the dotnet core app
What if, you feel or you wish you had some more commands ? One way is you can always fork the dotnet cli project from github and follow the contribution guidelines and create the command you wish to have and submit your contribution. Though that’s the best way to add a new feature to the main dotnet cli itself, not many of us would want to take that route. For such scenarios, dotnet core provides two different extensibility models which we can follow to add custom commands that can be executed by “dotnet” driver
- Via Nuget packages that works on per-project basis
- Via scripts that are available at system PATH.
In this post, we will see how we can create a custom command added to the system path. The caveat in this approach is that, this command runs only in the machine wherever its present in the path. If we need this to be redistributed, we have to copy this over to every machine where we need to run this.
Let’s create a custom command called
rebuild which cleans the build outputs every time and then build the app.
Step 1 : Create a simple bash script – on Mac/Linux or a batch script on Windows
Since, I am creating this demo on a Mac, I will be using bash script. The Key point in this approach is that the script should be named in this format
dotnet-<custom command name> Add the following shell script statements and save the file as
#!/bin/bash echo "Cleaning project ..." rm -rf bin/ obj/ echo "Clean Succeeded" echo "Building project..." dotnet build echo "Build Complete"
Step 2 : Make this script an executable
Running the below command sets the execute attribute to true
chmod +x dotnet-rebuild
Step 3 : Add the script to the System’s PATH environment variable
This is the very crucial step as if we miss anything out, dotnet driver wouldn’t be able to execute the script properly. Make sure you give the full path of the file while creating a symbolic link. The other easiest option is to add the full path of the script to the environment variable “PATH”
ln -s /Users/z066157/Desktop/dotnet-rebuild /usr/local/bin/dotnet-rebuild
Step 4 : Execute the command
To test the command really works, run
dotnet build first and then try doing
dotnet build Project hwapp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
As you can see, if the project was built already, the normal build command would skip compilation. Now let’s trigger the rebuild command we just created.
dotnet rebuild Clean Succeeded Building project... Project hwapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing Compiling hwapp for .NETCoreApp,Version=v1.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:00.9050844 Build Complete
That’s pretty much the few steps to extend dotnet commands. Looks cool and easy, right?