1212 [string ]$ImageName , # Image name (defaults to a name based on the directory).
1313 [string ]$BuildAgentPath = ' C:\BuildAgent' ,
1414 [switch ]$LoadEnvFromKeyVault , # Forces loading environment variables form the key vault.
15+ [switch ]$VsDebug , # Enable the remote debugger.
1516 [Parameter (ValueFromRemainingArguments )]
1617 [string []]$BuildArgs # Arguments passed to `Build.ps1` within the container.
1718)
@@ -36,13 +37,13 @@ function New-EnvJson
3637 $envVarNames = $EnvironmentVariableList -split ' ,' | ForEach-Object { $_.Trim () } | Where-Object { $_ -ne ' ' }
3738
3839 # Build hashtable with environment variable values
39- $secrets = @ { }
40+ $envVariables = @ { }
4041 foreach ($envVarName in $envVarNames )
4142 {
4243 $value = [Environment ]::GetEnvironmentVariable($envVarName )
4344 if (-not [string ]::IsNullOrEmpty($value ))
4445 {
45- $secrets [$envVarName ] = $value
46+ $envVariables [$envVarName ] = $value
4647 }
4748 }
4849
@@ -63,10 +64,10 @@ function New-EnvJson
6364 $secretWithValue = Get-AzKeyVaultSecret - VaultName " PostSharpBuildEnv" - Name $secret.Name
6465 $envName = $secretWithValue.Name -Replace " -" , " _"
6566 $envValue = (ConvertFrom-SecureString $secretWithValue.SecretValue - AsPlainText)
66- $secrets [$envName ] = $envValue
67+ $envVariables [$envName ] = $envValue
6768 }
6869 }
69-
70+
7071 # Convert to JSON and save
7172 $jsonPath = Join-Path $dockerContextDirectory " env.g.json"
7273
@@ -81,7 +82,7 @@ function New-EnvJson
8182 exit 1
8283 }
8384
84- $secrets | ConvertTo-Json - Depth 10 | Set-Content - Path $jsonPath - Encoding UTF8
85+ $envVariables | ConvertTo-Json - Depth 10 | Set-Content - Path $jsonPath - Encoding UTF8
8586 Write-Host " Created secrets file: $jsonPath " - ForegroundColor Cyan
8687
8788
@@ -160,6 +161,9 @@ if (-not (Test-Path $dockerContextDirectory))
160161$volumeMappings = @ (" -v" , " ${SourceDirName} :${SourceDirName} " )
161162$MountPoints = @ ($SourceDirName )
162163
164+ # We must add a MountPoint anyway so the directory is created in the container.
165+ $MountPoints += " c:\packages"
166+
163167# Define static Git system directory for mapping
164168$gitSystemDir = " $BuildAgentPath \system\git"
165169
@@ -183,8 +187,27 @@ if (-not $NoNuGetCache)
183187 $volumeMappings += @ (" -v" , " ${nugetCacheDir} :c:\packages" )
184188}
185189
186- # We must add a MountPoint anyway so the directory is created in the container.
187- $MountPoints += " c:\packages"
190+ # Mount VS Remote Debugger
191+ if ( $VsDebug )
192+ {
193+ if ( -not $env: DevEnvDir )
194+ {
195+ Write-Host " Environment variable 'DevEnvDir' is not defined." - ForegroundColor Red
196+ exit 1
197+ }
198+
199+ $remoteDebuggerHostDir = " $ ( $env: DevEnvDir ) Remote Debugger\x64"
200+ if ( -not (Test-Path $remoteDebuggerHostDir ))
201+ {
202+ Write-Host " Directory '$remoteDebuggerHostDir ' does not exist." - ForegroundColor Red
203+ exit 1
204+ }
205+
206+ $remoteDebuggerContainerDir = " C:\msvsmon"
207+ $volumeMappings += @ (" -v" , " ${remoteDebuggerHostDir} :${remoteDebuggerContainerDir} :ro" )
208+ $MountPoints += $remoteDebuggerContainerDir
209+
210+ }
188211
189212# Discover symbolic links in source-dependencies and add their targets to mount points
190213$sourceDependenciesDir = Join-Path $SourceDirName " source-dependencies"
@@ -241,33 +264,37 @@ else
241264# Run the build within the container
242265if (-not $BuildImage )
243266{
244- if ($Interactive )
267+
268+ # Delete now and not in the container because it's much faster and lock error messages are more relevant.
269+ Write-Host " Building the product in the container." - ForegroundColor Green
270+
271+ # Prepare Build.ps1 arguments
272+ $buildCommand = " $SourceDirName \Build.ps1"
273+ if ( $VsDebug )
245274 {
246- docker run -- rm - it -- memory= 12g @volumeMappings - w $SourceDirName $ImageName pwsh
247- if ($LASTEXITCODE -ne 0 )
248- {
249- Write-Host " Docker run (interactive) failed with exit code $LASTEXITCODE " - ForegroundColor Red
250- exit $LASTEXITCODE
251- }
275+ $BuildArgs = @ (" -VsDebug" ) + $BuildArgs
276+ }
277+ $buildArgsString = $BuildArgs -join " "
278+ $buildCommand += " $buildArgsString "
279+
280+ if ( $Interactive )
281+ {
282+ $pwshArgs = " -NoExit"
252283 }
253284 else
254285 {
255- # Delete now and not in the container because it's much faster and lock error messages are more relevant.
256- Write-Host " Building the product in the container." - ForegroundColor Green
257-
258- # Prepare Build.ps1 arguments
259- $buildCommand = " $SourceDirName \Build.ps1"
260- $buildArgsString = $BuildArgs -join " "
261- $buildCommand += " $buildArgsString "
262- Write-Host " Executing in container: `" .\Build.ps1 $buildArgsString `" ." - ForegroundColor Cyan
286+ $pwshArgs = " -NonInteractive"
287+ }
288+
289+ Write-Host " Executing in container: `" .\Build.ps1 $buildArgsString `" ." - ForegroundColor Cyan
263290
264- docker run -- rm -- memory= 12g @volumeMappings - w $SourceDirName $ImageName pwsh - NonInteractive - Command $buildCommand
265- if ($LASTEXITCODE -ne 0 )
266- {
267- Write-Host " Docker run (build) failed with exit code $LASTEXITCODE " - ForegroundColor Red
268- exit $LASTEXITCODE
269- }
291+ docker run -- rm -- memory= 12g @volumeMappings - w $SourceDirName $ImageName pwsh $pwshArgs - Command $buildCommand
292+ if ($LASTEXITCODE -ne 0 )
293+ {
294+ Write-Host " Docker run (build) failed with exit code $LASTEXITCODE " - ForegroundColor Red
295+ exit $LASTEXITCODE
270296 }
297+
271298}
272299else
273300{
0 commit comments