diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 799a077..dfc7556 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,11 @@ jobs: - run: dotnet build IOViewer.slnx -c Release --no-restore - run: dotnet test IOViewer.slnx -c Release --no-build - run: dotnet pack IOViewer.slnx -c Release --no-build -o artifacts + - name: Check package dependencies + shell: pwsh + run: | + ./tools/check-packages.ps1 -PackageDirectory artifacts + ./tools/test-package-checks.ps1 - uses: actions/upload-artifact@v7 with: name: packages-${{ matrix.os }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b28f981..1cd6a01 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -134,9 +134,10 @@ Push a tag matching the package version, such as `v0.2.0`, to run `release.yml`. The workflow builds and tests the solution, publishes the NuGet packages through Trusted Publishing, and then attaches packages, symbols, source and checksums to a GitHub release, marked as a prerelease when the version has a hyphen (`v0.3.0-preview.1`). The release notes are the section of `CHANGELOG.md` for the version, so add that section before -the tag; the workflow stops before publishing when it is missing. `tools/pack-release.ps1` also stops it when the -`IOViewer` package would depend on a missing or preview IOScene lower bound, or on a dependency bound that nuget.org -refuses (a bound with metadata, or a prerelease label of one part that does not start with a letter). +the tag; the workflow stops before publishing when it is missing. CI and `tools/pack-release.ps1` both run +`tools/check-packages.ps1` on the packed `.nupkg` files. It requires `IOScene` as the library package's only dependency, +with a stable lower bound, and rejects dependency bounds that nuget.org refuses (a bound with metadata, or a +prerelease label of one part that does not start with a letter). Before the tag, run the Revit test with a real Revit 2022 file, which CI does not have: set `IOVIEWER_RVT_FILE` to its path and run `dotnet test IOViewer.slnx -c Release --filter Cli_ConvertRvtToHtml`. The NuGet policy must name this repository, `release.yml`, the configured NuGet profile and the `IOViewer*` package pattern. diff --git a/tools/check-packages.ps1 b/tools/check-packages.ps1 new file mode 100644 index 0000000..9014b98 --- /dev/null +++ b/tools/check-packages.ps1 @@ -0,0 +1,56 @@ +param( + [Parameter(Mandatory)] + [string]$PackageDirectory +) + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +Write-Host 'Checking package dependencies...' +Add-Type -AssemblyName System.IO.Compression.FileSystem +$checkedViewer = $false +foreach ($package in Get-ChildItem -LiteralPath $PackageDirectory -File -Filter '*.nupkg') { + $zip = [IO.Compression.ZipFile]::OpenRead($package.FullName) + try { + $entry = $zip.Entries | Where-Object { $_.FullName -notlike '*/*' -and $_.Name -like '*.nuspec' } | Select-Object -First 1 + if (-not $entry) { throw "$($package.Name) has no package manifest" } + $reader = [IO.StreamReader]::new($entry.Open()) + try { $nuspec = [xml]$reader.ReadToEnd() } finally { $reader.Dispose() } + } + finally { $zip.Dispose() } + + $id = $nuspec.SelectSingleNode("/*[local-name()='package']/*[local-name()='metadata']/*[local-name()='id']") + if (-not $id -or [string]::IsNullOrWhiteSpace($id.InnerText)) { throw "$($package.Name) has no package ID" } + $dependencies = @($id.ParentNode.SelectNodes("./*[local-name()='dependencies']//*[local-name()='dependency']")) + $isViewer = $id.InnerText -eq 'IOViewer' + if ($isViewer) { + $checkedViewer = $true + if ($dependencies.Count -eq 0) { throw "$($package.Name) has no dependency on IOScene" } + } + + foreach ($dependency in $dependencies) { + $dependencyId = $dependency.GetAttribute('id') + $range = $dependency.GetAttribute('version') + if ($isViewer) { + if ($dependencyId -ne 'IOScene') { + throw "$($package.Name) has an unexpected dependency '$dependencyId'; only IOScene is allowed in the library" + } + # The range disables NU5104. Keep preview IOScene contracts out of published packages. + $lower = $range.TrimStart('[', '(').Split(',')[0].Trim() + if ($lower -eq '' -or $lower.Contains('-')) { + throw "$($package.Name) depends on IOScene '$range', whose lower bound is missing or a preview; release on a published IOScene" + } + } + + # NuGetGallery ManifestValidator.ValidateDependencyVersion rejects metadata and single-part prerelease labels + # that do not start with a letter. Pack accepts both. + foreach ($bound in $range.Trim('[', ']', '(', ')').Split(',')) { + $bound = $bound.Trim() + $label = if ($bound.Contains('-')) { $bound.Substring($bound.IndexOf('-') + 1) } else { '' } + if ($bound.Contains('+') -or ($label -ne '' -and -not $label.Contains('.') -and $label -notmatch '^[a-z][0-9a-z-]*$')) { + throw "$($package.Name) depends on $dependencyId '$range', whose bound '$bound' nuget.org refuses" + } + } + } +} +if (-not $checkedViewer) { throw "No IOViewer package found in '$PackageDirectory'; its IOScene dependency was not checked" } diff --git a/tools/pack-release.ps1 b/tools/pack-release.ps1 index 96cc3d2..977f058 100644 --- a/tools/pack-release.ps1 +++ b/tools/pack-release.ps1 @@ -77,46 +77,7 @@ Write-Host "Packing NuGet packages..." Get-ChildItem -LiteralPath $Output -File | Where-Object { $_.Extension -in '.nupkg', '.snupkg' } | Remove-Item Invoke-DotNet pack (Join-Path $root 'IOViewer.slnx') -c Release --no-build -o $Output -Write-Host "Checking package dependencies..." -Add-Type -AssemblyName System.IO.Compression.FileSystem -$checkedViewer = $false -foreach ($package in Get-ChildItem -LiteralPath $Output -Filter '*.nupkg') { - $zip = [System.IO.Compression.ZipFile]::OpenRead($package.FullName) - try { - $entry = @($zip.Entries | Where-Object { $_.FullName -notlike '*/*' -and $_.Name -like '*.nuspec' })[0] - $reader = New-Object System.IO.StreamReader($entry.Open()) - try { $nuspec = [xml]$reader.ReadToEnd() } finally { $reader.Dispose() } - } - finally { $zip.Dispose() } - $dependencies = @($nuspec.SelectNodes("//*[local-name()='dependency']")) - # IOScene lower bound: replaces the NU5104 check that the IOScene range switches off: no package built on a preview contract reaches nuget.org. - if ($package.Name -eq "IOViewer.$version.nupkg") { - $checkedViewer = $true - $scene = @($dependencies | Where-Object { $_.GetAttribute('id') -eq 'IOScene' }) - if ($scene.Count -eq 0) { throw "$($package.Name) has no dependency on IOScene" } - foreach ($dependency in $scene) { - $range = $dependency.GetAttribute('version') - $lower = $range.TrimStart('[', '(').Split(',')[0].Trim() - if ($lower -eq '' -or $lower.Contains('-')) { - throw "$($package.Name) depends on IOScene '$range', whose lower bound is missing or a preview; release on a published IOScene" - } - } - } - # Bounds nuget.org accepts: nuget.org refuses a dependency bound with metadata, or one whose prerelease label has one part and does not start - # with a letter (NuGetGallery ManifestValidator.ValidateDependencyVersion); dotnet pack accepts both, so only the push would fail. - foreach ($dependency in $dependencies) { - $range = $dependency.GetAttribute('version') - foreach ($bound in $range.Trim('[', ']', '(', ')').Split(',')) { - $bound = $bound.Trim() - $label = if ($bound.Contains('-')) { $bound.Substring($bound.IndexOf('-') + 1) } else { '' } - if ($bound.Contains('+') -or ($label -ne '' -and -not $label.Contains('.') -and $label -notmatch '^[a-z][0-9a-z-]*$')) { - throw "$($package.Name) depends on $($dependency.GetAttribute('id')) '$range', whose bound '$bound' nuget.org refuses" - } - } - } -} -# NuGet names the package by the normalized version; a Version written otherwise would skip the IOScene lower-bound check without this. -if (-not $checkedViewer) { throw "IOViewer.$version.nupkg was not found in '$Output'; its IOScene dependency was not checked" } +& (Join-Path $PSScriptRoot 'check-packages.ps1') -PackageDirectory $Output # 6. Checksums Write-Host "Calculating SHA-256 sums..." diff --git a/tools/test-package-checks.ps1 b/tools/test-package-checks.ps1 new file mode 100644 index 0000000..9c7bca4 --- /dev/null +++ b/tools/test-package-checks.ps1 @@ -0,0 +1,52 @@ +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +Add-Type -AssemblyName System.IO.Compression.FileSystem +$stableRange = '[0.1.0, 0.2.0-0.0)' +$cases = @( + @{ Range = $stableRange; Extra = ''; Error = $null } + @{ Range = $stableRange; Extra = ''; Error = "unexpected dependency 'RvtIO.Scene'" } + @{ Range = '[0.1.1-preview.1, 0.2.0-0.0)'; Extra = ''; Error = 'lower bound is missing or a preview' } + @{ Range = '[0.1.0, 0.2.0-0)'; Extra = ''; Error = "whose bound '0.2.0-0' nuget.org refuses" } +) + +$fixtures = [IO.Directory]::CreateTempSubdirectory('ioviewer-package-checks-').FullName +$package = Join-Path $fixtures 'IOViewer.1.0.0.nupkg' +try { + foreach ($case in $cases) { + $archive = [IO.Compression.ZipFile]::Open($package, [IO.Compression.ZipArchiveMode]::Create) + try { + $writer = [IO.StreamWriter]::new($archive.CreateEntry('IOViewer.nuspec').Open()) + try { + $writer.Write(@" + + + IOViewer + 1.0.0 + + + $($case.Extra) + + + +"@) + } finally { $writer.Dispose() } + } finally { $archive.Dispose() } + + $message = $null + try { & (Join-Path $PSScriptRoot 'check-packages.ps1') -PackageDirectory $fixtures } + catch { $message = $_.Exception.Message } + if ($case.Error) { + if ($message -notlike "*$($case.Error)*") { throw "Expected '$($case.Error)', got '$message'" } + } elseif ($message) { + throw "The valid package failed: $message" + } + + [IO.File]::Delete($package) + } +} +finally { + [IO.File]::Delete($package) + [IO.Directory]::Delete($fixtures) +} +Write-Host "Passed $($cases.Count) package check cases."