1
1
# Requires -Version 7.3
2
2
3
- # This script generates response documents for ./request-examples
3
+ # This script generates HTTP response files (*.json) for .ps1 files in ./request-examples
4
4
5
5
function Get-WebServer-ProcessId {
6
- $processId = $null
7
- if ($IsMacOs || $IsLinux ) {
8
- $processId = $ (lsof - ti:14141 )
6
+ $webProcessId = $null
7
+ if ($IsMacOS -Or $IsLinux ) {
8
+ $webProcessId = $ (lsof - ti:14141 )
9
9
}
10
10
elseif ($IsWindows ) {
11
- $processId = $ (Get-NetTCPConnection - LocalPort 14141 - ErrorAction SilentlyContinue).OwningProcess?[0 ]
11
+ $webProcessId = $ (Get-NetTCPConnection - LocalPort 14141 - ErrorAction SilentlyContinue).OwningProcess?[0 ]
12
12
}
13
13
else {
14
- throw [ System.Exception ] " Unsupported operating system."
14
+ throw " Unsupported operating system."
15
15
}
16
16
17
- return $processId
17
+ return $webProcessId
18
18
}
19
19
20
- function Kill -WebServer {
21
- $processId = Get-WebServer - ProcessId
20
+ function Stop -WebServer {
21
+ $webProcessId = Get-WebServer - ProcessId
22
22
23
- if ($processId -ne $null ) {
23
+ if ($webProcessId -ne $null ) {
24
24
Write-Output " Stopping web server"
25
- Get-Process - Id $processId | Stop-Process - ErrorVariable stopErrorMessage
25
+ Get-Process - Id $webProcessId | Stop-Process - ErrorVariable stopErrorMessage
26
26
27
27
if ($stopErrorMessage ) {
28
28
throw " Failed to stop web server: $stopErrorMessage "
@@ -32,16 +32,28 @@ function Kill-WebServer {
32
32
33
33
function Start-WebServer {
34
34
Write-Output " Starting web server"
35
- Start-Job - ScriptBlock { dotnet run -- project ..\src\Examples\GettingStarted\GettingStarted.csproj } | Out-Null
35
+ $startTimeUtc = Get-Date - AsUTC
36
+ $job = Start-Job - ScriptBlock {
37
+ dotnet run -- project ..\src\Examples\GettingStarted\GettingStarted.csproj -- configuration Debug -- property:TreatWarningsAsErrors= True -- urls= http:// 0.0 .0.0 :14141
38
+ }
36
39
37
40
$webProcessId = $null
41
+ $timeout = [timespan ]::FromSeconds(30 )
42
+
38
43
Do {
39
44
Start-Sleep - Seconds 1
45
+ $hasTimedOut = ($ (Get-Date - AsUTC) - $startTimeUtc ) -gt $timeout
40
46
$webProcessId = Get-WebServer - ProcessId
41
- } While ($webProcessId -eq $null )
47
+ } While ($webProcessId -eq $null -and -not $hasTimedOut )
48
+
49
+ if ($hasTimedOut ) {
50
+ Write-Host " Failed to start web server, dumping output."
51
+ Receive-Job - Job $job
52
+ throw " Failed to start web server."
53
+ }
42
54
}
43
55
44
- Kill - WebServer
56
+ Stop -WebServer
45
57
Start-WebServer
46
58
47
59
try {
@@ -55,10 +67,10 @@ try {
55
67
& $scriptFile.FullName > .\request-examples \$jsonFileName
56
68
57
69
if ($LastExitCode -ne 0 ) {
58
- throw [ System.Exception ] " Example request from '$ ( $scriptFile.Name ) ' failed with exit code $LastExitCode ."
70
+ throw " Example request from '$ ( $scriptFile.Name ) ' failed with exit code $LastExitCode ."
59
71
}
60
72
}
61
73
}
62
74
finally {
63
- Kill - WebServer
75
+ Stop -WebServer
64
76
}
0 commit comments