Skip to content

Commit a1cf39f

Browse files
author
Andrew
committed
updated pproperty to camelCasing
2 parents 0777706 + b6f3150 commit a1cf39f

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

powershell-adapter/Tests/powershellgroup.resource.tests.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,54 @@ Describe 'PowerShell adapter resource tests' {
129129
$t.properties | Should -Contain "BaseProperty"
130130
}
131131

132+
It 'Verify highest module version is loaded' {
133+
134+
$srcPath = Join-Path $PSScriptRoot 'TestClassResource'
135+
$pathRoot1 = Join-Path $TestDrive 'A'
136+
$pathRoot2 = Join-Path $TestDrive 'B'
137+
$path1 = Join-Path $pathRoot1 'TestClassResource' '1.0'
138+
$path2 = Join-Path $pathRoot1 'TestClassResource' '1.1'
139+
$path3 = Join-Path $pathRoot2 'TestClassResource' '2.0'
140+
$path4 = Join-Path $pathRoot2 'TestClassResource' '2.0.1'
141+
142+
New-Item -ItemType Directory -Force -Path $path1 | Out-Null
143+
New-Item -ItemType Directory -Force -Path $path2 | Out-Null
144+
New-Item -ItemType Directory -Force -Path $path3 | Out-Null
145+
New-Item -ItemType Directory -Force -Path $path4 | Out-Null
146+
147+
$files = Get-ChildItem -Recurse -File -Path $srcPath
148+
$files | Copy-Item -Destination $path1
149+
$files | Copy-Item -Destination $path2
150+
$files | Copy-Item -Destination $path3
151+
$files | Copy-Item -Destination $path4
152+
153+
$filePath = Join-Path $path1 'TestClassResource.psd1'
154+
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'1.0`'") | Set-Content $filePath
155+
$filePath = Join-Path $path2 'TestClassResource.psd1'
156+
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'1.1`'") | Set-Content $filePath
157+
$filePath = Join-Path $path3 'TestClassResource.psd1'
158+
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'2.0`'") | Set-Content $filePath
159+
$filePath = Join-Path $path4 'TestClassResource.psd1'
160+
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'2.0.1`'") | Set-Content $filePath
161+
162+
163+
$oldPath = $env:PSModulePath
164+
try {
165+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1
166+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2
167+
168+
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
169+
$LASTEXITCODE | Should -Be 0
170+
$resources = $r | ConvertFrom-Json
171+
$r = @($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'})
172+
$r.Count | Should -Be 1
173+
$r[0].Version | Should -Be '2.0.1'
174+
}
175+
finally {
176+
$env:PSModulePath = $oldPath
177+
}
178+
}
179+
132180
It 'Verify adapted_dsc_type field in Get' {
133181
$oldPath = $env:PATH
134182
try {

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
$script:CurrentCacheSchemaVersion = 1
4+
$script:CurrentCacheSchemaVersion = 2
55

66
function Write-DscTrace {
77
param(
@@ -42,7 +42,6 @@ function Get-DSCResourceModules
4242
if($null -ne $containsDSCResource)
4343
{
4444
$dscModulePsd1List.Add($psd1) | Out-Null
45-
break
4645
}
4746
}
4847
}
@@ -107,7 +106,9 @@ function FindAndParseResourceDefinitions
107106
[CmdletBinding(HelpUri = '')]
108107
param(
109108
[Parameter(Mandatory = $true)]
110-
[string]$filePath
109+
[string]$filePath,
110+
[Parameter(Mandatory = $true)]
111+
[string]$moduleVersion
111112
)
112113

113114
if (-not (Test-Path $filePath))
@@ -155,6 +156,7 @@ function FindAndParseResourceDefinitions
155156
#TODO: ModuleName, Version and ParentPath should be taken from psd1 contents
156157
$DscResourceInfo.ModuleName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
157158
$DscResourceInfo.ParentPath = [System.IO.Path]::GetDirectoryName($filePath)
159+
$DscResourceInfo.Version = $moduleVersion
158160

159161
$DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
160162
Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties
@@ -194,7 +196,7 @@ function LoadPowerShellClassResourcesFromModule
194196
$scriptPath = $moduleInfo.Path;
195197
}
196198

197-
$Resources = FindAndParseResourceDefinitions $scriptPath
199+
$Resources = FindAndParseResourceDefinitions $scriptPath $moduleInfo.Version
198200

199201
if ($moduleInfo.NestedModules)
200202
{
@@ -309,11 +311,23 @@ function Invoke-DscCacheRefresh {
309311
$dscResourceModulePsd1s = Get-DSCResourceModules
310312
if($null -ne $dscResourceModulePsd1s) {
311313
$modules = Get-Module -ListAvailable -Name ($dscResourceModulePsd1s)
314+
$processedModuleNames = @{}
312315
foreach ($mod in $modules)
313316
{
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+
}
317331
}
318332
}
319333
}

reboot_pending/reboot_pending.dsc.resource.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
"schema": {
1717
"embedded": {
1818
"$schema": "https://json-schema.org/draft/2020-12/schema",
19-
"type": "[object, null]",
19+
"type": "null",
2020
"properties": {
2121
"rebootPending": {
22-
"type": "boolean"
22+
"type": "boolean",
23+
"readOnly": true
2324
}
2425
}
2526
}

0 commit comments

Comments
 (0)