Last active
February 12, 2025 08:44
-
-
Save lizardkingLK/cfdcee47e7370a0cf5d4397681d1bb76 to your computer and use it in GitHub Desktop.
A powershell script that generates a new dotnet solution for C# that is TDD first and ready to run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter( | |
Position = 1, | |
Mandatory = $true, | |
HelpMessage = 'Please enter solution directory to create in (i.e.: C:/Users/User/Desktop)', | |
ParameterSetName = 'parameters.solution')] | |
[string] | |
$solutionDirectory, | |
[Parameter(Position = 2, | |
Mandatory = $true, | |
HelpMessage = 'Please enter solution name (i.e.: Sorting)', | |
ParameterSetName = 'parameters.solution')] | |
[string] | |
$solutionName, | |
[Parameter(Position = 3, | |
Mandatory = $true, | |
HelpMessage = 'Please enter project name (i.e.: BubbleSort)', | |
ParameterSetName = 'parameters.solution')] | |
[string] | |
$projectName | |
) | |
# validates solution directory | |
if (-not (Test-Path($solutionDirectory))) { | |
Write-Error "error. path given $solutionDirectory is invalid" | |
Exit | |
} | |
# validates solution name | |
if ($solutionName -eq $null) { | |
Write-Error "error. name given solution name $solutionName is invalid" | |
Exit | |
} | |
# validates project name | |
if ($projectName -eq $null) { | |
Write-Error "error. name given project name $projectName is invalid" | |
Exit | |
} | |
# current directory | |
$currentDirectory = $PWD.Path | |
# create directory for solution | |
$solutionPath = "$(Resolve-Path -Path $solutionDirectory)/$solutionName" | |
New-Item -ItemType Directory -Path $solutionPath | |
# set directory | |
Set-Location -Path $solutionPath | |
# create solution file | |
& dotnet new sln | |
# create classlib project and add it to solution | |
$classLibName = "$solutionName.$projectName" | |
$classLibPath = "$solutionPath/src/$classLibName" | |
& dotnet new classlib --name $classLibName -o $classLibPath --force | |
& dotnet sln add "$classLibPath/$classLibName.csproj" | |
# create console project and add it to solution | |
$programName = "$solutionName.Program" | |
$programPath = "$solutionPath/src/$programName" | |
& dotnet new console --use-program-main --name $programName -o $programPath --force | |
& dotnet sln add "$programPath/$programName.csproj" | |
# create test project and add it to solution | |
$testsName = "$solutionName.$projectName.Tests" | |
$testsPath = "$solutionPath/tests/$testsName" | |
& dotnet new xunit --name $testsName -o $testsPath --force | |
& dotnet sln add "$testsPath/$testsName.csproj" | |
# add reference of classlib project to console project | |
& dotnet add "$programPath/$programName.csproj" reference "$classLibPath/$classLibName.csproj" | |
# add reference of classlib project to tests project | |
& dotnet add "$testsPath/$testsName.csproj" reference "$classLibPath/$classLibName.csproj" | |
# build solution | |
& dotnet build | |
# reset current location | |
Set-Location -Path $currentDirectory | |
# open in vscode | |
& code $solutionPath | |
# open in neovim | |
# & nvim $solutionPath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements