Skip to content

Commit 8df5ab3

Browse files
committed
Adds developer mode to prevent cache overwrite
1 parent 42320b5 commit 8df5ab3

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

cache/framework_cache.psm1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
Manually enabling the feature is no longer required.
99
#>
1010

11+
# Ensures that VS Code is not generating the cache file
12+
if ($null -ne $env:TERM_PROGRAM) {
13+
Write-IcingaFrameworkCodeCache -DeveloperMode;
14+
return;
15+
}
16+
1117
Write-IcingaFrameworkCodeCache;
1218

1319
Import-Module icinga-powershell-framework -Global -Force;
1420
Import-Module icinga-powershell-framework -Force;
21+
22+
if ($null -ne $env:TERM_PROGRAM -Or $Global:Icinga.Protected.DeveloperMode) {
23+
Copy-IcingaFrameworkCacheTemplate;
24+
}

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
### Enhancements
1515

1616
* [#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
17+
* [#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.
1718

1819
## 1.9.1 (2022-05-13)
1920

icinga-powershell-framework.psm1

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ function Import-IcingaLib()
9797

9898
function Write-IcingaFrameworkCodeCache()
9999
{
100+
param (
101+
[switch]$DeveloperMode = $FALSE
102+
);
103+
100104
[string]$CacheFile = Get-IcingaFrameworkCodeCacheFile;
101105
[string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib\';
102106
[string]$CacheContent = '';
@@ -109,9 +113,27 @@ function Write-IcingaFrameworkCodeCache()
109113
}
110114

111115
$CacheContent += "Export-ModuleMember -Function @( '*' ) -Alias @( '*' ) -Variable @( '*' )";
116+
117+
if ($DeveloperMode -Or $Global:Icinga.Protected.DeveloperMode) {
118+
[ScriptBlock]$CodeCache = [ScriptBlock]::Create($CacheContent);
119+
. $CodeCache;
120+
121+
Copy-IcingaFrameworkCacheTemplate;
122+
return;
123+
}
124+
112125
Set-Content -Path $CacheFile -Value $CacheContent;
113126

114127
Remove-IcingaFrameworkDependencyFile;
128+
129+
if ($Global:Icinga.Protected.DeveloperMode) {
130+
Copy-IcingaFrameworkCacheTemplate;
131+
}
132+
}
133+
134+
function Copy-IcingaFrameworkCacheTemplate()
135+
{
136+
Copy-Item -Path (Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath '\templates\framework_cache.psm1.template') -Destination (Get-IcingaFrameworkCodeCacheFile) -Force;
115137
}
116138

117139
function Publish-IcingaEventLogDocumentation()
@@ -198,11 +220,12 @@ function Invoke-IcingaCommand()
198220
[CmdletBinding()]
199221
param (
200222
$ScriptBlock,
201-
[switch]$SkipHeader = $FALSE,
202-
[switch]$Manage = $FALSE, # Only for backwards compatibility, has no use at all
203-
[switch]$Shell = $FALSE,
204-
[switch]$RebuildCache = $FALSE,
205-
[array]$ArgumentList = @()
223+
[switch]$SkipHeader = $FALSE,
224+
[switch]$Manage = $FALSE, # Only for backwards compatibility, has no use at all
225+
[switch]$Shell = $FALSE,
226+
[switch]$RebuildCache = $FALSE,
227+
[switch]$DeveloperMode = $FALSE,
228+
[array]$ArgumentList = @()
206229
);
207230

208231
Import-LocalizedData `
@@ -226,8 +249,12 @@ function Invoke-IcingaCommand()
226249
Write-IcingaConsoleHeader -HeaderLines $Headers;
227250
}
228251

229-
if ($RebuildCache) {
230-
Write-IcingaFrameworkCodeCache;
252+
if ($DeveloperMode) {
253+
$Global:Icinga.Protected.DeveloperMode = $TRUE;
254+
}
255+
256+
if ($RebuildCache -Or $DeveloperMode) {
257+
Write-IcingaFrameworkCodeCache -DeveloperMode:$DeveloperMode;
231258
}
232259

233260
if ($null -ne $psISE) {
@@ -247,10 +274,16 @@ function Invoke-IcingaCommand()
247274
$Version = $args[2];
248275
$Shell = $args[3];
249276
$IcingaShellArgs = $args[4];
277+
$DeveloperMode = $args[5];
250278

251279
# Load our Icinga Framework
252280
Use-Icinga;
253281

282+
if ($DeveloperMode) {
283+
$Global:Icinga.Protected.DeveloperMode = $TRUE;
284+
Copy-IcingaFrameworkCacheTemplate;
285+
}
286+
254287
$Host.UI.RawUI.WindowTitle = ([string]::Format('Icinga for Windows {0}', $Version));
255288

256289
# Set the location to the Icinga Framework module folder
@@ -274,7 +307,7 @@ function Invoke-IcingaCommand()
274307
return "> "
275308
}
276309

277-
} -Args $ScriptBlock, $PSScriptRoot, $IcingaFrameworkData.PrivateData.Version, ([bool]$Shell), $ArgumentList;
310+
} -Args $ScriptBlock, $PSScriptRoot, $IcingaFrameworkData.PrivateData.Version, ([bool]$Shell), $ArgumentList, $DeveloperMode;
278311
}
279312

280313
function Start-IcingaShellAsUser()

lib/core/framework/New-IcingaEnvironmentVariable.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function New-IcingaEnvironmentVariable()
5757
if ($Global:Icinga.ContainsKey('Protected') -eq $FALSE) {
5858
$Global:Icinga.Add('Protected', @{ });
5959

60+
$Global:Icinga.Protected.Add('DeveloperMode', $FALSE);
6061
$Global:Icinga.Protected.Add('DebugMode', $FALSE);
6162
$Global:Icinga.Protected.Add('JEAContext', $FALSE);
6263
$Global:Icinga.Protected.Add('RunAsDaemon', $FALSE);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<#
2+
### Note ###
3+
4+
This file is shipping plain with Icinga for Windows for each version.
5+
Once the module is loaded, this content will entirely be replaced with
6+
all modules and components shipped by the Icinga PowerShell Framework.
7+
8+
Manually enabling the feature is no longer required.
9+
#>
10+
11+
# Ensures that VS Code is not generating the cache file
12+
if ($null -ne $env:TERM_PROGRAM) {
13+
Write-IcingaFrameworkCodeCache -DeveloperMode;
14+
return;
15+
}
16+
17+
Write-IcingaFrameworkCodeCache;
18+
19+
Import-Module icinga-powershell-framework -Global -Force;
20+
Import-Module icinga-powershell-framework -Force;
21+
22+
if ($null -ne $env:TERM_PROGRAM -Or $Global:Icinga.Protected.DeveloperMode) {
23+
Copy-IcingaFrameworkCacheTemplate;
24+
}

0 commit comments

Comments
 (0)