Easier to add more package managers changes

This commit is contained in:
Kamalei Zestri 2025-03-29 20:30:18 -05:00
parent 698f1644c3
commit fd30a33e22
6 changed files with 147 additions and 78 deletions

View File

@ -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
}

View File

@ -0,0 +1,47 @@
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"]
}

View File

@ -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..."

View File

@ -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

View File

@ -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
}

View File

@ -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
@ -140,12 +149,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 {