Refactor: Move Winget download logic to core module

Extracts the Winget app download logic from the main UI script into a new `Invoke-WingetDownload` function within the core Winget module.

This change decouples the UI event handling from the business logic, improving modularity and maintainability. It also introduces more robust error handling for the download process by wrapping the logic in a try/catch block.
This commit is contained in:
rbalsleyMSFT
2025-06-19 12:23:13 -07:00
parent 00ee79d33c
commit 12607cca44
3 changed files with 68 additions and 53 deletions
-49
View File
@@ -187,55 +187,6 @@ $window.Add_Loaded({
$script:uiState.Controls.wingetPanel.Visibility = if ($script:uiState.Controls.chkInstallWingetApps.IsChecked) { 'Visible' } else { 'Collapsed' }
$script:uiState.Controls.wingetSearchPanel.Visibility = 'Collapsed' # Keep search hidden initially
$script:uiState.Controls.btnDownloadSelected.Add_Click({
param($buttonSender, $clickEventArgs)
$selectedApps = $script:uiState.Controls.lstWingetResults.Items | Where-Object { $_.IsSelected }
if (-not $selectedApps) {
[System.Windows.MessageBox]::Show("No applications selected to download.", "Download Winget Apps", "OK", "Information")
return
}
$buttonSender.IsEnabled = $false
$script:uiState.Controls.pbOverallProgress.Visibility = 'Visible'
$script:uiState.Controls.pbOverallProgress.Value = 0
$script:uiState.Controls.txtStatus.Text = "Starting Winget app downloads..."
# Define necessary task-specific variables locally
$localAppsPath = $script:uiState.Controls.txtApplicationPath.Text
$localAppListJsonPath = $script:uiState.Controls.txtAppListJsonPath.Text
$localWindowsArch = $script:uiState.Controls.cmbWindowsArch.SelectedItem
$localOrchestrationPath = Join-Path -Path $script:uiState.Controls.txtApplicationPath.Text -ChildPath "Orchestration"
# Create hashtable for task-specific arguments to pass to Invoke-ParallelProcessing
$taskArguments = @{
AppsPath = $localAppsPath
AppListJsonPath = $localAppListJsonPath
WindowsArch = $localWindowsArch
OrchestrationPath = $localOrchestrationPath
}
# Select only necessary properties before passing to Invoke-ParallelProcessing
$itemsToProcess = $selectedApps | Select-Object Name, Id, Source, Version # Include Version if needed
# Invoke the centralized parallel processing function
# Pass task type and task-specific arguments
Invoke-ParallelProcessing -ItemsToProcess $itemsToProcess `
-ListViewControl $script:uiState.Controls.lstWingetResults `
-IdentifierProperty 'Id' `
-StatusProperty 'DownloadStatus' `
-TaskType 'WingetDownload' `
-TaskArguments $taskArguments `
-CompletedStatusText "Completed" `
-ErrorStatusPrefix "Error: " `
-WindowObject $window `
-MainThreadLogPath $script:uiState.LogFilePath
# Final status update (handled by Invoke-ParallelProcessing)
$script:uiState.Controls.pbOverallProgress.Visibility = 'Collapsed'
$buttonSender.IsEnabled = $true
})
# BYO Apps UI logic (Keep existing logic)
$script:uiState.Controls.btnBrowseAppSource.Add_Click({
$selectedPath = Show-ModernFolderPicker -Title "Select Application Source Folder"
@@ -423,6 +423,13 @@ function Register-EventHandlers {
-PostClearAction $postClearScriptBlock
})
$State.Controls.btnDownloadSelected.Add_Click({
param($eventSource, $routedEventArgs)
$window = [System.Windows.Window]::GetWindow($eventSource)
$localState = $window.Tag
Invoke-WingetDownload -State $localState -Button $eventSource
})
# M365 Apps/Office tab Event
$State.Controls.chkInstallOffice.Add_Checked({
param($eventSource, $routedEventArgs)
@@ -686,6 +686,67 @@ function Start-WingetAppDownloadTask {
return $returnObject
}
function Invoke-WingetDownload {
param(
[psobject]$State,
[object]$Button
)
try {
$selectedApps = $State.Controls.lstWingetResults.Items | Where-Object { $_.IsSelected }
if (-not $selectedApps) {
[System.Windows.MessageBox]::Show("No applications selected to download.", "Download Winget Apps", "OK", "Information")
return
}
$Button.IsEnabled = $false
$State.Controls.pbOverallProgress.Visibility = 'Visible'
$State.Controls.pbOverallProgress.Value = 0
$State.Controls.txtStatus.Text = "Starting Winget app downloads..."
# Define necessary task-specific variables locally
$localAppsPath = $State.Controls.txtApplicationPath.Text
$localAppListJsonPath = $State.Controls.txtAppListJsonPath.Text
$localWindowsArch = $State.Controls.cmbWindowsArch.SelectedItem
$localOrchestrationPath = Join-Path -Path $State.Controls.txtApplicationPath.Text -ChildPath "Orchestration"
# Create hashtable for task-specific arguments to pass to Invoke-ParallelProcessing
$taskArguments = @{
AppsPath = $localAppsPath
AppListJsonPath = $localAppListJsonPath
WindowsArch = $localWindowsArch
OrchestrationPath = $localOrchestrationPath
}
# Select only necessary properties before passing to Invoke-ParallelProcessing
$itemsToProcess = $selectedApps | Select-Object Name, Id, Source, Version # Include Version if needed
# Invoke the centralized parallel processing function
# Pass task type and task-specific arguments
Invoke-ParallelProcessing -ItemsToProcess $itemsToProcess `
-ListViewControl $State.Controls.lstWingetResults `
-IdentifierProperty 'Id' `
-StatusProperty 'DownloadStatus' `
-TaskType 'WingetDownload' `
-TaskArguments $taskArguments `
-CompletedStatusText "Completed" `
-ErrorStatusPrefix "Error: " `
-WindowObject $State.Window `
-MainThreadLogPath $State.LogFilePath
# Final status update is handled by Invoke-ParallelProcessing, but we need to re-enable the button
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
$Button.IsEnabled = $true
}
catch {
WriteLog "FATAL Error in Invoke-WingetDownload: $($_.Exception.ToString())"
[System.Windows.MessageBox]::Show("A critical error occurred while starting the Winget download: $($_.Exception.Message)", "Error", "OK", "Error")
# Reset UI state on error
if ($Button) { $Button.IsEnabled = $true }
if ($State.Controls.pbOverallProgress) { $State.Controls.pbOverallProgress.Visibility = 'Collapsed' }
if ($State.Controls.txtStatus) { $State.Controls.txtStatus.Text = "Winget download failed to start." }
}
}
function Update-WingetVersionFields {
param(
[psobject]$State,
@@ -699,8 +760,4 @@ function Update-WingetVersionFields {
})
}
# --------------------------------------------------------------------------
# SECTION: Module Export
# --------------------------------------------------------------------------
Export-ModuleMember -Function *