From 2d858a8a06df252a69b76e4da134d0622d17de88 Mon Sep 17 00:00:00 2001 From: Gal Szkolnik Date: Tue, 29 Jun 2021 09:43:27 -0400 Subject: [PATCH] Add SessionData class to setup script Added [SessionData] static class to setup script Utilized SessionData for scoop refreshing, so it will happen once per login session - otherwise it's a nuisance. --- Setup-ScriptEnv.ps1 | 69 ++++++++++++++++++-- scoop/profile.d/smart-refresh-on-startup.ps1 | 7 ++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Setup-ScriptEnv.ps1 b/Setup-ScriptEnv.ps1 index 126f314..e3d3083 100644 --- a/Setup-ScriptEnv.ps1 +++ b/Setup-ScriptEnv.ps1 @@ -34,12 +34,73 @@ Get-Command Reload-MyScripts -ErrorAction SilentlyContinue | ForEach-Object { . ####################################################################### function ProfileCode_common {[CmdletBinding()]param() function Get-PowerShellPath {[CmdletBinding()]param() - Get-Process -PID $PID | ForEach-Object { $_.Path, $_.Parent.Path } | Where-Object { $_ -match $('(powershell|pwsh)' + "`$") } | Select-Object -First 1 + Get-Process -PID $PID | ForEach-Object { $_.Path, $_.Parent.Path } | Where-Object { $_ -match $('(?:powershell|pwsh)(?:.exe)?' + "`$") } | Select-Object -First 1 } function ConvertTo-Base64 { -param([string]$String) - return [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($String)) +param([string]$String,[switch]$AsPowershellCommandLine) + return "$(if($AsPowershellCommandLine){"$(Get-PowerShellPath) -EncodedCommand "} )$([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($String)))" +} + +class SessionData { + static [hashtable]$Data = [ordered]@{} + static [string]$Path = $null + + static [void] Init() { + if( -not [SessionData]::Data.SessionID ) { + [SessionData]::Data.SessionID = + Get-CimInstance -ClassName Win32_Service -Filter 'State="Running" AND DisplayName like "%[_]%"' -Property 'DisplayName','Name' | + Select-Object -ExpandProperty DisplayName | + ForEach-Object { + $_ -split '_' | Select-Object -Last 1 + } | + Where-Object { $_ -notin ('agent') } | + Select-Object -First 1 + + [SessionData]::Data.CreateTimestamp = Get-Date + + [SessionData]::Path = Join-Path $env:tmp "session.store.$([SessionData]::Data.SessionID).xmlcli" + } + } + + static [int] GetSessionID() { + if( -not [SessionData]::Data.SessionID ) { + [SessionData]::Init() + + [SessionData]::Save() + } + return [SessionData]::Data.SessionID + } + + static [void] Save() { + if( -not [SessionData]::Path ) { [SessionData]::Init() } + + [SessionData]::Data | Export-Clixml -Path $([SessionData]::Path) + } + + static [void] Load() { + if( -not [SessionData]::Path ) { [SessionData]::Init() } + + [SessionData]::Data = Import-Clixml -Path $([SessionData]::Path) + } + + static [void] Set([string]$Name,[object]$Value) { + $null = [SessionData]::GetSessionID() + [SessionData]::Data[$Name] = $Value + [SessionData]::Save() + } + + static [object] Get([string]$Name,[object]$DefaultValue=$null) { + [SessionData]::Load() + if( -not [SessionData]::Data.ContainsKey($Name) ) { + if( $DefaultValue ) { + [SessionData]::Set($Name,$DefaultValue) + } else { + return $DefaultValue + } + } + return [SessionData]::Data[$Name] + } } function Invoke-ExpressionEx { @@ -428,7 +489,7 @@ function _Init { } } else { - $status = "Need $status" + #$status = $status -replace "Need ", '' -replace '^ ?','Need ' Write-Error "Cannot write into [$_]'$p'! Please re-run with sudo." } } diff --git a/scoop/profile.d/smart-refresh-on-startup.ps1 b/scoop/profile.d/smart-refresh-on-startup.ps1 index 8e30394..6d9446b 100644 --- a/scoop/profile.d/smart-refresh-on-startup.ps1 +++ b/scoop/profile.d/smart-refresh-on-startup.ps1 @@ -1,3 +1,10 @@ +param([switch]$Force) + +if( -not $global:ThisSeszion -or $Force ) { + $global:MyModulesWarning = @() +} + + scoop autocomplete-on if( Test-Path (Join-Path $env:SCOOP last-scoop-refresh.clixml) ) {