Skip to content

Commit

Permalink
Add workflow to automate updating references to Carter NuGet package (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mderriey authored and jchannon committed Nov 21, 2022
1 parent 251aec4 commit ac53ac2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
47 changes: 47 additions & 0 deletions .github/workflows/update-carter-package-references.yml
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 jonathan.channon@gmail.com
git add .
git commit -m "Update Carter NuGet package references"
git push
fi
2 changes: 1 addition & 1 deletion template/content/CarterTemplate.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>CarterTemplate</AssemblyName>
Expand Down
86 changes: 86 additions & 0 deletions update-carter-package-dependencies.ps1
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

0 comments on commit ac53ac2

Please sign in to comment.