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-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
@@ -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' {
@@ -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) {
@@ -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) {