33 Build the project
44. DESCRIPTION
55 Build the project and copy the DLL to the Module directory.
6+ . PARAMETER Configuration
7+ The configuration of the build (`Debug` or `Release`) [Default: `Debug`]
8+ . PARAMETER TargetFramework
9+ The target framework of the build (`net7.0` or `net8.0`) [Default: `net8.0`]
10+ . PARAMETER Clean
11+ If specified, cleans the project before building.
612. EXAMPLE
7- . . /Build.ps1
13+ ./Build.ps1
814 Run the build script to build the project (in debug configuration) and copy the DLL to the Module directory.
915. EXAMPLE
10- . . /Build.ps1 -Configuration Release
11- Run the build script to build the project ( in release configuration) and copy the DLL to the Module directory .
16+ ./Build.ps1 -Configuration Release -Clean
17+ Clean and build the project in release configuration.
1218#>
1319
1420[CmdletBinding (DefaultParameterSetName = ' Build' )]
@@ -21,17 +27,33 @@ param(
2127 # The target framework of the build (`net7.0` or `net8.0`) [Default: `net8.0`]
2228 [ValidateSet (" net7.0" , " net8.0" )]
2329 [ValidateNotNullOrEmpty ()]
24- [string ] $TargetFramework = " net8.0"
30+ [string ] $TargetFramework = " net8.0" ,
31+
32+ # Clean the project before building
33+ [switch ] $Clean
2534)
2635
2736# Path to the Predictor project
2837$Project = " $PSScriptRoot \Predictor\PSFavoritePredictor.csproj"
2938
39+ # Clean the project if the -Clean switch is specified
40+ if ($Clean ) {
41+ Write-Host " Cleaning project ($Configuration )..."
42+ dotnet clean $Project - c $Configuration -f $TargetFramework
43+ }
44+
3045# Build the Predictor DLL
46+ Write-Host " Building project ($Configuration )..."
3147dotnet build $Project - c $Configuration -f $TargetFramework
3248
49+ # Fail fast: If dotnet build failed, do not attempt to copy files
50+ if ($LASTEXITCODE -ne 0 ) {
51+ Write-Error " Build failed. Please check the output above."
52+ exit $LASTEXITCODE
53+ }
54+
3355# Items to copy to the Module directory
34- @ (
56+ $Items = @ (
3557 @ {
3658 Source = " $PSScriptRoot \Predictor\bin\$Configuration \$TargetFramework \PSFavoritePredictor.dll"
3759 Destination = " $PSScriptRoot \Module\Library\PSFavoritePredictor.dll"
@@ -44,10 +66,43 @@ dotnet build $Project -c $Configuration -f $TargetFramework
4466 Source = " $PSScriptRoot \LICENSE"
4567 Destination = " $PSScriptRoot \Module\LICENSE"
4668 }
47- ) | ForEach-Object {
48- if (Test-Path - Path $_.Destination ) {
49- Remove-Item - Path $_.Destination - Force
69+ )
70+
71+ # Copy items with Error Handling
72+ foreach ($item in $Items ) {
73+ try {
74+ # Ensure the destination directory exists
75+ $destDir = Split-Path - Path $item.Destination - Parent
76+ if (! (Test-Path - Path $destDir )) {
77+ New-Item - ItemType Directory - Path $destDir - Force | Out-Null
78+ Write-Verbose " Created directory: $destDir "
79+ }
80+
81+ # If the destination file already exists, remove it before copying the new file
82+ if (Test-Path - Path $item.Destination ) {
83+ Remove-Item - Path $item.Destination - Force
84+ }
85+
86+ # Copy the file to the destination
87+ Copy-Item - Path $item.Source - Destination $item.Destination - Force
88+ Write-Output " Copied $ ( $item.Source ) to $ ( $item.Destination ) "
89+ }
90+ catch {
91+ $msg = $_.Exception.Message
92+ # Specific handling for the locked DLL issue vs other errors
93+ if ($item.Destination -like " *PSFavoritePredictor.dll" -and ($msg -like " *used by another process*" -or $msg -like " *Access to the path*denied*" )) {
94+ Write-Host " "
95+ Write-Host " CRITICAL ERROR: Access Denied to PSFavoritePredictor.dll" - ForegroundColor Red
96+ Write-Host " The DLL is likely locked because the module is loaded in an active PowerShell session." - ForegroundColor Yellow
97+ Write-Host " To fix this, please close all PowerShell windows and try again." - ForegroundColor Yellow
98+ Write-Host " "
99+ }
100+ else {
101+ Write-Error " Failed to copy $ ( $item.Source ) to $ ( $item.Destination ) : $msg "
102+ }
103+ exit 1
50104 }
51- Copy-Item - Path $_.Source - Destination $_.Destination - Force
52- Write-Output " Copied $ ( $_.Source ) to $ ( $_.Destination ) "
53105}
106+
107+
108+ Write-Host " Build and Copy completed successfully!" - ForegroundColor Green
0 commit comments