-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathgenerate-examples.ps1
More file actions
76 lines (61 loc) · 2.2 KB
/
generate-examples.ps1
File metadata and controls
76 lines (61 loc) · 2.2 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
#Requires -Version 7.3
# This script generates HTTP response files (*.json) for .ps1 files in ./request-examples
function Get-WebServer-ProcessId {
$webProcessId = $null
if ($IsMacOS -Or $IsLinux) {
$webProcessId = $(lsof -ti:14141)
}
elseif ($IsWindows) {
$webProcessId = $(Get-NetTCPConnection -LocalPort 14141 -ErrorAction SilentlyContinue).OwningProcess?[0]
}
else {
throw "Unsupported operating system."
}
return $webProcessId
}
function Stop-WebServer {
$webProcessId = Get-WebServer-ProcessId
if ($webProcessId -ne $null) {
Write-Output "Stopping web server"
Get-Process -Id $webProcessId | Stop-Process -ErrorVariable stopErrorMessage
if ($stopErrorMessage) {
throw "Failed to stop web server: $stopErrorMessage"
}
}
}
function Start-WebServer {
Write-Output "Starting web server"
$startTimeUtc = Get-Date -AsUTC
$job = Start-Job -ScriptBlock {
dotnet run --project ../src/Examples/GettingStarted/GettingStarted.csproj --framework net8.0 --configuration Debug --property:TreatWarningsAsErrors=True --urls=http://0.0.0.0:14141
}
$webProcessId = $null
$timeout = [timespan]::FromSeconds(30)
Do {
Start-Sleep -Seconds 1
$hasTimedOut = ($(Get-Date -AsUTC) - $startTimeUtc) -gt $timeout
$webProcessId = Get-WebServer-ProcessId
} While ($webProcessId -eq $null -and -not $hasTimedOut)
if ($hasTimedOut) {
Write-Host "Failed to start web server, dumping output."
Receive-Job -Job $job
throw "Failed to start web server."
}
}
Stop-WebServer
Start-WebServer
try {
Remove-Item -Force -Path .\request-examples\*.json
$scriptFiles = Get-ChildItem .\request-examples\*.ps1
foreach ($scriptFile in $scriptFiles) {
$jsonFileName = [System.IO.Path]::GetFileNameWithoutExtension($scriptFile.Name) + "_Response.json"
Write-Output "Writing file: $jsonFileName"
& $scriptFile.FullName > .\request-examples\$jsonFileName
if ($LastExitCode -ne 0) {
throw "Example request from '$($scriptFile.Name)' failed with exit code $LastExitCode."
}
}
}
finally {
Stop-WebServer
}