|
| 1 | +$resourceModuleRoot = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent |
| 2 | + |
| 3 | +# Import localization helper functions. |
| 4 | +$helperName = 'PowerShellGet.LocalizationHelper' |
| 5 | +$dscResourcesFolderFilePath = Join-Path -Path $resourceModuleRoot -ChildPath "Modules\$helperName\$helperName.psm1" |
| 6 | +Import-Module -Name $dscResourcesFolderFilePath |
| 7 | + |
| 8 | +$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_PSRepository' -ScriptRoot $PSScriptRoot |
| 9 | + |
| 10 | +# Import resource helper functions. |
| 11 | +$helperName = 'PowerShellGet.ResourceHelper' |
| 12 | +$dscResourcesFolderFilePath = Join-Path -Path $resourceModuleRoot -ChildPath "Modules\$helperName\$helperName.psm1" |
| 13 | +Import-Module -Name $dscResourcesFolderFilePath |
| 14 | + |
| 15 | +<# |
| 16 | + .SYNOPSIS |
| 17 | + Returns the current state of the repository. |
| 18 | +
|
| 19 | + .PARAMETER Name |
| 20 | + Specifies the name of the repository to manage. |
| 21 | +#> |
| 22 | +function Get-TargetResource { |
| 23 | + <# |
| 24 | + These suppressions are added because this repository have other Visual Studio Code workspace |
| 25 | + settings than those in DscResource.Tests DSC test framework. |
| 26 | + Only those suppression that contradict this repository guideline is added here. |
| 27 | + #> |
| 28 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')] |
| 29 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')] |
| 30 | + [CmdletBinding()] |
| 31 | + [OutputType([System.Collections.Hashtable])] |
| 32 | + param |
| 33 | + ( |
| 34 | + [Parameter(Mandatory = $true)] |
| 35 | + [System.String] |
| 36 | + $Name |
| 37 | + ) |
| 38 | + |
| 39 | + $returnValue = @{ |
| 40 | + Ensure = 'Absent' |
| 41 | + Name = $Name |
| 42 | + SourceLocation = $null |
| 43 | + ScriptSourceLocation = $null |
| 44 | + PublishLocation = $null |
| 45 | + ScriptPublishLocation = $null |
| 46 | + InstallationPolicy = $null |
| 47 | + PackageManagementProvider = $null |
| 48 | + Trusted = $false |
| 49 | + Registered = $false |
| 50 | + } |
| 51 | + |
| 52 | + Write-Verbose -Message ($localizedData.GetTargetResourceMessage -f $Name) |
| 53 | + |
| 54 | + $repository = Get-PSRepository -Name $Name -ErrorAction 'SilentlyContinue' |
| 55 | + |
| 56 | + if ($repository) { |
| 57 | + $returnValue.Ensure = 'Present' |
| 58 | + $returnValue.SourceLocation = $repository.SourceLocation |
| 59 | + $returnValue.ScriptSourceLocation = $repository.ScriptSourceLocation |
| 60 | + $returnValue.PublishLocation = $repository.PublishLocation |
| 61 | + $returnValue.ScriptPublishLocation = $repository.ScriptPublishLocation |
| 62 | + $returnValue.InstallationPolicy = $repository.InstallationPolicy |
| 63 | + $returnValue.PackageManagementProvider = $repository.PackageManagementProvider |
| 64 | + $returnValue.Trusted = $repository.Trusted |
| 65 | + $returnValue.Registered = $repository.Registered |
| 66 | + } |
| 67 | + else { |
| 68 | + Write-Verbose -Message ($localizedData.RepositoryNotFound -f $Name) |
| 69 | + } |
| 70 | + |
| 71 | + return $returnValue |
| 72 | +} |
| 73 | + |
| 74 | +<# |
| 75 | + .SYNOPSIS |
| 76 | + Determines if the repository is in the desired state. |
| 77 | +
|
| 78 | + .PARAMETER Ensure |
| 79 | + If the repository should be present or absent on the server |
| 80 | + being configured. Default values is 'Present'. |
| 81 | +
|
| 82 | + .PARAMETER Name |
| 83 | + Specifies the name of the repository to manage. |
| 84 | +
|
| 85 | + .PARAMETER SourceLocation |
| 86 | + Specifies the URI for discovering and installing modules from |
| 87 | + this repository. A URI can be a NuGet server feed, HTTP, HTTPS, |
| 88 | + FTP or file location. |
| 89 | +
|
| 90 | + .PARAMETER ScriptSourceLocation |
| 91 | + Specifies the URI for the script source location. |
| 92 | +
|
| 93 | + .PARAMETER PublishLocation |
| 94 | + Specifies the URI of the publish location. For example, for |
| 95 | + NuGet-based repositories, the publish location is similar |
| 96 | + to http://someNuGetUrl.com/api/v2/Packages. |
| 97 | +
|
| 98 | + .PARAMETER ScriptPublishLocation |
| 99 | + Specifies the URI for the script publish location. |
| 100 | +
|
| 101 | + .PARAMETER InstallationPolicy |
| 102 | + Specifies the installation policy. Valid values are 'Trusted' |
| 103 | + or 'Untrusted'. The default value is 'Untrusted'. |
| 104 | +
|
| 105 | + .PARAMETER PackageManagementProvider |
| 106 | + Specifies a OneGet package provider. Default value is 'NuGet'. |
| 107 | +#> |
| 108 | +function Test-TargetResource { |
| 109 | + <# |
| 110 | + These suppressions are added because this repository have other Visual Studio Code workspace |
| 111 | + settings than those in DscResource.Tests DSC test framework. |
| 112 | + Only those suppression that contradict this repository guideline is added here. |
| 113 | + #> |
| 114 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')] |
| 115 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')] |
| 116 | + [CmdletBinding()] |
| 117 | + [OutputType([System.Boolean])] |
| 118 | + param |
| 119 | + ( |
| 120 | + [Parameter()] |
| 121 | + [ValidateSet('Present', 'Absent')] |
| 122 | + [System.String] |
| 123 | + $Ensure = 'Present', |
| 124 | + |
| 125 | + [Parameter(Mandatory = $true)] |
| 126 | + [System.String] |
| 127 | + $Name, |
| 128 | + |
| 129 | + [Parameter()] |
| 130 | + [System.String] |
| 131 | + $SourceLocation, |
| 132 | + |
| 133 | + [Parameter()] |
| 134 | + [System.String] |
| 135 | + $ScriptSourceLocation, |
| 136 | + |
| 137 | + [Parameter()] |
| 138 | + [System.String] |
| 139 | + $PublishLocation, |
| 140 | + |
| 141 | + [Parameter()] |
| 142 | + [System.String] |
| 143 | + $ScriptPublishLocation, |
| 144 | + |
| 145 | + [Parameter()] |
| 146 | + [ValidateSet('Trusted', 'Untrusted')] |
| 147 | + [System.String] |
| 148 | + $InstallationPolicy = 'Untrusted', |
| 149 | + |
| 150 | + [Parameter()] |
| 151 | + [System.String] |
| 152 | + $PackageManagementProvider = 'NuGet' |
| 153 | + ) |
| 154 | + |
| 155 | + Write-Verbose -Message ($localizedData.TestTargetResourceMessage -f $Name) |
| 156 | + |
| 157 | + $returnValue = $false |
| 158 | + |
| 159 | + $getTargetResourceResult = Get-TargetResource -Name $Name |
| 160 | + |
| 161 | + if ($Ensure -eq $getTargetResourceResult.Ensure) { |
| 162 | + if ($getTargetResourceResult.Ensure -eq 'Present' ) { |
| 163 | + $returnValue = Test-DscParameterState ` |
| 164 | + -CurrentValues $getTargetResourceResult ` |
| 165 | + -DesiredValues $PSBoundParameters ` |
| 166 | + -ValuesToCheck @( |
| 167 | + 'SourceLocation' |
| 168 | + 'ScriptSourceLocation' |
| 169 | + 'PublishLocation' |
| 170 | + 'ScriptPublishLocation' |
| 171 | + 'InstallationPolicy' |
| 172 | + 'PackageManagementProvider' |
| 173 | + ) |
| 174 | + } |
| 175 | + else { |
| 176 | + $returnValue = $true |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if ($returnValue) { |
| 181 | + Write-Verbose -Message ($localizedData.InDesiredState -f $Name) |
| 182 | + } |
| 183 | + else { |
| 184 | + Write-Verbose -Message ($localizedData.NotInDesiredState -f $Name) |
| 185 | + } |
| 186 | + |
| 187 | + return $returnValue |
| 188 | +} |
| 189 | + |
| 190 | +<# |
| 191 | + .SYNOPSIS |
| 192 | + Creates, removes or updates the repository. |
| 193 | +
|
| 194 | + .PARAMETER Ensure |
| 195 | + If the repository should be present or absent on the server |
| 196 | + being configured. Default values is 'Present'. |
| 197 | +
|
| 198 | + .PARAMETER Name |
| 199 | + Specifies the name of the repository to manage. |
| 200 | +
|
| 201 | + .PARAMETER SourceLocation |
| 202 | + Specifies the URI for discovering and installing modules from |
| 203 | + this repository. A URI can be a NuGet server feed, HTTP, HTTPS, |
| 204 | + FTP or file location. |
| 205 | +
|
| 206 | + .PARAMETER ScriptSourceLocation |
| 207 | + Specifies the URI for the script source location. |
| 208 | +
|
| 209 | + .PARAMETER PublishLocation |
| 210 | + Specifies the URI of the publish location. For example, for |
| 211 | + NuGet-based repositories, the publish location is similar |
| 212 | + to http://someNuGetUrl.com/api/v2/Packages. |
| 213 | +
|
| 214 | + .PARAMETER ScriptPublishLocation |
| 215 | + Specifies the URI for the script publish location. |
| 216 | +
|
| 217 | + .PARAMETER InstallationPolicy |
| 218 | + Specifies the installation policy. Valid values are 'Trusted' |
| 219 | + or 'Untrusted'. The default value is 'Untrusted'. |
| 220 | +
|
| 221 | + .PARAMETER PackageManagementProvider |
| 222 | + Specifies a OneGet package provider. Default value is 'NuGet'. |
| 223 | +#> |
| 224 | +function Set-TargetResource { |
| 225 | + <# |
| 226 | + These suppressions are added because this repository have other Visual Studio Code workspace |
| 227 | + settings than those in DscResource.Tests DSC test framework. |
| 228 | + Only those suppression that contradict this repository guideline is added here. |
| 229 | + #> |
| 230 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')] |
| 231 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')] |
| 232 | + [CmdletBinding()] |
| 233 | + param |
| 234 | + ( |
| 235 | + [Parameter()] |
| 236 | + [ValidateSet('Present', 'Absent')] |
| 237 | + [System.String] |
| 238 | + $Ensure = 'Present', |
| 239 | + |
| 240 | + [Parameter(Mandatory = $true)] |
| 241 | + [System.String] |
| 242 | + $Name, |
| 243 | + |
| 244 | + [Parameter()] |
| 245 | + [System.String] |
| 246 | + $SourceLocation, |
| 247 | + |
| 248 | + [Parameter()] |
| 249 | + [System.String] |
| 250 | + $ScriptSourceLocation, |
| 251 | + |
| 252 | + [Parameter()] |
| 253 | + [System.String] |
| 254 | + $PublishLocation, |
| 255 | + |
| 256 | + [Parameter()] |
| 257 | + [System.String] |
| 258 | + $ScriptPublishLocation, |
| 259 | + |
| 260 | + [Parameter()] |
| 261 | + [ValidateSet('Trusted', 'Untrusted')] |
| 262 | + [System.String] |
| 263 | + $InstallationPolicy = 'Untrusted', |
| 264 | + |
| 265 | + [Parameter()] |
| 266 | + [System.String] |
| 267 | + $PackageManagementProvider = 'NuGet' |
| 268 | + ) |
| 269 | + |
| 270 | + $getTargetResourceResult = Get-TargetResource -Name $Name |
| 271 | + |
| 272 | + # Determine if the repository should be present or absent. |
| 273 | + if ($Ensure -eq 'Present') { |
| 274 | + $repositoryParameters = New-SplatParameterHashTable ` |
| 275 | + -FunctionBoundParameters $PSBoundParameters ` |
| 276 | + -ArgumentNames @( |
| 277 | + 'Name' |
| 278 | + 'SourceLocation' |
| 279 | + 'ScriptSourceLocation' |
| 280 | + 'PublishLocation' |
| 281 | + 'ScriptPublishLocation' |
| 282 | + 'InstallationPolicy' |
| 283 | + 'PackageManagementProvider' |
| 284 | + ) |
| 285 | + |
| 286 | + # Determine if the repository is already present. |
| 287 | + if ($getTargetResourceResult.Ensure -eq 'Present') { |
| 288 | + Write-Verbose -Message ($localizedData.RepositoryExist -f $Name) |
| 289 | + |
| 290 | + # Repository exist, update the properties. |
| 291 | + Set-PSRepository @repositoryParameters -ErrorAction 'Stop' |
| 292 | + } |
| 293 | + else { |
| 294 | + Write-Verbose -Message ($localizedData.RepositoryDoesNotExist -f $Name) |
| 295 | + |
| 296 | + # Repository did not exist, create the repository. |
| 297 | + Register-PSRepository @repositoryParameters -ErrorAction 'Stop' |
| 298 | + } |
| 299 | + } |
| 300 | + else { |
| 301 | + if ($getTargetResourceResult.Ensure -eq 'Present') { |
| 302 | + Write-Verbose -Message ($localizedData.RemoveExistingRepository -f $Name) |
| 303 | + |
| 304 | + # Repository did exist, remove the repository. |
| 305 | + Unregister-PSRepository -Name $Name -ErrorAction 'Stop' |
| 306 | + } |
| 307 | + } |
| 308 | +} |
0 commit comments