PSDependScripts/Nuget.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 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 |
<# .SYNOPSIS Installs a package from a Nuget repository like Nuget.org using nuget.exe .DESCRIPTION Installs a package from a Nuget repository like Nuget.org using nuget.exe Relevant Dependency metadata: Name: The name of the package Version: Used to identify existing installs meeting this criteria. Defaults to 'latest' Source: Source Uri for Nuget. Defaults to https://www.nuget.org/api/v2/ Target: Required path to save this module. No Default Example: To install PSDeploy to C:\temp\PSDeploy, I would specify C:\temp .PARAMETER Force If specified and Target already exists, remove existing item before saving .PARAMETER PSDependAction Test, or Install the package. Defaults to Install Test: Return true or false on whether the dependency is in place Install: Install the dependency .EXAMPLE @{ 'Newtonsoft.Json' = @{ DependencyType = 'Nuget' Target = 'C:\Temp' Version = '12.0.2' } } # Install Newtonsoft.Json via Nuget.org, to C:\temp, at version 12.0.2 .EXAMPLE @{ 'MyCompany.Models' = @{ DependencyType = 'Nuget' Source = 'https://nuget.int.feed/' Target = 'C:\Temp' } } # Install the latest version of MyCompany.Models on an internal nuget feed, to C:\temp .EXAMPLE @{ PSDependOptions = @{ DependencyType = 'Nuget' Target = ".\Staging" } 'Portable.BouncyCastle' = @{ Version = 'latest' Parameters = @{ Name = 'BouncyCastle.Crypto' } } 'MimeKit' = 'latest' 'Newtonsoft.Json' = 'latest' } # Installs the list of Nuget packages from Nuget.org using the Global PSDependOptions to limit repetition. Packages will be downloaded to the Staging directory in the current working directory. Since the DLL included with Portable.BouncyCastle is actually named 'BouncyCastle.Crypto', we specify that in the parameters. #> [cmdletbinding()] param( [PSTypeName('PSDepend.Dependency')] [psobject[]]$Dependency, [switch]$Force, [ValidateSet('Test', 'Install')] [string[]]$PSDependAction = @('Install'), [Alias('DLLName')] [string]$Name ) # Extract data from Dependency $DependencyName = $Dependency.DependencyName if ($null -ne $Dependency.Name) { $DependencyName = $Dependency.Name } $Version = $Dependency.Version if(-not $Version) { $Version = 'latest' } $Source = $Dependency.Source if(-not $Dependency.Source) { $Source = 'https://www.nuget.org/api/v2/' } # We use target as a proxy for Scope $Target = $Dependency.Target if(-not $Dependency.Target) { Write-Error "Nuget requires a Dependency Target. Skipping [$DependencyName]" return } $Credential = $Dependency.Credential if(-not (Get-Command Nuget -ErrorAction SilentlyContinue)) { Write-Error "Nuget requires Nuget.exe. Ensure this is in your path, or explicitly specified in $ModuleRoot\PSDepend.Config's NugetPath. Skipping [$DependencyName]" } Write-Verbose -Message "Getting dependency [$DependencyName] from Nuget source [$Source]" # This code works for both install and save scenarios. $PackagePath = Join-Path $Target $DependencyName $NameIs = if ($PSBoundParameters.ContainsKey('Name')) { $Name } else { $DependencyName } if(Test-Path $PackagePath) { if($null -eq (Get-ChildItem $PackagePath -Filter "$NameIs*" -Include '*.exe', '*.dll' -Recurse)) { # For now, skip if we don't find a DLL matching the expected name Write-Error "Could not find existing DLL for dependency [$DependencyName] in package path [$PackagePath]" return } Write-Verbose "Found existing package [$DependencyName]" # Thanks to Brandon Padgett! $Path = (Get-ChildItem $PackagePath -Filter "$NameIs*" -Include '*.exe', '*.dll' -Recurse | Select-Object -First 1).FullName $ExistingVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($Path).FileVersion $GetGalleryVersion = { (Find-NugetPackage -Name $DependencyName -PackageSourceUrl $Source -Credential $Credential -IsLatest).Version } # Version string, and equal to current if( $Version -and $Version -ne 'latest' -and $Version -eq $ExistingVersion) { Write-Verbose "You have the requested version [$Version] of [$DependencyName]" if($PSDependAction -contains 'Test') { return $True } return $null } # latest, and we have latest if( $Version -and ($Version -eq 'latest' -or $Version -like '') -and ($GalleryVersion = [System.Version](& $GetGalleryVersion)) -le [System.Version]$ExistingVersion ) { Write-Verbose "You have the latest version of [$DependencyName], with installed version [$ExistingVersion] and Source version [$GalleryVersion]" if($PSDependAction -contains 'Test') { return $True } return $null } Write-Verbose "Removing existing [$PackagePath]`nContinuing to install [$DependencyName]: Requested version [$version], existing version [$ExistingVersion]" if($PSDependAction -contains 'Install') { if($Force) { Remove-Item $PackagePath -Force -Recurse } else { Write-Verbose "Use -Force to remove existing [$PackagePath]`nSkipping install of [$DependencyName]: Requested version [$version], existing version [$ExistingVersion]" if( $PSDependAction -contains 'Test') { return $false } return $null } } } #No dependency found, return false if we're testing alone... if( $PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1) { return $False } if($PSDependAction -contains 'Install') { $TargetExists = Test-Path $Target -PathType Container Write-Verbose "Saving [$DependencyName] with path [$Target]" $NugetParams = '-Source', $Source, '-ExcludeVersion', '-NonInteractive', '-OutputDirectory', $Target if(-not $TargetExists) { Write-Verbose "Creating directory path to [$Target]" $Null = New-Item -ItemType Directory -Path $Target -Force -ErrorAction SilentlyContinue } if($Version -and $Version -notlike 'latest') { $NugetParams += '-version', $Version } $NugetParams = 'install', $DependencyName + $NugetParams Invoke-ExternalCommand Nuget -Arguments $NugetParams } |