From 89919494e5030f700664cfe01e9ffe3a49d48cd8 Mon Sep 17 00:00:00 2001 From: KamaleiZestri <38802353+KamaleiZestri@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:33:16 -0500 Subject: [PATCH 1/2] Refactor preferChocolatey system to handle other package managers easier (#3296) * Easier to add more package managers changes * style fixes --- .../private/Get-WinUtilSelectedPackages.ps1 | 59 +++++++++++++++++++ .../private/Set-PackageManagerPreference.ps1 | 45 ++++++++++++++ functions/public/Invoke-WPFGetInstalled.ps1 | 12 ++-- functions/public/Invoke-WPFInstall.ps1 | 42 +++---------- functions/public/Invoke-WPFUnInstall.ps1 | 42 +++---------- scripts/main.ps1 | 23 ++++++-- 6 files changed, 145 insertions(+), 78 deletions(-) create mode 100644 functions/private/Get-WinUtilSelectedPackages.ps1 create mode 100644 functions/private/Set-PackageManagerPreference.ps1 diff --git a/functions/private/Get-WinUtilSelectedPackages.ps1 b/functions/private/Get-WinUtilSelectedPackages.ps1 new file mode 100644 index 00000000..af4bb1ef --- /dev/null +++ b/functions/private/Get-WinUtilSelectedPackages.ps1 @@ -0,0 +1,59 @@ +function Get-WinUtilSelectedPackages +{ + <# + .SYNOPSIS + Sorts given packages based on installer preference and availability. + + .OUTPUTS + Hashtable. Key = Package Manager, Value = ArrayList of packages to install + #> + param ( + [Parameter(Mandatory=$true)] + $PackageList, + [Parameter(Mandatory=$true)] + [PackageManagers]$Preference + ) + + if ($PackageList.count -eq 1) { + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + } else { + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + } + + $packages = [System.Collections.Hashtable]::new() + $packagesWinget = [System.Collections.ArrayList]::new() + $packagesChoco = [System.Collections.ArrayList]::new() + $packages[[PackageManagers]::Winget] = $packagesWinget + $packages[[PackageManagers]::Choco] = $packagesChoco + + Write-Debug "Checking packages using Preference '$($Preference)'" + + foreach ($package in $PackageList) { + switch ($Preference) { + "Choco" { + if ($package.choco -eq "na") { + Write-Debug "$($package.content) has no Choco value." + $packagesWinget.add($package.winget) + Write-Host "Queueing $($package.winget) for Winget" + } else { + $null = $packagesChoco.add($package.choco) + Write-Host "Queueing $($package.choco) for Chocolatey" + } + break + } + "Winget" { + if ($package.winget -eq "na") { + Write-Debug "$($package.content) has no Winget value." + $packagesChoco.add($package.choco) + Write-Host "Queueing $($package.choco) for Chocolatey" + } else { + $null = $packagesWinget.add($($package.winget)) + Write-Host "Queueing $($package.winget) for Winget" + } + break + } + } + } + + return $packages +} diff --git a/functions/private/Set-PackageManagerPreference.ps1 b/functions/private/Set-PackageManagerPreference.ps1 new file mode 100644 index 00000000..9b4fedc9 --- /dev/null +++ b/functions/private/Set-PackageManagerPreference.ps1 @@ -0,0 +1,45 @@ +function Set-PackageManagerPreference { + <# + .SYNOPSIS + Sets the currently selected package manager to global "ManagerPreference" in sync. + Also persists preference across Winutil restarts via preference.ini. + + Reads from preference.ini if no argument sent. + + .PARAMETER preferedPackageManager + The PackageManager that was selected. + #> + param( + [Parameter(Position=0, Mandatory=$false)] + [PackageManagers]$preferedPackageManager + ) + + $preferencePath = "$env:LOCALAPPDATA\winutil\preferences.ini" + $oldChocoPath = "$env:LOCALAPPDATA\winutil\preferChocolatey.ini" + + #Try loading from file if no argument given. + if ($null -eq $preferedPackageManager) { + # Backwards compat for preferChocolatey.ini + if (Test-Path -Path $oldChocoPath) { + $preferedPackageManager = [PackageManagers]::Choco + Remove-Item -Path $oldChocoPath + } + else { + $potential = Get-Content -Path $preferencePath -TotalCount 1 + if ($potential) + {$preferedPackageManager = [PackageManagers]$potential} + } + } + + #If no preference argument, .ini file bad read, and $sync empty then default to winget. + if ($null -eq $preferedPackageManager -and $null -eq $sync["ManagerPreference"]) + { $preferedPackageManager = [PackageManagers]::Winget } + + + $sync["ManagerPreference"] = [PackageManagers]::$preferedPackageManager + Write-Debug "Manager Preference changed to '$($sync["ManagerPreference"])'" + + + # Write preference to file to persist across restarts. + Out-File -FilePath $preferencePath -InputObject $sync["ManagerPreference"] +} diff --git a/functions/public/Invoke-WPFGetInstalled.ps1 b/functions/public/Invoke-WPFGetInstalled.ps1 index f33ea479..8bb245ec 100644 --- a/functions/public/Invoke-WPFGetInstalled.ps1 +++ b/functions/public/Invoke-WPFGetInstalled.ps1 @@ -18,15 +18,15 @@ function Invoke-WPFGetInstalled { if (($sync.ChocoRadioButton.IsChecked -eq $false) -and ((Test-WinUtilPackageManager -winget) -eq "not-installed") -and $checkbox -eq "winget") { return } - $preferChoco = $sync.ChocoRadioButton.IsChecked + $managerPreference = $sync["ManagerPreference"] $sync.ItemsControl.Dispatcher.Invoke([action] { $sync.ItemsControl.Items | ForEach-Object { $_.Visibility = [Windows.Visibility]::Collapsed } $null = $sync.itemsControl.Items.Add($sync.LoadingLabel) }) - Invoke-WPFRunspace -ParameterList @(("preferChoco", $preferChoco),("checkbox", $checkbox),("ShowOnlyCheckedApps", ${function:Show-OnlyCheckedApps})) -DebugPreference $DebugPreference -ScriptBlock { + Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox),("ShowOnlyCheckedApps", ${function:Show-OnlyCheckedApps})) -DebugPreference $DebugPreference -ScriptBlock { param ( [string]$checkbox, - [boolean]$preferChoco, + [PackageManagers]$managerPreference, [scriptblock]$ShowOnlyCheckedApps ) $sync.ProcessRunning = $true @@ -34,8 +34,10 @@ function Invoke-WPFGetInstalled { if ($checkbox -eq "winget") { Write-Host "Getting Installed Programs..." - if ($preferChoco) { $Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox "choco" } - else { $Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox $checkbox } + switch ($managerPreference) { + "Choco"{$Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox "choco"; break} + "Winget"{$Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox $checkbox; break} + } } elseif ($checkbox -eq "tweaks") { Write-Host "Getting Installed Tweaks..." diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index 97069662..c4187034 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -21,40 +21,16 @@ function Invoke-WPFInstall { [System.Windows.MessageBox]::Show($WarningMsg, $AppTitle, [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning) return } - $ChocoPreference = $($sync.ChocoRadioButton.IsChecked) - $installHandle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { - param($PackagesToInstall, $ChocoPreference, $DebugPreference) - if ($PackagesToInstall.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) - } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) - } - $packagesWinget, $packagesChoco = { - $packagesWinget = [System.Collections.ArrayList]::new() - $packagesChoco = [System.Collections.ArrayList]::new() - foreach ($package in $PackagesToInstall) { - if ($ChocoPreference) { - if ($package.choco -eq "na") { - $packagesWinget.add($package.winget) - Write-Host "Queueing $($package.winget) for Winget install" - } else { - $null = $packagesChoco.add($package.choco) - Write-Host "Queueing $($package.choco) for Chocolatey install" - } - } - else { - if ($package.winget -eq "na") { - $packagesChoco.add($package.choco) - Write-Host "Queueing $($package.choco) for Chocolatey install" - } else { - $null = $packagesWinget.add($($package.winget)) - Write-Host "Queueing $($package.winget) for Winget install" - } - } - } - return $packagesWinget, $packagesChoco - }.Invoke($PackagesToInstall) + $ManagerPreference = $sync["ManagerPreference"] + + Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock { + param($PackagesToInstall, $ManagerPreference, $DebugPreference) + + $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference + + $packagesWinget = $packagesSorted[[PackageManagers]::Winget] + $packagesChoco = $packagesSorted[[PackageManagers]::Choco] try { $sync.ProcessRunning = $true diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index d3d4cfb4..55a5a912 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -29,46 +29,20 @@ function Invoke-WPFUnInstall { $confirm = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon) if($confirm -eq "No") {return} - $ChocoPreference = $($sync.ChocoRadioButton.IsChecked) - Invoke-WPFRunspace -ArgumentList @(("PackagesToUninstall", $PackagesToUninstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { - param($PackagesToUninstall, $ChocoPreference, $DebugPreference) - if ($PackagesToUninstall.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) - } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) - } - $packagesWinget, $packagesChoco = { - $packagesWinget = [System.Collections.ArrayList]::new() - $packagesChoco = [System.Collections.ArrayList]::new() + $ManagerPreference = $sync["ManagerPreference"] - foreach ($package in $PackagesToUninstall) { - if ($ChocoPreference) { - if ($package.choco -eq "na") { - $packagesWinget.add($package.winget) - Write-Host "Queueing $($package.winget) for Winget uninstall" - } else { - $null = $packagesChoco.add($package.choco) - Write-Host "Queueing $($package.choco) for Chocolatey uninstall" - } - } - else { - if ($package.winget -eq "na") { - $packagesChoco.add($package.choco) - Write-Host "Queueing $($package.choco) for Chocolatey uninstall" - } else { - $null = $packagesWinget.add($($package.winget)) - Write-Host "Queueing $($package.winget) for Winget uninstall" - } - } - } - return $packagesWinget, $packagesChoco - }.Invoke($PackagesToUninstall) + Invoke-WPFRunspace -ArgumentList @(("PackagesToUninstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock { + param($PackagesToUninstall, $ManagerPreference, $DebugPreference) + + $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference + $packagesWinget = $packagesSorted[[PackageManagers]::Winget] + $packagesChoco = $packagesSorted[[PackageManagers]::Choco] try { $sync.ProcessRunning = $true - # Install all selected programs in new window + # Uninstall all selected programs in new window if($packagesWinget.Count -gt 0) { Install-WinUtilProgramWinget -Action Uninstall -Programs $packagesWinget } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index ff0e8d7c..c341f200 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -1,3 +1,12 @@ +# Create enums +Add-Type @" +public enum PackageManagers +{ + Winget, + Choco +} +"@ + # SPDX-License-Identifier: MIT # Set the maximum number of threads for the RunspacePool to the number of threads on the machine $maxthreads = [int]$env:NUMBER_OF_PROCESSORS @@ -142,12 +151,14 @@ Invoke-WPFUIElements -configVariable $sync.configs.feature -targetGridName "feat $xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)} -#Persist the Chocolatey preference across winutil restarts -$ChocoPreferencePath = "$env:LOCALAPPDATA\winutil\preferChocolatey.ini" -$sync.ChocoRadioButton.Add_Checked({New-Item -Path $ChocoPreferencePath -Force }) -$sync.ChocoRadioButton.Add_Unchecked({Remove-Item $ChocoPreferencePath -Force}) -if (Test-Path $ChocoPreferencePath) { - $sync.ChocoRadioButton.IsChecked = $true +#Persist Package Manager preference across winutil restarts +$sync.ChocoRadioButton.Add_Checked({Set-PackageManagerPreference Choco}) +$sync.WingetRadioButton.Add_Checked({Set-PackageManagerPreference Winget}) +Set-PackageManagerPreference + +switch ($sync["ManagerPreference"]) { + "Choco" {$sync.ChocoRadioButton.IsChecked = $true; break} + "Winget" {$sync.WingetRadioButton.IsChecked = $true; break} } $sync.keys | ForEach-Object { From 955d0bd6669e46cb20c6b9ea815c2753d9496315 Mon Sep 17 00:00:00 2001 From: Chris Titus Date: Mon, 14 Apr 2025 13:59:58 -0500 Subject: [PATCH 2/2] =?UTF-8?q?Revert=20"Refactor=20preferChocolatey=20sys?= =?UTF-8?q?tem=20to=20handle=20other=20package=20managers=20eas=E2=80=A6"?= =?UTF-8?q?=20(#3323)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 89919494e5030f700664cfe01e9ffe3a49d48cd8. --- .../private/Get-WinUtilSelectedPackages.ps1 | 59 ------------------- .../private/Set-PackageManagerPreference.ps1 | 45 -------------- functions/public/Invoke-WPFGetInstalled.ps1 | 12 ++-- functions/public/Invoke-WPFInstall.ps1 | 42 ++++++++++--- functions/public/Invoke-WPFUnInstall.ps1 | 42 ++++++++++--- scripts/main.ps1 | 23 ++------ 6 files changed, 78 insertions(+), 145 deletions(-) delete mode 100644 functions/private/Get-WinUtilSelectedPackages.ps1 delete mode 100644 functions/private/Set-PackageManagerPreference.ps1 diff --git a/functions/private/Get-WinUtilSelectedPackages.ps1 b/functions/private/Get-WinUtilSelectedPackages.ps1 deleted file mode 100644 index af4bb1ef..00000000 --- a/functions/private/Get-WinUtilSelectedPackages.ps1 +++ /dev/null @@ -1,59 +0,0 @@ -function Get-WinUtilSelectedPackages -{ - <# - .SYNOPSIS - Sorts given packages based on installer preference and availability. - - .OUTPUTS - Hashtable. Key = Package Manager, Value = ArrayList of packages to install - #> - param ( - [Parameter(Mandatory=$true)] - $PackageList, - [Parameter(Mandatory=$true)] - [PackageManagers]$Preference - ) - - if ($PackageList.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) - } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) - } - - $packages = [System.Collections.Hashtable]::new() - $packagesWinget = [System.Collections.ArrayList]::new() - $packagesChoco = [System.Collections.ArrayList]::new() - $packages[[PackageManagers]::Winget] = $packagesWinget - $packages[[PackageManagers]::Choco] = $packagesChoco - - Write-Debug "Checking packages using Preference '$($Preference)'" - - foreach ($package in $PackageList) { - switch ($Preference) { - "Choco" { - if ($package.choco -eq "na") { - Write-Debug "$($package.content) has no Choco value." - $packagesWinget.add($package.winget) - Write-Host "Queueing $($package.winget) for Winget" - } else { - $null = $packagesChoco.add($package.choco) - Write-Host "Queueing $($package.choco) for Chocolatey" - } - break - } - "Winget" { - if ($package.winget -eq "na") { - Write-Debug "$($package.content) has no Winget value." - $packagesChoco.add($package.choco) - Write-Host "Queueing $($package.choco) for Chocolatey" - } else { - $null = $packagesWinget.add($($package.winget)) - Write-Host "Queueing $($package.winget) for Winget" - } - break - } - } - } - - return $packages -} diff --git a/functions/private/Set-PackageManagerPreference.ps1 b/functions/private/Set-PackageManagerPreference.ps1 deleted file mode 100644 index 9b4fedc9..00000000 --- a/functions/private/Set-PackageManagerPreference.ps1 +++ /dev/null @@ -1,45 +0,0 @@ -function Set-PackageManagerPreference { - <# - .SYNOPSIS - Sets the currently selected package manager to global "ManagerPreference" in sync. - Also persists preference across Winutil restarts via preference.ini. - - Reads from preference.ini if no argument sent. - - .PARAMETER preferedPackageManager - The PackageManager that was selected. - #> - param( - [Parameter(Position=0, Mandatory=$false)] - [PackageManagers]$preferedPackageManager - ) - - $preferencePath = "$env:LOCALAPPDATA\winutil\preferences.ini" - $oldChocoPath = "$env:LOCALAPPDATA\winutil\preferChocolatey.ini" - - #Try loading from file if no argument given. - if ($null -eq $preferedPackageManager) { - # Backwards compat for preferChocolatey.ini - if (Test-Path -Path $oldChocoPath) { - $preferedPackageManager = [PackageManagers]::Choco - Remove-Item -Path $oldChocoPath - } - else { - $potential = Get-Content -Path $preferencePath -TotalCount 1 - if ($potential) - {$preferedPackageManager = [PackageManagers]$potential} - } - } - - #If no preference argument, .ini file bad read, and $sync empty then default to winget. - if ($null -eq $preferedPackageManager -and $null -eq $sync["ManagerPreference"]) - { $preferedPackageManager = [PackageManagers]::Winget } - - - $sync["ManagerPreference"] = [PackageManagers]::$preferedPackageManager - Write-Debug "Manager Preference changed to '$($sync["ManagerPreference"])'" - - - # Write preference to file to persist across restarts. - Out-File -FilePath $preferencePath -InputObject $sync["ManagerPreference"] -} diff --git a/functions/public/Invoke-WPFGetInstalled.ps1 b/functions/public/Invoke-WPFGetInstalled.ps1 index 8bb245ec..f33ea479 100644 --- a/functions/public/Invoke-WPFGetInstalled.ps1 +++ b/functions/public/Invoke-WPFGetInstalled.ps1 @@ -18,15 +18,15 @@ function Invoke-WPFGetInstalled { if (($sync.ChocoRadioButton.IsChecked -eq $false) -and ((Test-WinUtilPackageManager -winget) -eq "not-installed") -and $checkbox -eq "winget") { return } - $managerPreference = $sync["ManagerPreference"] + $preferChoco = $sync.ChocoRadioButton.IsChecked $sync.ItemsControl.Dispatcher.Invoke([action] { $sync.ItemsControl.Items | ForEach-Object { $_.Visibility = [Windows.Visibility]::Collapsed } $null = $sync.itemsControl.Items.Add($sync.LoadingLabel) }) - Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox),("ShowOnlyCheckedApps", ${function:Show-OnlyCheckedApps})) -DebugPreference $DebugPreference -ScriptBlock { + Invoke-WPFRunspace -ParameterList @(("preferChoco", $preferChoco),("checkbox", $checkbox),("ShowOnlyCheckedApps", ${function:Show-OnlyCheckedApps})) -DebugPreference $DebugPreference -ScriptBlock { param ( [string]$checkbox, - [PackageManagers]$managerPreference, + [boolean]$preferChoco, [scriptblock]$ShowOnlyCheckedApps ) $sync.ProcessRunning = $true @@ -34,10 +34,8 @@ function Invoke-WPFGetInstalled { if ($checkbox -eq "winget") { Write-Host "Getting Installed Programs..." - switch ($managerPreference) { - "Choco"{$Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox "choco"; break} - "Winget"{$Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox $checkbox; break} - } + if ($preferChoco) { $Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox "choco" } + else { $Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox $checkbox } } elseif ($checkbox -eq "tweaks") { Write-Host "Getting Installed Tweaks..." diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index c4187034..97069662 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -21,16 +21,40 @@ function Invoke-WPFInstall { [System.Windows.MessageBox]::Show($WarningMsg, $AppTitle, [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning) return } + $ChocoPreference = $($sync.ChocoRadioButton.IsChecked) + $installHandle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { + param($PackagesToInstall, $ChocoPreference, $DebugPreference) + if ($PackagesToInstall.count -eq 1) { + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + } else { + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + } + $packagesWinget, $packagesChoco = { + $packagesWinget = [System.Collections.ArrayList]::new() + $packagesChoco = [System.Collections.ArrayList]::new() - $ManagerPreference = $sync["ManagerPreference"] - - Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock { - param($PackagesToInstall, $ManagerPreference, $DebugPreference) - - $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference - - $packagesWinget = $packagesSorted[[PackageManagers]::Winget] - $packagesChoco = $packagesSorted[[PackageManagers]::Choco] + foreach ($package in $PackagesToInstall) { + if ($ChocoPreference) { + if ($package.choco -eq "na") { + $packagesWinget.add($package.winget) + Write-Host "Queueing $($package.winget) for Winget install" + } else { + $null = $packagesChoco.add($package.choco) + Write-Host "Queueing $($package.choco) for Chocolatey install" + } + } + else { + if ($package.winget -eq "na") { + $packagesChoco.add($package.choco) + Write-Host "Queueing $($package.choco) for Chocolatey install" + } else { + $null = $packagesWinget.add($($package.winget)) + Write-Host "Queueing $($package.winget) for Winget install" + } + } + } + return $packagesWinget, $packagesChoco + }.Invoke($PackagesToInstall) try { $sync.ProcessRunning = $true diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index 55a5a912..d3d4cfb4 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -29,20 +29,46 @@ function Invoke-WPFUnInstall { $confirm = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon) if($confirm -eq "No") {return} + $ChocoPreference = $($sync.ChocoRadioButton.IsChecked) - $ManagerPreference = $sync["ManagerPreference"] + Invoke-WPFRunspace -ArgumentList @(("PackagesToUninstall", $PackagesToUninstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { + param($PackagesToUninstall, $ChocoPreference, $DebugPreference) + if ($PackagesToUninstall.count -eq 1) { + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + } else { + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + } + $packagesWinget, $packagesChoco = { + $packagesWinget = [System.Collections.ArrayList]::new() + $packagesChoco = [System.Collections.ArrayList]::new() - Invoke-WPFRunspace -ArgumentList @(("PackagesToUninstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock { - param($PackagesToUninstall, $ManagerPreference, $DebugPreference) - - $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference - $packagesWinget = $packagesSorted[[PackageManagers]::Winget] - $packagesChoco = $packagesSorted[[PackageManagers]::Choco] + foreach ($package in $PackagesToUninstall) { + if ($ChocoPreference) { + if ($package.choco -eq "na") { + $packagesWinget.add($package.winget) + Write-Host "Queueing $($package.winget) for Winget uninstall" + } else { + $null = $packagesChoco.add($package.choco) + Write-Host "Queueing $($package.choco) for Chocolatey uninstall" + } + } + else { + if ($package.winget -eq "na") { + $packagesChoco.add($package.choco) + Write-Host "Queueing $($package.choco) for Chocolatey uninstall" + } else { + $null = $packagesWinget.add($($package.winget)) + Write-Host "Queueing $($package.winget) for Winget uninstall" + } + } + } + return $packagesWinget, $packagesChoco + }.Invoke($PackagesToUninstall) try { $sync.ProcessRunning = $true - # Uninstall all selected programs in new window + # Install all selected programs in new window if($packagesWinget.Count -gt 0) { Install-WinUtilProgramWinget -Action Uninstall -Programs $packagesWinget } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index c341f200..ff0e8d7c 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -1,12 +1,3 @@ -# Create enums -Add-Type @" -public enum PackageManagers -{ - Winget, - Choco -} -"@ - # SPDX-License-Identifier: MIT # Set the maximum number of threads for the RunspacePool to the number of threads on the machine $maxthreads = [int]$env:NUMBER_OF_PROCESSORS @@ -151,14 +142,12 @@ Invoke-WPFUIElements -configVariable $sync.configs.feature -targetGridName "feat $xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)} -#Persist Package Manager preference across winutil restarts -$sync.ChocoRadioButton.Add_Checked({Set-PackageManagerPreference Choco}) -$sync.WingetRadioButton.Add_Checked({Set-PackageManagerPreference Winget}) -Set-PackageManagerPreference - -switch ($sync["ManagerPreference"]) { - "Choco" {$sync.ChocoRadioButton.IsChecked = $true; break} - "Winget" {$sync.WingetRadioButton.IsChecked = $true; break} +#Persist the Chocolatey preference across winutil restarts +$ChocoPreferencePath = "$env:LOCALAPPDATA\winutil\preferChocolatey.ini" +$sync.ChocoRadioButton.Add_Checked({New-Item -Path $ChocoPreferencePath -Force }) +$sync.ChocoRadioButton.Add_Unchecked({Remove-Item $ChocoPreferencePath -Force}) +if (Test-Path $ChocoPreferencePath) { + $sync.ChocoRadioButton.IsChecked = $true } $sync.keys | ForEach-Object {