Automatically create list of nuget packages and li

2019-07-11 07:05发布

Is there any way to get an automatically updated list of all used nuget packages in my solution, including a link to the corresponding license, which I can display within my app?

Running the following from Package Manager Console within Visual Studio gives me the required information:

Get-Project | Get-Package | select Id, Version, LicenseUrl 

How to get this list a) automatically updated on each change and b) get it into my app?

Target is to have an Info/About dialog showing all this data.

2条回答
淡お忘
2楼-- · 2019-07-11 07:22

I found one way, pretty sure it has some limitations...

I'm calling this in the pre-build event:

powershell.exe -ExecutionPolicy Bypass -File $(ProjectDir)\Resources\tools\PreBuildScript.ps1  $(ProjectDir) $(SolutionDir) $(TargetDir)

And here is how resources\tools\PreBuildScript.ps1 looks like:

param (
    [Parameter(Mandatory=$True)]
    [string]$ProjectDir,
    [Parameter(Mandatory=$True)]
    [string]$SolutionDir,
    [Parameter(Mandatory=$True)]
    [string]$TargetDir
)
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
$nupkgs = Get-ChildItem -Recurse -Filter *.nupkg -Path "$SolutionDir\packages" 
$nuspecs = $nupkgs | %{ [IO.Compression.ZipFile]::OpenRead($_.FullName).Entries | where {$_.Fullname.EndsWith('.nuspec')} } 
$metadata = $nuspecs | %{ 
    ([xml]([System.IO.StreamReader]$_.Open()).ReadToEnd()) | %{New-Object PSObject -Property @{
        Version = $_.package.metadata.version
        Authors = $_.package.metadata.authors 
        Title =  IF ([string]::IsNullOrWhitespace($_.package.metadata.title)){$_.package.metadata.id} else {$_.package.metadata.title}
        LicenseUrl  = $_.package.metadata.licenseUrl
    }}
} 
$metadata | %{ '{0} {1}{4}Autor(en): {2}{4}Lizenz: {3}{4}{4}' -f $_.Title, $_.Version, $_.Authors, $_.LicenseUrl, [Environment]::NewLine } | Out-File "$ProjectDir\Resources\ThirdPartyLicenseOverview.txt"

This gives me an (ugly) textfile Resources\ThirdPartyLicenseOverview.txt that I can include as embedded resource to use it within my app.

Not the final solution but one step on the way...

查看更多
萌系小妹纸
3楼-- · 2019-07-11 07:36

Is there any way to get an automatically updated list of all used nuget packages in my solution, including a link to the corresponding license.

As far as I am aware there is nothing currently available to get the list automatically updated on each change and get it into app.

We could not get the license information directly from the command line as part of a CI build, need to create an application to open the .nupkg zip file, extract the license url from the .nuspec file and download the license from this url.

Alternatively, you could use the package manager console window inside Visual Studio and with a bit of PowerShell download the license files. But if you want get it into your app, you could not use the package manager console, in this case you could not get the licenses.

Besides, we could use PowerShell script to get the package Id Version and download the license files, but we still need require someone to run the script to get the Id, version and download the licenses. If you still want to automatically updated on each change, you need use PowerShell to monitor the package.config. The PowerShell script should be invoked and executed automatically after any change in the package, but it will be difficult to achieve.

查看更多
登录 后发表回答