-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathGet-OpenApiDocument.ps1
116 lines (95 loc) · 3.75 KB
/
Get-OpenApiDocument.ps1
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
### Gets the OpenAPI document from the locally running Functions app.
Param(
[string]
[Parameter(Mandatory=$false)]
$FunctionAppPath = ".",
[string]
[Parameter(Mandatory=$false)]
$BaseUri = "http://localhost:7071/api/",
[string]
[Parameter(Mandatory=$false)]
[ValidateSet("swagger.json", "openapi/v2.json", "openapi/v3.json", "swagger.yaml", "openapi/v2.yaml", "openapi/v3.yaml")]
$Endpoint = "swagger.json",
[string]
[Parameter(Mandatory=$false)]
$OutputPath = "generated",
[string]
[Parameter(Mandatory=$false)]
$OutputFilename = "swagger.json",
[int]
[Parameter(Mandatory=$false)]
$Delay = 30,
[switch]
[Parameter(Mandatory=$false)]
$UseCodespaces,
[switch]
[Parameter(Mandatory=$false)]
$UseWindows,
[switch]
[Parameter(Mandatory=$false)]
$Help
)
function Show-Usage {
Write-Output " This downloads the OpenAPI document from the locally running Azure Functions app.
Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) ``
[-FunctionAppPath <function app directory>] ``
[-BaseUri <function app base URI>] ``
[-Endpoint <endpoint for OpenAPI document>] ``
[-OutputPath <output directory for generated OpenAPI document>] ``
[-OutputFilename <OpenAPI document name>] ``
[-Delay <delay in second between run function app and document generation>] ``
[-UseCodespaces] ``
[-UseWindows] ``
[-Help]
Options:
-FunctionAppPath Function app path, relative to the repository root. It can be the project directory or compiled app directory.
Default: '.'
-BaseUri Function app base URI.
Default: 'http://localhost:7071/api/'
-Endpoint OpenAPI document endpoint.
Default: 'swagger.json'
-OutputPath Output directory to store the generated OpenAPI document, relative to the repository root.
Default: 'generated'
-OutputFilename Output filename for the generated OpenAPI document.
Default: 'swagger.json'
-Delay Delay in second between the function app run and document generation.
Default: 30
-UseCodespaces Switch indicating whether to use GitHub Codespaces or not.
-UseWindows Switch indicating whether to run on Windows OS or not.
-Help Show this message.
"
Exit 0
}
# Show usage
$needHelp = $Help -eq $true
if ($needHelp -eq $true) {
Show-Usage
Exit 0
}
# Check the function app execution path
$func = $(Get-Command func).Source
if ($UseWindows -eq $true) {
$func = $func.Replace(".ps1", ".cmd")
}
$currentDirectory = $(pwd).Path
$repositoryRoot = $env:GITHUB_WORKSPACE
if ($UseCodespaces -eq $true) {
$repositoryRoot = $env:CODESPACE_VSCODE_FOLDER
}
cd "$repositoryRoot/$FunctionAppPath"
# Run the function app in the background
Start-Process -NoNewWindow "$func" @("start","--verbose","false")
Start-Sleep -s $Delay
# Download the OpenAPI document
$requestUri = "$($BaseUri.TrimEnd('/'))/$($Endpoint.TrimStart('/'))"
$filepath = "$repositoryRoot/$($OutputPath.TrimEnd('/'))/$($OutputFilename.TrimStart('/'))"
if ($(Test-Path -Path "$repositoryRoot/$($OutputPath.TrimEnd('/'))" -PathType Container) -eq $false) {
New-Item -Path "$repositoryRoot/$($OutputPath.TrimEnd('/'))" -ItemType Directory
}
Invoke-RestMethod -Method Get -Uri $requestUri | ConvertTo-Json -Depth 100 | Out-File -FilePath $filepath -Force
# Stop the function app
$process = $(get-Process -Name func)
if ($process -ne $null) {
Stop-Process -Id $process.Id
}
cd $currentDirectory