mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
feat: Add restore defaults and centralize cleanup logic
Introduces a "Restore Defaults" feature in the UI to reset the environment. This action removes generated configuration files, ISOs, downloaded apps, updates, drivers, and FFUs. The post-build cleanup logic is refactored from the main build script into a new common function. This new function is used by both the standard build process and the new restore defaults feature, promoting code reuse and simplifying maintenance.
This commit is contained in:
@@ -664,6 +664,126 @@ function Invoke-SaveConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-RestoreDefaults {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[psobject]$State
|
||||
)
|
||||
try {
|
||||
$rootPath = $State.FFUDevelopmentPath
|
||||
|
||||
# Normalize potential array values to single strings
|
||||
function Normalize-PathScalar {
|
||||
param([object]$value)
|
||||
if ($null -eq $value) { return $null }
|
||||
if ($value -is [System.Array]) {
|
||||
foreach ($v in $value) {
|
||||
if (-not [string]::IsNullOrWhiteSpace([string]$v)) {
|
||||
return [string]$v
|
||||
}
|
||||
}
|
||||
return $null
|
||||
}
|
||||
return [string]$value
|
||||
}
|
||||
|
||||
$appsPath = Join-Path $rootPath 'Apps'
|
||||
$driversRaw = Normalize-PathScalar -value $State.Controls.txtDriversFolder.Text
|
||||
if ([string]::IsNullOrWhiteSpace($driversRaw)) {
|
||||
$driversPath = Join-Path $rootPath 'Drivers'
|
||||
}
|
||||
else {
|
||||
$driversPath = $driversRaw
|
||||
}
|
||||
$ffuCaptureRaw = Normalize-PathScalar -value $State.Controls.txtFFUCaptureLocation.Text
|
||||
$ffuCapturePath = if ([string]::IsNullOrWhiteSpace($ffuCaptureRaw)) { Join-Path $rootPath 'FFU' } else { $ffuCaptureRaw }
|
||||
|
||||
$captureISOPath = Join-Path $rootPath 'WinPECaptureFFUFiles\WinPE-Capture.iso'
|
||||
$deployISOPath = Join-Path $rootPath 'WinPEDeployFFUFiles\WinPE-Deploy.iso'
|
||||
$appsISOPath = Join-Path $rootPath 'Apps.iso'
|
||||
|
||||
$msg = "Restore Defaults will:`n`n- Delete generated config and app/driver list JSON files`n- Remove ISO files (Capture, Deploy, Apps) if present`n- Remove Apps/Update/downloaded artifacts`n- Remove driver folder contents (not the folder)`n- Remove FFU files in the capture folder`n`nSample/template files and VM/VHDX cache are NOT removed.`n`nProceed?"
|
||||
$result = [System.Windows.MessageBox]::Show($msg, "Confirm Restore Defaults", "YesNo", "Warning")
|
||||
if ($result -ne [System.Windows.MessageBoxResult]::Yes) {
|
||||
WriteLog "RestoreDefaults: User cancelled."
|
||||
return
|
||||
}
|
||||
|
||||
WriteLog "RestoreDefaults: Starting environment reset."
|
||||
WriteLog "RestoreDefaults: Paths -> Apps=$appsPath Drivers=$driversPath FFUCapture=$ffuCapturePath"
|
||||
|
||||
# Remove JSON artifact files if present
|
||||
$artifactFiles = @(
|
||||
(Join-Path $rootPath 'config\FFUConfig.json'),
|
||||
(Join-Path $appsPath 'AppList.json'),
|
||||
(Join-Path $driversPath 'Drivers.json'),
|
||||
(Join-Path $appsPath 'UserAppList.json')
|
||||
) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
|
||||
|
||||
foreach ($file in $artifactFiles) {
|
||||
if ((-not [string]::IsNullOrWhiteSpace($file)) -and (Test-Path -LiteralPath $file)) {
|
||||
try {
|
||||
WriteLog "RestoreDefaults: Removing $file"
|
||||
Remove-Item -LiteralPath $file -Force -ErrorAction Stop
|
||||
}
|
||||
catch {
|
||||
WriteLog "RestoreDefaults: Failed removing $file : $($_.Exception.Message)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Force all cleanup flags true
|
||||
Invoke-FFUPostBuildCleanup `
|
||||
-RootPath $rootPath `
|
||||
-AppsPath $appsPath `
|
||||
-DriversPath $driversPath `
|
||||
-FFUCapturePath $ffuCapturePath `
|
||||
-CaptureISOPath $captureISOPath `
|
||||
-DeployISOPath $deployISOPath `
|
||||
-AppsISOPath $appsISOPath `
|
||||
-RemoveCaptureISO:$true `
|
||||
-RemoveDeployISO:$true `
|
||||
-RemoveAppsISO:$true `
|
||||
-RemoveDrivers:$true `
|
||||
-RemoveFFU:$true `
|
||||
-RemoveApps:$true `
|
||||
-RemoveUpdates:$true
|
||||
|
||||
# Clear UI lists / state
|
||||
if ($null -ne $State.Data.allDriverModels) { $State.Data.allDriverModels.Clear() }
|
||||
if ($null -ne $State.Controls.lstDriverModels) { $State.Controls.lstDriverModels.Items.Refresh() }
|
||||
if ($null -ne $State.Controls.lstApplications) {
|
||||
try {
|
||||
if ($State.Controls.lstApplications.ItemsSource) { $State.Controls.lstApplications.ItemsSource = $null }
|
||||
$State.Controls.lstApplications.Items.Clear()
|
||||
} catch {}
|
||||
}
|
||||
if ($null -ne $State.Controls.lstWingetResults) {
|
||||
try {
|
||||
if ($State.Controls.lstWingetResults.ItemsSource) { $State.Controls.lstWingetResults.ItemsSource = $null }
|
||||
$State.Controls.lstWingetResults.Items.Clear()
|
||||
} catch {}
|
||||
}
|
||||
if ($null -ne $State.Controls.lstAppsScriptVariables) {
|
||||
try {
|
||||
if ($State.Controls.lstAppsScriptVariables.ItemsSource) { $State.Controls.lstAppsScriptVariables.ItemsSource = $null }
|
||||
$State.Controls.lstAppsScriptVariables.Items.Clear()
|
||||
} catch {}
|
||||
}
|
||||
|
||||
$State.Data.lastConfigFilePath = $null
|
||||
|
||||
Initialize-UIDefaults -State $State
|
||||
|
||||
WriteLog "RestoreDefaults: Completed."
|
||||
[System.Windows.MessageBox]::Show("Environment restored to defaults.", "Restore Defaults", "OK", "Information")
|
||||
}
|
||||
catch {
|
||||
WriteLog "RestoreDefaults: Failed with $($_.Exception.Message)"
|
||||
[System.Windows.MessageBox]::Show("Restore Defaults failed:`n$($_.Exception.Message)", "Error", "OK", "Error")
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-AutoLoadPreviousEnvironment {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
|
||||
@@ -216,11 +216,11 @@ function Register-EventHandlers {
|
||||
}
|
||||
else {
|
||||
$localState.Controls.txtCustomVMSwitchName.Visibility = 'Collapsed'
|
||||
if ($localState.Data.vmSwitchMap.ContainsKey($selectedItem)) {
|
||||
if ($null -ne $selectedItem -and $localState.Data.vmSwitchMap.ContainsKey($selectedItem)) {
|
||||
$localState.Controls.txtVMHostIPAddress.Text = $localState.Data.vmSwitchMap[$selectedItem]
|
||||
}
|
||||
else {
|
||||
$localState.Controls.txtVMHostIPAddress.Text = '' # Clear IP if not found in map
|
||||
$localState.Controls.txtVMHostIPAddress.Text = '' # Clear IP if not found or key null
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -950,6 +950,12 @@ function Register-EventHandlers {
|
||||
$localState = $window.Tag
|
||||
Invoke-LoadConfiguration -State $localState
|
||||
})
|
||||
$State.Controls.btnRestoreDefaults.Add_Click({
|
||||
param($eventSource, $routedEventArgs)
|
||||
$window = [System.Windows.Window]::GetWindow($eventSource)
|
||||
$localState = $window.Tag
|
||||
Invoke-RestoreDefaults -State $localState
|
||||
})
|
||||
$State.Controls.btnBuildConfig.Add_Click({
|
||||
param($eventSource, $routedEventArgs)
|
||||
$window = [System.Windows.Window]::GetWindow($eventSource)
|
||||
|
||||
@@ -170,6 +170,7 @@ function Initialize-UIControls {
|
||||
$State.Controls.btnBrowseDriversJsonPath = $window.FindName('btnBrowseDriversJsonPath')
|
||||
$State.Controls.chkUpdateADK = $window.FindName('chkUpdateADK')
|
||||
$State.Controls.btnLoadConfig = $window.FindName('btnLoadConfig')
|
||||
$State.Controls.btnRestoreDefaults = $window.FindName('btnRestoreDefaults')
|
||||
$State.Controls.btnBuildConfig = $window.FindName('btnBuildConfig')
|
||||
|
||||
# Monitor Tab
|
||||
@@ -198,11 +199,11 @@ function Initialize-VMSwitchData {
|
||||
if ($State.Controls.cmbVMSwitchName.Items.Count -gt 1) {
|
||||
$State.Controls.cmbVMSwitchName.SelectedIndex = 0
|
||||
$firstSwitch = $State.Controls.cmbVMSwitchName.SelectedItem
|
||||
if ($State.Data.vmSwitchMap.ContainsKey($firstSwitch)) {
|
||||
if ($null -ne $firstSwitch -and $State.Data.vmSwitchMap.ContainsKey($firstSwitch)) {
|
||||
$State.Controls.txtVMHostIPAddress.Text = $State.Data.vmSwitchMap[$firstSwitch]
|
||||
}
|
||||
else {
|
||||
$State.Controls.txtVMHostIPAddress.Text = $State.Defaults.generalDefaults.VMHostIPAddress # Use default if IP not found
|
||||
$State.Controls.txtVMHostIPAddress.Text = $State.Defaults.generalDefaults.VMHostIPAddress # Use default if IP not found or key null
|
||||
}
|
||||
$State.Controls.txtCustomVMSwitchName.Visibility = 'Collapsed'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user