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 #17193

Merged
merged 30 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6dc1aff
Merged commit
Apr 17, 2020
9f2fe05
Add track2 config
Jun 19, 2020
f032822
Update swagger_to_sdk_config.json
Oct 21, 2020
291253b
Merged commit
Apr 17, 2020
e58ca0e
Add track2 config
Jun 19, 2020
8e7de55
Update swagger_to_sdk_config.json
Oct 21, 2020
593b028
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-net
ShivangiReja Nov 18, 2020
aa4f21a
Merge branch 'master' of https://github.com/openapi-env-test/azure-sd…
ShivangiReja Nov 18, 2020
2e378bd
Merged commit
Apr 17, 2020
a44ec42
Add track2 config
Jun 19, 2020
6b7f1d1
Update swagger_to_sdk_config.json
Oct 21, 2020
66ac771
Merge branch 'master' of https://github.com/openapi-env-test/azure-sd…
ShivangiReja Nov 19, 2020
29dddd3
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-net
ShivangiReja Nov 19, 2020
cfef820
SDK automation script
ShivangiReja Nov 19, 2020
0bd382c
Add new line
ShivangiReja Nov 19, 2020
1292e42
Add dotnet-install script
ShivangiReja Nov 20, 2020
05c4071
Update script
ShivangiReja Nov 24, 2020
4d02ac6
Update .init script
ShivangiReja Nov 25, 2020
b4231c6
Format scripts
ShivangiReja Nov 25, 2020
6f1f5df
Add new line at the end of the file
ShivangiReja Nov 25, 2020
bb961ae
Nit fix
ShivangiReja Nov 25, 2020
c5ef13f
PR feedback
ShivangiReja Dec 1, 2020
6913ed4
Add ValidateNotNullOrEmpty for input json param
ShivangiReja Dec 1, 2020
e38e727
PR feedback
ShivangiReja Dec 1, 2020
4a66a48
Remove "= $GenerateInput"
ShivangiReja Dec 1, 2020
8bc15b2
PR feedback
ShivangiReja Dec 1, 2020
595dd69
Fix file name
ShivangiReja Dec 1, 2020
21168ee
Fix script path in config
ShivangiReja Dec 2, 2020
8b0ae73
Update schema path
ShivangiReja Dec 2, 2020
fc17611
Update init script sothat it can work in linux env
ShivangiReja Dec 4, 2020
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
134 changes: 134 additions & 0 deletions eng/scripts/Automation-Sdk-Generate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $InputJsonPath,

[Parameter()]
[string] $OutputJsonPath,

[Parameter()]
[string] $RepoRoot = "${PSScriptRoot}/../.."
)

$inputFileContent = Get-Content $InputJsonPath | ConvertFrom-Json
[string[]] $inputFilePaths = $inputFileContent.changedFiles;
$inputFilePaths += $inputFileContent.relatedReadmeMdFiles;
$inputFilePaths = $inputFilePaths | Select-Object -Unique

$changedFilePaths = $inputFilePaths -join "`n";
Write-Host "List of changed swagger files and related readmes:`n$changedFilePaths`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."
$sdksInfo = @{}
$headSha = $inputFileContent.headSha
$repoHttpsUrl = $inputFileContent.repoHttpsUrl
foreach ($path in $autorestFilesPath) {
$fileContent = Get-Content $path
foreach ($inputFilePath in $inputFilePaths) {
$isUpdatedLines = $false
$escapedInputFilePath = [System.Text.RegularExpressions.Regex]::Escape($inputFilePath)
$regexForMatchingShaAndPath = "https:\/\/[^`"]*[\/][0-9a-f]{4,40}[\/]$escapedInputFilePath"
$updatedLines = foreach ($line in $fileContent) {
if ($line -match $regexForMatchingShaAndPath) {
$line -replace $regexForMatchingShaAndPath, "$repoHttpsUrl/blob/$headSha/$inputFilePath"

$isUpdatedLines = $true
$sdkpath = (get-item $path).Directory.Parent.FullName | Resolve-Path -Relative
if (!$sdksInfo.ContainsKey($sdkpath)) {
$specReadmePath = $inputFileContent.relatedReadmeMdFiles -match $inputFilePath
$sdksInfo.Add($sdkpath, $specReadmePath)
}
}
else {
$line
}
}
if ($isUpdatedLines) {
$updatedLines | Out-File -FilePath $path
}
}
}

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

$packages = @()
foreach ($sdkPath in $sdksInfo.Keys) {
$packageName = Split-Path $sdkPath -Leaf
Write-Host "Generating code for " $packageName
$artifacts = @()
$hasBreakingChange = $null
$content = $null
$srcPath = Join-Path $sdkPath 'src'
dotnet msbuild /restore /t:GenerateCode $srcPath
if (!$LASTEXITCODE) {
$result = "succeeded"
Write-Host "Successfully generated code for" $packageName "`n"

dotnet pack $srcPath /p:RunApiCompat=$false
if (!$LASTEXITCODE) {
$result = "succeeded"
$artifactsPath = "$RepoRoot/artifacts/packages/Debug/$packageName"
$artifacts += Get-ChildItem $artifactsPath -Filter *.nupkg -Recurse | Select-Object -ExpandProperty FullName | Resolve-Path -Relative

$logs = & dotnet build /nologo $srcPath /t:RunApiCompat /p:TargetFramework=netstandard2.0 /flp:v=m`;
if (!$LASTEXITCODE) {
$hasBreakingChange = $false
}
else {
$logFile = $logs | select-object -skip 2
$breakingChanges = $logFile -join ",`n"
$content = "Breaking Changes: $breakingChanges"
$hasBreakingChange = $true
}
}
else {
$result = "failed"
Write-Error "Error occurred while generating artifacts for $packageName `n"
}
}
else {
$result = "failed"
Write-Error "Error occurred while generating code for $packageName `n"
}

$path = , $sdkPath

$readmeMd = $sdksInfo[$sdkPath]

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

$downloadUrlPrefix = $inputFileContent.installInstructionInput.downloadUrlPrefix
$full = $null
if ($artifacts.count -gt 0) {
$fileName = Split-Path $artifacts[0] -Leaf
$full = "Download the $packageName package from [here]($downloadUrlPrefix/$fileName)"
}
$installInstructions = [PSCustomObject]@{
full = $full
lite = $full
}

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

if ($OutputJsonPath) {
Write-Host "`nGenerating output JSON..."
ConvertTo-Json @{
packages = $packages
} -depth 5 | Out-File -FilePath $OutputJsonPath
}
44 changes: 44 additions & 0 deletions eng/scripts/Automation-Sdk-Init.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[string] $RepoRoot = "${PSScriptRoot}/../.."
[string] $dotnetInstallScriptVersion = "v1"

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

$uri = "https://dot.net/$dotnetInstallScriptVersion/dotnet-install.ps1"
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
& $installScript -Version $dotnetSdkVersion

if (Test-Path $installScript) {
Remove-Item $installScript
}
13 changes: 13 additions & 0 deletions eng/swagger_to_sdk_config_track.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://github.com/Azure/azure-rest-api-specs/blob/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json",
"generateOptions": {
"generateScript": {
"path": "pwsh ./scripts/Automation-Sdk-Generate.ps1"
}
},
"initOptions": {
"initScript": {
"path": "pwsh ./scripts/Automation-Sdk-Init.ps1"
}
}
}