-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workflow to automate updating references to Carter NuGet package (#…
…304)
- Loading branch information
Showing
3 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Update Carter NuGet package references | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
includePrerelease: | ||
type: boolean | ||
default: false | ||
description: Whether to include prerelease versions when searching for the latest version of the Carter NuGet package | ||
|
||
commitAndPush: | ||
type: boolean | ||
default: false | ||
description: Whether to commit and push the changes; Useful to test the process by looking at the resulting Git diff | ||
|
||
jobs: | ||
update-carter-package-references: | ||
name: Update Carter NuGet package references | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Update package references with latest version from NuGet | ||
run: >- | ||
./update-carter-package-dependencies.ps1 | ||
-IncludePrerelease:$${{ github.event.inputs.includePrerelease }} | ||
shell: pwsh | ||
|
||
- name: Show Git diff | ||
run: git diff | ||
|
||
# https://github.com/marketplace/actions/checkout#push-a-commit-using-the-built-in-token | ||
- if: github.event.inputs.commitAndPush | ||
name: Commit changes and push if necessary | ||
run: | | ||
GIT_CHANGES=$(git status --porcelain) | ||
if [ "$GIT_CHANGES" == "" ]; then | ||
echo "No changes to commit" | ||
else | ||
echo "Committing and pushing changes" | ||
git config user.name Jonathan Channon | ||
git config user.email [email protected] | ||
git add . | ||
git commit -m "Update Carter NuGet package references" | ||
git push | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |