4 KiB
Developer Guide
Prerequisites
In order to build .NET Command Line Interface, you need the following installed on you machine.
For Windows
- git (available from http://www.git-scm.com/) on the PATH.
For Linux
- git (available from http://www.git-scm.com/) on the PATH.
For OS X
- Xcode
- git (available from http://www.git-scm.com/) on the PATH.
- Install OpenSSL (a .NET Core requirement)
- brew update
- brew install openssl
- mkdir -p /usr/local/lib
- ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
- ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
Building/Running
- Run
build.cmd
orbuild.sh
from the root depending on your OS. If you don't want to execute tests, runbuild.cmd /t:Compile
or./build.sh /t:Compile
.- To build the CLI in macOS Sierra, you need to set the DOTNET_RUNTIME_ID environment variable by running
export DOTNET_RUNTIME_ID=osx.10.11-x64
.
- To build the CLI in macOS Sierra, you need to set the DOTNET_RUNTIME_ID environment variable by running
- The CLI that is built (we call it stage 2) will be laid out in the
bin\2\{RID}\dotnet
folder. You can rundotnet.exe
ordotnet
from that folder to try out thedotnet
command.
A simple test
Using the dotnet
built in the previous step:
cd {new directory}
dotnet new
dotnet restore
dotnet run
Running tests
- To run all tests, invoke
build.cmd
orbuild.sh
which will build the product and run the tests. - To run a specific test project:
- Run
scripts\cli-test-env.bat
on Windows, or sourcescripts/cli-test-env.sh
on Linux or OS X. This will add the stage 2dotnet
folder to your path and set up other environment variables which are used for running tests. cd
into the test's directory- Run
dotnet test
- Refer to the command-line help for
dotnet test
if you want to run a specific test in the test project
- Run
Adding a Command
The dotnet CLI supports several models for adding new commands:
- In the CLI itself via
dotnet.dll
- Through a
tool
NuGet package - Through MSBuild tasks & targets in a NuGet package
- Via the user's
PATH
Commands in dotnet.dll
Developers are generally encouraged to avoid adding commands to dotnet.dll
or the CLI installer directly. This is appropriate for very general commands such as restore, build, publish, test, and clean, but is generally too broad of a distribution mechanism for new commands. Please create an issue and engage the team if you feel there is a missing core command that you would like to add.
Tools NuGet packages
Many existing extensions, including those for ASP.NET Web applications, extend the CLI using Tools NuGet packages. For an example of a working packaged command look at TestAssets/TestPackages/dotnet-hello/v1/
.
MSBuild tasks & targets
NuGet allows adding tasks and targets to a project through a NuGet package. This mechanism, in fact, is how all .NET Core projects pull in the .NET SDK. Extending the CLI through this model has several advantages:
- Targets have access to the MSBuild Project Context, allowing them to reason about the files and properties being used to build a particular project.
- Targets are not CLI-specific, making them easy to share across command-line and IDE environments
Commands added as targets can be invoked once the target project adds a reference to the containing NuGet package and restores.
Targets are invoked by calling dotnet msbuild /t:{TargetName}
Commands on the PATH
The dotnet CLI considers any executable on the path named dotnet-{commandName}
to be a command it can call out to.
Things to Know
- Any added commands are usually invoked through
dotnet {command}
. As a result of this, stdout and stderr are redirected through the driver (dotnet
) and buffered by line. As a result of this, child commands should use Console.WriteLine in any cases where they expect output to be written immediately. Any uses of Console.Write should be followed by Console.WriteLine to ensure the output is written.