Saturday, March 18, 2023

.NET command-line interface (CLI)


# [Overview of .NET CLI](https://learn.microsoft.com/en-us/dotnet/core/tools/)

The .NET command-line interface (CLI) is a cross-platform tool chain for developing,
 building, running, and publishing .NET applications. 

#### Here is list of command to create list of projects with console app template using dotnet cli

```cs
	$solutionName="ConsoleApp"
	$solutionPath="c:\temp\dotnet\ConsoleApp"
	dotnet new sln -n $solutionName -o $solutionPath

	cd $solutionPath

 	dotnet new console  -o  "$solutionName.UI"  -lang "c#"  -f net7.0
 	dotnet new console  -o  "$solutionName.UI.Tests"
 	dotnet new console  -o  "$solutionName.ApiService"
 	dotnet new console  -o  "$solutionName.ApiService.Tests"
 	dotnet new console  -o  "$solutionName.Business"
 	dotnet new console  -o  "$solutionName.Business.Tests"
 	dotnet new console  -o  "$solutionName.Data"
 	dotnet new console  -o  "$solutionName.Data.Tests"
```
#### Adding projects to solution using dotnet cli

> Note: Below ls command will work for `Powershell`

```sh
 	dotnet sln "$solutionName.sln" add (ls -r **/*.csproj) 
```

#### Adding project references using dotnet cli

```sh
  	dotnet add "$solutionName.UI/$solutionName.UI.csproj" reference 
   		"$solutionName.ApiService/$solutionName.ApiService.csproj"

	dotnet add "$solutionName.ApiService/$solutionName.ApiService.csproj" 
 	 reference "$solutionName.Business/$solutionName.Business.csproj"

    dotnet add "$solutionName.Business/$solutionName.Business.csproj" 
  		reference "$solutionName.Data/$solutionName.Data.csproj"

  	dotnet add "$solutionName.UI.Tests/$solutionName.UI.Tests.csproj" 
   		reference "$solutionName.UI/$solutionName.UI.csproj"

  	dotnet add "$solutionName.ApiService.Tests/$solutionName.ApiService.Tests.csproj"
   		reference "$solutionName.ApiService/$solutionName.ApiService.csproj"

  	dotnet add "$solutionName.Business.Tests/$solutionName.Business.Tests.csproj"
 		reference "$solutionName.Business/$solutionName.Business.csproj"

  	dotnet add "$solutionName.Data.Tests/$solutionName.Data.Tests.csproj"
 		reference "$solutionName.Data/$solutionName.Data.csproj"
```

#### Build solution using dotnet cli

```sh
	dotnet build "$solutionName.sln"
```  


#### Here is list of commands to create list of projects using specific project type templates  using dotnet cli

####  Following list of commands to create solution file for adding specific project later

```sh
/*Input to specify project type 
  List of avaiable project types :webApp |mvc|angular|react|blazorserver|blazorwasm|wpf|winforms|      */ 

 $projectType= "WebApp"  
    
 $solutionPath="c:\temp\dotnet"

 if ($projectType -eq "WebApp"){ $solutionName=$projectType }
 	else{ $solutionName="$projectType-App"}

 if ($projectType -eq "blazorwasm"){ $framework="net6.0"} else{$framework="net7.0" }
	$solutionPath="$solutionPath\$solutionName"

	dotnet new sln -n $solutionName -o $solutionPath
```
####  Following list of commands to create projects with specific project types for each layer.

```sh
	cd $solutionPath

/*Create C# project with specific ui project template and specific framework.*/
 	dotnet new $projectType.ToLower() -o  "$solutionName.UI"  -lang "c#"  -f $framework
 	
/*Create test project for ui project with nunit project template*/
    dotnet new nunit -o  "$solutionName.UI.Tests"
	
/*Create C# project with webapi project template for api service layer*/
	dotnet new webapi  -o  "$solutionName.ApiService"
 	
/*Create test project with xunit project template for Api project.*/
	dotnet new xunit   -o  "$solutionName.ApiService.Tests"
 	
/*Create C# project with classlib project template for business layer.*/
	dotnet new classlib  -o  "$solutionName.Business"
    
/*Create C# project with mstest project template for Business project.*/
 	dotnet new mstest  -o  "$solutionName.Business.Tests"
 	
/*Create C# project with classlib project template for data layer.*/
    dotnet new classlib -o  "$solutionName.Data"

/*Create C# project with nunit project template for Data layer*/
	dotnet new nunit -o  "$solutionName.Data.Tests"

```
#### Adding projects to solution file.

