|
1 | 1 | # Copyright (c) Microsoft Corporation.
|
2 | 2 | # Licensed under the MIT License.
|
3 | 3 |
|
4 |
| -$script:CurrentCacheSchemaVersion = 1 |
| 4 | +$script:CurrentCacheSchemaVersion = 2 |
5 | 5 |
|
6 | 6 | function Write-DscTrace {
|
7 | 7 | param(
|
@@ -42,7 +42,6 @@ function Get-DSCResourceModules
|
42 | 42 | if($null -ne $containsDSCResource)
|
43 | 43 | {
|
44 | 44 | $dscModulePsd1List.Add($psd1) | Out-Null
|
45 |
| - break |
46 | 45 | }
|
47 | 46 | }
|
48 | 47 | }
|
@@ -107,7 +106,9 @@ function FindAndParseResourceDefinitions
|
107 | 106 | [CmdletBinding(HelpUri = '')]
|
108 | 107 | param(
|
109 | 108 | [Parameter(Mandatory = $true)]
|
110 |
| - [string]$filePath |
| 109 | + [string]$filePath, |
| 110 | + [Parameter(Mandatory = $true)] |
| 111 | + [string]$moduleVersion |
111 | 112 | )
|
112 | 113 |
|
113 | 114 | if (-not (Test-Path $filePath))
|
@@ -155,6 +156,7 @@ function FindAndParseResourceDefinitions
|
155 | 156 | #TODO: ModuleName, Version and ParentPath should be taken from psd1 contents
|
156 | 157 | $DscResourceInfo.ModuleName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
|
157 | 158 | $DscResourceInfo.ParentPath = [System.IO.Path]::GetDirectoryName($filePath)
|
| 159 | + $DscResourceInfo.Version = $moduleVersion |
158 | 160 |
|
159 | 161 | $DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
|
160 | 162 | Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties
|
@@ -194,7 +196,7 @@ function LoadPowerShellClassResourcesFromModule
|
194 | 196 | $scriptPath = $moduleInfo.Path;
|
195 | 197 | }
|
196 | 198 |
|
197 |
| - $Resources = FindAndParseResourceDefinitions $scriptPath |
| 199 | + $Resources = FindAndParseResourceDefinitions $scriptPath $moduleInfo.Version |
198 | 200 |
|
199 | 201 | if ($moduleInfo.NestedModules)
|
200 | 202 | {
|
@@ -309,11 +311,23 @@ function Invoke-DscCacheRefresh {
|
309 | 311 | $dscResourceModulePsd1s = Get-DSCResourceModules
|
310 | 312 | if($null -ne $dscResourceModulePsd1s) {
|
311 | 313 | $modules = Get-Module -ListAvailable -Name ($dscResourceModulePsd1s)
|
| 314 | + $processedModuleNames = @{} |
312 | 315 | foreach ($mod in $modules)
|
313 | 316 | {
|
314 |
| - [System.Collections.Generic.List[DscResourceInfo]]$r = LoadPowerShellClassResourcesFromModule -moduleInfo $mod |
315 |
| - if ($r) { |
316 |
| - $DscResources.AddRange($r) |
| 317 | + if (-not ($processedModuleNames.ContainsKey($mod.Name))) { |
| 318 | + $processedModuleNames.Add($mod.Name, $true) |
| 319 | + |
| 320 | + # from several modules with the same name select the one with the highest version |
| 321 | + $selectedMod = $modules | Where-Object Name -EQ $mod.Name |
| 322 | + if ($selectedMod.Count -gt 1) { |
| 323 | + "Found $($selectedMod.Count) modules with name '$($mod.Name)'" | Write-DscTrace -Operation Trace |
| 324 | + $selectedMod = $selectedMod | Sort-Object -Property Version -Descending | Select-Object -First 1 |
| 325 | + } |
| 326 | + |
| 327 | + [System.Collections.Generic.List[DscResourceInfo]]$r = LoadPowerShellClassResourcesFromModule -moduleInfo $selectedMod |
| 328 | + if ($r) { |
| 329 | + $DscResources.AddRange($r) |
| 330 | + } |
317 | 331 | }
|
318 | 332 | }
|
319 | 333 | }
|
|
0 commit comments