From b4a4ac3312097f1ff70b55836a881cf331e7c97d Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Sat, 24 Jan 2026 21:21:42 -0500 Subject: [PATCH] Update src/core.ps1 --- src/core.ps1 | 43 ++++++++++++------------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/src/core.ps1 b/src/core.ps1 index 51ad763..5cc82df 100644 --- a/src/core.ps1 +++ b/src/core.ps1 @@ -1,27 +1,27 @@ # --- core.ps1: ensure functions are available without double-loading --- - function Ensure-SamyFunctionsLoaded { [CmdletBinding()] param() # Pick a "canary" function that DEFINITELY lives in samy.functions.ps1 - # Replace this with a function name you know is in samy.functions.ps1. + # If you rename/move functions, update this value. $canary = 'Initialize-NuGetProvider' + # If bootstrap already loaded samy.functions.ps1, we are done. if (Get-Command $canary -ErrorAction SilentlyContinue) { - # Functions already loaded by bootstrap (or previously in session) return } - # If SamyFunctionsUrl isn't set, build it from known bases + # If SamyFunctionsUrl isn't set, build it from known bases. + # This supports IWR ... | iex runs where PSScriptRoot/PSCommandPath are blank. if ([string]::IsNullOrWhiteSpace($Script:SamyFunctionsUrl)) { if (-not [string]::IsNullOrWhiteSpace($Script:ChunkBase)) { - # Bootstrap path (matches your chunk downloader) + # Bootstrap path (matches the chunk downloader) $Script:SamyFunctionsUrl = "$Script:ChunkBase/samy.functions.ps1?raw=1" } elseif (-not [string]::IsNullOrWhiteSpace($Script:SamyGitRepo)) { - # Repo base (your config usually sets this) + # Repo base (config.ps1 usually sets this) $Script:SamyFunctionsUrl = "$Script:SamyGitRepo/src/samy.functions.ps1?raw=1" } elseif ( @@ -41,6 +41,10 @@ function Ensure-SamyFunctionsLoaded { try { $functionsContent = (Invoke-WebRequest -UseBasicParsing -Uri $Script:SamyFunctionsUrl -ErrorAction Stop).Content + if ([string]::IsNullOrWhiteSpace($functionsContent)) { + throw "Downloaded content was empty." + } + . ([ScriptBlock]::Create($functionsContent)) } catch { @@ -88,31 +92,8 @@ function Invoke-ScriptAutomationMonkey { # Initialize config + URLs (moved out of core body) Initialize-SamyConfig - # Load remote functions (kept as-is, including verification style) - try { - # Write-Host "[Info] Loading functions from: $Script:SamyFunctionsUrl" -ForegroundColor Cyan - #$functionsContent = (Invoke-WebRequest -UseBasicParsing $Script:SamyFunctionsUrl -ErrorAction Stop).Content - - if ([string]::IsNullOrWhiteSpace($functionsContent)) { - throw "Downloaded content was empty." - } - - if ($functionsContent -notmatch '(?im)^\s*function\s+Initialize-NuGetProvider\b') { - Write-Host "[Warning] samy.functions.ps1 loaded, but Initialize-NuGetProvider not found in content." -ForegroundColor Yellow - } - - . ([ScriptBlock]::Create($functionsContent)) - - if (Get-Command Initialize-NuGetProvider -ErrorAction SilentlyContinue) { - Write-Host "[Success] Initialize-NuGetProvider is loaded and available." -ForegroundColor Green - } - else { - throw "Dot-sourcing completed, but Initialize-NuGetProvider is still not available." - } - } - catch { - throw "Failed to load samy.functions.ps1 from $Script:SamyFunctionsUrl. $($_.Exception.Message)" - } + # Ensure core functions exist (bootstrap may have already loaded them) + Ensure-SamyFunctionsLoaded if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) { $Global:LogCache = [System.Collections.ArrayList]::new()