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 {