Skip to content

Commit 63b7240

Browse files
authored
SDK Generation automation (Azure#27179)
* Dotnet SDK automation * dotnet SDK automation * update swagger regex * add task scripts * install dotnet sdk * use output file to return package name * update env * update readme in autorest.md * logout the autorest.md * use relative path * GenerateAndBuild Script * direct use powershell script * move automation scripts to eng/scripts * correct sdk folder path * correct sdk repo root folder path * update init script path * Add request condition argument check * resolve build failure * remove unused import * move codegen_to_sdk_config.json to eng directory * add dataplane generation * add AssertNull check * remove unused Azure.Core dependency * update AssertNull * add log * remove unused code * make message parameter nullable and default parameter * resolve comments
1 parent fb454ab commit 63b7240

File tree

8 files changed

+267
-0
lines changed

8 files changed

+267
-0
lines changed

eng/codegen_to_sdk_config.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"init": {
3+
"initScript": {
4+
"path": "./eng/scripts/automation/init.sh",
5+
"logPrefix": "[DotNet]",
6+
"stderr":{
7+
"storeAllLog": true
8+
}
9+
}
10+
},
11+
"generateAndBuild": {
12+
"generateAndBuildScript": {
13+
"path": "./eng/scripts/automation/Invoke-GenerateAndBuild.ps1",
14+
"script": "pwsh",
15+
"logPrefix": "[DotNet-Generate]",
16+
"stderr":{
17+
"storeLogByFilter": "[error|Error|Exception]"
18+
}
19+
}
20+
},
21+
"mockTest": {
22+
"mockTestScript": {
23+
"path": "./eng/scripts/automation/Invoke-MockTest.ps1",
24+
"script": "pwsh",
25+
"logPrefix": "[GO-MockTest]",
26+
"stderr":{
27+
"storeLogByFilter": "[error|Error|Exception]"
28+
}
29+
}
30+
}
31+
}

eng/scripts/automation/GenerateAndBuildLib.ps1

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,89 @@ function New-DataPlanePackageFolder() {
122122
return $projectFolder
123123
}
124124

