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 automation script #585

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
173 changes: 173 additions & 0 deletions eng/scripts/Sdk-Generate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
param(
$GenerateInput,
$GenerateOutput,
$RepoRoot = "${PSScriptRoot}/../.."
)

$input = Get-Content $GenerateInput | ConvertFrom-Json
$inputFiles = $input.changedFiles;
$inputFiles += $input.relatedReadmeMdFiles;
$inputFiles = $inputFiles | select -Unique
Write-Output "List Of changed swagger files and related readmes" $inputFiles "`n"

$autorestFilesPath = Get-ChildItem -Path "$RepoRoot/sdk" -Filter autorest.md -Recurse | Resolve-Path -Relative

Write-Host "Updating autorest.md files for all the changed swaggers."
$sdkPaths = @()
$headSha = $input.headSha
$repoHttpsUrl = $input.repoHttpsUrl
foreach ($inputFile in $inputFiles)
{
foreach ($path in $autorestFilesPath)
{
$fileContent = Get-Content $path
$isUpdatedLines = $false
$updatedLines = foreach($line in $fileContent)
{
if($line -match "[\/][0-9a-f]{4,40}[\/]$inputFile")
{
# replacing sha
$line = $line -replace "[\/][0-9a-f]{4,40}[\/]$inputFile", "/$headSha/$inputFile"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we ever end up allowing branches or tags we might need to update this regex.

# replacing repo path
$line -replace "https:\/\/[^`"]+?$headSha", "$repoHttpsUrl/blob/$headSha"

$isUpdatedLines = $true
$sdkPaths += (get-item $path).Directory.Parent.FullName | Resolve-Path -Relative
}
else
{
$line
}
}
if($isUpdatedLines)
{
$updatedLines | Out-File -FilePath $path
}
}
}

Write-Host "Updated autorest.md files for all the changed swaggers. `n"

function Test-PreviousScript()
{
$result = $null
if($LASTEXITCODE -eq 0)
{
$result = "succeeded"
}
else
{
$result = "failed"
}
Write-Output $result
}

$sdkPaths = $sdkPaths | select -Unique
$packages = @()
$artifactsPath = "$RepoRoot/artifacts/packages"
if(!(Test-Path $artifactsPath))
{
New-Item -ItemType Directory -Force -Path $artifactsPath
}
foreach ($sdkPath in $sdkPaths)
{
$packageName = Split-Path $sdkPath -Leaf
Write-Host "Generating code for " $packageName
$srcPath = Join-Path $sdkPath 'src'
dotnet msbuild /restore /t:GenerateCode $srcPath

$artifacts = @()
$hasBreakingChange = $null
$content = $null
$result = Test-PreviousScript
if($result -eq "succeeded")
{
Write-Host "Successfully generated code for" $packageName "`n"

$csprojPath = Get-ChildItem $srcPath -Filter *.csproj -Recurse
dotnet pack $csprojPath --output $artifactsPath /p:RunApiCompat=$false
$result = Test-PreviousScript
if($result -eq "succeeded")
{
$artifacts += Get-ChildItem $artifactsPath -Filter *$packageName* -Recurse | Select-Object -ExpandProperty FullName | Resolve-Path -Relative

$logFilePath = Join-Path "$srcPath" 'log.txt'
if(!(Test-Path $logFilePath))
{
New-Item $logFilePath
}
dotnet build $csprojPath /t:RunApiCompat /p:TargetFramework=netstandard2.0 /flp:v=m`;LogFile=$logFilePath

$result = Test-PreviousScript
if($result -eq "succeeded")
{
$hasBreakingChange = $false
}
else
{
$logFile = Get-Content -Path $logFilePath | select-object -skip 2
$breakingChanges = foreach($Obj in $logFile)
{
$begin = ""
$end = ",`n"
$begin + $Obj + $end
}
$content = "Breaking Changes: $breakingChanges"
$hasBreakingChange = $true
$result = "succeeded"
}
if (Test-Path $logFilePath)
{
Remove-Item $logFilePath
}
}
}
else
{
Write-Host "Error occurred while generating code for" $packageName "`n"
}

$path = @()
$path += $sdkPath

$packageNameArr = $packageName.Split(".")
$name = $input.relatedReadmeMdFiles -match $packageNameArr[2]
$readmeMd = @()
if($packageName -match "Azure.ResourceManager")
{
$readmeMd += $name -match "resource-plane"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These path names might not be something we should rely on. Don't we know which readme's based on the matching input files in the readmes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the script and added readme's based on the matching readme input files but now in order to add readme path in output json, it expects readme.md as an input file in autorest.md. Currently for few SDKs we use directly spec(json file) as an input file. I have already created an issue to use readmes in autorest.md.

}
elseif($packageName -match "Azure.")
{
$readmeMd += $name -match "data-plane"
}

$changelog = [PSCustomObject]@{
content = $content
hasBreakingChange = $hasBreakingChange
}

$downloadUrlPrefix = $input.installInstructionInput.downloadUrlPrefix
$installInstructions = [PSCustomObject]@{
full = "Download the $packageName from [here]($downloadUrlPrefix)"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that link will work so we should test to see what it does. I was expecting we would need to add the package name to the download line as well to get this link to be useful.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should be prefix + filename

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

lite = "Download the $packagename from [here]($downloadUrlPrefix)"
}

$packageInfo = [PSCustomObject]@{
packageName = $packageName
path = $path
readmeMd = $readmeMd
changelog = $changelog
artifacts = $artifacts
installInstructions = $installInstructions
result = $result
}
$packages += $packageInfo
}

if ($GenerateOutput) {
Write-Host "`nGenerating output JSON..."
ConvertTo-Json @{
packages = $packages
} -depth 5 | Out-File -FilePath $GenerateOutput
}
1 change: 1 addition & 0 deletions eng/scripts/Sdk-init.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: ./dotnet-install.ps1 -Version 5.0.100
9 changes: 7 additions & 2 deletions swagger_to_sdk_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
"csharp": "",
"reflect-api-versions": "",
"license-header": "MICROSOFT_MIT_NO_VERSION",
"sdkrel:csharp-sdks-folder": "./sdk",
"use": "@microsoft.azure/[email protected]"
"use": "@microsoft.azure/[email protected]",
"sdkrel:csharp-sdks-folder": "./sdk"
},
"advanced_options": {
"create_sdk_pull_requests": true,
"sdk_generation_pull_request_base": "integration_branch",
"main_branch": "master"
},
"version": "0.2.0"
},
"generateOptions": {
"genearteScript": {
"path": "autorest"
}
}
}
28 changes: 28 additions & 0 deletions swagger_to_sdk_config_track2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://github.com/Azure/azure-rest-api-specs/blob/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json",
"meta": {
"autorest_options": {
"csharp": "",
"reflect-api-versions": "",
"use": "https://github.com/Azure/autorest.csharp/releases/download/3.0.0-dev.20200617.1/autorest-csharp-v3-3.0.0-dev.20200617.1.tgz",
"license-header": "MICROSOFT_MIT_NO_VERSION",
"sdkrel:csharp-sdks-folder": "./sdk"
},
"advanced_options": {
"create_sdk_pull_requests": true,
"sdk_generation_pull_request_base": "integration_branch",
"main_branch": "master"
},
"version": "0.2.0"
},
"generateOptions": {
"generateScript": {
"path": "pwsh ./eng/scripts/Sdk-Generate.ps1"
}
},
"initOptions": {
"initScript": {
"path": "pwsh ./eng/scripts/Sdk-init.ps1"
}
}
}