-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage-downloader.psm1
222 lines (190 loc) · 7.41 KB
/
package-downloader.psm1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
# Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
[cmdletbinding()]
param()
$global:PkgDownloaderSettings = New-Object PSObject -Property @{
DefaultToolsDir = "$env:LOCALAPPDATA\Microsoft\Web Tools\Publish\package-downloader\"
NuGetDownloadUrl = 'http://nuget.org/nuget.exe'
}
<#
.SYNOPSIS
This will return nuget from the $toolsDir. If it is not there then it
will automatically be downloaded before the call completes.
#>
function Get-Nuget{
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\Microsoft\Web Tools\Publish\tools\"),
$nugetDownloadUrl = $global:PkgDownloaderSettings.NuGetDownloadUrl
)
process{
if(!(Test-Path $toolsDir)){
New-Item -Path $toolsDir -ItemType Directory | out-null
}
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
$nugetDir = ([System.IO.Path]::GetDirectoryName($nugetDestPath))
if(!(Test-Path $nugetDir)){
New-Item -Path $nugetDir -ItemType Directory | Out-Null
}
'Downloading nuget.exe' | Write-Verbose
(New-Object System.Net.WebClient).DownloadFile($nugetDownloadUrl, $nugetDestPath) | Out-Null
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
}
}
# return the path of the file
$nugetDestPath
}
}
function Execute-CommandString{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[string[]]$command,
[switch]
$ignoreExitCode
)
process{
foreach($cmdToExec in $command){
'Executing command [{0}]' -f $cmdToExec | Write-Verbose
cmd.exe /D /C $cmdToExec
if(-not $ignoreExitCode -and ($LASTEXITCODE -ne 0)){
$msg = ('The command [{0}] exited with code [{1}]' -f $cmdToExec, $LASTEXITCODE)
throw $msg
}
}
}
}
<#
.SYNOPSIS
This will return the path to where the given NuGet package is installed
under %localappdata%. If the package is not found then empty/null is returned.
#>
function Get-PackageDownloaderInstallPath{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
$name,
[Parameter(Mandatory=$true,Position=1)] # later we can make this optional
$version,
[Parameter(Position=2)]
$toolsDir = $global:PkgDownloaderSettings.DefaultToolsDir
)
process{
$pathToFoundPkgFolder = $null
$toolsDir=(get-item $toolsDir).FullName
$expectedNuGetPkgFolder = ((Get-Item -Path (join-path $toolsDir (('{0}.{1}' -f $name, $version))) -ErrorAction SilentlyContinue))
if($expectedNuGetPkgFolder){
$pathToFoundPkgFolder = $expectedNuGetPkgFolder.FullName
}
$pathToFoundPkgFolder
}
}
<#
.SYNOPSIS
This will return the path to where the given NuGet package is installed.
#>
function Enable-PackageDownloader{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
$name,
[Parameter(Mandatory=$true,Position=1)] # later we can make this optional
$version,
[Parameter(Position=2)]
$toolsDir = $global:PkgDownloaderSettings.DefaultToolsDir,
[Parameter(Position=3)]
[string]$nugetUrl = $null
)
process{
if(!(Test-Path $toolsDir)){
New-Item -Path $toolsDir -ItemType Directory | out-null
}
$toolsDir = (Get-Item $toolsDir).FullName.TrimEnd('\')
# if it's already installed just return the path
$installPath = (Get-PackageDownloaderInstallPath -name $name -version $version -toolsDir $toolsDir)
if(!$installPath){
# install the nuget package and then return the path
$outdir = (get-item (Resolve-Path $toolsDir)).FullName.TrimEnd("\") # nuget.exe doesn't work well with trailing slash
# set working directory to avoid needing to specify OutputDiretory, having issues with spaces
Push-Location | Out-Null
Set-Location $outdir | Out-Null
$cmdArgs = @('install',$name,'-Version',$version,'-prerelease')
if($nugetUrl -and !([string]::IsNullOrWhiteSpace($nugetUrl))){
$cmdArgs += "-source"
$cmdArgs += $nugetUrl
}
$nugetCommand = ('"{0}" {1}' -f (Get-Nuget -toolsDir $outdir), ($cmdArgs -join ' ' ))
'Calling nuget to install a package with the following args. [{0}]' -f $nugetCommand | Write-Verbose
Execute-CommandString -command $nugetCommand | Out-Null
Pop-Location | Out-Null
$installPath = (Get-PackageDownloaderInstallPath -name $name -version $version -toolsDir $toolsDir)
}
# it should be set by now so throw if not
if(!$installPath){
throw ('Unable to restore nuget package. [name={0},version={1},toolsDir={2}]' -f $name, $version, $toolsDir)
}
$installPath
}
}
<#
This will ensure that the given module is imported into the PS session. If not then
it will be imported from %localappdata%. The package will be restored using
Enable-PackageDownloader.
This function assumes that the name of the PS module is the name of the .psm1 file
and that file is in the tools\ folder in the NuGet package.
.EXAMPLE
Enable-NuGetModule -name 'publish-module' -version '1.0.2-beta2'
.EXAMPLE
Enable-NuGetModule -name 'publish-module-blob' -version '1.0.2-beta2'
#>
# For now this function has to be declared directly in the publish
# .ps1 file, not sure why.
function Enable-NuGetModule{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
$name,
$moduleFileName,
[Parameter(Mandatory=$true,Position=1)] # later we can make this optional
$version,
[Parameter(Position=2)]
$toolsDir = $global:PkgDownloaderSettings.DefaultToolsDir,
[Parameter(Position=3)]
$nugetUrl = $null
)
process{
if(!(get-module $name)){
$installDir = Enable-PackageDownloader -name $name -version $version -nugetUrl $nugetUrl
if(!$moduleFileName){$moduleFileName = $name}
$moduleFile = (join-path $installDir ("tools\{0}.psm1" -f $moduleFileName))
'Loading module from [{0}]' -f $moduleFile | Write-Verbose
Import-Module $moduleFile -DisableNameChecking -Global -Force
}
else{
'module [{0}] is already loaded skipping' -f $name | Write-Verbose
}
}
}
function Get-LatestVersionForPackageDownloader{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
$name,
[switch]$prerelease,
[Parameter(Position=1)]
$toolsDir = $global:PkgDownloaderSettings.DefaultToolsDir
)
process{
$nugetArgs = @('list',$name)
if($prerelease){
$nugetArgs += '-prerelease'
}
'Getting pack versions for [{0}]' -f $name | Write-Verbose
'Calling nuget with the following args [{0}]' -f ($nugetArgs -join ' ') | Write-Verbose
&(Get-Nuget) $nugetArgs | where{$_.StartsWith('{0} ' -f $name)} |sort
}
}
Export-ModuleMember -function *