Skip to content

Commit

Permalink
Updates from code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
anmenaga committed May 10, 2024
1 parent 787db7f commit f9da35c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ Describe 'PowerShell adapter resource tests' {
$winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml"

if ($IsLinux -or $IsMacOS) {
$cacheFilePath = Join-Path $env:HOME ".dsc" "dscv3classcache.json"
$cacheFilePath = Join-Path $env:HOME "dsc" "PSAdapterCache.json"
}
else
{
$cacheFilePath = Join-Path $env:LocalAppData "dscv3classcache.json"
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dscv3classcache-v5.json"
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
}
}
AfterAll {
Expand Down
6 changes: 3 additions & 3 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Describe 'PowerShell adapter resource tests' {
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot

if ($IsLinux -or $IsMacOS) {
$cacheFilePath = Join-Path $env:HOME ".dsc" "dscv3classcache.json"
$cacheFilePath = Join-Path $env:HOME "dsc" "PSAdapterCache.json"
}
else
{
$cacheFilePath = Join-Path $env:LocalAppData "dscv3classcache.json"
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dscv3classcache-v5.json"
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
}
}
AfterAll {
Expand Down
17 changes: 8 additions & 9 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ function Invoke-DscCacheRefresh {

$refreshCache = $false

$cacheFileSuffix = ".json"
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
if ($PSVersionTable.PSVersion.Major -le 5) {
$cacheFileSuffix = "-v5.json"
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
}

$cacheFilePath = Join-Path $env:LocalAppData "dscv3classcache$cacheFileSuffix"
if ($IsLinux -or $IsMacOS) {
$cacheFilePath = Join-Path $env:HOME ".dsc" "dscv3classcache$cacheFileSuffix"
$cacheFilePath = Join-Path $env:HOME "dsc" "PSAdapterCache.json"
}

if (Test-Path $cacheFilePath) {
Expand Down Expand Up @@ -90,7 +88,7 @@ function Invoke-DscCacheRefresh {
$trace = @{'Debug' = "Checking cache for stale PSModulePath"} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)

$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -ea SilentlyContinue}
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}

$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
Expand Down Expand Up @@ -205,7 +203,7 @@ function Invoke-DscCacheRefresh {

# fill in resource files (and their last-write-times) that will be used for up-do-date checks
$lastWriteTimes = @{}
Get-ChildItem -Recurse -Path $dscResource.ParentPath | % {
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1","*.psd1","*psm1","*.mof" -ea SilentlyContinue | % {
$lastWriteTimes.Add($_.FullName, $_.LastWriteTime)
}

Expand All @@ -218,14 +216,15 @@ function Invoke-DscCacheRefresh {

[dscResourceCache]$cache = [dscResourceCache]::new()
$cache.ResourceCache = $dscResourceCacheEntries
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -ea SilentlyContinue}
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
$cache.PSModulePaths = $m.FullName

# save cache for future use
# TODO: replace this with a high-performance serializer
$trace = @{'Debug' = "Saving Get-DscResource cache to '$cacheFilePath'"} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$cache | ConvertTo-Json -Depth 90 | Out-File $cacheFilePath
$jsonCache = $cache | ConvertTo-Json -Depth 90
New-Item -Force -Path $cacheFilePath -Value $jsonCache -Type File | Out-Null
}

return $dscResourceCacheEntries
Expand Down

0 comments on commit f9da35c

Please sign in to comment.