Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK Generation automation #27179

Merged
merged 41 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
35f8df9
Merge pull request #1 from Azure/main
chunyu3 Aug 20, 2021
12538d7
Merge pull request #2 from Azure/main
chunyu3 Aug 26, 2021
23fa699
Dotnet SDK automation
chunyu3 Dec 6, 2021
5e325ae
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
chunyu3 Dec 6, 2021
9c6923f
dotnet SDK automation
chunyu3 Dec 7, 2021
eee1334
update swagger regex
chunyu3 Dec 15, 2021
8879514
add task scripts
chunyu3 Dec 31, 2021
1c09fc2
install dotnet sdk
chunyu3 Dec 31, 2021
f3a9195
use output file to return package name
chunyu3 Jan 2, 2022
bd083a2
update env
chunyu3 Jan 2, 2022
4306908
update readme in autorest.md
chunyu3 Jan 4, 2022
f43b115
Merge branch 'sdkpipeline' of https://github.com/Azure/azure-sdk-for-…
chunyu3 Jan 4, 2022
1896a80
logout the autorest.md
chunyu3 Jan 4, 2022
f28329c
use relative path
chunyu3 Jan 4, 2022
05e23c2
GenerateAndBuild Script
chunyu3 Jan 6, 2022
873081a
direct use powershell script
chunyu3 Jan 7, 2022
300e5a7
move automation scripts to eng/scripts
chunyu3 Jan 17, 2022
376d328
correct sdk folder path
chunyu3 Jan 18, 2022
be610b2
correct sdk repo root folder path
chunyu3 Jan 18, 2022
22b80c6
update init script path
chunyu3 Jan 18, 2022
fbbe7bf
Merge pull request #3 from Azure/main
chunyu3 Jan 28, 2022
fe85104
Add request condition argument check
chunyu3 Jan 28, 2022
d737102
Merge branch 'main' of https://github.com/chunyu3/azure-sdk-for-net i…
chunyu3 Jan 28, 2022
d5cda14
resolve build failure
chunyu3 Jan 29, 2022
e97212a
remove unused import
chunyu3 Jan 29, 2022
7034f7d
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
chunyu3 Jan 30, 2022
4412fbc
Merge branch 'main' of https://github.com/chunyu3/azure-sdk-for-net i…
chunyu3 Feb 20, 2022
3f9ef09
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
chunyu3 Feb 22, 2022
8d39b30
move codegen_to_sdk_config.json to eng directory
chunyu3 Feb 22, 2022
86a64a8
add dataplane generation
chunyu3 Feb 22, 2022
782a836
add AssertNull check
chunyu3 Feb 23, 2022
45eea89
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-net int…
chunyu3 Feb 23, 2022
0ea99d6
remove unused Azure.Core dependency
chunyu3 Feb 23, 2022
ef0d0a7
update AssertNull
chunyu3 Feb 23, 2022
71dcf20
add log
chunyu3 Feb 24, 2022
12c9325
remove unused code
chunyu3 Feb 24, 2022
212aa22
make message parameter nullable and default parameter
chunyu3 Feb 25, 2022
679ac1f
Merge pull request #4 from Azure/main
chunyu3 Mar 1, 2022
2f35e85
resolve comments
chunyu3 Mar 7, 2022
42e2cc8
Merge pull request #5 from Azure/main
chunyu3 Mar 7, 2022
957e1a5
Merge pull request #6 from chunyu3/main
chunyu3 Mar 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions eng/codegen_to_sdk_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"init": {
"initScript": {
"path": "./eng/scripts/automation/init.sh",
"logPrefix": "[DotNet]",
"stderr":{
"storeAllLog": true
}
}
},
"generateAndBuild": {
"generateAndBuildScript": {
"path": "./eng/scripts/automation/Invoke-GenerateAndBuild.ps1",
"script": "pwsh",
"logPrefix": "[DotNet-Generate]",
"stderr":{
"storeLogByFilter": "[error|Error|Exception]"
}
}
},
"mockTest": {
"mockTestScript": {
"path": "./eng/scripts/automation/Invoke-MockTest.ps1",
"script": "pwsh",
"logPrefix": "[GO-MockTest]",
"stderr":{
"storeLogByFilter": "[error|Error|Exception]"
}
}
}
}
78 changes: 78 additions & 0 deletions eng/scripts/automation/GenerateAndBuildLib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,89 @@ function New-DataPlanePackageFolder() {
return $projectFolder
}

