-
Notifications
You must be signed in to change notification settings - Fork 39
Added a PowerShell script to validate NuGet packages #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dakanev-msft
wants to merge
1
commit into
main
Choose a base branch
from
dakanev/validate-nuget-packages-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,150 @@ | ||
| <# | ||
|
|
||
| .SYNOPSIS | ||
|
|
||
| This script validates NuGet packages for required attributes. | ||
|
|
||
| .EXAMPLE | ||
|
|
||
| Microsoft.Omex.Tools.ValidateNuGetPackages.ps1 c:\omex\nuget | ||
|
|
||
| .DESCRIPTION | ||
|
|
||
| There are several required attributes in NuGet specification files (nuspec) which are required by Microsoft | ||
| when the NuGet packages are published to NuGet.org. | ||
|
|
||
| If these attributes are missing, the packages cannot be published. | ||
|
|
||
| .NOTES | ||
|
|
||
| The script is intended to be used as a step in the pull request validation pipeline. | ||
| The script exits with code 1 if the validation failed. | ||
|
|
||
| The script verifies if the following attributes are present in the nuspec file: | ||
| - title | ||
| - description | ||
| - project url | ||
| - tags | ||
|
|
||
|
|
||
| .PARAMETER NuGetFolder | ||
| Path which contains the NuGet packages to be scanned. | ||
| The path is scanned recursively for *.nupkg files. | ||
|
|
||
| #> | ||
|
|
||
|
|
||
| param ( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$NuGetFolder | ||
| ) | ||
|
|
||
| <# | ||
| The validates the NuGet package for the required attributes | ||
| and returns false if some of them is missing. | ||
| #> | ||
| function ValidateNuGetPackage([string]$nugetPackage) { | ||
| Write-Host "Validating $nugetPackage" | ||
|
|
||
| $contents = ExtractNuSpec($nugetPackage) | ||
| $xmldoc = [xml]$contents | ||
|
|
||
| $result = $True | ||
|
|
||
| if ($xmldoc.package.metadata.title.Length -eq 0) | ||
| { | ||
| Write-Error "Title is missing." | ||
| $result = $False | ||
| } | ||
|
|
||
| if ($xmldoc.package.metadata.description.Length -eq 0) | ||
| { | ||
| Write-Error "Description is missing." | ||
| $result = $False | ||
| } | ||
|
|
||
| if ($xmldoc.package.metadata.projecturl.Length -eq 0) | ||
| { | ||
| Write-Error "ProjectUrl is missing." | ||
| $result = $False | ||
| } | ||
|
|
||
| if ($xmldoc.package.metadata.tags.Length -eq 0) | ||
| { | ||
| Write-Error "Tags is missing." | ||
| $result = $False | ||
| } | ||
|
|
||
| if ($result -eq $True) | ||
| { | ||
| Write-Host "Successfully validated NuGet package $nugetPackage" | ||
| } | ||
| else | ||
| { | ||
| Write-Error "NuGet package validation failed for $nugetPackage" | ||
| } | ||
|
|
||
| return $result | ||
| } | ||
|
|
||
| <# | ||
| The function extracts the contents of the NuGet specification file | ||
| from the NuGet archive. | ||
| #> | ||
| function ExtractNuSpec([string]$nugetPackage) | ||
| { | ||
| $zip = [io.compression.zipfile]::OpenRead($nugetPackage) | ||
| $file = $null | ||
| foreach ($entry in $zip.Entries) | ||
| { | ||
| if ($entry.FullName.EndsWith('.nuspec')) | ||
| { | ||
| $file = $entry | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if ($null -eq $file) | ||
| { | ||
| Write-Error "Unable to find NuGet specification in NuGet package $nugetPackage" | ||
| return $false | ||
| } | ||
|
|
||
| $stream = $file.Open() | ||
| $reader = New-Object IO.StreamReader($stream) | ||
| $text = $reader.ReadToEnd() | ||
|
|
||
| $reader.Close() | ||
| $stream.Close() | ||
| $zip.Dispose() | ||
|
|
||
| return $text | ||
| } | ||
|
|
||
| Add-Type -assembly "system.io.compression.filesystem" | ||
|
|
||
| Write-Host "Scanning folder $NuGetFolder" | ||
|
|
||
| #Scanning the input folder recursively to get a list of NuGet packages to validate | ||
| $nugetFiles = Get-ChildItem "$NuGetFolder\\*.nupkg" -Recurse | ||
|
|
||
| Write-Host "The following $($nugetFiles.Count) NuGet packages will be validated" | ||
| foreach ($file in $nugetFiles) | ||
| { | ||
| Write-Host $file | ||
| } | ||
|
|
||
| $result = $true | ||
| foreach ($file in $nugetFiles) | ||
| { | ||
| $currentResult = ValidateNuGetPackage $file | ||
| if ($currentResult -eq $false) | ||
| { | ||
| $result = $false | ||
| } | ||
| } | ||
|
|
||
| if ($result -eq $false) | ||
| { | ||
| exit 1 | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this script? I think we are running
dotnet packas part of PR build (or we should) so better way might be changing its severity (simular to what ADO does) to fail on missing data or other errors. Also look into https://devblogs.microsoft.com/dotnet/package-validation/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The publishing to NuGet.org fails if some attributes are missing, e.g. projecturl
I took a look at the package validation link, but I could not find a way to validate if a particular attribute is present in the nuspec file.
I agree that having this validation in the pack step is the preferred way. Do you know how we could use the pack validation to achieve this?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it's some kind of parameter that ADO pass to
dotnet packtask, but github does not, so we can just compare logs.Also if we need some custom validation I think having MSBuild target in
Directories.Build.targetswould be much simpler and it would run automatically.For example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I tried to add these conditions to Directories.Build.targets but it didn't work because these attributes were always evaluated as empty - for some reason it did not take into account attributes from csproj files.