|
| 1 | +$support = $args[0] # STS, LTS, or Preview |
| 2 | +$projectsJson = $args[1] # JSON string of projects to upgrade |
| 3 | + |
| 4 | +Write-Host "Upgrading projects to $support versions" |
| 5 | +Write-Host "" |
| 6 | + |
| 7 | +# Convert JSON string to array of strings |
| 8 | +$projects = ConvertFrom-Json $projectsJson |
| 9 | + |
| 10 | +Write-Host "Parsed $($projects.Length) projects to upgrade" |
| 11 | +Write-Host "" |
| 12 | + |
| 13 | +# Install .NET Upgrade Assistant global tool |
| 14 | +dotnet tool install --global upgrade-assistant |
| 15 | + |
| 16 | +# Get the git repository directory |
| 17 | +$gitRepoDir = (git rev-parse --show-toplevel) |
| 18 | + |
| 19 | +# Create an array of projects that to track failure attempts |
| 20 | +$failedProjects = @() |
| 21 | + |
| 22 | +# Iterate all upgrade projects |
| 23 | +foreach ($projectDir in $projects) { |
| 24 | + Write-Host "Attempting to upgrade project: $projectDir" |
| 25 | + Write-Host "" |
| 26 | + |
| 27 | + # Remove the git repository directory from the project directory |
| 28 | + $relativePath = $projectDir.Replace($gitRepoDir, "") |
| 29 | + |
| 30 | + # Remove invalid characters from the branch name, then remove leading hyphen |
| 31 | + $branchName = $relativePath -replace '[^A-Za-z0-9._-]', '-' |
| 32 | + $branchName = $branchName -replace '^[-]+', '' |
| 33 | + |
| 34 | + # Format the branch name |
| 35 | + $branch = "auto-upgrade/$branchName" |
| 36 | + |
| 37 | + # Create a new branch |
| 38 | + git checkout -b $branch |
| 39 | + |
| 40 | + # Normalize the project directory |
| 41 | + $projectDir = [IO.Path]::GetFullPath($projectDir) |
| 42 | + |
| 43 | + # Capture all output from the upgrade-assistant command |
| 44 | + $output = upgrade-assistant upgrade "$projectDir" ` |
| 45 | + --non-interactive ` |
| 46 | + --operation Inplace ` |
| 47 | + -t $support ` |
| 48 | + 2>&1 |
| 49 | + |
| 50 | + # Check if the exit code is 0 (success), or if there is output from a failure |
| 51 | + if ($LASTEXITCODE -eq 0 -and -not ($output -match "Exception")) { |
| 52 | + |
| 53 | + Write-Host $output |
| 54 | + Write-Host "" |
| 55 | + |
| 56 | + # Check for changes, report the exit code |
| 57 | + git diff --exit-code |
| 58 | + |
| 59 | + # If there aren't any, delete the |
| 60 | + # branch and continue to the next project |
| 61 | + if ($LASTEXITCODE -ne 0) { |
| 62 | + $failedProjects += $projectDir |
| 63 | + |
| 64 | + Write-Host "No changes detected for project: $projectDir" |
| 65 | + Write-Host "" |
| 66 | + |
| 67 | + # Delete the branch if there aren't any changes |
| 68 | + git checkout main |
| 69 | + git branch -D $branch |
| 70 | + Write-Host "" |
| 71 | + |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + # Commit the changes |
| 76 | + git add . |
| 77 | + git commit -m ".NET Version Sweeper: Upgraded $projectDir" |
| 78 | + |
| 79 | + # Push the branch to the repository |
| 80 | + git push origin $branch |
| 81 | + |
| 82 | + # Format the pull request message |
| 83 | + $pullRequestMessage = "⚡ This is an automated pull request (powered by the |
| 84 | + [.NET Versioon Sweeper](https://github.com/dotnet/versionsweeper) and |
| 85 | + the [.NET Upgrade Assistant](https://github.com/dotnet/upgrade-assistant)]) |
| 86 | + to _upgrade_ the **target framework moniker (TFM)** for the '$projectDir' |
| 87 | + project to the $support version. For more information, see |
| 88 | + [.NET Support Policy](https://dotnet.microsoft.com/platform/support/policy)." |
| 89 | + |
| 90 | + # Create a pull request |
| 91 | + gh pr create ` |
| 92 | + --base main ` |
| 93 | + --head $branch ` |
| 94 | + --title "[$support] Upgrade $projectDir" ` |
| 95 | + --body $pullRequestMessage |
| 96 | + } |
| 97 | + else { |
| 98 | + $failedProjects += $projectDir |
| 99 | + |
| 100 | + Write-Host "Unable to upgrade project: $projectDir" |
| 101 | + Write-Host "" |
| 102 | + |
| 103 | + # Write the output as a warning |
| 104 | + Write-Warning -Message ([String]::Join("`n", $output)) |
| 105 | + Write-Host "" |
| 106 | + |
| 107 | + # Delete the branch if the upgrade fails |
| 108 | + git checkout main |
| 109 | + git branch -D $branch |
| 110 | + Write-Host "" |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +# If there are any failed projects, log them |
| 115 | +if ($failedProjects.Length -gt 0) { |
| 116 | + Write-Host "Failed to upgrade $($failedProjects.Length) projects:" |
| 117 | + Write-Host "" |
| 118 | + |
| 119 | + foreach ($failedProject in $failedProjects) { |
| 120 | + Write-Host $failedProject |
| 121 | + } |
| 122 | +} |
0 commit comments