```sh
/*Adding all projects to solution.*/
    dotnet sln "$solutionName.sln" add (ls -r **/*.csproj) 
```

```sh
 dotnet add "$solutionName.UI/$solutionName.UI.csproj"
   	reference "$solutionName.ApiService/$solutionName.ApiService.csproj"

 dotnet add "$solutionName.ApiService/$solutionName.ApiService.csproj" 
   	reference "$solutionName.Business/$solutionName.Business.csproj"

 dotnet add "$solutionName.Business/$solutionName.Business.csproj"
   	reference "$solutionName.Data/$solutionName.Data.csproj"

 dotnet add "$solutionName.UI.Tests/$solutionName.UI.Tests.csproj"
   	reference "$solutionName.UI/$solutionName.UI.csproj"

 dotnet add "$solutionName.ApiService.Tests/$solutionName.ApiService.Tests.csproj"
 	reference "$solutionName.ApiService/$solutionName.ApiService.csproj"

 dotnet add "$solutionName.Business.Tests/$solutionName.Business.Tests.csproj"
 	reference "$solutionName.Business/$solutionName.Business.csproj"
  
 dotnet add "$solutionName.Data.Tests/$solutionName.Data.Tests.csproj" 
 	reference "$solutionName.Data/$solutionName.Data.csproj"
```
#### Build solution

```sh
 	dotnet build "$solutionName.sln"
```
 
- References
    [dotnet tools](https://learn.microsoft.com/en-us/dotnet/core/tools/)

	- List dotnet runtimes installed 
   	  
    	```sh
    		dotnet  --list-runtimes
    	```
	- List dotnet SDKs installed 
    
      ```sh
			dotnet  --list-sdks
	  ```
   		
	- Latest installed dotnet version  
   
   	 ```sh
 			dotnet  --version
   	 ```
 
 

- New project or Project item
   
 	Below Command show help for the C# console application template:

	```sh
      	dotnet new console -h
     ```
    
    Create a C# console application project:
  		
    ```sh
     	dotnet new console
  	```
    
  	Create an C# console application project in the current directory
  	  
      ```sh
		dotnet new console --language "C#"
  	  ```
  	  
      Create an C# Blazor server application project in the current directory with no https protocal
  		
      ```sh
  		dotnet new blazorserver -o BlazorApp --no-https
  	  ```
 
 	  Create an C# Blazor web assembly project  in the current directory with no https protocal
 	  
      ```sh
  			dotnet new blazorwasm -o BlazorWasmApp --no-https
  	  ```

 	  Create a .NET Standard 4.0 class library project in the specified directory:
  		
      ```sh
  			dotnet new classlib --framework "netstandard4.0" -o MyLibrary
  	  ```
  		
      Create a new ASP.NET Core C# MVC project in the current directory with no authentication:
  			
      ```sh
  			dotnet new mvc -au None
  	  ```
  		
      Create a new xUnit project:
        
      ```sh
          dotnet new xunit
      ```
        
      Create a global.json in the current directory setting the SDK version to 3.1.101:
         
      ```sh
          dotnet new globaljson --sdk-version 3.1.101
      ```
      
      Show help for the VB console application template:
         
      ```sh
         dotnet new console --language "VB" -h
      ```
 
 	- Create Solution file 
   
 	   Below Command show help for solution
  		 ```sh
    		dotnet sln [command] -h|--help
   		 ```
    
     Syntax
  	  ```sh
    	dotnet sln [] [command]
   	  ```
     
     Creates a .sln file in the current folder, with the same name as the folder:
     ```sh
     	dotnet new sln
	 ```
     Creates a .sln file in the current folder, with the specified file name:
     ```sh
        dotnet new sln --name MyApp
     ```
    
    Creates a .sln file in the specified folder, with the same name as the folder:
    ```sh
    	dotnet new sln --output C:\temp\App
     ```
 

No comments:

Post a Comment