-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
update-carter-package-dependencies.ps1
86 lines (70 loc) · 2.51 KB
/
update-carter-package-dependencies.ps1
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
[CmdletBinding()]
param(
[Parameter()]
[switch]
$IncludePrerelease
)
function Get-LatestCarterPackageVersion {
[CmdletBinding()]
param(
[Parameter()]
[switch]
$IncludePrerelease
)
#
# NuGet Server API docs
# https://learn.microsoft.com/en-us/nuget/api/overview
#
$nugetApiServiceIndexUrl = 'https://api.nuget.org/v3/index.json'
$serviceIndexResponse = Invoke-RestMethod -Method Get -Uri $nugetApiServiceIndexUrl
$nugetApiSearchQueryServiceUrl = $serviceIndexResponse.resources |
Where-Object '@type' -eq 'SearchQueryService' |
Select-Object -First 1 |
Select-Object -ExpandProperty '@id'
#
# https://learn.microsoft.com/en-us/nuget/api/search-query-service-resource#search-for-packages
#
$queryParameters = @{
q = 'Carter'
prerelease = if ($IncludePrerelease) { 'true' } else { 'false' }
semVerLevel = '2.0.0'
}
$searchResponse = Invoke-RestMethod `
-Method Get `
-Uri $nugetApiSearchQueryServiceUrl `
-Body $queryParameters
$latestPackage = $searchResponse.data |
Where-Object 'id' -eq 'Carter' |
Sort-Object -Property 'version' -Descending |
Select-Object -First 1
Write-Output -InputObject $latestPackage.version
}
function Update-CarterPackageReferences {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]
$RootPath,
[Parameter(Mandatory = $true)]
[string]
$PackageVersion
)
$projectFiles = Get-ChildItem `
-Path $RootPath `
-Recurse `
-File `
-Filter '*.csproj'
$regexPattern = '(?<beforeVersion><PackageReference\s+Include="Carter"\s+Version=")[\s\S]+?(?<afterVersion>"\s*/>)'
foreach ($projectFile in $projectFiles) {
$contents = Get-Content -Path $projectFile.FullName -Raw -Encoding utf8
if ([System.Text.RegularExpressions.Regex]::IsMatch($contents, $regexPattern)) {
$newContents = [System.Text.RegularExpressions.Regex]::Replace(
$contents,
$regexPattern,
('${{beforeVersion}}{0}${{afterVersion}}' -f $PackageVersion))
Set-Content -Path $projectFile.FullName -Value $newContents -Encoding utf8 -NoNewline
}
}
}
$targetCarterPackageVersion = Get-LatestCarterPackageVersion -IncludePrerelease:$IncludePrerelease
Update-CarterPackageReferences -RootPath $PSScriptRoot -PackageVersion $targetCarterPackageVersion