-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.ps1
More file actions
234 lines (203 loc) · 8.77 KB
/
Build.ps1
File metadata and controls
234 lines (203 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# *** DO NOT EDIT THIS FILE DIRECTLY ***
# This file is auto-generated from src/PostSharp.Engineering.BuildTools/Resources/Build.ps1
# Edit the source version, then run `./Build.ps1 generate-scripts` to regenerate this file.
[CmdletBinding(PositionalBinding = $false)]
param(
[switch]$Interactive, # Opens an interactive PowerShell session
[switch]$StartVsmon, # Enable the remote debugger.
[switch]$NoCache, # Bypass the build cache for `eng`, and force a rebuild.
[switch]$Snapshot, # Copy built output to temp directory to avoid file locking during execution.
[switch]$SnapshotRepo, # Copy the repo to a temp directory and execute the build from there.
[Parameter(ValueFromRemainingArguments)]
[string[]]$BuildArgs # Arguments passed to `Build.ps1` within the container.
)
# Require PowerShell 7.4 or higher (the version installed on GitHub build agents)
if ($PSVersionTable.PSVersion -lt [Version]'7.4')
{
Write-Error "This script requires PowerShell 7.4 or higher (run with 'pwsh', not 'powershell'). Current version: $($PSVersionTable.PSVersion)"
exit 1
}
####
# These settings are replaced by the generate-scripts command.
$EngPath = 'eng'
$ProductName = 'PostSharpEngineering'
####
if ($StartVsmon)
{
$vsmonport = 4024
Write-Host "Starting Visual Studio Remote Debugger, listening at port $vsmonport." -ForegroundColor Cyan
$vsmonProcess = Start-Process -FilePath "C:\msvsmon\msvsmon.exe" `
-ArgumentList "/noauth","/anyuser","/silent","/port:$vsmonport","/timeout:2147483647" `
-NoNewWindow -PassThru
}
# Change the prompt and window title in Docker.
if ($env:RUNNING_IN_DOCKER)
{
function global:prompt
{
$host.UI.RawUI.WindowTitle = "[docker] " + (Get-Location).Path
"[docker] $( Get-Location )> "
}
}
if (-not $Interactive -or $BuildArgs)
{
# The generate-scripts command implies -NoCache
if ($BuildArgs -contains 'generate-scripts')
{
$NoCache = $true
}
# Change the working directory so we can use a global.json that is specific to eng.
$previousLocation = Get-Location
$engSrcPath = Join-Path $PSScriptRoot $EngPath "src"
Set-Location $engSrcPath
$snapshotDir = $null
$snapshotRepoDir = $null
try
{
# SnapshotRepo mode: copy the repo to a temp directory before building
if ($SnapshotRepo)
{
$snapshotRepoDir = Join-Path $env:TEMP "eng-snapshot-repo-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
Write-Host "Creating snapshot of repository at $snapshotRepoDir..." -ForegroundColor Cyan
$snapshotStopwatch = [Diagnostics.Stopwatch]::StartNew()
& robocopy $PSScriptRoot $snapshotRepoDir /E /XD bin obj artifacts /NJH /NJS /NP | Out-Null
if ($LASTEXITCODE -ge 8)
{
throw "robocopy failed with exit code $LASTEXITCODE"
}
$snapshotStopwatch.Stop()
Write-Host "Snapshot created in $($snapshotStopwatch.Elapsed.TotalSeconds.ToString('F1'))s." -ForegroundColor Cyan
# Redirect paths to the snapshot
$engSrcPath = Join-Path $snapshotRepoDir $EngPath "src"
Set-Location $engSrcPath
}
# Build caching: check if we need to rebuild
$projectPath = Join-Path $engSrcPath "Build$ProductName.csproj"
# Find the output DLL by looking for the first DLL in bin/Debug/*/
$binDebugPath = Join-Path $engSrcPath "bin" "Debug"
$outputDll = $null
$tfmDir = $null
if (Test-Path $binDebugPath)
{
$outputDll = Get-ChildItem -Path $binDebugPath -Filter "Build$ProductName.dll" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 | ForEach-Object { $_.FullName }
if ($outputDll)
{
$tfmDir = Split-Path $outputDll -Parent
}
}
$needsBuild = $false
if ($NoCache)
{
# Cache bypassed by -NoCache switch
$needsBuild = $true
}
elseif (-not $outputDll -or -not (Test-Path $outputDll))
{
# DLL doesn't exist, need to build
$needsBuild = $true
}
else
{
# Get the DLL's last write time
$dllTime = (Get-Item $outputDll).LastWriteTime
# Check files in $EngPath/src (non-recursive)
$engSrcFiles = Get-ChildItem -Path (Join-Path $PSScriptRoot $EngPath "src") -File -ErrorAction SilentlyContinue
# Check files in $EngPath (non-recursive)
$engFiles = Get-ChildItem -Path (Join-Path $PSScriptRoot $EngPath) -File -ErrorAction SilentlyContinue
# Check files in $ScriptRoot (non-recursive)
$rootFiles = Get-ChildItem -Path $PSScriptRoot -File -ErrorAction SilentlyContinue
# Combine all files and check if any are newer than the DLL
$allFiles = @($engSrcFiles) + @($engFiles) + @($rootFiles)
foreach ($file in $allFiles)
{
if ($file.LastWriteTime -gt $dllTime)
{
$needsBuild = $true
break
}
}
}
if ($needsBuild)
{
# Build is needed
Write-Host "Building Build$ProductName..." -ForegroundColor Cyan
& dotnet build $projectPath
if ($LASTEXITCODE -ne 0)
{
throw "Build failed with exit code $LASTEXITCODE"
}
# Re-find the output DLL after build
if (Test-Path $binDebugPath)
{
$outputDll = Get-ChildItem -Path $binDebugPath -Filter "Build$ProductName.dll" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 | ForEach-Object { $_.FullName }
if ($outputDll)
{
$tfmDir = Split-Path $outputDll -Parent
}
}
# Update the DLL timestamp to mark the cache as valid
if ($outputDll -and (Test-Path $outputDll))
{
(Get-Item $outputDll).LastWriteTime = Get-Date
}
else
{
throw "Build succeeded but output DLL '$outputDll' not found."
}
}
# Run the project using dotnet exec (faster than dotnet run)
if (-not $outputDll -or -not (Test-Path $outputDll))
{
throw "Output DLL not found. Expected path: '$outputDll'."
}
# Snapshot mode: copy the TFM output directory to temp to avoid file locking during execution
$execDll = $outputDll
if ($Snapshot -and $tfmDir)
{
$snapshotDir = Join-Path $env:TEMP "eng-snapshot-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
Write-Host "Creating snapshot of build output at $snapshotDir..." -ForegroundColor Cyan
Copy-Item -Path $tfmDir -Destination $snapshotDir -Recurse -Force
$execDll = Join-Path $snapshotDir "Build$ProductName.dll"
}
# Set the repository directory via environment variable (needed when running from snapshot)
$env:ENG_REPO_DIRECTORY = if ($snapshotRepoDir) { $snapshotRepoDir } else { $PSScriptRoot }
& dotnet exec $execDll $BuildArgs
if ($StartVsmon)
{
Write-Host ""
Write-Host "Killing vsmon.exe."
$vsmonProcess.Kill()
}
}
finally
{
Set-Location $previousLocation
# Cleanup snapshot directory if it was created
if ($snapshotDir -and (Test-Path $snapshotDir))
{
Write-Host "Cleaning up snapshot directory..." -ForegroundColor Cyan
Remove-Item -Path $snapshotDir -Recurse -Force -ErrorAction SilentlyContinue
}
# Copy artifacts back from repo snapshot
if ($snapshotRepoDir -and (Test-Path $snapshotRepoDir))
{
$snapshotArtifacts = Join-Path $snapshotRepoDir "artifacts"
$repoArtifacts = Join-Path $PSScriptRoot "artifacts"
if (Test-Path $snapshotArtifacts)
{
Write-Host "Copying artifacts from snapshot back to repository..." -ForegroundColor Cyan
if (Test-Path $repoArtifacts)
{
Remove-Item -Path $repoArtifacts -Recurse -Force
}
Copy-Item -Path $snapshotArtifacts -Destination $repoArtifacts -Recurse -Force
}
}
# Reset environment variable
$env:ENG_REPO_DIRECTORY = ""
}
}
if ($Interactive)
{
Write-Host "Entering interactive PowerShell." -ForegroundColor Green
}