From ada2b4ce0ee56c537dc710ce5c56690fecfac878 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 9 May 2023 12:57:40 +0200 Subject: [PATCH] Fixes recursive loop on first loading of Framework --- cache/framework_cache.psm1 | 17 +++++++++++++++++ icinga-powershell-framework.psm1 | 6 +++++- .../New-IcingaEnvironmentVariable.psm1 | 4 ++++ templates/framework_cache.psm1.template | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cache/framework_cache.psm1 b/cache/framework_cache.psm1 index aec9f15..ddd9019 100644 --- a/cache/framework_cache.psm1 +++ b/cache/framework_cache.psm1 @@ -8,6 +8,21 @@ Manually enabling the feature is no longer required. #> +# Ensure we only load this module once +if ($null -ne $Global:Icinga -And $Global:Icinga.ContainsKey('CacheBuilding') -And $Global:Icinga['CacheBuilding']) { + return; +} + +if ($null -eq $Global:Icinga) { + $Global:Icinga = @{ }; +} + +if ($Global:Icinga.ContainsKey('CacheBuilding') -eq $FALSE) { + $Global:Icinga.Add('CacheBuilding', $TRUE); +} else { + $Global:Icinga.CacheBuilding = $TRUE; +} + # Ensures that VS Code is not generating the cache file if ($null -ne $env:TERM_PROGRAM) { Write-IcingaFrameworkCodeCache -DeveloperMode; @@ -22,3 +37,5 @@ Import-Module icinga-powershell-framework -Force; if ($null -ne $env:TERM_PROGRAM -Or $Global:Icinga.Protected.DeveloperMode) { Copy-IcingaFrameworkCacheTemplate; } + +$Global:Icinga.CacheBuilding = $FALSE; diff --git a/icinga-powershell-framework.psm1 b/icinga-powershell-framework.psm1 index 6896d84..69766ca 100644 --- a/icinga-powershell-framework.psm1 +++ b/icinga-powershell-framework.psm1 @@ -132,7 +132,11 @@ function Write-IcingaFrameworkCodeCache() [System.IO.File]::WriteAllLines($CacheFile, $CacheContent, (New-Object System.Text.UTF8Encoding $TRUE)); - Remove-IcingaFrameworkDependencyFile; + $DependencyFile = Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'framework_dependencies.json'; + + if ((Test-Path $DependencyFile)) { + Remove-Item -Path $DependencyFile -Force | Out-Null; + } if ($Global:Icinga.Protected.DeveloperMode) { Copy-IcingaFrameworkCacheTemplate; diff --git a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 index 42c0935..92ed1a3 100644 --- a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 +++ b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 @@ -15,6 +15,10 @@ function New-IcingaEnvironmentVariable() $Global:Icinga = @{ }; } + if ($Global:Icinga.ContainsKey('CacheBuilding') -eq $FALSE) { + $Global:Icinga.Add('CacheBuilding', $FALSE); + } + # Session specific configuration for this shell if ($Global:Icinga.ContainsKey('Private') -eq $FALSE) { $Global:Icinga.Add('Private', @{ }); diff --git a/templates/framework_cache.psm1.template b/templates/framework_cache.psm1.template index aec9f15..ddd9019 100644 --- a/templates/framework_cache.psm1.template +++ b/templates/framework_cache.psm1.template @@ -8,6 +8,21 @@ Manually enabling the feature is no longer required. #> +# Ensure we only load this module once +if ($null -ne $Global:Icinga -And $Global:Icinga.ContainsKey('CacheBuilding') -And $Global:Icinga['CacheBuilding']) { + return; +} + +if ($null -eq $Global:Icinga) { + $Global:Icinga = @{ }; +} + +if ($Global:Icinga.ContainsKey('CacheBuilding') -eq $FALSE) { + $Global:Icinga.Add('CacheBuilding', $TRUE); +} else { + $Global:Icinga.CacheBuilding = $TRUE; +} + # Ensures that VS Code is not generating the cache file if ($null -ne $env:TERM_PROGRAM) { Write-IcingaFrameworkCodeCache -DeveloperMode; @@ -22,3 +37,5 @@ Import-Module icinga-powershell-framework -Force; if ($null -ne $env:TERM_PROGRAM -Or $Global:Icinga.Protected.DeveloperMode) { Copy-IcingaFrameworkCacheTemplate; } + +$Global:Icinga.CacheBuilding = $FALSE;