-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateAPI.ps1
149 lines (113 loc) · 5.47 KB
/
generateAPI.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
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
# check if $env:API_KEY is set, if not: exit
if ($null -eq $env:AGRAVITY_OPEN_API_KEY) {
Write-Host "Please set API_KEY environment variable"
# wait for user input
Write-Host "Press any key to continue ..."
exit
}
# check if $env:API_KEY is set, if not: exit
if ($null -eq $env:OPENAPI_GENERATOR) {
Write-Host "Please set OPENAPI_GENERATOR path to the openapi-generator-cli.jar"
# wait for user input
Write-Host "Press any key to continue ..."
exit
}
# check REST API endpoint /version if backend is running, catch it and if it is not running: exit
$version = (Invoke-RestMethod -Uri http://localhost:7072/api/version -Headers @{"x-functions-key" = $env:AGRAVITY_OPEN_API_KEY} -Method Get -ContentType "application/json" -ErrorAction SilentlyContinue)
if ($null -eq $version) {
Write-Host "Please start backend before generating API"
# wait for user input
Write-Host "Press any key to continue ..."
exit
}
$apiVersion = $version.version
# check if apiVersion is valid
if ($null -eq $apiVersion) {
Write-Host "Please provide apiVersion as parameter"
# wait for user input
Write-Host "Press any key to continue ..."
exit
}
# echo apiVersion
Write-Host "Generate API with apiVersion: $apiVersion"
# wait for user input
Write-Host "Press any key to continue ..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host "Delete folder .\src"
# delete folder .\src without error output
Remove-Item -Path .\src -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Calling public API"
# download file with Authentication header (Bearer token)
Invoke-WebRequest -Uri "http://localhost:7072/api/openapi/v3.json" -Headers @{"x-functions-key" = $env:AGRAVITY_OPEN_API_KEY} -OutFile "openapi.json"
# check if openapi.json exists and is not empty; if not: exit
if (!(Test-Path "openapi.json") -or (Get-Content "openapi.json" -Raw) -eq "") {
Write-Host "openapi.json is empty or does not exist"
exit
}
# update @openapitools/openapi-generator-cli version
#npm install @openapitools/openapi-generator-cli -g
# generate API
$params="packageName=Agravity.Public,library=restsharp,targetFramework=net8.0;net6.0;netstandard2.0,packageVersion="+$apiVersion
# Write-Host("Executing: openapi-generator generate -i https://api.agravity.com/v2/swagger.json -g csharp-netcore -o src -p "+$params);
#npx @openapitools/openapi-generator-cli generate -i openapi.json -g csharp-netcore -o . --additional-properties=$params
java -jar $env:OPENAPI_GENERATOR generate -i openapi.json -g csharp -o . --additional-properties=$params
# delete openapi.json
Remove-Item -Path .\openapi.json -Force
#change directory to src
Set-Location .\src
# replace all "Dictionary>" with "Dictionary<string, object>>"
Get-ChildItem -Recurse -File | ForEach-Object { (Get-Content $_.FullName) -replace "Dictionary>", "Dictionary<string, object>>" | Set-Content $_.FullName }
Start-Sleep -s 2
# replace all "Dictionary<string, Object>" with "Dictionary<string, object>"
Get-ChildItem -Recurse -File | ForEach-Object { (Get-Content $_.FullName) -replace "Dictionary<string, Object>", "Dictionary<string, object>" | Set-Content $_.FullName }
Start-Sleep -s 2
#replace all "Dictionary>" with "Dictionary<string, object>>"
Get-ChildItem -Recurse -File | ForEach-Object { (Get-Content $_.FullName) -replace "Dictionary>", "Dictionary<string, object>>" | Set-Content $_.FullName }
# wait 2 seconds
Start-Sleep -s 2
#replace all "StaticDefinedStaticDefinedList" with "StaticDefinedList"
Get-ChildItem -Recurse -File | ForEach-Object { (Get-Content $_.FullName) -replace "StaticDefinedStaticDefinedList", "StaticDefinedList" | Set-Content $_.FullName }
# go one directory up
Set-Location ..
# git discard all neccessary files
# git discard files
git checkout -- .\.gitignore
git checkout -- .\generateAPI.ps1
git checkout -- .\Agravity.Public.sln
git checkout -- .\extract_thirdparty_licenses.bat
git checkout -- .\icon.png
git checkout -- .\nuget.exe
git checkout -- .\openapitools.json
git checkout -- .\src\Agravity.Public\Agravity.Public.nuspec
git checkout -- .\src\Agravity.Public\Agravity.Public.csproj
git checkout -- .\src\Agravity.Public\THIRD-PARTY-NOTICES.TXT
#replace old version with new version in csproj file
$csproj = Get-Content .\src\Agravity.Public\Agravity.Public.csproj
$csproj = $csproj -replace "<Version>.*</Version>", "<Version>$apiVersion</Version>"
$csproj | Set-Content .\src\Agravity.Public\Agravity.Public.csproj
# copy README.md from root to src
Copy-Item .\README.md .\src\Agravity.Public\README.md
# wait 2 seconds
Start-Sleep -s 2
# echo apiVersion
Write-Host "Build and Publish with apiVersion: $apiVersion"
# build project with release
dotnet build .\src\Agravity.Public\Agravity.Public.csproj -c Release
# execute extract third party licenses
.\extract_thirdparty_licenses.bat
# build nuget package with version
#create command
dotnet pack .\src\Agravity.Public\Agravity.Public.csproj -c Release -o .\out /p:Version=$apiVersion
# Set API Key (once)
# nuget setApiKey xyz
# prompt to publish
Write-Host "Publish package in version $apiVersion? (y/n)"
$publish = Read-Host
# check if publish
if ($publish -eq "y") {
# publish nuget package
dotnet nuget push .\out\Agravity.Public."$apiVersion".nupkg -s https://api.nuget.org/v3/index.json
code.cmd .\changelog.md
Write-Host "Press any key to finish."
Read-Host
}