Skip to content

Instantly share code, notes, and snippets.

@mhudasch
Last active August 5, 2021 14:50
Show Gist options
  • Save mhudasch/ab05a4ef1ff2930d0c219443c7560785 to your computer and use it in GitHub Desktop.
Save mhudasch/ab05a4ef1ff2930d0c219443c7560785 to your computer and use it in GitHub Desktop.
Extract all license infomation of all used nuget packages
$lic = New-Item -ItemType Directory -Name "lic" -Force -ErrorAction "Stop";
$projects = @( Get-Project -All | ? { $_.ProjectName } | % { Get-Package -ProjectName $_.ProjectName } );
$deps = $projects | %{ $_ | Add-Member -MemberType "NoteProperty" -Value "$($_.Id)@$($_.Version)" -Name "Key"; $_ } | Sort -Property "Key" -Unique;
$deps | %{ $pkg = $_; if($null -eq $pkg.LicenseUrl) { Set-Content -Path ($lic.FullName + "\no_license_" + $pkg.Id + ".txt") -Value "NO-LICENCE" } }
$loaded = $deps | %{ $pkg = $_; if($null -ne $pkg.LicenseUrl) { Try { $extMatch = [regex]::Match($pkg.LicenseUrl, '^.*?(\.html?|\.txt)$'); if($extMatch.Success){$ext = $extMatch.Groups[1].Value;} else {$ext = ".html"} $cnt = (New-Object System.Net.WebClient).DownloadString($pkg.LicenseUrl); Write-Host "$($pkg.LicenseUrl) downloaded for $($pkg.Id)."; @{ PKg = $pkg; Content = $cnt; } | Write-Output } Catch [system.exception] { Write-Host "Could not download license for $($pkg.Id) -> '$($pkg.LicenseUrl)'" } } }
$loaded | %{ $pkg = $_["Pkg"]; $cnt = $_["Content"]; $l = if($cnt -cmatch "Apache"){"apache_"}elseif($cnt -cmatch "MIT"){"mit_"}elseif($cnt -cmatch "GPL"){"gpl_"}else{"unknown_"} $ext = if($cnt -imatch "<html"){".html"}else{".txt"} $p = ($lic.FullName + "\" + $l + $pkg.Id + $ext); Set-Content $p -Value $cnt; }
@mhudasch
Copy link
Author

Save file with extension as used in the url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment