-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate-dependency.ps1
More file actions
282 lines (258 loc) · 9.73 KB
/
update-dependency.ps1
File metadata and controls
282 lines (258 loc) · 9.73 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
param(
# Path to the dependency, which can be either of the following:
# - a submodule
# - a [.properties](https://en.wikipedia.org/wiki/.properties) file with `version` (e.g. 1.0.0) and `repo` (e.g. https://github.com/getsentry/dependency)
# - a CMake file (.cmake, CMakeLists.txt) or any file containing FetchContent_Declare with `GIT_REPOSITORY` and `GIT_TAG`
# - a script (.sh, .ps1) that takes the executes a given action based on a given argument:
# * `get-version` - return the currently specified dependency version
# * `get-repo` - return the repository url (e.g. https://github.com/getsentry/dependency)
# * `set-version` - update the dependency version (passed as another string argument after this one)
[Parameter(Mandatory = $true)][string] $Path,
# RegEx pattern that will be matched against available versions when picking the latest one
[string] $Pattern = '',
# Specific version - if passed, no discovery is performed and the version is set directly
[string] $Tag = ''
)
Set-StrictMode -Version latest
. "$PSScriptRoot/common.ps1"
if (-not (Test-Path $Path ))
{
throw "Dependency $Path doesn't exit";
}
# If it's a directory, we consider it a submodule dependendency. Otherwise, it must a properties-style file or a script.
$isSubmodule = (Test-Path $Path -PathType Container)
function SetOutput([string] $name, $value)
{
if (Test-Path env:GITHUB_OUTPUT)
{
"$name=$value" | Tee-Object $env:GITHUB_OUTPUT -Append
}
else
{
"$name=$value"
}
}
if (-not $isSubmodule)
{
$isScript = $Path -match '\.(ps1|sh)$'
$isCMake = $Path -match '\.(cmake|txt)$' -or ((Test-Path $Path -PathType Leaf) -and $Path -notmatch '\.(ps1|sh)$' -and ((Get-Content $Path -Raw -ErrorAction SilentlyContinue) -match 'FetchContent_Declare'))
function DependencyConfig ([Parameter(Mandatory = $true)][string] $action, [string] $value = $null)
{
if ($isScript)
{
if (Get-Command 'chmod' -ErrorAction SilentlyContinue)
{
chmod +x $Path
if ($LastExitCode -ne 0)
{
throw 'chmod failed';
}
}
try
{
$result = & $Path $action $value
$failed = -not $?
}
catch
{
$result = $_
$failed = $true
}
if ($failed)
{
throw "Script execution failed: $Path $action $value | output: $result"
}
return $result
}
elseif ($isCMake)
{
switch ($action)
{
'get-version'
{
$content = Get-Content $Path -Raw
if ($content -match '(?m)^\s*GIT_TAG\s+(?:"([^"]+)"|([^\s#]+))')
{
if ($Matches[1]) {
return $Matches[1]
} else {
return $Matches[2]
}
}
throw "Could not find GIT_TAG in CMake file $Path"
}
'get-repo'
{
$content = Get-Content $Path -Raw
if ($content -match '(?m)^\s*GIT_REPOSITORY\s+([^\s]+)')
{
return $Matches[1]
}
throw "Could not find GIT_REPOSITORY in CMake file $Path"
}
'set-version'
{
$content = Get-Content $Path
$updated = $false
for ($i = 0; $i -lt $content.Length; $i++)
{
if ($content[$i] -match '^(\s*GIT_TAG\s+)(")([^"]+)(".*)$')
{
# Quoted version - preserve quotes
$content[$i] = $Matches[1] + $Matches[2] + $value + $Matches[4]
$updated = $true
break
}
elseif ($content[$i] -match '^(\s*GIT_TAG\s+)([^\s#]+)(.*)$')
{
# Unquoted version
$content[$i] = $Matches[1] + $value + $Matches[3]
$updated = $true
break
}
}
if (-not $updated)
{
throw "Could not find GIT_TAG line to update in CMake file $Path"
}
$content | Out-File $Path
# Verify the update worked
$readVersion = DependencyConfig 'get-version'
if ("$readVersion" -ne "$value")
{
throw "Update failed - read-after-write yielded '$readVersion' instead of expected '$value'"
}
}
Default
{
throw "Unknown action $action"
}
}
}
else
{
switch ($action)
{
'get-version'
{
return (Get-Content $Path -Raw | ConvertFrom-StringData).version
}
'get-repo'
{
return (Get-Content $Path -Raw | ConvertFrom-StringData).repo
}
'set-version'
{
$content = Get-Content $Path
$content = $content -replace '^(?<prop>version *= *).*$', "`${prop}$value"
$content | Out-File $Path
$readVersion = (Get-Content $Path -Raw | ConvertFrom-StringData).version
if ("$readVersion" -ne "$value")
{
throw "Update failed - read-after-write yielded '$readVersion' instead of expected '$value'"
}
}
Default
{
throw "Unknown action $action"
}
}
}
}
}
if ("$Tag" -eq '')
{
if ($isSubmodule)
{
git submodule update --init --no-fetch --single-branch $Path
Push-Location $Path
try
{
$originalTag = $(git describe --tags)
git fetch --tags
[string[]]$tags = $(git tag --list)
$url = $(git remote get-url origin)
$mainBranch = $(git remote show origin | Select-String 'HEAD branch: (.*)').Matches[0].Groups[1].Value
}
finally
{
Pop-Location
}
}
else
{
$originalTag = DependencyConfig 'get-version'
$url = DependencyConfig 'get-repo'
# Get tags for a repo without cloning.
[string[]]$tags = $(git ls-remote --refs --tags $url)
$tags = $tags | ForEach-Object { ($_ -split '\s+')[1] -replace '^refs/tags/', '' }
$headRef = ($(git ls-remote $url HEAD) -split '\s+')[0]
if ("$headRef" -eq '')
{
throw "Couldn't determine repository head (no ref returned by ls-remote HEAD"
}
$mainBranch = (git ls-remote --heads $url | Where-Object { $_.StartsWith($headRef) }) -replace '.*\srefs/heads/', ''
}
$url = $url -replace '\.git$', ''
if ("$Pattern" -eq '')
{
# Use a default pattern that excludes pre-releases
$Pattern = '^v?([0-9.]+)$'
}
Write-Host "Filtering tags with pattern '$Pattern'"
$tags = $tags -match $Pattern
if ($tags.Length -le 0)
{
throw "Found no tags matching pattern '$Pattern'"
}
$tags = & "$PSScriptRoot/sort-versions.ps1" $tags
Write-Host "Sorted tags: $tags"
$latestTag = $tags[-1]
if (("$originalTag" -ne '') -and ("$latestTag" -ne '') -and ("$latestTag" -ne "$originalTag"))
{
do
{
# It's possible that the dependency was updated to a pre-release version manually in which case we don't want to
# roll back, even though it's not the latest version matching the configured pattern.
if ((GetComparableVersion $originalTag) -ge (GetComparableVersion $latestTag))
{
Write-Host "SemVer represented by the original tag '$originalTag' is newer than the latest tag '$latestTag'. Skipping update."
$latestTag = $originalTag
break
}
# Verify that the latest tag actually points to a different commit. Otherwise, we don't need to update.
$refs = $(git ls-remote --tags $url)
$refOriginal = (($refs -match "refs/tags/$originalTag" ) -split '[ \t]') | Select-Object -First 1
$refLatest = (($refs -match "refs/tags/$latestTag" ) -split '[ \t]') | Select-Object -First 1
if ($refOriginal -eq $refLatest)
{
Write-Host "Latest tag '$latestTag' points to the same commit as the original tag '$originalTag'. Skipping update."
$latestTag = $originalTag
break
}
} while ($false)
}
$latestTagNice = ($latestTag -match '^[0-9]') ? "v$latestTag" : $latestTag
SetOutput 'originalTag' $originalTag
SetOutput 'latestTag' $latestTag
SetOutput 'latestTagNice' $latestTagNice
SetOutput 'url' $url
SetOutput 'mainBranch' $mainBranch
if ("$originalTag" -eq "$latestTag")
{
return
}
$Tag = $latestTag
}
if ($isSubmodule)
{
Write-Host "Updating submodule $Path to $Tag"
Push-Location $Path
git checkout $Tag
Pop-Location
}
else
{
Write-Host "Updating 'version' in $Path to $Tag"
DependencyConfig 'set-version' $tag
}