@@ -126,29 +126,6 @@ function UnzipFileFromUrl {
126126 Remove-Item - Path $tempZip
127127}
128128
129- function Run {
130- param (
131- [string ]$cmd ,
132- [object ]$second = $null
133- )
134-
135- # If second argument provided, treat as executable path + args; otherwise treat first as a PowerShell command string
136- if ($null -ne $second ) {
137- $argList = @ ()
138- if ($second -is [System.Array ]) { $argList = $second } else { $argList = @ ($second ) }
139- Write-Debug " Running: $cmd $ ( $argList -join ' ' ) "
140- $proc = Start-Process - FilePath $cmd - ArgumentList $argList - Wait - PassThru - WindowStyle Hidden
141- }
142- else {
143- Write-Debug " Running: $cmd "
144- $proc = Start-Process - FilePath " powershell" - ArgumentList @ (' -NoProfile' , ' -Command' , $cmd ) - Wait - PassThru - WindowStyle Hidden
145- }
146- if ($proc.ExitCode -ne 0 ) {
147- Write-Err " Process $cmd $ ( $argList -join ' ' ) failed with exit code $ ( $proc.ExitCode ) "
148- }
149- return $proc.ExitCode
150- }
151-
152129function CreateProjectPath {
153130 param (
154131 [string ]$solutionPath ,
@@ -168,14 +145,67 @@ function CreateCleanArchitectureProjects {
168145 [string ]$projectPath = " src" ,
169146 [string ]$targetFramework
170147 )
148+
149+ function Remove-DefaultClassFile {
150+ param (
151+ [string ] $projectPath
152+ )
153+ $classFile = Join-Path - Path $projectPath - ChildPath " Class1.cs"
154+ if (Test-Path $classFile ) {
155+ $cmd = " Remove-Item -Path `" $classFile `" "
156+ $rc = Run $cmd
157+ Write-Debug " Removed default class file: $classFile "
158+ }
159+ }
160+
161+ function AddReferencesToProject {
162+ param (
163+ [string ] $projectPath ,
164+ [string []] $references
165+ )
166+ # Find the project's .csproj file
167+ $projCsproj = Get-ChildItem - Path $projectPath - Filter ' *.csproj' - File - ErrorAction SilentlyContinue | Select-Object - First 1
168+ if (-not $projCsproj ) {
169+ Write-Warn " No .csproj found in project folder: $projectPath . Skipping reference addition."
170+ return
171+ }
172+ $projCsprojPath = $projCsproj.FullName
173+
174+ foreach ($ref in $references ) {
175+ # Determine the sibling project directory (projects live under the same parent 'src' folder)
176+ $parentDir = Split-Path - Path $projectPath - Parent
177+ $refProjPath = Join-Path - Path $parentDir - ChildPath $ref
178+ $refProjFile = Join-Path - Path $refProjPath - ChildPath " $ ( $ref ) .csproj"
179+ Write-Debug " Reference project file: $refProjFile "
180+ Write-Debug " Reference project path: $refProjPath "
181+ Write-Debug " projCsprojPath: $projCsprojPath "
182+ if (Test-Path $refProjFile ) {
183+ Write-Debug " Adding reference to project: $refProjFile in $projCsprojPath "
184+ # Use dotnet CLI directly so we can check the exit code
185+ $cmd = " dotnet reference add `" $refProjFile `" --project `" $projCsprojPath `" "
186+ $rc = Run $cmd
187+ if ($rc -ne 0 ) {
188+ Write-Warn " dotnet add returned exit code $rc when adding reference $refProjFile to $projCsprojPath "
189+ }
190+ else {
191+ Write-RunDone $true
192+ }
193+ }
194+ else {
195+ Write-Warn " Reference project file not found: $refProjFile "
196+ }
197+ }
198+ }
199+
171200 $projects = @ (
172- @ { Name = " Core " ; Suffix = " .Core " ; Type = " classlib" },
173- @ { Name = " Infrastructure " ; Suffix = " .Infrastructure " ; Type = " classlib" },
174- @ { Name = " Domain " ; Suffix = " .Domain " ; Type = " classlib" }
201+ @ { Name = " Domain " ; Suffix = " .Domain " ; Type = " classlib" ; References = @ () },
202+ @ { Name = " Core " ; Suffix = " .Core " ; Type = " classlib" ; References = @ ( " Domain " ) },
203+ @ { Name = " Infrastructure " ; Suffix = " .Infrastructure " ; Type = " classlib" ; References = @ ( " Core " ) }
175204 )
176205 foreach ($proj in $projects ) {
177206 $projName = $proj.Name
178207 $projType = $proj.Type
208+ $projRefs = $proj.References
179209 # Build path as: $solutionPath\$projectPath\$projName
180210 $projPath = Join-Path - Path (Join-Path - Path $solutionPath - ChildPath $projectPath ) - ChildPath $projName
181211 Write-Debug " Creating project: $projName at $projPath "
@@ -193,6 +223,17 @@ function CreateCleanArchitectureProjects {
193223 Write-Warning " Failed to add project $projName to solution."
194224 exit 2
195225 }
226+
227+ # Remove Class1.cs
228+ Remove-DefaultClassFile $projPath
229+
230+ if (-not $projRefs.Count -eq 0 ) {
231+ # Add references
232+ Write-Debug " project path $projPath "
233+ Write-Debug " project refs $projRefs "
234+ AddReferencesToProject - projectPath $projPath - references $projRefs
235+ }
236+
196237 CreateSubFolderForEachCleanArchitectureProjects - ProjectPath $projPath - projectName $projName
197238 }
198239}
@@ -238,6 +279,7 @@ $ScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
238279$ScriptNamespace = " TirsvadScript.CreateVSSolution"
239280# Path to the 'inc' directory for sourced libraries
240281$ScriptDirectoryInc = Join-Path - Path $ScriptDirectory - ChildPath (Join-Path - Path $ScriptNamespace - ChildPath " inc" )
282+ $ScriptDirectoryTemplates = Join-Path - Path $ScriptDirectory - ChildPath (Join-Path - Path $ScriptNamespace - ChildPath " templates" )
241283
242284# ###############################################################################
243285# Include logging library early so helper functions (Write-Warn etc.) are available
@@ -246,7 +288,10 @@ $ScriptDirectoryInc = Join-Path -Path $ScriptDirectory -ChildPath (Join-Path -Pa
246288
247289# Dependencies
248290$dependenciesTupleList = New-Object ' System.Collections.Generic.List[System.Tuple[string,string,string]]'
249- $dependenciesTupleList.Add ([Tuple ]::Create(" TirsvadScript.Logging" , " TirsvadScript.LoggingLoaded" , " https://github.com/TirsvadScript/PS.Logging/releases/download/v0.1.0/TirsvadScript.Logging.zip" ))
291+ # $dependenciesTupleList.Add([Tuple]::Create("TirsvadScript.Logging", "TirsvadScript.LoggingLoaded", "https://github.com/TirsvadScript/PS.Logging/releases/download/v0.1.0/TirsvadScript.Logging.zip"))
292+
293+ # internal include of dependencies
294+ . " $ScriptDirectoryInc \command-handler.ps1"
250295
251296CheckDependencies
252297# exit0
@@ -263,7 +308,7 @@ $startingLocation = Get-Location
263308$solutionPath = Join-Path - Path $startingLocation - ChildPath $SolutionName
264309if (-not (Test-Path $solutionPath )) {
265310 New-Item - ItemType Directory - Path $solutionPath | Out-Null
266- Write-Ok " Created solution directory: $solutionPath "
311+ Write-Debug " Created solution directory: $solutionPath "
267312}
268313
269314# Check if solutionDirectory is empty
@@ -316,13 +361,21 @@ switch ($ProjectTemplate) {
316361 " wpf" {
317362 $projectType = " wpf"
318363 $ProjectPath = Join-Path - Path (Join-Path - Path $solutionPath - ChildPath " src" ) - ChildPath " WpfUI"
319- Write-Info " Created WPF solution: $SolutionName "
320364 $cmd = " dotnet new $projectType -n `" WpfUI`" -o `" ${ProjectPath} `" -f `" $targetFramework `" "
321365 $rc = Run $cmd
322366 if ($rc -ne 0 ) {
323367 Write-Warn " 'dotnet new' failed for project WpfUI (rc=$rc )."
324368 exit 2
325369 }
370+ # Add project to solution
371+ $slnFile = Join-Path - Path $solutionPath - ChildPath " $ ( $SolutionName ) .sln"
372+ $projFile = Join-Path - Path $ProjectPath - ChildPath " WpfUI.csproj"
373+ $added = AddProjectToSolution - slnPath $slnFile - projPath $projFile
374+ if (-not $added ) {
375+ Write-Warning " Failed to add project $projName to solution."
376+ exit 2
377+ }
378+ Write-Info " Created WPF solution: $SolutionName "
326379 }
327380 default {
328381 Write-Err " Unsupported project template: $ProjectTemplate "
0 commit comments