-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Save-NuGetTool.ps1
51 lines (41 loc) · 1.37 KB
/
Save-NuGetTool.ps1
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
<#
.Synopsis
Downloads a NuGet package and extracts /tools.
Author: Roman Kuzmin
.Description
The command downloads "PackageId.zip" from the NuGet Gallery to the current
location and extracts "/tools" as the directory "PackageId". If these items
exist remove them manually or use another location. "PackageId.zip" is
removed after successful unzipping.
.Parameter PackageId
Specifies the package ID.
.Link
https://github.com/nightroman/PowerShelf
#>
param([Parameter()]$PackageId)
$ErrorActionPreference = 'Stop'
$here = $PSCmdlet.GetUnresolvedProviderPathFromPSPath('')
$zip = "$here\$PackageId.zip"
$dir = "$here\$PackageId"
if (Test-Path -LiteralPath $zip) {Write-Error "Remove '$zip' or use another directory."}
if (Test-Path -LiteralPath $dir) {Write-Error "Remove '$dir' or use another directory."}
$web = New-Object System.Net.WebClient
$web.UseDefaultCredentials = $true
try {
$web.DownloadFile("http://nuget.org/api/v2/package/$PackageId", $zip)
}
catch {
Write-Error "Cannot download the package : $_"
}
$shell = New-Object -ComObject Shell.Application
$from = $shell.NameSpace("$zip\tools")
if (!$from) {
Write-Error "Missing package item '$zip\tools'."
}
$one, $two = @($from.Items())
if (!$two -and $one.Name -eq $PackageId) {
$from = $shell.NameSpace($one.Path)
}
$null = mkdir $dir
$shell.NameSpace($dir).CopyHere($from.Items(), 4)
Remove-Item -LiteralPath $zip