Adds developer mode to prevent cache overwrite

This commit is contained in:
Lord Hepipud 2022-05-24 13:11:25 +02:00
parent 42320b55b3
commit 8df5ab35bb
5 changed files with 77 additions and 8 deletions

View file

@ -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;
}

View file

@ -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)

View file

@ -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()

View file

@ -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);

View file

@ -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;
}