Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PSAdapter to properly handle multiple modules with same name #489

Merged
merged 4 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,53 @@ Describe 'PowerShell adapter resource tests' {
$t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}
$t.properties | Should -Contain "BaseProperty"
}

It 'Verify highest module version is loaded' {

$srcPath = Join-Path $PSScriptRoot 'TestClassResource'
$pathRoot1 = Join-Path $TestDrive 'A'
$pathRoot2 = Join-Path $TestDrive 'B'
$path1 = Join-Path $pathRoot1 'TestClassResource' '0.0.1'
anmenaga marked this conversation as resolved.
Show resolved Hide resolved
$path2 = Join-Path $pathRoot1 'TestClassResource' '0.0.2'
$path3 = Join-Path $pathRoot2 'TestClassResource' '0.0.3'
$path4 = Join-Path $pathRoot2 'TestClassResource' '0.0.4'

New-Item -ItemType Directory -Force -Path $path1 | Out-Null
New-Item -ItemType Directory -Force -Path $path2 | Out-Null
New-Item -ItemType Directory -Force -Path $path3 | Out-Null
New-Item -ItemType Directory -Force -Path $path4 | Out-Null

$files = Get-ChildItem -Recurse -File -Path $srcPath
$files | Copy-Item -Destination $path1
$files | Copy-Item -Destination $path2
$files | Copy-Item -Destination $path3
$files | Copy-Item -Destination $path4

$filePath = Join-Path $path1 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'0.0.1`'") | Set-Content $filePath
$filePath = Join-Path $path2 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'0.0.2`'") | Set-Content $filePath
$filePath = Join-Path $path3 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'0.0.3`'") | Set-Content $filePath
$filePath = Join-Path $path4 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = `'0.0.1`'", "ModuleVersion = `'0.0.4`'") | Set-Content $filePath


$oldPath = $env:PSModulePath
try {
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2

$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$r = @($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'})
$r.Count | Should -Be 1
$r[0].Version | Should -Be '0.0.4'
}
finally {
$env:PSModulePath = $oldPath
}

}
}
28 changes: 21 additions & 7 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

$script:CurrentCacheSchemaVersion = 1
$script:CurrentCacheSchemaVersion = 2

function Write-DscTrace {
param(
Expand Down Expand Up @@ -42,7 +42,6 @@ function Get-DSCResourceModules
if($null -ne $containsDSCResource)
{
$dscModulePsd1List.Add($psd1) | Out-Null
break
}
}
}
Expand Down Expand Up @@ -107,7 +106,9 @@ function FindAndParseResourceDefinitions
[CmdletBinding(HelpUri = '')]
param(
[Parameter(Mandatory = $true)]
[string]$filePath
[string]$filePath,
[Parameter(Mandatory = $true)]
[string]$moduleVersion
)

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

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

$Resources = FindAndParseResourceDefinitions $scriptPath
$Resources = FindAndParseResourceDefinitions $scriptPath $moduleInfo.Version

if ($moduleInfo.NestedModules)
{
Expand Down Expand Up @@ -309,11 +311,23 @@ function Invoke-DscCacheRefresh {
$dscResourceModulePsd1s = Get-DSCResourceModules
if($null -ne $dscResourceModulePsd1s) {
$modules = Get-Module -ListAvailable -Name ($dscResourceModulePsd1s)
$processedModuleNames = @{}
foreach ($mod in $modules)
{
[System.Collections.Generic.List[DscResourceInfo]]$r = LoadPowerShellClassResourcesFromModule -moduleInfo $mod
if ($r) {
$DscResources.AddRange($r)
if (-not ($processedModuleNames.ContainsKey($mod.Name))) {
$processedModuleNames.Add($mod.Name, $true)

# from several modules with the same name select the one with the highest version
$selectedMod = $modules | Where-Object Name -EQ $mod.Name
if ($selectedMod.Count -gt 1) {
"Found $($selectedMod.Count) modules with name '$($mod.Name)'" | Write-DscTrace -Operation Trace
$selectedMod = $selectedMod | Sort-Object -Property Version -Descending | Select-Object -First 1
}

[System.Collections.Generic.List[DscResourceInfo]]$r = LoadPowerShellClassResourcesFromModule -moduleInfo $selectedMod
if ($r) {
$DscResources.AddRange($r)
}
}
}
}
Expand Down
Loading