Skip to content

Commit ec043ac

Browse files
committed
refactor: streamline conditional checks and improve code readability in update-dependency.ps1
1 parent 4a2197b commit ec043ac

File tree

1 file changed

+49
-110
lines changed

1 file changed

+49
-110
lines changed

updater/scripts/update-dependency.ps1

Lines changed: 49 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,24 @@ if ($Path -match '^(.+\.cmake)(#(.+))?$') {
3939
$isCMakeFile = $false
4040
}
4141

42-
if (-not (Test-Path $Path ))
43-
{
42+
if (-not (Test-Path $Path )) {
4443
throw "Dependency $Path doesn't exit";
4544
}
4645

4746
# If it's a directory, we consider it a submodule dependendency. Otherwise, it must a properties-style file or a script.
4847
$isSubmodule = (Test-Path $Path -PathType Container)
4948

50-
function SetOutput([string] $name, $value)
51-
{
52-
if (Test-Path env:GITHUB_OUTPUT)
53-
{
49+
function SetOutput([string] $name, $value) {
50+
if (Test-Path env:GITHUB_OUTPUT) {
5451
"$name=$value" | Tee-Object $env:GITHUB_OUTPUT -Append
55-
}
56-
else
57-
{
52+
} else {
5853
"$name=$value"
5954
}
6055
}
6156

62-
if (-not $isSubmodule)
63-
{
57+
if (-not $isSubmodule) {
6458
$isScript = $Path -match '\.(ps1|sh)$'
65-
function DependencyConfig ([Parameter(Mandatory = $true)][string] $action, [string] $value = $null)
66-
{
59+
function DependencyConfig ([Parameter(Mandatory = $true)][string] $action, [string] $value = $null) {
6760
if ($isCMakeFile) {
6861
# CMake file handling
6962
switch ($action) {
@@ -84,64 +77,48 @@ if (-not $isSubmodule)
8477
'set-version' {
8578
Update-CMakeFile $Path $cmakeDep $value
8679
}
87-
Default {
80+
default {
8881
throw "Unknown action $action"
8982
}
9083
}
91-
}
92-
elseif ($isScript)
93-
{
94-
if (Get-Command 'chmod' -ErrorAction SilentlyContinue)
95-
{
84+
} elseif ($isScript) {
85+
if (Get-Command 'chmod' -ErrorAction SilentlyContinue) {
9686
chmod +x $Path
97-
if ($LastExitCode -ne 0)
98-
{
87+
if ($LastExitCode -ne 0) {
9988
throw 'chmod failed';
10089
}
10190
}
102-
try
103-
{
91+
try {
10492
$result = & $Path $action $value
10593
$failed = -not $?
106-
}
107-
catch
108-
{
94+
} catch {
10995
$result = $_
11096
$failed = $true
11197
}
112-
if ($failed)
113-
{
98+
if ($failed) {
11499
throw "Script execution failed: $Path $action $value | output: $result"
115100
}
116101
return $result
117-
}
118-
else
119-
{
120-
switch ($action)
121-
{
122-
'get-version'
123-
{
102+
} else {
103+
switch ($action) {
104+
'get-version' {
124105
return (Get-Content $Path -Raw | ConvertFrom-StringData).version
125106
}
126-
'get-repo'
127-
{
107+
'get-repo' {
128108
return (Get-Content $Path -Raw | ConvertFrom-StringData).repo
129109
}
130-
'set-version'
131-
{
110+
'set-version' {
132111
$content = Get-Content $Path
133112
$content = $content -replace '^(?<prop>version *= *).*$', "`${prop}$value"
134113
$content | Out-File $Path
135114

136115
$readVersion = (Get-Content $Path -Raw | ConvertFrom-StringData).version
137116

138-
if ("$readVersion" -ne "$value")
139-
{
117+
if ("$readVersion" -ne "$value") {
140118
throw "Update failed - read-after-write yielded '$readVersion' instead of expected '$value'"
141119
}
142120
}
143-
Default
144-
{
121+
default {
145122
throw "Unknown action $action"
146123
}
147124
}
@@ -152,27 +129,20 @@ if (-not $isSubmodule)
152129
. "$PSScriptRoot/cmake-functions.ps1"
153130
}
154131

155-
if ("$Tag" -eq '')
156-
{
157-
if ($isSubmodule)
158-
{
132+
if ("$Tag" -eq '') {
133+
if ($isSubmodule) {
159134
git submodule update --init --no-fetch --single-branch $Path
160135
Push-Location $Path
161-
try
162-
{
136+
try {
163137
$originalTag = $(git describe --tags)
164138
git fetch --tags
165139
[string[]]$tags = $(git tag --list)
166140
$url = $(git remote get-url origin)
167141
$mainBranch = $(git remote show origin | Select-String 'HEAD branch: (.*)').Matches[0].Groups[1].Value
168-
}
169-
finally
170-
{
142+
} finally {
171143
Pop-Location
172144
}
173-
}
174-
else
175-
{
145+
} else {
176146
$originalTag = DependencyConfig 'get-version'
177147
$url = DependencyConfig 'get-repo'
178148

@@ -181,8 +151,7 @@ if ("$Tag" -eq '')
181151
$tags = $tags | ForEach-Object { ($_ -split '\s+')[1] -replace '^refs/tags/', '' }
182152

183153
$headRef = ($(git ls-remote $url HEAD) -split '\s+')[0]
184-
if ("$headRef" -eq '')
185-
{
154+
if ("$headRef" -eq '') {
186155
throw "Couldn't determine repository head (no ref returned by ls-remote HEAD"
187156
}
188157
$mainBranch = (git ls-remote --heads $url | Where-Object { $_.StartsWith($headRef) }) -replace '.*\srefs/heads/', ''
@@ -191,64 +160,42 @@ if ("$Tag" -eq '')
191160
$url = $url -replace '\.git$', ''
192161

193162
# Filter by GitHub release titles if pattern is provided
194-
if ("$GhTitlePattern" -ne '')
195-
{
163+
if ("$GhTitlePattern" -ne '') {
196164
Write-Host "Filtering tags by GitHub release title pattern '$GhTitlePattern'"
197165

198166
# Parse GitHub repo owner/name from URL
199-
if ($url -notmatch 'github\.com[:/]([^/]+)/([^/]+?)(?:\.git)?$')
200-
{
167+
if ($url -notmatch 'github\.com[:/]([^/]+)/([^/]+?)(?:\.git)?$') {
201168
throw "Could not parse GitHub owner/repo from URL: $url"
202169
}
203170

204171
$owner, $repo = $Matches[1], $Matches[2]
205172

206-
try
207-
{
208-
# Fetch releases from GitHub API
209-
$releases = @(gh api "repos/$owner/$repo/releases" --jq '.[] | {tag_name: .tag_name, name: .name}' | ConvertFrom-Json)
210-
211-
# Find tags that have matching release titles
212-
$validTags = @{}
213-
foreach ($release in $releases)
214-
{
215-
if ($release.name -match $GhTitlePattern)
216-
{
217-
$validTags[$release.tag_name] = $true
218-
}
219-
}
220-
221-
# Filter tags to only include those with matching release titles
222-
$originalTagCount = $tags.Length
223-
$tags = @($tags | Where-Object { $validTags.ContainsKey($_) })
224-
Write-Host "GitHub release title filtering: $originalTagCount -> $($tags.Count) tags"
173+
# Fetch releases from GitHub API
174+
$releases = @(gh api "repos/$owner/$repo/releases" --jq '.[] | {tag_name: .tag_name, name: .name}' | ConvertFrom-Json)
225175

226-
if ($tags.Count -eq 0)
227-
{
228-
throw "Found no tags with GitHub releases matching title pattern '$GhTitlePattern'"
176+
# Find tags that have matching release titles
177+
$validTags = @{}
178+
foreach ($release in $releases) {
179+
if ($release.name -match $GhTitlePattern) {
180+
$validTags[$release.tag_name] = $true
229181
}
230182
}
231-
catch
232-
{
233-
if ($_.Exception.Message -like "*Found no tags with GitHub releases matching title pattern*")
234-
{
235-
throw
236-
}
237-
throw "Failed to fetch GitHub releases for $owner/$repo`: $($_.Exception.Message)"
238-
}
183+
184+
# Filter tags to only include those with matching release titles
185+
$originalTagCount = $tags.Length
186+
$tags = @($tags | Where-Object { $validTags.ContainsKey($_) })
187+
Write-Host "GitHub release title filtering: $originalTagCount -> $($tags.Count) tags"
239188
}
240189

241-
if ("$Pattern" -eq '')
242-
{
190+
if ("$Pattern" -eq '') {
243191
# Use a default pattern that excludes pre-releases
244192
$Pattern = '^v?([0-9.]+)$'
245193
}
246194

247195
Write-Host "Filtering tags with pattern '$Pattern'"
248196
$tags = $tags -match $Pattern
249197

250-
if ($tags.Length -le 0)
251-
{
198+
if ($tags.Length -le 0) {
252199
throw "Found no tags matching pattern '$Pattern'"
253200
}
254201

@@ -257,14 +204,11 @@ if ("$Tag" -eq '')
257204
Write-Host "Sorted tags: $tags"
258205
$latestTag = $tags[-1]
259206

260-
if (("$originalTag" -ne '') -and ("$latestTag" -ne '') -and ("$latestTag" -ne "$originalTag"))
261-
{
262-
do
263-
{
207+
if (("$originalTag" -ne '') -and ("$latestTag" -ne '') -and ("$latestTag" -ne "$originalTag")) {
208+
do {
264209
# It's possible that the dependency was updated to a pre-release version manually in which case we don't want to
265210
# roll back, even though it's not the latest version matching the configured pattern.
266-
if ((GetComparableVersion $originalTag) -ge (GetComparableVersion $latestTag))
267-
{
211+
if ((GetComparableVersion $originalTag) -ge (GetComparableVersion $latestTag)) {
268212
Write-Host "SemVer represented by the original tag '$originalTag' is newer than the latest tag '$latestTag'. Skipping update."
269213
$latestTag = $originalTag
270214
break
@@ -274,8 +218,7 @@ if ("$Tag" -eq '')
274218
$refs = $(git ls-remote --tags $url)
275219
$refOriginal = (($refs -match "refs/tags/$originalTag" ) -split '[ \t]') | Select-Object -First 1
276220
$refLatest = (($refs -match "refs/tags/$latestTag" ) -split '[ \t]') | Select-Object -First 1
277-
if ($refOriginal -eq $refLatest)
278-
{
221+
if ($refOriginal -eq $refLatest) {
279222
Write-Host "Latest tag '$latestTag' points to the same commit as the original tag '$originalTag'. Skipping update."
280223
$latestTag = $originalTag
281224
break
@@ -291,23 +234,19 @@ if ("$Tag" -eq '')
291234
SetOutput 'url' $url
292235
SetOutput 'mainBranch' $mainBranch
293236

294-
if ("$originalTag" -eq "$latestTag")
295-
{
237+
if ("$originalTag" -eq "$latestTag") {
296238
return
297239
}
298240

299241
$Tag = $latestTag
300242
}
301243

302-
if ($isSubmodule)
303-
{
244+
if ($isSubmodule) {
304245
Write-Host "Updating submodule $Path to $Tag"
305246
Push-Location $Path
306247
git checkout $Tag
307248
Pop-Location
308-
}
309-
else
310-
{
249+
} else {
311250
Write-Host "Updating 'version' in $Path to $Tag"
312251
DependencyConfig 'set-version' $tag
313252
}

0 commit comments

Comments
 (0)