If, like me, you create new DotNet projects using the same layout and options; the following script should help.
Type $profile
in your Powershell Windows Terminal and edit the file at the path given by putting the following code in:
# ---------- dot net new simple alias for project creation
function Show-Menu {
param (
[string]$Title
)
Clear-Host;
Write-Host -ForegroundColor Green "================ $Title ================";
Write-Host "1: " -NoNewline; Write-Host -ForegroundColor Yellow "C# Console Application";
Write-Host "2: " -NoNewline; Write-Host -ForegroundColor Yellow "C# Library";
Write-Host "3: " -NoNewline; Write-Host -ForegroundColor Yellow "C# ASP.Net WebApp";
Write-Host "Q: " -NoNewline; Write-Host -ForegroundColor Yellow "Press 'Q' to quit";
}
function dotnetnew() {
param (
[switch]$Verbose = $false,
[switch]$OmitTest = $false,
[string]$Name
)
$dir = Get-Location;
$command = "";
do {
$title = "Create new dotnet project";
if ($Name -ne "") {
$Title = $Title + ": $($Name)";
}
Show-Menu -Title $title;
$selection = Read-Host "What app do you want to create?";
switch ($selection) {
'1' { $command = "console" }
'2' { $command = "classlib" }
'3' { $command = "webapp" }
}
} until ($selection -eq 'q' -or $command -ne '');
if ($selection -eq 'q') {
Write-Host;
Break;
}
while ($Name -eq '') {
$Name = Read-Host "What name do you want to give this '$($command)' project?";
}
if ($Verbose -eq $true) {
Write-Host -ForegroundColor Yellow "`nCreating project '$($Name)' into '$dir'.";
$continue = Read-Host "Are you sure you want to continue (y/n)?";
if ($continue -ne 'y' -and $continue -ne 'Y') {
Write-Host;
Break;
}
}
# Create project
Write-Host -ForegroundColor Gray "`nCreating..." -NoNewline;
$projectOptionsArgs = @()
$projectOptionsArgs = $projectOptionsArgs + "--framework"
$projectOptionsArgs = $projectOptionsArgs + "net6.0"
$projectOptionsArgs = $projectOptionsArgs + "--language"
$projectOptionsArgs = $projectOptionsArgs + "c#"
# dotnet new $($command) --name $($Name) $($projectOptions)
$commandArgs = @()
$commandArgs = $commandArgs + "new"
$commandArgs = $commandArgs + $command
$commandArgs = $commandArgs + "--name"
$commandArgs = $commandArgs + "$($Name)"
$commandArgs = $commandArgs + $projectOptionsArgs
& dotnet $commandArgs | out-null
if ($Verbose -eq $true) { Write-Host -ForegroundColor Gray "`nCreated $($command) project"; }
else { Write-Host -ForegroundColor Gray "." -NoNewline; }
# Create test project with references
if ($OmitTest -eq $false) {
# dotnet new mstest --name $($Name).Tests --output ./Tests/ $($projectOptions)
$commandArgs = @()
$commandArgs = $commandArgs + "new"
$commandArgs = $commandArgs + "mstest"
$commandArgs = $commandArgs + "--name"
$commandArgs = $commandArgs + "$($Name).Tests"
$commandArgs = $commandArgs + "--output"
$commandArgs = $commandArgs + "./$($Name)/Tests/"
$commandArgs = $commandArgs + $projectOptionsArgs
& dotnet $commandArgs | out-null
if ($Verbose -ne $true) { Write-Host -ForegroundColor Gray "." -NoNewline; }
# dotnet add package FluentAssertions
$commandArgs = @()
$commandArgs = $commandArgs + "add"
$commandArgs = $commandArgs + "./$($Name)/Tests/$($Name).Tests.csproj"
$commandArgs = $commandArgs + "package"
$commandArgs = $commandArgs + "FluentAssertions"
& dotnet $commandArgs | out-null
if ($Verbose -ne $true) { Write-Host -ForegroundColor Gray "." -NoNewline; }
# dotnet add reference ./$($Name).csproj
$commandArgs = @()
$commandArgs = $commandArgs + "add"
$commandArgs = $commandArgs + "./$($Name)/Tests/$($Name).Tests.csproj"
$commandArgs = $commandArgs + "reference"
$commandArgs = $commandArgs + "./$($Name)/$($Name).csproj"
& dotnet $commandArgs | out-null
if ($Verbose -eq $true) { Write-Host -ForegroundColor Gray "Created tests project"; }
else { Write-Host -ForegroundColor Gray "." -NoNewline; }
}
# Create solution and add both projects
# change into project directory
& cd $Name
# dotnet new sln
$commandArgs = @()
$commandArgs = $commandArgs + "new"
$commandArgs = $commandArgs + "sln"
& dotnet $commandArgs | out-null
if ($Verbose -ne $true) { Write-Host -ForegroundColor Gray "." -NoNewline; }
# dotnet sln ./$($Name).sln add ./$($Name).csproj ./Tests/$($Name).Tests.csproj
$commandArgs = @()
$commandArgs = $commandArgs + "sln"
$commandArgs = $commandArgs + "./$($Name).sln"
$commandArgs = $commandArgs + "add"
$commandArgs = $commandArgs + "./$($Name).csproj"
if ($OmitTest -eq $false) {
$commandArgs = $commandArgs + "./Tests/$($Name).Tests.csproj"
}
& dotnet $commandArgs | out-null
if ($Verbose -eq $true) { Write-Host -ForegroundColor Gray "Created solution file"; }
else { Write-Host -ForegroundColor Gray "." -NoNewline; }
# dotnet new gitignore
$commandArgs = @()
$commandArgs = $commandArgs + "new"
$commandArgs = $commandArgs + "gitignore"
& dotnet $commandArgs | out-null
if ($Verbose -eq $true) { Write-Host -ForegroundColor Gray "Created gitignore file"; }
else { Write-Host -ForegroundColor Gray "." -NoNewline; }
# Change directory back
& cd ..
# Finished
Write-Host -ForegroundColor Blue "`n`nAll Finished. Project has been created in '$($dir)\$($Name)'`n"
}
Set-Alias dn -value "dotnetnew"
Then all you need to do is type dn
and you get some simple options to create a basic C# console, library or webapp project including a tests project.
You can also use the command line parameters to bypass some of the options:
# Create project without an MSTest project
dn -Name "MyProject" -OmitTest
# Create project and output verbose logging
dn -Name "MyProject" -Verbose