diff --git a/.github/workflows/update-carter-package-references.yml b/.github/workflows/update-carter-package-references.yml new file mode 100644 index 0000000..949cbf3 --- /dev/null +++ b/.github/workflows/update-carter-package-references.yml @@ -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 diff --git a/template/content/CarterTemplate.csproj b/template/content/CarterTemplate.csproj index c6f85b5..082ee52 100644 --- a/template/content/CarterTemplate.csproj +++ b/template/content/CarterTemplate.csproj @@ -1,4 +1,4 @@ - + net7.0 CarterTemplate diff --git a/update-carter-package-dependencies.ps1 b/update-carter-package-dependencies.ps1 new file mode 100644 index 0000000..187a8f9 --- /dev/null +++ b/update-carter-package-dependencies.ps1 @@ -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 = '(?"\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