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.
This commit is contained in:
rbalsleyMSFT
2025-07-18 17:25:39 -07:00
parent c535126605
commit 9df663dc9b
4 changed files with 31 additions and 30 deletions
+2 -11
View File
@@ -622,15 +622,6 @@ if ($WindowsSKU -like "*LTS*") {
# Set the log path for the common logger # Set the log path for the common logger
Set-CommonCoreLogPath -Path $LogFile 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 #FUNCTIONS
@@ -4159,7 +4150,7 @@ if ($InstallApps) {
# If there are no existing apps, use the original AppList.json directly # If there are no existing apps, use the original AppList.json directly
if (-not $hasExistingApps) { if (-not $hasExistingApps) {
WriteLog "No existing applications found. Using original AppList.json for all apps." 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 { else {
# Compare apps in AppList.json with existing installations # Compare apps in AppList.json with existing installations
@@ -4210,7 +4201,7 @@ if ($InstallApps) {
# Download missing apps # Download missing apps
WriteLog "Downloading missing applications" WriteLog "Downloading missing applications"
Get-Apps -AppList $modifiedAppListPath Get-Apps -AppList $modifiedAppListPath -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath
# Cleanup modified app list # Cleanup modified app list
Remove-Item -Path $modifiedAppListPath -Force Remove-Item -Path $modifiedAppListPath -Force
@@ -153,13 +153,6 @@ function Invoke-ParallelProcessing {
# Set the log path for this parallel thread # Set the log path for this parallel thread
Set-CommonCoreLogPath -Path $localJobArgs['_currentLogFilePathForJob'] 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 # Execute the appropriate background task based on $localTaskType
switch ($localTaskType) { switch ($localTaskType) {
'WingetDownload' { 'WingetDownload' {
@@ -16,7 +16,13 @@ function Get-Application {
[string]$AppId, [string]$AppId,
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateSet('winget', 'msstore')] [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 # 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 # If app is in Win32 folder, add the silent install command to the WinGetWin32Apps.json file
elseif ($appFolderPath -match 'Win32') { elseif ($appFolderPath -match 'Win32') {
WriteLog "$AppName is a Win32 app. Adding silent install command to $orchestrationpath\WinGetWin32Apps.json" WriteLog "$AppName is a Win32 app. Adding silent install command to $OrchestrationPath\WinGetWin32Apps.json"
$result = Add-Win32SilentInstallCommand -AppFolder $AppName -AppFolderPath $appFolderPath $result = Add-Win32SilentInstallCommand -AppFolder $AppName -AppFolderPath $appFolderPath -OrchestrationPath $OrchestrationPath
} }
else { else {
# For any other case, set result to 0 (success) # For any other case, set result to 0 (success)
@@ -164,7 +170,13 @@ function Get-Apps {
[CmdletBinding()] [CmdletBinding()]
param ( param (
[Parameter(Mandatory = $true)] [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 # Load and validate app list
@@ -189,7 +201,7 @@ function Get-Apps {
} }
# Ensure WinGet is available # Ensure WinGet is available
Confirm-WinGetInstallation Confirm-WinGetInstallation -WindowsArch $WindowsArch
# Create necessary folders # Create necessary folders
$win32Folder = Join-Path -Path $AppsPath -ChildPath "Win32" $win32Folder = Join-Path -Path $AppsPath -ChildPath "Win32"
@@ -205,7 +217,7 @@ function Get-Apps {
foreach ($wingetApp in $wingetApps) { foreach ($wingetApp in $wingetApps) {
try { 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 { catch {
WriteLog "Error occurred while processing $($wingetApp.Name): $_" WriteLog "Error occurred while processing $($wingetApp.Name): $_"
@@ -222,7 +234,7 @@ function Get-Apps {
foreach ($storeApp in $StoreApps) { foreach ($storeApp in $StoreApps) {
try { 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 { catch {
WriteLog "Error occurred while processing $($storeApp.Name): $_" WriteLog "Error occurred while processing $($storeApp.Name): $_"
@@ -257,7 +269,10 @@ function Install-WinGet {
} }
function Confirm-WinGetInstallation { function Confirm-WinGetInstallation {
[CmdletBinding()] [CmdletBinding()]
param() param(
[Parameter(Mandatory = $true)]
[string]$WindowsArch
)
WriteLog 'Checking if WinGet is installed...' WriteLog 'Checking if WinGet is installed...'
$minVersion = [version]"1.8.1911" $minVersion = [version]"1.8.1911"
@@ -304,7 +319,9 @@ function Confirm-WinGetInstallation {
function Add-Win32SilentInstallCommand { function Add-Win32SilentInstallCommand {
param ( param (
[string]$AppFolder, [string]$AppFolder,
[string]$AppFolderPath [string]$AppFolderPath,
[Parameter(Mandatory = $true)]
[string]$OrchestrationPath
) )
$appName = $AppFolder $appName = $AppFolder
$installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include "*.exe", "*.msi" -File -ErrorAction Stop $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include "*.exe", "*.msi" -File -ErrorAction Stop
@@ -331,7 +348,7 @@ function Add-Win32SilentInstallCommand {
} }
# Path to the JSON file # Path to the JSON file
$wingetWin32AppsJson = "$orchestrationPath\WinGetWin32Apps.json" $wingetWin32AppsJson = "$OrchestrationPath\WinGetWin32Apps.json"
# Initialize or load existing JSON data # Initialize or load existing JSON data
if (Test-Path -Path $wingetWin32AppsJson) { if (Test-Path -Path $wingetWin32AppsJson) {
@@ -593,8 +593,8 @@ function Start-WingetAppDownloadTask {
} }
try { try {
# Call Get-Application (ensure it's available via dot-sourcing and uses $global:LogFile) # Call Get-Application
$resultCode = Get-Application -AppName $appName -AppId $appId -Source $source -ErrorAction Stop $resultCode = Get-Application -AppName $appName -AppId $appId -Source $source -AppsPath $AppsPath -WindowsArch $WindowsArch -OrchestrationPath $OrchestrationPath -ErrorAction Stop
# Determine status based on result code # Determine status based on result code
switch ($resultCode) { switch ($resultCode) {