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

Features: Adds developer mode to prevent cache overwrite #525

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions cache/framework_cache.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
49 changes: 41 additions & 8 deletions icinga-powershell-framework.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -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()
Expand Down Expand Up @@ -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 `
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions lib/core/framework/New-IcingaEnvironmentVariable.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions templates/framework_cache.psm1.template
Original file line number Diff line number Diff line change
@@ -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;
}