function New-MgmtPackageFolder() {
param(
[string]$service = "",
[string]$packageName = "",
[string]$sdkPath = "",
[string]$commitid = "",
[string]$readme = "",
[string]$AUTOREST_CONFIG_FILE = "autorest.md",
[string]$outputJsonFile = "newPacakgeOutput.json"
)

$projectFolder="$sdkPath/sdk/$packageName/Azure.ResourceManager.*"
if (Test-Path -Path $projectFolder) {
Write-Host "Path exists!"
$folderinfo = Get-ChildItem -Path $projectFolder
$foldername = $folderinfo.Name
$projectFolder = "$sdkPath/sdk/$packageName/$foldername"
} else {
Write-Host "Path doesn't exist. create template."
dotnet new -i $sdkPath/eng/templates/Azure.ResourceManager.Template
$projectFolder="$sdkPath/sdk/$packageName/Azure.ResourceManager.$packageName"
Write-Host "Create project folder $projectFolder"
New-Item -Path $projectFolder -ItemType Directory
# Set-Location $projectFolder
Push-Location $projectFolder
dotnet new azuremgmt --provider $packageName --includeCI true --force
Pop-Location
}

# update the readme url if needed.
if ($commitid -ne "") {
Write-Host "Updating autorest.md file."
$swaggerInfo = Get-SwaggerInfo -dir "$projectFolder/src"
$org = $swaggerInfo[0]
$rp = $swaggerInfo[1]
$permalinks = "https://github.com/$org/azure-rest-api-specs/blob/$commitid/specification/$rp/resource-manager/readme.md"
$requirefile = "require: $permalinks"
$rquirefileRex = "require *:.*.md"
$file="$projectFolder/src/$AUTOREST_CONFIG_FILE"
(Get-Content $file) -replace $rquirefileRex, "$requirefile" | Set-Content $file
} elseif ($readme -ne "") {
Write-Host "Updating required file $readme in autorest.md file."
$requirefile = "require: $readme"
$rquirefileRex = "require *:.*.md"
$file="$projectFolder/src/$AUTOREST_CONFIG_FILE"
(Get-Content $file) -replace $rquirefileRex, "$requirefile" | Set-Content $file

$readmefilestr = Get-Content $file
Write-Output "autorest.md:$readmefilestr"
}

$path=$projectFolder
$path=$path.Replace($sdkPath + "/", "")
$outputJson = [PSCustomObject]@{
projectFolder = $projectFolder
path = $path
}

$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile

return $projectFolder
}
function Invoke-Generate() {
param(
[string]$sdkfolder= ""
)
$sdkfolder = $sdkfolder -replace "\\", "/"
Set-Location $sdkfolder/src
dotnet build /t:GenerateCode
}
function Get-ResourceProviderFromReadme($readmeFile) {
$readmeFileRegex = "(?<specName>.*)/resource-manager/readme.md"
try
{
if ($readmeFile -match $readmeFileRegex)
{
return $matches["specName"]
}
}
catch
{
Write-Error "Error parsing readme info"
Write-Error $_
}
Write-Host "Cannot find resource provider info"
}
51 changes: 51 additions & 0 deletions eng/scripts/automation/Invoke-GenerateAndBuild.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#Requires -Version 7.0
param (
[string]$inputJsonFile="generateInput.json",
[string]$outputJsonFile="output.json"
)

. (Join-Path $PSScriptRoot GenerateAndBuildLib.ps1)

$inputJson = Get-Content $inputJsonFile | Out-String | ConvertFrom-Json
$swaggerDir = $inputJson.specFolder
$swaggerDir = $swaggerDir -replace "\\", "/"
$readmeFile = $inputJson.relatedReadmeMdFile
$readmeFile = $readmeFile -replace "\\", "/"
$commitid = $inputJson.headSha
$serviceType = $inputJson.serviceType

Write-Host "swaggerDir:$swaggerDir, readmeFile:$readmeFile"

$packageName = Get-ResourceProviderFromReadme $readmeFile
$sdkPath = (Join-Path $PSScriptRoot .. .. ..)
$sdkPath = Resolve-Path $sdkPath
$sdkPath = $sdkPath -replace "\\", "/"

