-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateAPI.ps1
245 lines (190 loc) · 10.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# check if $env:OAUTH2_TOKEN is set, if not: exit
if ($null -eq $env:AGRAVITY_OAUTH2_TOKEN) {
Write-Host "Please set OAUTH2_TOKEN environment variable"
exit
}
# check if $env:OPENAPI_GENERATOR 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 with API key $env:API_KEY as "x-functions-key" header, catch it and if it is not running: exit
$version = (Invoke-RestMethod -Uri http://localhost:7072/api/version -Method Get -ContentType "application/json" -ErrorAction SilentlyContinue).version
if ($null -eq $version) {
Write-Host "Please start backend before generating API"
exit
}
# get portal path from apiPath.txt file (optional)
$apiPath = Get-Content .\apiPath.txt -ErrorAction SilentlyContinue
if ($null -eq $apiPath) {
$apiPath="..\..\portal_unleashed"
}
#add "\api" to apiPath
$apiPath = $apiPath + "\libs\api"
# echo apiVersion
Write-Host "Generate API with apiVersion: $version"
# wait for user input
Write-Host "Press any key to continue ..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# delete folder .\src without error output
Remove-Item -Path .\src -Recurse -Force -ErrorAction SilentlyContinue
# generate API
Write-Host "Calling private API"
# download file with Authentication header (Bearer token)
Invoke-WebRequest -Uri "http://localhost:7071/api/openapi/v3.json" -Headers @{"Authorization" = "$env:AGRAVITY_OAUTH2_TOKEN"} -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
}
#$params="apiModulePrefix=Agravity,configurationPrefix=Agravity,modelFileSuffix=.agravity,serviceFileSuffix=.agravity,ngVersion=14.0.0,npmName=agravityAPI-private,npmVersion=$version"
$params = "apiModulePrefix=Agravity,configurationPrefix=Agravity,modelFileSuffix=.agravity,serviceFileSuffix=.agravity,ngVersion=17.3.5,npmName=@agravity/private,supportsES6=true,npmRepository=https://registry.npmjs.org/,useSingleRequestParameter=true"
#npx @openapitools/openapi-generator-cli generate -i openapi.json -g typescript-angular -o src/agravityAPI-private/ --additional-properties=$params
java -jar $env:OPENAPI_GENERATOR generate -i openapi.json -g typescript-angular -o src/agravityAPI-private/ --additional-properties=$params
# delete openapi.json
Remove-Item -Path .\openapi.json -Force
Write-Host "Calling public API"
Invoke-WebRequest -Uri "http://localhost:7072/api/openapi/v3.json" -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
}
$params = "apiModulePrefix=AgravityPublic,configurationPrefix=AgravityPublic,modelFileSuffix=.pub.agravity,serviceFileSuffix=.pub.agravity,ngVersion=17.3.5,npmName=@agravity/public,supportsES6=true,npmRepository=https://registry.npmjs.org/,useSingleRequestParameter=true"
#npx @openapitools/openapi-generator-cli generate -i openapi.json -g typescript-angular -o src/agravityAPI-public/ --additional-properties=$params
java -jar $env:OPENAPI_GENERATOR generate -i openapi.json -g typescript-angular -o src/agravityAPI-public/ --additional-properties=$params
# delete openapi.json
Remove-Item -Path .\openapi.json -Force
Write-Host "Generate complete"
Write-Host "Start replacements"
function ReplaceStringInFiles {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$FolderPath,
[Parameter(Mandatory = $true, Position = 1)]
[string]$SearchString,
[Parameter(Mandatory = $true, Position = 2)]
[string]$ReplaceString
)
Get-ChildItem -Path $FolderPath -Recurse |
ForEach-Object {
if (-not $_.PSIsContainer) {
$content = Get-Content $_.FullName -Raw
if ($content -match $SearchString) {
$content = $content -replace $SearchString, $ReplaceString
Set-Content $_.FullName -Value $content -NoNewline
}
}
}
}
ReplaceStringInFiles -FolderPath "src" -SearchString "add_properties\?: \{ \[key: string\]: object; \} \| null;" -ReplaceString "add_properties?: { [key: string]: any; } | null;"
Write-Host "Replace add_properties complete"
ReplaceStringInFiles -FolderPath "src" -SearchString "default_value\?: object \| null;" -ReplaceString "default_value?: any | null;"
Write-Host "Replace default_value complete"
ReplaceStringInFiles -FolderPath "src" -SearchString "custom\?: \{ \[key: string\]: object; \} \| null;" -ReplaceString "custom?: any | null;"
Write-Host "Replace custom complete"
ReplaceStringInFiles -FolderPath "src" -SearchString "ai\?: object \| null;" -ReplaceString "ai?: any | null;"
Write-Host "Replace ai complete"
# add line in file src\agravityAPI-private\api\assetManagement.agravity.ts after line 482
#$fileContent = Get-Content "src\agravityAPI-private\api\assetManagement.agravity.ts"
#$fileContent[348] = $fileContent[348] + ",`n" + " body: assetBulkUpdate"
# write file
#$fileContent | Set-Content "src\agravityAPI-private\api\assetManagement.agravity.ts"
#Write-Host "Add line in file src\agravityAPI-private\api\assetManagement.agravity.ts after line 482 complete"
$fileContent = Get-Content "src\agravityAPI-private\api\assetVersioning.agravity.ts"
$fileContent[233] = " // headers = headers.set('Content-Type', httpContentTypeSelected);"
# write file
$fileContent | Set-Content "src\agravityAPI-private\api\assetVersioning.agravity.ts"
Write-Host "Remove line in file src\agravityAPI-private\api\assetVersioning.agravity.ts after line 150 complete"
$fileContent = Get-Content "src\agravityAPI-public\api\publicCollectionSecureUpload.pub.agravity.ts"
$fileContent[263] = " // headers = headers.set('Content-Type', httpContentTypeSelected);"
# write file
$fileContent | Set-Content "src\agravityAPI-public\api\publicCollectionSecureUpload.pub.agravity.ts"
Write-Host "Remove line in file src\agravityAPI-public\api\publicCollectionSecureUpload.pub.agravity.ts after line 150 complete"
# add line "export * from './encoder';" at the end of index.ts in src\agravityAPI-private
$fileContent = Get-Content "src\agravityAPI-private\index.ts"
$fileContent += "export * from './encoder';"
# write file
$fileContent | Set-Content "src\agravityAPI-private\index.ts"
# add line "export * from './encoder';" at the end of index.ts in src\agravityAPI-public
$fileContent = Get-Content "src\agravityAPI-public\index.ts"
$fileContent += "export * from './encoder';"
# write file
$fileContent | Set-Content "src\agravityAPI-public\index.ts"
ReplaceStringInFiles -FolderPath "src" -SearchString "`r`n" -ReplaceString "`n"
# Get-ChildItem -Path "src" -Recurse -File | ForEach-Object {(Get-Content $_.FullName -Raw) -replace , | Set-Content $_.FullName -NoNewline }
# set author to git user
$author = (git config user.name)
$licence = "MIT License"
$description = "The Agravity GlobalDAM API which allowes authenticated user to access the Agravity GlobalDAM Backend"
$repoUrl = ' "url": "git+https://github.com/agravityio/agravity-sdk-typescript"'
#$repoUrl = '"repository": { "type": "git","url": "git+https://github.com/agravityio/agravity-sdk-typescript"},'
$access = '"access": "public",'
#set array of keywords: agravity, dam, globaldam
$keywords = @("agravity", "dam", "globaldam","apifirst","api","sdk","typescript")
#create copy of keywords and add "private" to it
$privateKeywords = $keywords
$privateKeywords += "private"
# parse package.json, change stuff and write it back
$json = Get-Content -Path src/agravityAPI-private/package.json -Raw | ConvertFrom-Json
$json.keywords = $privateKeywords
$json.author = $author
$json.license = $licence
$json.description = $description
$json | ConvertTo-Json -Depth 100 | Set-Content -Path src/agravityAPI-private/package.json
# add $repoUrl to package.json in line 20
$fileContent = Get-Content "src\agravityAPI-private\package.json"
$fileContent[7] = $repoUrl
$fileContent[39] = $fileContent[39] + "`n " + $access
# write file
$fileContent | Set-Content "src\agravityAPI-private\package.json"
############### public ###############
# parse package.json, change stuff and write it back
#create copy of keywords and add "public" to it
$publicKeywords = $keywords
$publicKeywords += "public"
$description = "The Agravity GlobalDAM API which allowes API key authenticated access the Agravity GlobalDAM Backend"
$json = Get-Content -Path src/agravityAPI-public/package.json -Raw | ConvertFrom-Json
$json.keywords = $publicKeywords
$json.author = $author
$json.license = $licence
$json.description = $description
$json | ConvertTo-Json -Depth 100 | Set-Content -Path src/agravityAPI-public/package.json
# add $repoUrl to package.json in line 12
$fileContent = Get-Content "src\agravityAPI-public\package.json"
$fileContent[7] = $repoUrl
$fileContent[39] = $fileContent[39] + "`n " + $access
# write file
$fileContent | Set-Content "src\agravityAPI-public\package.json"
# pretty print whole project using prettier
npx prettier --write src/**
######################### ASK FOR COPY SRC FILES TO AGRVITY-ANGULAR-APP #########################
# ask for copy src files to agrvity-angular-app
Write-Host "Do you want to copy src files to unleashed portal ($apiPath)? (y/n)"
$answer = Read-Host
if ($answer -eq "y") {
# copy src files to agrvity-angular-app
$privateSrc = $apiPath+"\private\src"
Remove-Item -Path $privateSrc -Recurse -Force -ErrorAction SilentlyContinue
Copy-Item -Path .\src\agravityAPI-private -Destination $privateSrc -Recurse -Force
$publicSrc = $apiPath+"\public\src"
Remove-Item -Path $publicSrc -Recurse -Force -ErrorAction SilentlyContinue
Copy-Item -Path .\src\agravityAPI-public -Destination $publicSrc -Recurse -Force
Write-Host "Copy complete"
}
# publish private and public package to npm
Write-Host "Do you want to publish private and public package to npm? (y/n)"
$answer = Read-Host
if ($answer -eq "y") {
# publish private package to npm
Set-Location src/agravityAPI-private
npm publish --access public
Set-Location ../agravityAPI-public
npm publish --access public
Set-Location ../..
Write-Host "Publish complete"
code.cmd .\changelog.md
}