-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupgrade.ps1
More file actions
104 lines (88 loc) · 2.94 KB
/
Copy pathupgrade.ps1
File metadata and controls
104 lines (88 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#Requires -Version 5.1
<#
.SYNOPSIS
Recursively upgrade all Go module dependencies in the monorepo.
.DESCRIPTION
Traverses every directory containing a go.mod file and runs:
go get -u ./...
go mod tidy
#>
$ErrorActionPreference = "Stop"
# Resolve root directory from script location.
$RootDir = Split-Path -Parent $MyInvocation.MyCommand.Path
function Write-Status {
param(
[string]$Message,
[ConsoleColor]$Color = [ConsoleColor]::White
)
Write-Host $Message -ForegroundColor $Color
}
function Upgrade-Module {
param([string]$ModDir)
# Relative path for display.
$relPath = $ModDir.Substring($RootDir.Length).TrimStart('\', '/')
if (-not $relPath) { $relPath = "(root)" }
Write-Status "[upgrade] $relPath" ([ConsoleColor]::Cyan)
# go get -u ./...
Push-Location $ModDir
$prevPref = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try {
$output = & go get -u ./... 2>&1 | Out-String
$getCode = $LASTEXITCODE
if ($getCode -ne 0) {
$ErrorActionPreference = $prevPref
Write-Status " X go get -u failed:" ([ConsoleColor]::Red)
Write-Host $output
return @{ Success = $false; FailedAt = "go get" }
}
# go mod tidy
$output = & go mod tidy 2>&1 | Out-String
$tidyCode = $LASTEXITCODE
if ($tidyCode -ne 0) {
$ErrorActionPreference = $prevPref
Write-Status " X go mod tidy failed:" ([ConsoleColor]::Red)
Write-Host $output
return @{ Success = $false; FailedAt = "go mod tidy" }
}
}
finally {
$ErrorActionPreference = $prevPref
Pop-Location
}
Write-Status " V done" ([ConsoleColor]::Green)
return @{ Success = $true }
}
# Find all go.mod files, excluding .git and .idea directories.
$modFiles = Get-ChildItem -Path $RootDir -Filter "go.mod" -Recurse -File |
Where-Object { $_.FullName -notmatch '[\\/]\.git[\\/]' -and $_.FullName -notmatch '[\\/]\.idea[\\/]' }
$Total = $modFiles.Count
$Success = 0
$Failed = @()
foreach ($modFile in $modFiles) {
$modDir = Split-Path -Parent $modFile.FullName
$result = Upgrade-Module -ModDir $modDir
if ($result.Success) {
$Success++
} else {
$relPath = $modDir.Substring($RootDir.Length).TrimStart('\', '/')
if (-not $relPath) { $relPath = "(root)" }
$Failed += "$relPath ($($result.FailedAt))"
}
}
# Summary.
Write-Host ""
Write-Status "=== Summary ===" ([ConsoleColor]::Cyan)
Write-Host "Total modules: $Total"
Write-Status "Succeeded: $Success" ([ConsoleColor]::Green)
if ($Failed.Count -gt 0) {
Write-Status "Failed: $($Failed.Count)" ([ConsoleColor]::Red)
Write-Host ""
Write-Status "Failed modules:" ([ConsoleColor]::Red)
foreach ($f in $Failed) {
Write-Host " X $f"
}
exit 1
} else {
Write-Status "All modules upgraded successfully!" ([ConsoleColor]::Green)
}