Skip to content

Commit eb17cbf

Browse files
Copilotvaind
andcommitted
Add CMake FetchContent_Declare support to updater
Co-authored-by: vaind <[email protected]>
1 parent 77ea6aa commit eb17cbf

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

updater/scripts/update-dependency.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function SetOutput([string] $name, $value)
3939
if (-not $isSubmodule)
4040
{
4141
$isScript = $Path -match '\.(ps1|sh)$'
42+
$isCMake = $Path -match '\.(cmake|txt)$' -or ((Test-Path $Path -PathType Leaf) -and ((Get-Content $Path -Raw -ErrorAction SilentlyContinue) -match 'FetchContent_Declare'))
4243
function DependencyConfig ([Parameter(Mandatory = $true)][string] $action, [string] $value = $null)
4344
{
4445
if ($isScript)
@@ -67,6 +68,60 @@ if (-not $isSubmodule)
6768
}
6869
return $result
6970
}
71+
elseif ($isCMake)
72+
{
73+
switch ($action)
74+
{
75+
'get-version'
76+
{
77+
$content = Get-Content $Path -Raw
78+
if ($content -match '(?m)^\s*GIT_TAG\s+([^\s#]+)')
79+
{
80+
return $Matches[1]
81+
}
82+
throw "Could not find GIT_TAG in CMake file $Path"
83+
}
84+
'get-repo'
85+
{
86+
$content = Get-Content $Path -Raw
87+
if ($content -match '(?m)^\s*GIT_REPOSITORY\s+([^\s]+)')
88+
{
89+
return $Matches[1]
90+
}
91+
throw "Could not find GIT_REPOSITORY in CMake file $Path"
92+
}
93+
'set-version'
94+
{
95+
$content = Get-Content $Path
96+
$updated = $false
97+
for ($i = 0; $i -lt $content.Length; $i++)
98+
{
99+
if ($content[$i] -match '^(\s*GIT_TAG\s+)[^\s#]+(.*)$')
100+
{
101+
$content[$i] = $Matches[1] + $value + $Matches[2]
102+
$updated = $true
103+
break
104+
}
105+
}
106+
if (-not $updated)
107+
{
108+
throw "Could not find GIT_TAG line to update in CMake file $Path"
109+
}
110+
$content | Out-File $Path
111+
112+
# Verify the update worked
113+
$readVersion = DependencyConfig 'get-version'
114+
if ("$readVersion" -ne "$value")
115+
{
116+
throw "Update failed - read-after-write yielded '$readVersion' instead of expected '$value'"
117+
}
118+
}
119+
Default
120+
{
121+
throw "Unknown action $action"
122+
}
123+
}
124+
}
70125
else
71126
{
72127
switch ($action)

updater/tests/update-dependency.Tests.ps1

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,104 @@ switch ($action)
247247
}
248248
}
249249
}
250+
251+
Context 'cmake-file' {
252+
It 'works with FetchContent_Declare' {
253+
$testFile = "$testDir/test.cmake"
254+
@(
255+
'FetchContent_Declare(',
256+
' sentry-native',
257+
" GIT_REPOSITORY $repoUrl",
258+
' GIT_TAG none # 0.9.1',
259+
' GIT_SHALLOW FALSE',
260+
' GIT_SUBMODULES "external/breakpad"',
261+
')'
262+
) | Out-File $testFile
263+
UpdateDependency $testFile
264+
$content = Get-Content $testFile
265+
$content[3] | Should -Match "GIT_TAG $currentVersion"
266+
$content[3] | Should -Match '# 0.9.1' # Comment should be preserved
267+
}
268+
269+
It 'works with CMakeLists.txt' {
270+
$testFile = "$testDir/CMakeLists.txt"
271+
@(
272+
'cmake_minimum_required(VERSION 3.10)',
273+
'FetchContent_Declare(',
274+
' some-dependency',
275+
" GIT_REPOSITORY $repoUrl",
276+
' GIT_TAG v0.0.1',
277+
')'
278+
) | Out-File $testFile
279+
UpdateDependency $testFile
280+
$content = Get-Content $testFile
281+
$content[4] | Should -Match "GIT_TAG $currentVersion"
282+
}
283+
284+
It 'handles indented GIT_TAG' {
285+
$testFile = "$testDir/test.cmake"
286+
@(
287+
'FetchContent_Declare(dependency',
288+
" GIT_REPOSITORY $repoUrl",
289+
' GIT_TAG old_version',
290+
')'
291+
) | Out-File $testFile
292+
UpdateDependency $testFile
293+
$content = Get-Content $testFile
294+
$content[2] | Should -Match "^\s+GIT_TAG $currentVersion"
295+
}
296+
297+
It 'version pattern match' {
298+
$testFile = "$testDir/test.cmake"
299+
$repo = 'https://github.com/getsentry/sentry-cli'
300+
@(
301+
'FetchContent_Declare(',
302+
' sentry-cli',
303+
" GIT_REPOSITORY $repo",
304+
' GIT_TAG 0.1.0',
305+
')'
306+
) | Out-File $testFile
307+
UpdateDependency $testFile '^0\.'
308+
$content = Get-Content $testFile
309+
$content[3] | Should -Match 'GIT_TAG 0.28.0'
310+
}
311+
312+
It 'fails when GIT_TAG is missing' {
313+
$testFile = "$testDir/test.cmake"
314+
@(
315+
'FetchContent_Declare(',
316+
' dependency',
317+
" GIT_REPOSITORY $repoUrl",
318+
')'
319+
) | Out-File $testFile
320+
{ UpdateDependency $testFile } | Should -Throw '*Could not find GIT_TAG*'
321+
}
322+
323+
It 'fails when GIT_REPOSITORY is missing' {
324+
$testFile = "$testDir/test.cmake"
325+
@(
326+
'FetchContent_Declare(',
327+
' dependency',
328+
' GIT_TAG v1.0.0',
329+
')'
330+
) | Out-File $testFile
331+
{ UpdateDependency $testFile } | Should -Throw '*Could not find GIT_REPOSITORY*'
332+
}
333+
334+
It 'detects FetchContent_Declare in any file' {
335+
$testFile = "$testDir/dependency.txt"
336+
@(
337+
'Some text before',
338+
'FetchContent_Declare(',
339+
' dependency',
340+
" GIT_REPOSITORY $repoUrl",
341+
' GIT_TAG v0.0.1',
342+
')',
343+
'Some text after'
344+
) | Out-File $testFile
345+
UpdateDependency $testFile
346+
$content = Get-Content $testFile
347+
$content[4] | Should -Match "GIT_TAG $currentVersion"
348+
}
349+
}
250350
}

0 commit comments

Comments
 (0)