NuGet

279 次浏览

获取项目中全部 NuGet 包以及对应的 License

我们在日常 .NET Core 项目开发的过程中,经常需要获得整个解决方案中所有项目所引用的 NuGet 包的列表、对应的 License,甚至有时候出于法律的考量,我们需要检查 NuGet 包中所有递归引用的内部包的 License。

如果我们手动进行检查,工作量是巨大的,因此我编写了一个 PowerShell 脚本,来自动化完成这个工作。

基本原理

首先,基于 dotnet 命令自带的 list package 方法,及 –include-transitive 参数,来获取全部包引用的列表。
随后,通过 WebClient 下载对应的 License 文件。

使用方法

将 .ps1 文件放置到项目解决方案文件 (.sln) 所在位置并运行即可。

*

In the process of daily .NET Core project development, we often need to obtain a list of NuGet packages referenced by all projects in the solution, corresponding licenses, and sometimes for legal considerations, we need to check all licenses of recursion internal packages in each directly referenced NuGet packages.

If we check it manually, the workload is huge, so I wrote a PowerShell script to automate this task.

Fundamental

It’s based on the list package method that comes with the dotnet command, and the –include-transitive parameter, to obtain a list of all package references. And then, download the corresponding license file through WebClient.

Instructions

Place the .ps1 file in the location of the project solution file (.sln) and run it.

*

# Place the file to .sln folder, and run

dotnet list package --include-transitive > NuGetPackageList.txt

mkdir ".\licenses" > nul 2> nul
echo "Package ID,Package Version,Package Spec URL,License URL" > LicenseList.csv

Select-String "(>)\s+(\S+)\s+(\S+)\s+(\S+)*" NuGetPackageList.txt  |  Select @{Name="Id"; Expression = {$_.Matches.Groups[2]}}, @{Name="Version"; Expression = {$_.Matches.Groups[3]}}  |  Sort-Object -Property @{Expression="Id"; Descending=$false}, @{Expression="Version"; Descending=$false} -unique | % {

    $packageId = $_.Id;
    $packageIdLowerCase = $packageId.ToString().ToLower();
    $packageVersion = $_.Version;
    $packageSpecUrl = "https://api.nuget.org/v3-flatcontainer/$packageIdLowerCase/$packageVersion/$packageIdLowerCase.nuspec"

    Write-Host
    Write-Host("o> $packageId")
    Write-Host("  $packageVersion`n  $packageSpecUrl") -ForegroundColor Green 

    Try {  
        # Refer to https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-manifest-nuspec
        [xml]$packageSpec = (New-Object System.Net.WebClient).DownloadString($packageSpecUrl);
        $packageLicenseUrl = $packageSpec.package.metadata.licenseUrl;

        Write-Host("  $packageLicenseUrl") -ForegroundColor Green 
        echo "$packageId,$packageVersion,$packageSpecUrl,$packageLicenseUrl" >> LicenseList.csv
        
        Try {
            $filePath = (Join-Path (pwd) 'licenses\') + "$packageId.$packageVersion.txt";
            (New-Object System.Net.WebClient).DownloadFile($packageLicenseUrl, $filePath);
        }
        Catch [system.exception] {
            # Write-Host ($error[0].Exception);
            Write-Host ("!!> Could not download license file for $packageId $packageVersion") -ForegroundColor DarkMagenta
        }
    }
    Catch [system.exception] {
        # Write-Host ($error[0].Exception);
        Write-Host ("!!> Could not read license for $packageId $packageVersion") -ForegroundColor Red
        echo "$packageId,$packageVersion,$packageSpecUrl,(none)" >> LicenseList.csv
    }

}
2,756 次浏览

.NET Standard / Core 项目发布 NuGet 包并使包中的配置文件自动生成到 Bin 下

之前在如何创建 NuGet 包的文章中,曾经提到如何将配置文件(文件夹)包含到 NuGet 包中,并在用户安装了这个 NuGet 包并 build 自己工程后,自动将 Config 文件夹复制到 Bin 下。但是当时我并没有找到 .NET Standard 或 .NET Core 项目的配置方法,因此 Squirrel Framework 只能选择通过 .NET Framework 4.6.2 项目形式生成 NuGet 包(.NET Framework 下 NuGet 的配置方式请查看这篇文章《如何在 NuGet 发布自己的类库包》)。

不过最近有时间,通过半天的尝试终于将 4.6.2 的工程切换到 .NET Standard,这次的关键点在于不必自己去写 .nuspec 配置文件之后通过 dotnet pack 命令进行 build,而是改为直接通过编辑项目文件(.csproj)并通过 Visual Studio 在每次 build 时,自动完成 NuGet 包的生成。具体配置步骤如下:

Continue reading

3,821 次浏览

如何在 NuGet 发布自己的类库包

我们可以将自己实现的类库发布到 NuGet 上,方便在未来开发应用程序时,对已有代码或结构进行复用。以下是在 NuGet 发布自己类库的一个简单介绍。

  1. 首先,我们需要到 nuget.org 注册并 获取 API key
  2. 随后,下载 NuGet 命令行工具,建议将 nuget.exe 注册到系统的环境变量中,方便后续步骤的操作
    Continue reading