Created
September 17, 2019 14:46
-
-
Save fqborges/5c5d3bac1449590874ebc5ec935e46fb to your computer and use it in GitHub Desktop.
Publish nuget packages dlls into a directory to be used by powershell 6 (core) scripts
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
| param ( | |
| [string]$Name = $(throw "-name is required."), | |
| [string]$Destination = './packages', | |
| [string]$Source = './package-cache' | |
| ) | |
| $TargetFramework = ".NETStandard2.0" | |
| function Get-Nuspec { | |
| param( $package ) | |
| $zip = [System.IO.Compression.ZipFile]::OpenRead($package.Source) | |
| $reader = [System.IO.StreamReader]::new($zip.GetEntry("$($package.name).nuspec").Open()) | |
| $nuspec = $reader.ReadToEnd() | |
| $reader.Close() | |
| $zip.Dispose() | |
| return $nuspec | |
| } | |
| function Export-NuspecDependencies { | |
| param( $nuspec ) | |
| $xml = [xml] $nuspec; | |
| $depsGroups = $xml.package.metadata.dependencies.group; | |
| $netstd2 = $depsGroups | Where-Object { $_.targetFramework -eq $TargetFramework } | |
| $group = $netstd2 | |
| $deps = @() | |
| if ($null -ne $group.dependency ) { | |
| $group.dependency | ForEach-Object { | |
| $deps += $_.id; | |
| } | |
| } | |
| return $deps | |
| } | |
| function Get-NugetPackageDependencies { | |
| param( $name ) | |
| $deps = @() | |
| $pendingDeps = @($name) | |
| While ( $pendingDeps.Length) { | |
| $next, $pendingDeps = $pendingDeps | |
| if (-not ($deps.Contains($next))) { | |
| $pkg = Get-Package -Name $next -ProviderName "Nuget" | |
| $deps += $pkg | |
| $nuspec = Get-Nuspec($pkg) | |
| $pendingDeps = $pendingDeps + (Export-NuspecDependencies($nuspec)) | |
| } | |
| } | |
| return $deps | |
| } | |
| function Export-PackageDll { | |
| param( $Name, $Destination, $Source ) | |
| if(-not (Test-Path $Destination)){ | |
| mkdir -p $Destination | |
| } | |
| $dependencies = Get-NugetPackageDependencies($Name) | |
| $dependencies | ForEach-Object { | |
| $pkg = $_ | |
| Copy-Item ` | |
| -Path "$Source/$($pkg.Name).$($pkg.Version)/lib/netstandard2.0/*.dll" ` | |
| -Destination $Destination ` | |
| -Recurse ` | |
| -Verbose | |
| } | |
| } | |
| Register-PackageSource -Location https://www.nuget.org/api/v2 -Name Nuget -ProviderName Nuget -Trusted -force | |
| Install-Package -Name $Name -ProviderName Nuget -Destination $Source | |
| Export-PackageDll -Name $Name -Destination $Destination -Source $Source | |
| Write-Host "Package $Name published successfully, import using the command bellow: " | |
| Write-Host "Add-Type -path $Destination/$Name.dll" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment