mirror of
https://github.com/ChrisTitusTech/winutil
synced 2026-04-06 06:38:31 +00:00
Fix import/export functionality (#4131)
* fix: cast selections to string to prevent PSCustomObject type issues * fix(presets): clear existing selections before importing to replace state instead of merging * refactor(impex): warn user when exporting empty selections or importing empty config
This commit is contained in:
@@ -14,7 +14,9 @@ function Update-WinUtilSelections {
|
|||||||
|
|
||||||
Write-Debug "JSON to import: $($flatJson)"
|
Write-Debug "JSON to import: $($flatJson)"
|
||||||
|
|
||||||
foreach ($cbkey in $flatJson) {
|
foreach ($item in $flatJson) {
|
||||||
|
# Ensure each item is treated as a string to handle PSCustomObject from JSON deserialization
|
||||||
|
$cbkey = [string]$item
|
||||||
$group = if ($cbkey.StartsWith("WPFInstall")) { "Install" }
|
$group = if ($cbkey.StartsWith("WPFInstall")) { "Install" }
|
||||||
elseif ($cbkey.StartsWith("WPFTweaks")) { "Tweaks" }
|
elseif ($cbkey.StartsWith("WPFTweaks")) { "Tweaks" }
|
||||||
elseif ($cbkey.StartsWith("WPFToggle")) { "Toggle" }
|
elseif ($cbkey.StartsWith("WPFToggle")) { "Toggle" }
|
||||||
@@ -26,7 +28,7 @@ function Update-WinUtilSelections {
|
|||||||
if (!$sync.selectedApps.Contains($cbkey)) {
|
if (!$sync.selectedApps.Contains($cbkey)) {
|
||||||
$sync.selectedApps.Add($cbkey)
|
$sync.selectedApps.Add($cbkey)
|
||||||
# The List type needs to be specified again, because otherwise Sort-Object will convert the list to a string if there is only a single entry
|
# The List type needs to be specified again, because otherwise Sort-Object will convert the list to a string if there is only a single entry
|
||||||
[System.Collections.Generic.List[pscustomobject]]$sync.selectedApps = $sync.SelectedApps | Sort-Object
|
[System.Collections.Generic.List[string]]$sync.selectedApps = $sync.SelectedApps | Sort-Object
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"Tweaks" {
|
"Tweaks" {
|
||||||
|
|||||||
@@ -44,7 +44,13 @@ function Invoke-WPFImpex {
|
|||||||
try {
|
try {
|
||||||
$Config = ConfigDialog
|
$Config = ConfigDialog
|
||||||
if ($Config) {
|
if ($Config) {
|
||||||
$allConfs = $sync.selectedApps + $sync.selectedTweaks + $sync.selectedToggles + $sync.selectedFeatures
|
$allConfs = ($sync.selectedApps + $sync.selectedTweaks + $sync.selectedToggles + $sync.selectedFeatures) | ForEach-Object { [string]$_ }
|
||||||
|
if (-not $allConfs) {
|
||||||
|
[System.Windows.MessageBox]::Show(
|
||||||
|
"No settings are selected to export. Please select at least one app, tweak, toggle, or feature before exporting.",
|
||||||
|
"Nothing to Export", "OK", "Warning")
|
||||||
|
return
|
||||||
|
}
|
||||||
$jsonFile = $allConfs | ConvertTo-Json
|
$jsonFile = $allConfs | ConvertTo-Json
|
||||||
$jsonFile | Out-File $Config -Force
|
$jsonFile | Out-File $Config -Force
|
||||||
"iex ""& { `$(irm https://christitus.com/win) } -Config '$Config'""" | Set-Clipboard
|
"iex ""& { `$(irm https://christitus.com/win) } -Config '$Config'""" | Set-Clipboard
|
||||||
@@ -70,6 +76,21 @@ function Invoke-WPFImpex {
|
|||||||
# TODO how to handle old style? detected json type then flatten it in a func?
|
# TODO how to handle old style? detected json type then flatten it in a func?
|
||||||
# $flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" }).ForEach({ $_.Value })
|
# $flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" }).ForEach({ $_.Value })
|
||||||
$flattenedJson = $jsonFile
|
$flattenedJson = $jsonFile
|
||||||
|
|
||||||
|
if (-not $flattenedJson) {
|
||||||
|
[System.Windows.MessageBox]::Show(
|
||||||
|
"The selected file contains no settings to import. No changes have been made.",
|
||||||
|
"Empty Configuration", "OK", "Warning")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
# Clear all existing selections before importing so the import replaces
|
||||||
|
# the current state rather than merging with it
|
||||||
|
$sync.selectedApps = [System.Collections.Generic.List[string]]::new()
|
||||||
|
$sync.selectedTweaks = [System.Collections.Generic.List[string]]::new()
|
||||||
|
$sync.selectedToggles = [System.Collections.Generic.List[string]]::new()
|
||||||
|
$sync.selectedFeatures = [System.Collections.Generic.List[string]]::new()
|
||||||
|
|
||||||
Update-WinUtilSelections -flatJson $flattenedJson
|
Update-WinUtilSelections -flatJson $flattenedJson
|
||||||
|
|
||||||
if (!$PARAM_NOUI) {
|
if (!$PARAM_NOUI) {
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ function Invoke-WPFPresets {
|
|||||||
$CheckBoxesToCheck = $sync.configs.preset.$preset
|
$CheckBoxesToCheck = $sync.configs.preset.$preset
|
||||||
}
|
}
|
||||||
|
|
||||||
# clear out the filtered pattern
|
# clear out the filtered pattern so applying a preset replaces the current
|
||||||
if (!$preset) {
|
# state rather than merging with it
|
||||||
switch ($checkboxfilterpattern) {
|
switch ($checkboxfilterpattern) {
|
||||||
"WPFTweak*" { $sync.selectedTweaks = [System.Collections.Generic.List[string]]::new() }
|
"WPFTweak*" { $sync.selectedTweaks = [System.Collections.Generic.List[string]]::new() }
|
||||||
"WPFInstall*" { $sync.selectedApps = [System.Collections.Generic.List[string]]::new() }
|
"WPFInstall*" { $sync.selectedApps = [System.Collections.Generic.List[string]]::new() }
|
||||||
@@ -41,8 +41,8 @@ function Invoke-WPFPresets {
|
|||||||
"WPFToggle" { $sync.selectedToggles = [System.Collections.Generic.List[string]]::new() }
|
"WPFToggle" { $sync.selectedToggles = [System.Collections.Generic.List[string]]::new() }
|
||||||
default {}
|
default {}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
if ($preset) {
|
||||||
Update-WinUtilSelections -flatJson $CheckBoxesToCheck
|
Update-WinUtilSelections -flatJson $CheckBoxesToCheck
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ function Invoke-WPFSelectedCheckboxesUpdate{
|
|||||||
if (!$sync.selectedApps.Contains($appKey)) {
|
if (!$sync.selectedApps.Contains($appKey)) {
|
||||||
$sync.selectedApps.Add($appKey)
|
$sync.selectedApps.Add($appKey)
|
||||||
# The List type needs to be specified again, because otherwise Sort-Object will convert the list to a string if there is only a single entry
|
# The List type needs to be specified again, because otherwise Sort-Object will convert the list to a string if there is only a single entry
|
||||||
[System.Collections.Generic.List[pscustomobject]]$sync.selectedApps = $sync.SelectedApps | Sort-Object
|
[System.Collections.Generic.List[string]]$sync.selectedApps = $sync.SelectedApps | Sort-Object
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|||||||
Reference in New Issue
Block a user