|
| 1 | +# 0803 - Command Line Tips and Text-Editors |
| 2 | + |
| 3 | +.NET and .NET Core have been built to be command-line driven first. Visual Studio and other code tools use the same capabilities to build and interact with your projects as the command-line tools. This means that regardless of which operating system you are using and what tools are available, you can always build and work with .NET code at the command-line. |
| 4 | + |
| 5 | +The terms .NET Command-line interface (CLI) and Software Development Kit (SDK) are used interchangably to refer to the `dotnet` command and tools that can run on Windows, Mac, or Linux |
| 6 | + |
| 7 | +## Acquisition and setup |
| 8 | + |
| 9 | +While we know .NET comes with Visual Studio, installing the command-line tools is easy from https://dot.net/download The website will detect which browser and operating system you're using and recommend an immediate download appropriate for your system. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Other downloads for other operating systems are available. |
| 14 | + |
| 15 | +### Install on Linux |
| 16 | + |
| 17 | +Installing on Linux can be performed at the command-line using various package managers and scripts. |
| 18 | + |
| 19 | +For example, on Ubuntu you can install the .NET 7 version of the SDK using the following command: |
| 20 | + |
| 21 | +```bash |
| 22 | +sudo apt-get update && \ |
| 23 | + sudo apt-get install -y dotnet-sdk-7.0 |
| 24 | +``` |
| 25 | + |
| 26 | +Additionally, there is a [Linux install script available](https://learn.microsoft.com/dotnet/core/install/linux-scripted-manual#scripted-install) that will attempt download and install the .NET SDK. |
| 27 | + |
| 28 | +```bash |
| 29 | +wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh |
| 30 | +``` |
| 31 | + |
| 32 | +Set the script to executable: |
| 33 | + |
| 34 | +```bash |
| 35 | +chmod +x ./dotnet-install.sh |
| 36 | +``` |
| 37 | + |
| 38 | +Finally execute the script to install .NET |
| 39 | + |
| 40 | +```bash |
| 41 | +./dotnet-install.sh --version latest |
| 42 | +``` |
| 43 | + |
| 44 | +[Complete and current directions for installing on Linux](https://learn.microsoft.com/dotnet/core/install/linux) are available on Microsoft Learn. |
| 45 | + |
| 46 | +## Which .NET? |
| 47 | + |
| 48 | +When downloading the SDK, its important to note that all SDKs are fully backwards compatible. You can optionally specify supported .NET versions to work with older projects. Unless you have a specific reason to support an older project on a framework that is not currently supported, always download the latest version of the SDK. |
| 49 | + |
| 50 | +You can choose to download a preview version, and know that you can choose which .NET SDK version you are using as they can be run side-by-side on any device. |
| 51 | + |
| 52 | +For example, you can create a new website and specify a framework version using the .NET 8 SDK with any of the following commands to start a website with a template for the .NET 6, .NET 7, or .NET 8 versions: |
| 53 | + |
| 54 | +```bash |
| 55 | +dotnet new web -f net6.0 |
| 56 | +dotnet new web -f net7.0 |
| 57 | +dotnet new web -f net8.0 |
| 58 | +``` |
| 59 | + |
| 60 | +## Version control with global.json |
| 61 | + |
| 62 | +With every version of the .NET tools, you can create a file called `global.json` that will specify which version of the .NET SDK should be used in that folder and all child folders. |
| 63 | + |
| 64 | +This file contains a minimum version number and directions about which version numbers of the .NET SDK are allowed to be used. |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "sdk": { |
| 69 | + "version": "6.0.300", |
| 70 | + "rollForward": "latestFeature", |
| 71 | + "allowPrerelease": true |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +More information about [global.json](https://learn.microsoft.com/dotnet/core/tools/global-json) and controlling the .NET SDK can be found on Microsoft Learn. |
| 77 | + |
| 78 | +## Which version do I have installed? |
| 79 | + |
| 80 | +You can report the current version of the .NET SDK detected and running for the current folder with: |
| 81 | + |
| 82 | +```bash |
| 83 | +dotnet --version |
| 84 | +``` |
| 85 | + |
| 86 | +You can see the complete list of runtimes and tools installed with: |
| 87 | + |
| 88 | +```bash |
| 89 | +dotnet --info |
| 90 | +``` |
| 91 | + |
| 92 | +## General Format of dotnet commands |
| 93 | + |
| 94 | +The command-line is set up around a basic "action verb noun" syntax. You specify a type of action you would like to take, and then an action with additional descriptors to take with that: |
| 95 | + |
| 96 | +```bash |
| 97 | +dotnet new web |
| 98 | +dotnet run |
| 99 | +dotnet build |
| 100 | +dotnet watch test |
| 101 | +dotnet ef database update |
| 102 | +``` |
| 103 | + |
| 104 | +All commands have help text available when the `--help` argument is added. |
| 105 | + |
| 106 | +All of the commands shown work the same on every operating system for the same version of the .NET SDK. |
| 107 | + |
| 108 | +## How do I get additional tools to work with .NET MAUI or WASM projects? |
| 109 | + |
| 110 | +There is an additional set of tools and downloads available that install SDKs and tools to work with iOS, Android, MacOS, and Web Assembly compilers under a feature called `workloads`. |
| 111 | + |
| 112 | +You can view the list of available workloads using: |
| 113 | + |
| 114 | +```bash |
| 115 | +dotnet workload list |
| 116 | +``` |
| 117 | + |
| 118 | +You can install a workload with Android tools using a command like |
| 119 | + |
| 120 | +```bash |
| 121 | +dotnet workload install android |
| 122 | +``` |
| 123 | + |
| 124 | +## How do I create an application? |
| 125 | + |
| 126 | +You can generate a new application from a template using: |
| 127 | + |
| 128 | +```bash |
| 129 | +dotnet new <TEMPLATE NAME> |
| 130 | +``` |
| 131 | + |
| 132 | +.. but which template should you use? Get a complete list of templates using: |
| 133 | + |
| 134 | +```bash |
| 135 | +dotnet new --list |
| 136 | +``` |
| 137 | + |
| 138 | +In .NET 8 and newer, the syntax has been simplified to: |
| 139 | + |
| 140 | +```bash |
| 141 | +dotnet new list |
| 142 | +``` |
| 143 | + |
| 144 | +Create a new web site with a command like: |
| 145 | + |
| 146 | +```bash |
| 147 | +dotnet new web -n MyCoolWebsite -o src -f net8.0 |
| 148 | +``` |
| 149 | + |
| 150 | +This creates a project called `MyCoolWebsite` in the `src` folder using the .NET 8 framework. |
| 151 | + |
| 152 | +You can start the website using the command: |
| 153 | + |
| 154 | +```bash |
| 155 | +dotnet run |
| 156 | +``` |
| 157 | + |
| 158 | +This will restore any missing packages, build, and launch the application for you. You can optionally run those steps individually with these commands: |
| 159 | + |
| 160 | +```bash |
| 161 | +dotnet restore |
| 162 | +dotnet build |
| 163 | +``` |
| 164 | + |
| 165 | +You can also instruct the command-line to clean any build artifacts on disk with: |
| 166 | + |
| 167 | +```bash |
| 168 | +dotnet clean |
| 169 | +``` |
| 170 | + |
| 171 | +All of these steps in a build and run process are optional, and you can skip them with appropriate command-line arguments: |
| 172 | + |
| 173 | +```bash |
| 174 | +dotnet run --no-build --no-restore |
| 175 | +dotnet build --no-restore |
| 176 | +``` |
| 177 | + |
| 178 | +### Release Configuration |
| 179 | + |
| 180 | +You can specify Debug, Release, or another configuration when building your project with the `-c` argument. When not specified, `Debug` is assumed |
| 181 | + |
| 182 | +```bash |
| 183 | +dotnet run -c Release |
| 184 | +``` |
| 185 | + |
| 186 | +### Building for other runtimes and processor architectures |
| 187 | + |
| 188 | +The runtime and processor of your target machine may not match your current workstation, and you can build for that target by using the `-r` argument with an appropriate term from the list of [available runtimes](https://learn.microsoft.com/dotnet/core/rid-catalog). |
| 189 | + |
| 190 | +For example, to build a project to run on a Raspberry Pi device running Linux, we would target the `linux-arm64` runtime: |
| 191 | + |
| 192 | +```bash |
| 193 | +dotnet build -r linux-arm64 |
| 194 | +``` |
| 195 | + |
| 196 | +### Bundling for publication |
| 197 | + |
| 198 | +The `publish` command will bundle a project and get it ready to distribute to another device. Many of the arguments on the `run` and `build` commands are also available on the `publish` command. |
| 199 | + |
| 200 | +Let's publish that project for a Raspberry Pi and output to the `dist` folder for distribution: |
| 201 | + |
| 202 | +```bash |
| 203 | +dotnet publish -c Release -r linux-arm64 -o dist |
| 204 | +``` |
| 205 | + |
| 206 | +The configuration for a publication should be `Release`, and starting with .NET 8 that is the assumed value if the `-c` switch is not used. |
| 207 | + |
| 208 | +### Self-contained |
| 209 | + |
| 210 | +Applications from .NET 7 and earlier are published in 'self-contained' mode with all required runtime libraries copied into the output folder. You can force this behavior in .NET 8 and later with the `--sc` argument. |
| 211 | + |
| 212 | +Projects can be built in 'framework dependent' mode where they depend on the .NET runtime to be installed and available on the target device. This results in a significantly smaller deployment. |
| 213 | + |
| 214 | +You can install _JUST_ the runtime using the runtime installer available at the https://dot.net/download ___location shown previously. |
| 215 | + |
| 216 | +### Testing |
| 217 | + |
| 218 | +You can execute unit tests within a project by using the `test` command: |
| 219 | + |
| 220 | +```bash |
| 221 | +dotnet test |
| 222 | +``` |
| 223 | + |
| 224 | +### Live Reload |
| 225 | + |
| 226 | +You can activate a live-reload feature for your web applications and test projects using the `watch` command. This will start the web application or unit test project and watch for changes in the project or its dependencies. Once a change is detected, it will restart the project or tests. |
| 227 | + |
| 228 | +```bash |
| 229 | +dotnet watch # Run a program |
| 230 | +``` |
| 231 | + |
| 232 | +```bash |
| 233 | +dotnet watch test # Execute unit tests |
| 234 | +``` |
| 235 | + |
| 236 | +## Text editor assistance with Omnisharp |
| 237 | + |
| 238 | +[Omnisharp](https://omnisharp.net) is a library that was built by the .NET Community to enable .NET syntax highlighting and improved interactions in various text editors. Omnisharp supports: |
| 239 | + |
| 240 | +- [Emacs](https://github.com/OmniSharp/omnisharp-emacs) |
| 241 | +- [Sublime Text](https://github.com/OmniSharp/omnisharp-sublime) |
| 242 | +- [Vim](https://github.com/OmniSharp/omnisharp-vim) |
| 243 | +- Visual Studio Code |
| 244 | + |
| 245 | +Visual Studio Code has an upgraded version of tools that integrate with Omnisharp to give it an improved experience. |
0 commit comments