mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Refactors driver download logic into a dedicated function
Extracts the driver download implementation from the button click event handler into a new `Invoke-DownloadSelectedDrivers` function. This change improves code modularity and maintainability by separating the business logic for downloading drivers from the UI event handling code. The event handler is now simplified to a single function call.
This commit is contained in:
@@ -512,4 +512,105 @@ function Invoke-GetModels {
|
||||
}
|
||||
}
|
||||
|
||||
# Function to handle the 'Download Selected Drivers' button click logic
|
||||
function Invoke-DownloadSelectedDrivers {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[psobject]$State,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[object]$Button
|
||||
)
|
||||
|
||||
$selectedDrivers = @($State.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })
|
||||
if (-not $selectedDrivers) {
|
||||
[System.Windows.MessageBox]::Show("No drivers selected to download.", "Download Drivers", "OK", "Information")
|
||||
return
|
||||
}
|
||||
|
||||
$Button.IsEnabled = $false
|
||||
$State.Controls.pbOverallProgress.Visibility = 'Visible'
|
||||
$State.Controls.pbOverallProgress.Value = 0
|
||||
$State.Controls.txtStatus.Text = "Preparing driver downloads..."
|
||||
|
||||
# Define common necessary task-specific variables locally
|
||||
# Ensure required selections are made
|
||||
if ($null -eq $State.Controls.cmbWindowsRelease.SelectedItem) {
|
||||
[System.Windows.MessageBox]::Show("Please select a Windows Release.", "Missing Information", "OK", "Warning")
|
||||
$Button.IsEnabled = $true
|
||||
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$State.Controls.txtStatus.Text = "Driver download cancelled."
|
||||
return
|
||||
}
|
||||
if ($null -eq $State.Controls.cmbWindowsArch.SelectedItem) {
|
||||
[System.Windows.MessageBox]::Show("Please select a Windows Architecture.", "Missing Information", "OK", "Warning")
|
||||
$Button.IsEnabled = $true
|
||||
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$State.Controls.txtStatus.Text = "Driver download cancelled."
|
||||
return
|
||||
}
|
||||
if (($selectedDrivers | Where-Object { $_.Make -eq 'HP' }) -and $null -ne $State.Controls.cmbWindowsVersion -and $null -eq $State.Controls.cmbWindowsVersion.SelectedItem) {
|
||||
[System.Windows.MessageBox]::Show("HP drivers are selected. Please select a Windows Version.", "Missing Information", "OK", "Warning")
|
||||
$Button.IsEnabled = $true
|
||||
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$State.Controls.txtStatus.Text = "Driver download cancelled."
|
||||
return
|
||||
}
|
||||
|
||||
$localDriversFolder = $State.Controls.txtDriversFolder.Text
|
||||
$localWindowsRelease = $State.Controls.cmbWindowsRelease.SelectedItem.Value
|
||||
$localWindowsArch = $State.Controls.cmbWindowsArch.SelectedItem
|
||||
$localWindowsVersion = if ($null -ne $State.Controls.cmbWindowsVersion -and $null -ne $State.Controls.cmbWindowsVersion.SelectedItem) { $State.Controls.cmbWindowsVersion.SelectedItem } else { $null }
|
||||
$coreStaticVars = Get-CoreStaticVariables
|
||||
$localHeaders = $coreStaticVars.Headers
|
||||
$localUserAgent = $coreStaticVars.UserAgent
|
||||
$compressDrivers = $State.Controls.chkCompressDriversToWIM.IsChecked
|
||||
|
||||
$State.Controls.txtStatus.Text = "Processing all selected drivers..."
|
||||
WriteLog "Processing all selected drivers: $($selectedDrivers.Model -join ', ')"
|
||||
|
||||
$taskArguments = @{
|
||||
DriversFolder = $localDriversFolder
|
||||
WindowsRelease = $localWindowsRelease
|
||||
WindowsArch = $localWindowsArch
|
||||
WindowsVersion = $localWindowsVersion
|
||||
Headers = $localHeaders
|
||||
UserAgent = $localUserAgent
|
||||
CompressToWim = $compressDrivers
|
||||
}
|
||||
|
||||
Invoke-ParallelProcessing -ItemsToProcess $selectedDrivers `
|
||||
-ListViewControl $State.Controls.lstDriverModels `
|
||||
-IdentifierProperty 'Model' `
|
||||
-StatusProperty 'DownloadStatus' `
|
||||
-TaskType 'DownloadDriverByMake' `
|
||||
-TaskArguments $taskArguments `
|
||||
-CompletedStatusText 'Completed' `
|
||||
-ErrorStatusPrefix 'Error: ' `
|
||||
-WindowObject $State.Window `
|
||||
-MainThreadLogPath $State.LogFilePath
|
||||
|
||||
$overallSuccess = $true
|
||||
# Check if any item has an error status after processing
|
||||
# We iterate over $State.Controls.lstDriverModels.Items because their DownloadStatus property was updated by Invoke-ParallelProcessing
|
||||
foreach ($item in ($State.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })) {
|
||||
# Check only originally selected items
|
||||
if ($item.DownloadStatus -like 'Error:*') {
|
||||
$overallSuccess = $false
|
||||
WriteLog "Error detected for model $($item.Model) (Make: $($item.Make)): $($item.DownloadStatus)"
|
||||
# No break here, log all errors
|
||||
}
|
||||
}
|
||||
|
||||
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$Button.IsEnabled = $true
|
||||
if ($overallSuccess) {
|
||||
$State.Controls.txtStatus.Text = "All selected driver downloads processed."
|
||||
[System.Windows.MessageBox]::Show("All selected driver downloads processed. Check status column for details.", "Download Process Finished", "OK", "Information")
|
||||
}
|
||||
else {
|
||||
$State.Controls.txtStatus.Text = "Driver downloads processed with some errors. Check status column and log."
|
||||
[System.Windows.MessageBox]::Show("Driver downloads processed, but some errors occurred. Please check the status column for each driver and the log file for details.", "Download Process Finished with Errors", "OK", "Warning")
|
||||
}
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function *
|
||||
@@ -672,100 +672,9 @@ function Register-EventHandlers {
|
||||
|
||||
$State.Controls.btnDownloadSelectedDrivers.Add_Click({
|
||||
param($buttonSender, $clickEventArgs)
|
||||
|
||||
$window = [System.Windows.Window]::GetWindow($buttonSender)
|
||||
$localState = $window.Tag
|
||||
|
||||
$selectedDrivers = @($localState.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })
|
||||
if (-not $selectedDrivers) {
|
||||
[System.Windows.MessageBox]::Show("No drivers selected to download.", "Download Drivers", "OK", "Information")
|
||||
return
|
||||
}
|
||||
|
||||
$buttonSender.IsEnabled = $false
|
||||
$localState.Controls.pbOverallProgress.Visibility = 'Visible'
|
||||
$localState.Controls.pbOverallProgress.Value = 0
|
||||
$localState.Controls.txtStatus.Text = "Preparing driver downloads..."
|
||||
|
||||
# Define common necessary task-specific variables locally
|
||||
# Ensure required selections are made
|
||||
if ($null -eq $localState.Controls.cmbWindowsRelease.SelectedItem) {
|
||||
[System.Windows.MessageBox]::Show("Please select a Windows Release.", "Missing Information", "OK", "Warning")
|
||||
$buttonSender.IsEnabled = $true
|
||||
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$localState.Controls.txtStatus.Text = "Driver download cancelled."
|
||||
return
|
||||
}
|
||||
if ($null -eq $localState.Controls.cmbWindowsArch.SelectedItem) {
|
||||
[System.Windows.MessageBox]::Show("Please select a Windows Architecture.", "Missing Information", "OK", "Warning")
|
||||
$buttonSender.IsEnabled = $true
|
||||
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$localState.Controls.txtStatus.Text = "Driver download cancelled."
|
||||
return
|
||||
}
|
||||
if (($selectedDrivers | Where-Object { $_.Make -eq 'HP' }) -and $null -ne $localState.Controls.cmbWindowsVersion -and $null -eq $localState.Controls.cmbWindowsVersion.SelectedItem) {
|
||||
[System.Windows.MessageBox]::Show("HP drivers are selected. Please select a Windows Version.", "Missing Information", "OK", "Warning")
|
||||
$buttonSender.IsEnabled = $true
|
||||
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$localState.Controls.txtStatus.Text = "Driver download cancelled."
|
||||
return
|
||||
}
|
||||
|
||||
$localDriversFolder = $localState.Controls.txtDriversFolder.Text
|
||||
$localWindowsRelease = $localState.Controls.cmbWindowsRelease.SelectedItem.Value
|
||||
$localWindowsArch = $localState.Controls.cmbWindowsArch.SelectedItem
|
||||
$localWindowsVersion = if ($null -ne $localState.Controls.cmbWindowsVersion -and $null -ne $localState.Controls.cmbWindowsVersion.SelectedItem) { $localState.Controls.cmbWindowsVersion.SelectedItem } else { $null }
|
||||
$coreStaticVars = Get-CoreStaticVariables
|
||||
$localHeaders = $coreStaticVars.Headers
|
||||
$localUserAgent = $coreStaticVars.UserAgent
|
||||
$compressDrivers = $localState.Controls.chkCompressDriversToWIM.IsChecked
|
||||
|
||||
$localState.Controls.txtStatus.Text = "Processing all selected drivers..."
|
||||
WriteLog "Processing all selected drivers: $($selectedDrivers.Model -join ', ')"
|
||||
|
||||
$taskArguments = @{
|
||||
DriversFolder = $localDriversFolder
|
||||
WindowsRelease = $localWindowsRelease
|
||||
WindowsArch = $localWindowsArch
|
||||
WindowsVersion = $localWindowsVersion
|
||||
Headers = $localHeaders
|
||||
UserAgent = $localUserAgent
|
||||
CompressToWim = $compressDrivers
|
||||
}
|
||||
|
||||
Invoke-ParallelProcessing -ItemsToProcess $selectedDrivers `
|
||||
-ListViewControl $localState.Controls.lstDriverModels `
|
||||
-IdentifierProperty 'Model' `
|
||||
-StatusProperty 'DownloadStatus' `
|
||||
-TaskType 'DownloadDriverByMake' `
|
||||
-TaskArguments $taskArguments `
|
||||
-CompletedStatusText 'Completed' `
|
||||
-ErrorStatusPrefix 'Error: ' `
|
||||
-WindowObject $window `
|
||||
-MainThreadLogPath $localState.LogFilePath
|
||||
|
||||
$overallSuccess = $true
|
||||
# Check if any item has an error status after processing
|
||||
# We iterate over $localState.Controls.lstDriverModels.Items because their DownloadStatus property was updated by Invoke-ParallelProcessing
|
||||
foreach ($item in ($localState.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })) {
|
||||
# Check only originally selected items
|
||||
if ($item.DownloadStatus -like 'Error:*') {
|
||||
$overallSuccess = $false
|
||||
WriteLog "Error detected for model $($item.Model) (Make: $($item.Make)): $($item.DownloadStatus)"
|
||||
# No break here, log all errors
|
||||
}
|
||||
}
|
||||
|
||||
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||
$buttonSender.IsEnabled = $true
|
||||
if ($overallSuccess) {
|
||||
$localState.Controls.txtStatus.Text = "All selected driver downloads processed."
|
||||
[System.Windows.MessageBox]::Show("All selected driver downloads processed. Check status column for details.", "Download Process Finished", "OK", "Information")
|
||||
}
|
||||
else {
|
||||
$localState.Controls.txtStatus.Text = "Driver downloads processed with some errors. Check status column and log."
|
||||
[System.Windows.MessageBox]::Show("Driver downloads processed, but some errors occurred. Please check the status column for each driver and the log file for details.", "Download Process Finished with Errors", "OK", "Warning")
|
||||
}
|
||||
Invoke-DownloadSelectedDrivers -State $localState -Button $buttonSender
|
||||
})
|
||||
|
||||
$State.Controls.btnClearDriverList.Add_Click({
|
||||
|
||||
Reference in New Issue
Block a user