125+
function New-MgmtPackageFolder() {
126+
param(
127+
[string]$service = "",
128+
[string]$packageName = "",
129+
[string]$sdkPath = "",
130+
[string]$commitid = "",
131+
[string]$readme = "",
132+
[string]$AUTOREST_CONFIG_FILE = "autorest.md",
133+
[string]$outputJsonFile = "newPacakgeOutput.json"
134+
)
135+
136+
$projectFolder="$sdkPath/sdk/$packageName/Azure.ResourceManager.*"
137+
if (Test-Path -Path $projectFolder) {
138+
Write-Host "Path exists!"
139+
$folderinfo = Get-ChildItem -Path $projectFolder
140+
$foldername = $folderinfo.Name
141+
$projectFolder = "$sdkPath/sdk/$packageName/$foldername"
142+
} else {
143+
Write-Host "Path doesn't exist. create template."
144+
dotnet new -i $sdkPath/eng/templates/Azure.ResourceManager.Template
145+
$projectFolder="$sdkPath/sdk/$packageName/Azure.ResourceManager.$packageName"
146+
Write-Host "Create project folder $projectFolder"
147+
New-Item -Path $projectFolder -ItemType Directory
148+
# Set-Location $projectFolder
149+
Push-Location $projectFolder
150+
dotnet new azuremgmt --provider $packageName --includeCI true --force
151+
Pop-Location
152+
}
153+
154+
# update the readme url if needed.
155+
if ($commitid -ne "") {
156+
Write-Host "Updating autorest.md file."
157+
$swaggerInfo = Get-SwaggerInfo -dir "$projectFolder/src"
158+
$org = $swaggerInfo[0]
159+
$rp = $swaggerInfo[1]
160+
$permalinks = "https://github.com/$org/azure-rest-api-specs/blob/$commitid/specification/$rp/resource-manager/readme.md"
161+
$requirefile = "require: $permalinks"
162+
$rquirefileRex = "require *:.*.md"
163+
$file="$projectFolder/src/$AUTOREST_CONFIG_FILE"
164+
(Get-Content $file) -replace $rquirefileRex, "$requirefile" | Set-Content $file
165+
} elseif ($readme -ne "") {
166+
Write-Host "Updating required file $readme in autorest.md file."
167+
$requirefile = "require: $readme"
168+
$rquirefileRex = "require *:.*.md"
169+
$file="$projectFolder/src/$AUTOREST_CONFIG_FILE"
170+
(Get-Content $file) -replace $rquirefileRex, "$requirefile" | Set-Content $file
171+
172+
$readmefilestr = Get-Content $file
173+
Write-Output "autorest.md:$readmefilestr"
174+
}
175+
176+
$path=$projectFolder
177+
$path=$path.Replace($sdkPath + "/", "")
178+
$outputJson = [PSCustomObject]@{
179+
projectFolder = $projectFolder
180+
path = $path
181+
}
182+
183+
$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile
184+
185+
return $projectFolder
186+
}
125187
function Invoke-Generate() {
126188
param(
127189
[string]$sdkfolder= ""
128190
)
129191
$sdkfolder = $sdkfolder -replace "\\", "/"
130192
Set-Location $sdkfolder/src
131193
dotnet build /t:GenerateCode
194+
}
195+
function Get-ResourceProviderFromReadme($readmeFile) {
196+
$readmeFileRegex = "(?<specName>.*)/resource-manager/readme.md"
197+
try
198+
{
199+
if ($readmeFile -match $readmeFileRegex)
200+
{
201+
return $matches["specName"]
202+
}
203+
}
204+
catch
205+
{
206+
Write-Error "Error parsing readme info"
207+
Write-Error $_
208+
}
209+
Write-Host "Cannot find resource provider info"
132210
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#Requires -Version 7.0
2+
param (
3+
[string]$inputJsonFile="generateInput.json",
4+
[string]$outputJsonFile="output.json"
5+
)
6+
7+
. (Join-Path $PSScriptRoot GenerateAndBuildLib.ps1)
8+
9+
$inputJson = Get-Content $inputJsonFile | Out-String | ConvertFrom-Json
10+
$swaggerDir = $inputJson.specFolder
11+
$swaggerDir = $swaggerDir -replace "\\", "/"
12+
$readmeFile = $inputJson.relatedReadmeMdFile
13+
$readmeFile = $readmeFile -replace "\\", "/"
14+
$commitid = $inputJson.headSha
15+
$serviceType = $inputJson.serviceType
16+
17+
Write-Host "swaggerDir:$swaggerDir, readmeFile:$readmeFile"
18+
19+
$packageName = Get-ResourceProviderFromReadme $readmeFile
20+
$sdkPath = (Join-Path $PSScriptRoot .. .. ..)
21+
$sdkPath = Resolve-Path $sdkPath
22+
$sdkPath = $sdkPath -replace "\\", "/"
23+
24+
$newpackageoutput = "newPackageOutput.json"
25+
if ( $serviceType -eq "resource-manager" ) {
26+
Write-Host "Generate resource-manager SDK client library."
27+
New-MgmtPackageFolder -service $service -packageName $packageName -sdkPath $sdkPath -commitid $commitid -readme $swaggerDir/$readmeFile -outputJsonFile $newpackageoutput
28+
} else {
29+
Write-Host "Generate data-plane SDK client library."
30+
Write-Host "Data-plane SDK Generation is not implemented currently."
31+
exit 1
32+
}
33+
if ( $? -ne $True) {
34+
Write-Error "Failed to create sdk project folder. exit code: $?"
35+
exit 1
36+
}
37+
$newpackageoutputJson = Get-Content $newpackageoutput | Out-String | ConvertFrom-Json
38+
$projectFolder = $newpackageoutputJson.projectFolder
39+
$path = $newpackageoutputJson.path
40+
Write-Host "projectFolder:$projectFolder"
41+
Remove-Item $newpackageoutput
42+
43+
Invoke-Generate -sdkfolder $projectFolder
44+
if ( $? -ne $True) {
45+
Write-Error "Failed to generate sdk. exit code: $?"
46+
exit 1
47+
}
48+
$outputJson = [PSCustomObject]@{
49+
packages = @([pscustomobject]@{packageName="$packageName"; result='succeeded'; path=@("$path");packageFolder="$path"})
50+
}
51+
$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile

eng/scripts/automation/Invoke-MockTest.ps1

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function Invoke-MgmtTestgen() {
2+
param(
3+
[string]$sdkDirectory = "",
4+
[string]$outputFolder = ""
5+
)
6+
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
if [ -z $1 ]; then
4+
echo "Please input inputfile"
5+
echo "Usage: generate-and-build.sh <inputfile> <outputfile>"
6+
exit 1
7+
fi
8+
9+
if [ -z $2 ]; then
10+
echo "Please input outputfile"
11+
echo "Usage: generate-and-build.sh <inputfile> <outputfile>"
12+
exit 1
13+
fi
14+
15+
pwsh eng/automation/Invoke-GenerateAndBuild.ps1 -inputJsonFile $1 -outputJsonFile $2
16+
17+
if [ "$?" != "0" ]; then
18+
echo "Failed to generate code."
19+
exit 1
20+
fi
21+
22+
cat $2

eng/scripts/automation/init.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[string] $RepoRoot = "${PSScriptRoot}/../../.."
2+
[string] $dotnetInstallScriptVersion = "v1"
3+
4+
function GetDotNetInstallScript() {
5+
$installScript = Join-Path $RepoRoot 'dotnet-install.sh'
6+
if (!(Test-Path $installScript)) {
7+
New-Item -Path $RepoRoot -Force -ItemType 'Directory' | Out-Null
8+
$maxRetries = 5
9+
$retries = 1
10+
11+
$uri = "https://dot.net/$dotnetInstallScriptVersion/dotnet-install.sh"
12+
while ($true) {
13+
try {
14+
Write-Host "GET $uri"
15+
Invoke-WebRequest $uri -OutFile $installScript
16+
break
17+
}
18+
catch {
19+
Write-Host "Failed to download '$uri'"
20+
Write-Error $_.Exception.Message -ErrorAction Continue
21+
}
22+
if (++$retries -le $maxRetries) {
23+
$delayInSeconds = [math]::Pow(2, $retries) - 1 # Exponential backoff
24+
Write-Host "Retrying. Waiting for $delayInSeconds seconds before next attempt ($retries of $maxRetries)."
25+
Start-Sleep -Seconds $delayInSeconds
26+
}
27+
else {
28+
throw "Unable to download file in $maxRetries attempts."
29+
}
30+
}
31+
}
32+
33+
return $installScript
34+
}
35+
36+
$GlobalJson = Get-Content -Raw -Path (Join-Path $RepoRoot 'global.json') | ConvertFrom-Json
37+
$dotnetSdkVersion = $GlobalJson.sdk.version
38+
39+
$installScript = GetDotNetInstallScript
40+
41+
$dotnet = Join-Path $RepoRoot ".dotnet"
42+
& bash $installScript --install-dir $dotnet --version $dotnetSdkVersion
43+
44+
if (Test-Path $installScript) {
45+
Remove-Item $installScript
46+
}
47+
$env:DOTNET_ROOT = '$dotnet'
48+
$env:Path = '$env:DOTNET_ROOT;$env:Path'
49+
dotnet --list-sdks

eng/scripts/automation/init.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
if [ -z $1 ]; then
4+
echo "Please input outputfile"
5+
echo "Usage: init.sh <outputfile>"
6+
exit 1
7+
fi
8+
echo $1
9+
10+
pwsh eng/scripts/automation/init.ps1
11+
12+
DIRECTORY=$(cd `dirname $0` && pwd)
13+
WORKFOLDER="$(realpath $DIRECTORY/../../../)"
14+
echo $WORKFOLDER
15+
export DOTNET_ROOT=$WORKFOLDER/.dotnet
16+
export PATH=$DOTNET_ROOT:$PATH
17+
which dotnet
18+
dotnet --list-sdks
19+
echo $1
20+
cat > $1 << EOF
21+
{
22+
"envs": {
23+
"PATH": "$DOTNET_ROOT:$PATH",
24+
"DOTNET_ROOT": "$DOTNET_ROOT"
25+
}
26+
}
27+
EOF
28+
29+
cat $1

0 commit comments

Comments
 (0)