From 9df663dc9b9a70317afa9a46491d93348f12d98b Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Fri, 18 Jul 2025 17:25:39 -0700 Subject: [PATCH] Refactor to eliminate global variable dependencies Removes the use of global variables for paths and architecture settings. Instead, these values are now passed explicitly as parameters to the relevant functions. This change improves code clarity, reduces the risk of side effects, and makes the functions more modular and easier to test. --- FFUDevelopment/BuildFFUVM.ps1 | 13 +------ .../FFU.Common/FFU.Common.Parallel.psm1 | 7 ---- .../FFU.Common/FFU.Common.Winget.psm1 | 37 ++++++++++++++----- .../FFUUI.Core/FFUUI.Core.Winget.psm1 | 4 +- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index 22aadf7..7dd386a 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -622,15 +622,6 @@ if ($WindowsSKU -like "*LTS*") { # Set the log path for the common logger Set-CommonCoreLogPath -Path $LogFile -# Set critical paths and configuration as global variables for module access -# This is done after Set-CommonCoreLogPath so this action itself can be logged. -# Ensure $AppsPath, $orchestrationPath, and $WindowsArch are fully initialized before this point. -$global:AppsPath = $AppsPath -$global:orchestrationPath = $orchestrationPath -$global:WindowsArch = $WindowsArch -WriteLog "Global script variables set for module access: AppsPath='$global:AppsPath', orchestrationPath='$global:orchestrationPath', WindowsArch='$global:WindowsArch'" - - #FUNCTIONS @@ -4159,7 +4150,7 @@ if ($InstallApps) { # If there are no existing apps, use the original AppList.json directly if (-not $hasExistingApps) { WriteLog "No existing applications found. Using original AppList.json for all apps." - Get-Apps -AppList $AppListPath + Get-Apps -AppList $AppListPath -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath } else { # Compare apps in AppList.json with existing installations @@ -4210,7 +4201,7 @@ if ($InstallApps) { # Download missing apps WriteLog "Downloading missing applications" - Get-Apps -AppList $modifiedAppListPath + Get-Apps -AppList $modifiedAppListPath -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath # Cleanup modified app list Remove-Item -Path $modifiedAppListPath -Force diff --git a/FFUDevelopment/FFU.Common/FFU.Common.Parallel.psm1 b/FFUDevelopment/FFU.Common/FFU.Common.Parallel.psm1 index 6808c5b..8388b36 100644 --- a/FFUDevelopment/FFU.Common/FFU.Common.Parallel.psm1 +++ b/FFUDevelopment/FFU.Common/FFU.Common.Parallel.psm1 @@ -153,13 +153,6 @@ function Invoke-ParallelProcessing { # Set the log path for this parallel thread Set-CommonCoreLogPath -Path $localJobArgs['_currentLogFilePathForJob'] - # Set other global variables if tasks rely on them (prefer passing as parameters) - $global:AppsPath = $localJobArgs['AppsPath'] - $global:WindowsArch = $localJobArgs['WindowsArch'] - if ($localJobArgs.ContainsKey('OrchestrationPath')) { - $global:OrchestrationPath = $localJobArgs['OrchestrationPath'] - } - # Execute the appropriate background task based on $localTaskType switch ($localTaskType) { 'WingetDownload' { diff --git a/FFUDevelopment/FFU.Common/FFU.Common.Winget.psm1 b/FFUDevelopment/FFU.Common/FFU.Common.Winget.psm1 index 892eb7e..d33b3c2 100644 --- a/FFUDevelopment/FFU.Common/FFU.Common.Winget.psm1 +++ b/FFUDevelopment/FFU.Common/FFU.Common.Winget.psm1 @@ -16,7 +16,13 @@ function Get-Application { [string]$AppId, [Parameter(Mandatory = $true)] [ValidateSet('winget', 'msstore')] - [string]$Source + [string]$Source, + [Parameter(Mandatory = $true)] + [string]$AppsPath, + [Parameter(Mandatory = $true)] + [string]$WindowsArch, + [Parameter(Mandatory = $true)] + [string]$OrchestrationPath ) # Validate app exists in repository @@ -109,8 +115,8 @@ function Get-Application { } # If app is in Win32 folder, add the silent install command to the WinGetWin32Apps.json file elseif ($appFolderPath -match 'Win32') { - WriteLog "$AppName is a Win32 app. Adding silent install command to $orchestrationpath\WinGetWin32Apps.json" - $result = Add-Win32SilentInstallCommand -AppFolder $AppName -AppFolderPath $appFolderPath + WriteLog "$AppName is a Win32 app. Adding silent install command to $OrchestrationPath\WinGetWin32Apps.json" + $result = Add-Win32SilentInstallCommand -AppFolder $AppName -AppFolderPath $appFolderPath -OrchestrationPath $OrchestrationPath } else { # For any other case, set result to 0 (success) @@ -164,7 +170,13 @@ function Get-Apps { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] - [string]$AppList + [string]$AppList, + [Parameter(Mandatory = $true)] + [string]$AppsPath, + [Parameter(Mandatory = $true)] + [string]$WindowsArch, + [Parameter(Mandatory = $true)] + [string]$OrchestrationPath ) # Load and validate app list @@ -189,7 +201,7 @@ function Get-Apps { } # Ensure WinGet is available - Confirm-WinGetInstallation + Confirm-WinGetInstallation -WindowsArch $WindowsArch # Create necessary folders $win32Folder = Join-Path -Path $AppsPath -ChildPath "Win32" @@ -205,7 +217,7 @@ function Get-Apps { foreach ($wingetApp in $wingetApps) { try { - Get-Application -AppName $wingetApp.Name -AppId $wingetApp.Id -Source 'winget' + Get-Application -AppName $wingetApp.Name -AppId $wingetApp.Id -Source 'winget' -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath } catch { WriteLog "Error occurred while processing $($wingetApp.Name): $_" @@ -222,7 +234,7 @@ function Get-Apps { foreach ($storeApp in $StoreApps) { try { - Get-Application -AppName $storeApp.Name -AppId $storeApp.Id -Source 'msstore' + Get-Application -AppName $storeApp.Name -AppId $storeApp.Id -Source 'msstore' -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath } catch { WriteLog "Error occurred while processing $($storeApp.Name): $_" @@ -257,7 +269,10 @@ function Install-WinGet { } function Confirm-WinGetInstallation { [CmdletBinding()] - param() + param( + [Parameter(Mandatory = $true)] + [string]$WindowsArch + ) WriteLog 'Checking if WinGet is installed...' $minVersion = [version]"1.8.1911" @@ -304,7 +319,9 @@ function Confirm-WinGetInstallation { function Add-Win32SilentInstallCommand { param ( [string]$AppFolder, - [string]$AppFolderPath + [string]$AppFolderPath, + [Parameter(Mandatory = $true)] + [string]$OrchestrationPath ) $appName = $AppFolder $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include "*.exe", "*.msi" -File -ErrorAction Stop @@ -331,7 +348,7 @@ function Add-Win32SilentInstallCommand { } # Path to the JSON file - $wingetWin32AppsJson = "$orchestrationPath\WinGetWin32Apps.json" + $wingetWin32AppsJson = "$OrchestrationPath\WinGetWin32Apps.json" # Initialize or load existing JSON data if (Test-Path -Path $wingetWin32AppsJson) { diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Winget.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Winget.psm1 index 29e19de..aff001d 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Winget.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Winget.psm1 @@ -593,8 +593,8 @@ function Start-WingetAppDownloadTask { } try { - # Call Get-Application (ensure it's available via dot-sourcing and uses $global:LogFile) - $resultCode = Get-Application -AppName $appName -AppId $appId -Source $source -ErrorAction Stop + # Call Get-Application + $resultCode = Get-Application -AppName $appName -AppId $appId -Source $source -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath -ErrorAction Stop # Determine status based on result code switch ($resultCode) {