diff --git a/cache/framework_cache.psm1 b/cache/framework_cache.psm1 index a7427d5e..aec9f15d 100644 --- a/cache/framework_cache.psm1 +++ b/cache/framework_cache.psm1 @@ -8,7 +8,17 @@ Manually enabling the feature is no longer required. #> +# Ensures that VS Code is not generating the cache file +if ($null -ne $env:TERM_PROGRAM) { + Write-IcingaFrameworkCodeCache -DeveloperMode; + return; +} + Write-IcingaFrameworkCodeCache; Import-Module icinga-powershell-framework -Global -Force; Import-Module icinga-powershell-framework -Force; + +if ($null -ne $env:TERM_PROGRAM -Or $Global:Icinga.Protected.DeveloperMode) { + Copy-IcingaFrameworkCacheTemplate; +} diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index b78d76f8..b473aa84 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Enhancements * [#40](https://github.com/Icinga/icinga-powershell-framework/issues/40) Adds support to set service recovery for the Icinga Agent and Icinga for Windows service, to restart them in case of a crash or error +* [#525](https://github.com/Icinga/icinga-powershell-framework/pull/525) Adds new developer mode for `icinga` command and improved cache handling, to ensure within `-DeveloperMode` and inside a VS Code environment, the framework cache file is never overwritten, while still all functions are loaded and imported. ## 1.9.1 (2022-05-13) diff --git a/icinga-powershell-framework.psm1 b/icinga-powershell-framework.psm1 index 4b588a04..127179e8 100644 --- a/icinga-powershell-framework.psm1 +++ b/icinga-powershell-framework.psm1 @@ -97,6 +97,10 @@ function Import-IcingaLib() function Write-IcingaFrameworkCodeCache() { + param ( + [switch]$DeveloperMode = $FALSE + ); + [string]$CacheFile = Get-IcingaFrameworkCodeCacheFile; [string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib\'; [string]$CacheContent = ''; @@ -109,9 +113,27 @@ function Write-IcingaFrameworkCodeCache() } $CacheContent += "Export-ModuleMember -Function @( '*' ) -Alias @( '*' ) -Variable @( '*' )"; + + if ($DeveloperMode -Or $Global:Icinga.Protected.DeveloperMode) { + [ScriptBlock]$CodeCache = [ScriptBlock]::Create($CacheContent); + . $CodeCache; + + Copy-IcingaFrameworkCacheTemplate; + return; + } + Set-Content -Path $CacheFile -Value $CacheContent; Remove-IcingaFrameworkDependencyFile; + + if ($Global:Icinga.Protected.DeveloperMode) { + Copy-IcingaFrameworkCacheTemplate; + } +} + +function Copy-IcingaFrameworkCacheTemplate() +{ + Copy-Item -Path (Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath '\templates\framework_cache.psm1.template') -Destination (Get-IcingaFrameworkCodeCacheFile) -Force; } function Publish-IcingaEventLogDocumentation() @@ -198,11 +220,12 @@ function Invoke-IcingaCommand() [CmdletBinding()] param ( $ScriptBlock, - [switch]$SkipHeader = $FALSE, - [switch]$Manage = $FALSE, # Only for backwards compatibility, has no use at all - [switch]$Shell = $FALSE, - [switch]$RebuildCache = $FALSE, - [array]$ArgumentList = @() + [switch]$SkipHeader = $FALSE, + [switch]$Manage = $FALSE, # Only for backwards compatibility, has no use at all + [switch]$Shell = $FALSE, + [switch]$RebuildCache = $FALSE, + [switch]$DeveloperMode = $FALSE, + [array]$ArgumentList = @() ); Import-LocalizedData ` @@ -226,8 +249,12 @@ function Invoke-IcingaCommand() Write-IcingaConsoleHeader -HeaderLines $Headers; } - if ($RebuildCache) { - Write-IcingaFrameworkCodeCache; + if ($DeveloperMode) { + $Global:Icinga.Protected.DeveloperMode = $TRUE; + } + + if ($RebuildCache -Or $DeveloperMode) { + Write-IcingaFrameworkCodeCache -DeveloperMode:$DeveloperMode; } if ($null -ne $psISE) { @@ -247,10 +274,16 @@ function Invoke-IcingaCommand() $Version = $args[2]; $Shell = $args[3]; $IcingaShellArgs = $args[4]; + $DeveloperMode = $args[5]; # Load our Icinga Framework Use-Icinga; + if ($DeveloperMode) { + $Global:Icinga.Protected.DeveloperMode = $TRUE; + Copy-IcingaFrameworkCacheTemplate; + } + $Host.UI.RawUI.WindowTitle = ([string]::Format('Icinga for Windows {0}', $Version)); # Set the location to the Icinga Framework module folder @@ -274,7 +307,7 @@ function Invoke-IcingaCommand() return "> " } - } -Args $ScriptBlock, $PSScriptRoot, $IcingaFrameworkData.PrivateData.Version, ([bool]$Shell), $ArgumentList; + } -Args $ScriptBlock, $PSScriptRoot, $IcingaFrameworkData.PrivateData.Version, ([bool]$Shell), $ArgumentList, $DeveloperMode; } function Start-IcingaShellAsUser() diff --git a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 index ee31946e..42c09350 100644 --- a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 +++ b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 @@ -57,6 +57,7 @@ function New-IcingaEnvironmentVariable() if ($Global:Icinga.ContainsKey('Protected') -eq $FALSE) { $Global:Icinga.Add('Protected', @{ }); + $Global:Icinga.Protected.Add('DeveloperMode', $FALSE); $Global:Icinga.Protected.Add('DebugMode', $FALSE); $Global:Icinga.Protected.Add('JEAContext', $FALSE); $Global:Icinga.Protected.Add('RunAsDaemon', $FALSE); diff --git a/templates/framework_cache.psm1.template b/templates/framework_cache.psm1.template new file mode 100644 index 00000000..aec9f15d --- /dev/null +++ b/templates/framework_cache.psm1.template @@ -0,0 +1,24 @@ +<# + ### Note ### + + This file is shipping plain with Icinga for Windows for each version. + Once the module is loaded, this content will entirely be replaced with + all modules and components shipped by the Icinga PowerShell Framework. + + Manually enabling the feature is no longer required. +#> + +# Ensures that VS Code is not generating the cache file +if ($null -ne $env:TERM_PROGRAM) { + Write-IcingaFrameworkCodeCache -DeveloperMode; + return; +} + +Write-IcingaFrameworkCodeCache; + +Import-Module icinga-powershell-framework -Global -Force; +Import-Module icinga-powershell-framework -Force; + +if ($null -ne $env:TERM_PROGRAM -Or $Global:Icinga.Protected.DeveloperMode) { + Copy-IcingaFrameworkCacheTemplate; +}