diff --git a/eng/scripts/Sdk-Generate.ps1 b/eng/scripts/Sdk-Generate.ps1 new file mode 100644 index 000000000000..73063b90260b --- /dev/null +++ b/eng/scripts/Sdk-Generate.ps1 @@ -0,0 +1,144 @@ +param( + [string] $InputJsonPath = $GenerateInput, + [string] $OutputJsonPath = $GenerateOutput, + [string] $RepoRoot = "${PSScriptRoot}/../.." +) + +$inputFileContent = Get-Content $InputJsonPath | ConvertFrom-Json +$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 ($inputFilePath in $inputFilePaths) { + foreach ($path in $autorestFilesPath) { + $fileContent = Get-Content $path + $isUpdatedLines = $false + $updatedLines = foreach ($line in $fileContent) { + $regexForMatchingShaAndPath = "[\/][0-9a-f]{4,40}[\/]$inputFilePath" + if ($line -match $regexForMatchingShaAndPath) { + $line -replace "https:\/\/[^`"]*$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" + +function Test-PreviousScript() { + $result = $null + if ($LASTEXITCODE -eq 0) { + $result = "succeeded" + } + else { + $result = "failed" + } + Write-Host "Result: $result" + return $result +} + +$packages = @() +foreach ($sdkPath in $sdksInfo.Keys) { + $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 /p:RunApiCompat=$false + $result = Test-PreviousScript + if ($result -eq "succeeded") { + $artifactsPath = "$RepoRoot/artifacts/packages/Debug/$packageName" + $artifacts += Get-ChildItem $artifactsPath -Filter *.nupkg -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 = $logFile -join ",`n" + $content = "Breaking Changes: $breakingChanges" + $hasBreakingChange = $true + $result = "succeeded" + } + + if (Test-Path $logFilePath) { + Remove-Item $logFilePath + } + } + } + else { + Write-Error "Error occurred while generating code for" $packageName "`n" + } + + $path = @() + $path += $sdkPath + + $readmeMd = @() + $readmeMd += $sdksInfo[$sdkPath] + + $changelog = [PSCustomObject]@{ + content = $content + hasBreakingChange = $hasBreakingChange + } + + $downloadUrlPrefix = $inputFileContent.installInstructionInput.downloadUrlPrefix + $fileName = Split-Path $artifacts[0] -Leaf + $installInstructions = [PSCustomObject]@{ + full = "Download the $packageName package from [here]($downloadUrlPrefix/$fileName)" + lite = "Download the $packagename package from [here]($downloadUrlPrefix/$fileName)" + } + + $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 +} diff --git a/eng/scripts/Sdk-init.ps1 b/eng/scripts/Sdk-init.ps1 new file mode 100644 index 000000000000..c22f6a341ec4 --- /dev/null +++ b/eng/scripts/Sdk-init.ps1 @@ -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 +} diff --git a/swagger_to_sdk_config.json b/swagger_to_sdk_config.json index dae7c82a86b1..97a9ffd5c981 100644 --- a/swagger_to_sdk_config.json +++ b/swagger_to_sdk_config.json @@ -6,8 +6,8 @@ "csharp": "", "reflect-api-versions": "", "license-header": "MICROSOFT_MIT_NO_VERSION", - "sdkrel:csharp-sdks-folder": "./sdk", - "use": "@microsoft.azure/autorest.csharp@2.3.82" + "use": "@microsoft.azure/autorest.csharp@2.3.84", + "sdkrel:csharp-sdks-folder": "./sdk" }, "advanced_options": { "create_sdk_pull_requests": true, @@ -15,5 +15,10 @@ "main_branch": "master" }, "version": "0.2.0" + }, + "generateOptions": { + "genearteScript": { + "path": "autorest" + } } } diff --git a/swagger_to_sdk_config_track2.json b/swagger_to_sdk_config_track2.json new file mode 100644 index 000000000000..4d415274d37a --- /dev/null +++ b/swagger_to_sdk_config_track2.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://github.com/Azure/azure-rest-api-specs/blob/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json", + "advancedOptions": { + "createSdkPullRequests": true, + "generationCallMode": "one-for-all-configs" + }, + "generateOptions": { + "generateScript": { + "path": "pwsh ./eng/scripts/Sdk-Generate.ps1" + } + }, + "initOptions": { + "initScript": { + "path": "pwsh ./eng/scripts/Sdk-init.ps1" + } + } +}