-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAdd-NugetPackage.ps1
More file actions
59 lines (53 loc) · 2.18 KB
/
Copy pathAdd-NugetPackage.ps1
File metadata and controls
59 lines (53 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<#
.SYNOPSIS
Loads a NuGet package DLL, downloading as needed.
.NOTES
Install-Package isn't working for arbitrary NuGet packages, so this allows us access the main DLL
assembly and types within the package.
.FUNCTIONALITY
PowerShell
.LINK
https://www.nuget.org/
.EXAMPLE
Add-NugetPackage.ps1 Serilog ; [Serilog.Core.Logger]::None -is [Serilog.ILogger]
True
#>
#Requires -Version 7
[CmdletBinding()] Param(
# The name of the NuGet package to load.
[Parameter(Position=0,Mandatory=$true)][string] $PackageName,
# Use this type name to test whether the package was loaded.
[Parameter(Position=1)][string] $TypeName
)
Begin
{
$Script:BaseDir = Join-Path ($IsWindows ? $env:LOCALAPPDATA : ($env:XDG_CACHE_HOME ?? "$(Resolve-Path ~/.cache)")) PSNuget
$Script:BinDir = Join-Path $BaseDir bin
$Script:LibDir = Join-Path $BaseDir lib
if(!(Test-Path $BinDir -Type Container)) {New-Item $BinDir -Type Directory |Out-Null}
if(!(Test-Path $LibDir -Type Container)) {New-Item $LibDir -Type Directory |Out-Null}
}
Process
{
try {[void][type]$TypeName; return} catch {}
$dll = Join-Path $BinDir "$PackageName.dll"
if(([System.AppDomain]::CurrentDomain.GetAssemblies() |Where-Object Location -eq $dll))
{
try {[void][type]$TypeName} catch {Write-Warning "'$PackageName' was loaded, but type '$TypeName' was not found: $_"}
return
}
if(Test-Path $dll -Type Leaf)
{
Add-Type -Path $dll
try {[void][type]$TypeName} catch {Write-Warning "'$PackageName' was found and loaded, but type '$TypeName' was not found: $_"}
return
}
$nuget = Join-Path $LibDir "$PackageName.nuget"
if(!(Test-Path $nuget -Type Leaf)) {Invoke-WebRequest "https://www.nuget.org/api/v2/package/$PackageName" -OutFile $nuget}
$dir = Join-Path $LibDir $PackageName
if(!(Test-Path $dir -Type Container)) {Expand-Archive -Path $nuget -DestinationPath $dir}
$libdll = Get-ChildItem $dir -Filter *.dll -Recurse |Select-Object -Last 1
New-Item -Type HardLink -Path $dll -Value $libdll |Out-Null
Add-Type -Path $dll
try {[void][type]$TypeName} catch {Write-Warning "Unable to find type '$TypeName': $_"}
}