Created
December 22, 2023 17:54
-
-
Save WillEhrendreich/48c2437c00b845a9083c34d2b76d7abc to your computer and use it in GitHub Desktop.
dotnet new behavior, sets lang, adds properties to proj file, output path changes, publish settings, extra flags, writes launch.json, global.json, etc.
This file contains hidden or 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
| function Create-VSCodeLaunchJson() | |
| { | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$projectName, | |
| [Parameter(Mandatory=$true)] | |
| [string]$projectExtension, | |
| [Parameter(Mandatory=$true)] | |
| [string]$vscodeDir) | |
| $launchJsonPath = "$vscodeDir/launch.json" | |
| $launchJsonContent = ' | |
| { | |
| "version": "0.2.0", | |
| "configurations": [ | |
| { | |
| "name": ".NET Core Launch (console)", | |
| "type": "coreclr", | |
| "request": "launch", | |
| "preLaunchTask": "build", | |
| "program": "${workspaceFolder}/bin/Debug/' + $projectName + '.dll", | |
| "args": [], | |
| "cwd": "${workspaceFolder}", | |
| "console": "internalConsole", | |
| "stopAtEntry": false | |
| }, | |
| { | |
| "name": ".NET Core Attach", | |
| "type": "coreclr", | |
| "request": "attach" | |
| } | |
| ] | |
| } | |
| ' | |
| # $launchJsonContent -replace '$projectName', $projectName -replace '$projectExtension', $projectExtension | Set-Content $launchJsonPath | |
| $launchJsonContent | Set-Content $launchJsonPath | |
| } | |
| function Create-VSCodeTasksJson | |
| { | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$projectName, | |
| [Parameter(Mandatory=$true)] | |
| [string]$projectExtension, | |
| [Parameter(Mandatory=$true)] | |
| [string]$vscodeDir | |
| ) | |
| $tasksJsonPath = "$vscodeDir/tasks.json" | |
| $tasksJsonContent = ' | |
| { | |
| "version": "2.0.0", | |
| "tasks": [ | |
| { | |
| "label": "build", | |
| "command": "dotnet", | |
| "type": "process", | |
| "args": [ | |
| "build", | |
| "${workspaceFolder}/'+ | |
| $projectName + "." + $projectExtension + | |
| '", | |
| "/property:GenerateFullPaths=true", | |
| "/consoleloggerparameters:NoSummary" | |
| ] | |
| }, | |
| { | |
| "label": "publish", | |
| "command": "dotnet", | |
| "type": "process", | |
| "args": [ | |
| "publish", | |
| "${workspaceFolder}/'+ | |
| $projectName + "." + $projectExtension + | |
| '", | |
| "/property:GenerateFullPaths=true", | |
| "/consoleloggerparameters:NoSummary" | |
| ] | |
| }, | |
| { | |
| "label": "watch", | |
| "command": "dotnet", | |
| "type": "process", | |
| "args": [ | |
| "watch", | |
| "run", | |
| "--project", | |
| "${workspaceFolder}/'+ | |
| $projectName + "." + $projectExtension + | |
| '" | |
| ] | |
| } | |
| ] | |
| } | |
| ' | |
| # $tasksJsonContent -replace '$projectName', $projectName -replace '$projectExtension', $projectExtension | Set-Content $tasksJsonPath | |
| $tasksJsonContent| Set-Content $tasksJsonPath | |
| } | |
| function AddFsFilesToWatch($projectpath) | |
| { | |
| Write-Host "now attempting to add fs files to watch with an item group" | |
| $content = Get-Content $projectpath | |
| $x = @' | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <Watch Include="**\*.fs" /> | |
| </ItemGroup> | |
| '@ | |
| $content -replace '</PropertyGroup>', $x | Set-Content $projectpath | |
| } | |
| function AddDefaultPublishPropertiesToProjectProperties($projectpath) | |
| { | |
| Write-Host "now attempting to add publish preferences to property group" | |
| $content = Get-Content $projectpath | |
| $AddedPathProperties = @' | |
| <PublishSingleFile>True</PublishSingleFile> | |
| <PublishReadyToRun>True</PublishReadyToRun> | |
| <PublishTrimmed>False</PublishTrimmed> | |
| </PropertyGroup> | |
| '@ | |
| $content -replace '</PropertyGroup>', $AddedPathProperties | Set-Content $projectpath | |
| } | |
| function AddOtherFlagsPropertiesToProjectProperties($projectpath) | |
| { | |
| Write-Host "now attempting to add Other Flags to property group" | |
| $content = Get-Content $projectpath | |
| $AddedProperties = @' | |
| <OtherFlags>$(OtherFlags) --test:GraphBasedChecking</OtherFlags> | |
| <OtherFlags>$(OtherFlags) --test:DumpCheckingGraph</OtherFlags> | |
| <OtherFlags>$(OtherFlags) --times:report.csv</OtherFlags> | |
| </PropertyGroup> | |
| '@ | |
| $content -replace '</PropertyGroup>', $AddedProperties | Set-Content $projectpath | |
| } | |
| function AddDefaultPathChangesToProjectProperties($projectpath) | |
| { | |
| Write-Host "now attempting to add path preferences to property group" | |
| $content = Get-Content $projectpath | |
| $AddedPathProperties = @' | |
| <AppendTargetFrameworkToOutputPath>False</AppendTargetFrameworkToOutputPath> | |
| <AppendRuntimeIdentifierToOutputPath>False</AppendRuntimeIdentifierToOutputPath> | |
| </PropertyGroup> | |
| '@ | |
| $content -replace '</PropertyGroup>', $AddedPathProperties | Set-Content $projectpath | |
| } | |
| function Create-VSCodeDebuggingInfo( | |
| [string]$projectName, | |
| [string]$projectExtension | |
| ) | |
| { | |
| Write-Host "now attempting to create vscode style debugging info (.vscode/launch.json, .vscode/tasks.json)" | |
| Write-Host "for project name :" $projectName | |
| # Create the .vscode directory | |
| $vscodeDir = ".vscode" | |
| New-Item -Name $vscodeDir -ItemType Directory | |
| Create-VSCodeLaunchJson $projectName $projectExtension $vscodeDir | |
| Create-VSCodeTasksJson $projectName $projectExtension $vscodeDir | |
| } | |
| function Create-DotnetProject | |
| { | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$projectName, | |
| [string[]]$tools, | |
| [switch]$globalJson, | |
| [string]$language | |
| ) | |
| Write-Host "now creating a new dotnet project named " + $projectName | |
| # Set the default language to "F#" | |
| $defaultLanguage = "F#" | |
| # Check if the language parameter is not specified | |
| if (-not $language) | |
| { | |
| # Check if the DOTNET_NEW_PREFERRED_LANG environment variable is set | |
| $envLanguage = $env:DOTNET_NEW_PREFERRED_LANG | |
| if ($envLanguage) | |
| { | |
| $language = $envLanguage | |
| } else | |
| { | |
| $language = $defaultLanguage | |
| } | |
| } | |
| # Map language options to dotnet new values | |
| $languageMap = @{ | |
| "c#" = "c#" | |
| "cs" = "c#" | |
| "csharp" = "c#" | |
| "f#" = "f#" | |
| "fs" = "f#" | |
| "fsharp" = "f#" | |
| "visualbasic" = "vb" | |
| "vb" = "vb" | |
| } | |
| $dotnetLanguage = $languageMap[$language] | |
| if (-not $dotnetLanguage) | |
| { | |
| Write-Error "Invalid language specified. Please use 'c#', 'cs', 'csharp', 'f#', 'fs', 'fsharp', 'visualbasic', or 'vb'." | |
| return | |
| } | |
| Write-Host "Lang:" + $dotnetLanguage | |
| # Determine project file extension | |
| $projectExtension = @{ | |
| "c#" = "csproj" | |
| "f#" = "fsproj" | |
| "vb" = "vbproj" | |
| }[$dotnetLanguage] | |
| # Create the project | |
| dotnet new console -lang $dotnetLanguage -n $projectName | |
| # Change directories | |
| Set-Location $projectName | |
| $projectPath = "./" + $projectName + "." + $projectExtension | |
| Write-Host "Project Path: " $projectPath | |
| Create-VSCodeDebuggingInfo $projectName $projectExtension | |
| # Create a tool manifest | |
| dotnet new tool-manifest | |
| # Install dotnet tools | |
| foreach ($tool in $tools) | |
| { | |
| dotnet tool install $tool | |
| } | |
| # Create the solution | |
| dotnet new sln | |
| # Add the project to the solution | |
| dotnet sln add $projectpath | |
| AddDefaultPathChangesToProjectProperties($projectpath) | |
| AddDefaultPublishPropertiesToProjectProperties($projectpath) | |
| AddOtherFlagsPropertiesToProjectProperties($projectpath) | |
| if ($dotnetLanguage -eq "f#") | |
| { | |
| AddFsFilesToWatch $projectpath | |
| } | |
| # Check if the --globaljson flag is specified | |
| if ($globalJson) | |
| { | |
| # Create a global.json file | |
| dotnet new globaljson --sdk-version $(dotnet --version) --roll-forward "latestMinor" | |
| } | |
| } | |
| Set-Alias -Name dnnew -Value Create-DotnetProject | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment