|
| 1 | +# Copyright (c) .NET Foundation and Contributors |
| 2 | +# See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +function UpdateTCoreLibraryVersion { |
| 5 | + Param( |
| 6 | + [string] $NewVersion, |
| 7 | + [string] $FilePath |
| 8 | + ) |
| 9 | + |
| 10 | + $versionRegex1 = "nanoFramework\.CoreLibrary\.\s*\d+\.\s*\d+\.\s*\d+" |
| 11 | + $versionRegex2 = "id=\""nanoFramework\.CoreLibrary\"" version=\""\s*\d+\.\s*\d+\.\s*\d+" |
| 12 | + |
| 13 | + $filecontent = Get-Content($FilePath) |
| 14 | + attrib $FilePath -r |
| 15 | + $filecontent -replace $versionRegex1, "nanoFramework.CoreLibrary.$NewVersion" | Out-File $FilePath -Encoding utf8 |
| 16 | + $filecontent = Get-Content($FilePath) |
| 17 | + $filecontent -replace $versionRegex2, "id=""nanoFramework.CoreLibrary"" version=""$NewVersion" | Out-File $FilePath -Encoding utf8 |
| 18 | +} |
| 19 | + |
| 20 | +function AddGeneratePathProperty { |
| 21 | + Param( |
| 22 | + [string] $NewVersion, |
| 23 | + [string] $FilePath |
| 24 | + ) |
| 25 | + |
| 26 | + $versionRegex1 = "Include=\""nanoFramework\.CoreLibrary\"" Version=\""\d+\.\d+\.\d+\"">" |
| 27 | + |
| 28 | + $filecontent = Get-Content($FilePath) |
| 29 | + attrib $FilePath -r |
| 30 | + $filecontent -replace $versionRegex1, "Include=""nanoFramework.CoreLibrary"" Version=""$NewVersion"" GeneratePathProperty=""true"">" | Out-File $FilePath -Encoding utf8 |
| 31 | +} |
| 32 | + |
| 33 | +"Updating dependency at nf-Visual-Studio-extension" | Write-Host |
| 34 | + |
| 35 | +# compute authorization header in format "AUTHORIZATION: basic 'encoded token'" |
| 36 | +# 'encoded token' is the Base64 of the string "nfbot:personal-token" |
| 37 | +$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:$env:MY_GITHUB_TOKEN")))" |
| 38 | + |
| 39 | +# uncomment these for local debug |
| 40 | +# [Environment]::SetEnvironmentVariable('NBGV_NuGetPackageVersion', '2.0.42', 'Process') |
| 41 | +# [Environment]::SetEnvironmentVariable('Agent_TempDirectory', 'e:\temp', 'Process') |
| 42 | + |
| 43 | +# init/reset these |
| 44 | +$commitMessage = "" |
| 45 | +$prTitle = "" |
| 46 | +$newBranchName = "develop-nfbot/update-dependencies/" + [guid]::NewGuid().ToString() |
| 47 | +$packageTargetVersion = $env:NBGV_NuGetPackageVersion |
| 48 | +$packageName = "nanoframework.corelibrary" |
| 49 | +$repoMainBranch = "main" |
| 50 | + |
| 51 | +# working directory is agent temp directory |
| 52 | +Write-Debug "Changing working directory to $env:Agent_TempDirectory" |
| 53 | +Set-Location "$env:Agent_TempDirectory" | Out-Null |
| 54 | + |
| 55 | +# clone repo and checkout develop branch |
| 56 | +Write-Debug "Init and featch nf-Visual-Studio-extension repo" |
| 57 | + |
| 58 | +git clone --depth 1 https://github.com/nanoframework/nf-Visual-Studio-extension repo |
| 59 | +Set-Location repo | Out-Null |
| 60 | +git config --global gc.auto 0 |
| 61 | +git config --global user.name nfbot |
| 62 | +git config --global user.email nanoframework@outlook.com |
| 63 | +git config --global core.autocrlf true |
| 64 | + |
| 65 | +Write-Host "Checkout $repoMainBranch branch..." |
| 66 | +git checkout --quiet $repoMainBranch | Out-Null |
| 67 | + |
| 68 | +# check if nuget package is already available from nuget.org |
| 69 | +$nugetApiUrl = "https://api.nuget.org/v3-flatcontainer/$packageName/index.json" |
| 70 | + |
| 71 | +function Get-LatestNugetVersion { |
| 72 | + param ( |
| 73 | + [string]$url |
| 74 | + ) |
| 75 | + try { |
| 76 | + $response = Invoke-RestMethod -Uri $url -Method Get |
| 77 | + return $response.versions[-1] |
| 78 | + } |
| 79 | + catch { |
| 80 | + throw "Error querying NuGet API: $_" |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +$latestNugetVersion = Get-LatestNugetVersion -url $nugetApiUrl |
| 85 | + |
| 86 | +while ($latestNugetVersion -ne $packageTargetVersion) { |
| 87 | + Write-Host "Latest version still not available from nuget.org feed. Waiting 5 minutes..." |
| 88 | + Start-Sleep -Seconds 300 |
| 89 | + $latestNugetVersion = Get-LatestNugetVersion -url $nugetApiUrl |
| 90 | +} |
| 91 | + |
| 92 | +Write-Host "Version $latestNugetVersion available from nuget.org feed. Proceeding with update." |
| 93 | + |
| 94 | +#################### |
| 95 | +# VS 2019 & 2022 |
| 96 | + |
| 97 | +"*****************************************************************************************************" | Write-Host |
| 98 | +"Updating nanoFramework.CoreLibrary package in VS2019 & VS2022 solution..." | Write-Host |
| 99 | + |
| 100 | +dotnet remove VisualStudio.Extension-2019/VisualStudio.Extension-vs2019.csproj package nanoFramework.CoreLibrary |
| 101 | +dotnet add VisualStudio.Extension-2019/VisualStudio.Extension-vs2019.csproj package nanoFramework.CoreLibrary --version $packageTargetVersion |
| 102 | +AddGeneratePathProperty -NewVersion $packageTargetVersion -FilePath 'VisualStudio.Extension-2019/VisualStudio.Extension-vs2019.csproj' |
| 103 | + |
| 104 | +dotnet remove VisualStudio.Extension-2022/VisualStudio.Extension-vs2022.csproj package nanoFramework.CoreLibrary |
| 105 | +dotnet add VisualStudio.Extension-2022/VisualStudio.Extension-vs2022.csproj package nanoFramework.CoreLibrary --version $packageTargetVersion |
| 106 | +AddGeneratePathProperty -NewVersion $packageTargetVersion -FilePath 'VisualStudio.Extension-2022/VisualStudio.Extension-vs2022.csproj' |
| 107 | +nuget restore -uselockfile |
| 108 | + |
| 109 | +##################### |
| 110 | + |
| 111 | +"Bumping nanoFramework.CoreLibrary to $packageTargetVersion." | Write-Host -ForegroundColor Cyan |
| 112 | + |
| 113 | +# build commit message |
| 114 | +$commitMessage += "Bumps nanoFramework.CoreLibrary to $packageTargetVersion.`n" |
| 115 | +# build PR title |
| 116 | +$prTitle = "Bumps nanoFramework.CoreLibrary to $packageTargetVersion" |
| 117 | + |
| 118 | +# need this line so nfbot flags the PR appropriately |
| 119 | +$commitMessage += "`n[version update]`n`n" |
| 120 | + |
| 121 | +# better add this warning line |
| 122 | +$commitMessage += "### :warning: This is an automated update. Merge only after all tests pass. :warning:`n" |
| 123 | + |
| 124 | +Write-Debug "Git branch" |
| 125 | + |
| 126 | +# check if anything was changed |
| 127 | +$repoStatus = "$(git status --short --porcelain)" |
| 128 | + |
| 129 | +if ($repoStatus -ne "") |
| 130 | +{ |
| 131 | + # update the extension manifests |
| 132 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'VisualStudio.Extension-2019/source.extension.vsixmanifest' |
| 133 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'VisualStudio.Extension-2022/source.extension.vsixmanifest' |
| 134 | + |
| 135 | + # update the project templates |
| 136 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'CSharp.TestApplication/CS.TestApplication-vs2019.vstemplate' |
| 137 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'CSharp.TestApplication/CS.TestApplication-vs2022.vstemplate' |
| 138 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'CSharp.TestApplication/NFUnitTest.nfproj' |
| 139 | + |
| 140 | + # update remaining project refs |
| 141 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'VisualStudio.Extension-2019/VisualStudio.Extension-vs2019.csproj' |
| 142 | + UpdateTCoreLibraryVersion -NewVersion $packageTargetVersion -FilePath 'VisualStudio.Extension-2022/VisualStudio.Extension-vs2022.csproj' |
| 143 | + |
| 144 | + # create branch to perform updates |
| 145 | + git branch $newBranchName |
| 146 | + |
| 147 | + Write-Debug "Checkout branch" |
| 148 | + |
| 149 | + # checkout branch |
| 150 | + git checkout $newBranchName |
| 151 | + |
| 152 | + Write-Debug "Add changes" |
| 153 | + |
| 154 | + # commit changes |
| 155 | + git add -A > $null |
| 156 | + |
| 157 | + Write-Debug "Commit changed files" |
| 158 | + |
| 159 | + git commit -m "$prTitle ***NO_CI***" -m "$commitMessage" > $null |
| 160 | + |
| 161 | + Write-Debug "Push changes" |
| 162 | + |
| 163 | + git -c http.extraheader="AUTHORIZATION: $auth" push --set-upstream origin $newBranchName > $null |
| 164 | + |
| 165 | + # start PR |
| 166 | + # we are pointing to the $repoMainBranch |
| 167 | + # considering that the base branch can be changed at the PR ther is no big deal about this |
| 168 | + $prRequestBody = @{title="$prTitle";body="$commitMessage";head="$newBranchName";base="$repoMainBranch"} | ConvertTo-Json |
| 169 | + $githubApiEndpoint = "https://api.github.com/repos/nanoframework/nf-Visual-Studio-extension/pulls" |
| 170 | + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
| 171 | + |
| 172 | + $headers = @{} |
| 173 | + $headers.Add("Authorization","$auth") |
| 174 | + $headers.Add("Accept","application/vnd.github.symmetra-preview+json") |
| 175 | + |
| 176 | + try |
| 177 | + { |
| 178 | + $result = Invoke-RestMethod -Method Post -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer -Uri $githubApiEndpoint -Header $headers -ContentType "application/json" -Body $prRequestBody |
| 179 | + 'Started PR with dependencies update...' | Write-Host -NoNewline |
| 180 | + 'OK' | Write-Host -ForegroundColor Green |
| 181 | + } |
| 182 | + catch |
| 183 | + { |
| 184 | + $result = $_.Exception.Response.GetResponseStream() |
| 185 | + $reader = New-Object System.IO.StreamReader($result) |
| 186 | + $reader.BaseStream.Position = 0 |
| 187 | + $reader.DiscardBufferedData() |
| 188 | + $responseBody = $reader.ReadToEnd(); |
| 189 | + |
| 190 | + throw "Error starting PR: $responseBody" |
| 191 | + } |
| 192 | +} |
| 193 | +else |
| 194 | +{ |
| 195 | + Write-Host "Nothing to udpate in Visual-Studio-extension" |
| 196 | +} |
0 commit comments