$newpackageoutput = "newPackageOutput.json"
if ( $serviceType -eq "resource-manager" ) {
Write-Host "Generate resource-manager SDK client library."
New-MgmtPackageFolder -service $service -packageName $packageName -sdkPath $sdkPath -commitid $commitid -readme $swaggerDir/$readmeFile -outputJsonFile $newpackageoutput
} else {
Write-Host "Generate data-plane SDK client library."
Write-Host "Data-plane SDK Generation is not implemented currently."
exit 1
}
if ( $? -ne $True) {
Write-Error "Failed to create sdk project folder. exit code: $?"
exit 1
}
$newpackageoutputJson = Get-Content $newpackageoutput | Out-String | ConvertFrom-Json
$projectFolder = $newpackageoutputJson.projectFolder
$path = $newpackageoutputJson.path
Write-Host "projectFolder:$projectFolder"
Remove-Item $newpackageoutput

Invoke-Generate -sdkfolder $projectFolder
if ( $? -ne $True) {
Write-Error "Failed to generate sdk. exit code: $?"
exit 1
}
$outputJson = [PSCustomObject]@{
packages = @([pscustomobject]@{packageName="$packageName"; result='succeeded'; path=@("$path");packageFolder="$path"})
chunyu3 marked this conversation as resolved.
Show resolved Hide resolved
}
$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile
Empty file.
7 changes: 7 additions & 0 deletions eng/scripts/automation/MgmtMockTestLib.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Invoke-MgmtTestgen() {
param(
[string]$sdkDirectory = "",
[string]$outputFolder = ""
)

}
22 changes: 22 additions & 0 deletions eng/scripts/automation/generate-and-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

if [ -z $1 ]; then
echo "Please input inputfile"
echo "Usage: generate-and-build.sh <inputfile> <outputfile>"
exit 1
fi

if [ -z $2 ]; then
echo "Please input outputfile"
echo "Usage: generate-and-build.sh <inputfile> <outputfile>"
exit 1
fi

pwsh eng/automation/Invoke-GenerateAndBuild.ps1 -inputJsonFile $1 -outputJsonFile $2

if [ "$?" != "0" ]; then
echo "Failed to generate code."
exit 1
fi

cat $2
49 changes: 49 additions & 0 deletions eng/scripts/automation/init.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[string] $RepoRoot = "${PSScriptRoot}/../../.."
[string] $dotnetInstallScriptVersion = "v1"

function GetDotNetInstallScript() {
$installScript = Join-Path $RepoRoot 'dotnet-install.sh'
if (!(Test-Path $installScript)) {
New-Item -Path $RepoRoot -Force -ItemType 'Directory' | Out-Null
$maxRetries = 5
$retries = 1

$uri = "https://dot.net/$dotnetInstallScriptVersion/dotnet-install.sh"
while ($true) {
try {
Write-Host "GET $uri"
Invoke-WebRequest $uri -OutFile $installScript
break
}
catch {
Write-Host "Failed to download '$uri'"
Write-Error $_.Exception.Message -ErrorAction Continue
}
if (++$retries -le $maxRetries) {
$delayInSeconds = [math]::Pow(2, $retries) - 1 # Exponential backoff
Write-Host "Retrying. Waiting for $delayInSeconds seconds before next attempt ($retries of $maxRetries)."
Start-Sleep -Seconds $delayInSeconds
}
else {
throw "Unable to download file in $maxRetries attempts."
}
}
}

return $installScript
}

$GlobalJson = Get-Content -Raw -Path (Join-Path $RepoRoot 'global.json') | ConvertFrom-Json
$dotnetSdkVersion = $GlobalJson.sdk.version

$installScript = GetDotNetInstallScript

$dotnet = Join-Path $RepoRoot ".dotnet"
& bash $installScript --install-dir $dotnet --version $dotnetSdkVersion

if (Test-Path $installScript) {
Remove-Item $installScript
}
$env:DOTNET_ROOT = '$dotnet'
$env:Path = '$env:DOTNET_ROOT;$env:Path'
dotnet --list-sdks
29 changes: 29 additions & 0 deletions eng/scripts/automation/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

if [ -z $1 ]; then
echo "Please input outputfile"
echo "Usage: init.sh <outputfile>"
exit 1
fi
echo $1

pwsh eng/scripts/automation/init.ps1

DIRECTORY=$(cd `dirname $0` && pwd)
WORKFOLDER="$(realpath $DIRECTORY/../../../)"
echo $WORKFOLDER
export DOTNET_ROOT=$WORKFOLDER/.dotnet
export PATH=$DOTNET_ROOT:$PATH
which dotnet
dotnet --list-sdks
echo $1
cat > $1 << EOF
{
"envs": {
"PATH": "$DOTNET_ROOT:$PATH",
"DOTNET_ROOT": "$DOTNET_ROOT"
}
}
EOF

cat $1