Switch to PreferChocolatey Checkbox

This commit is contained in:
Marterich 2024-08-21 22:49:12 +02:00
parent ccd8dc89b0
commit a3ab1409e5
9 changed files with 86 additions and 133 deletions

View File

@ -29,19 +29,19 @@ function Install-WinUtilProgramChoco {
throw "Private Function 'Install-WinUtilProgramChoco' expected Parameter 'ProgramsToInstall' to be of size 1 or greater, instead got $count,`nPlease double check your code and re-compile WinUtil."
}
Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0
Write-Host "==========================================="
Write-Host "-- Configuring Chocolatey pacakages ---"
Write-Host "==========================================="
Foreach ($Program in $ProgramsToInstall) {
Write-Progress -Activity "$manage Applications" -Status "$manage $($Program.choco) $($x + 1) of $count" -PercentComplete $($x/$count*100)
if($manage -eq "Installing") {
write-host "Starting install of $($Program.choco) with Chocolatey."
try {
$tryUpgrade = $false
$installOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.install-command.output.txt"
New-Item -ItemType File -Path $installOutputFilePath
$chocoInstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath).ExitCode
$chocoInstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath -NoNewWindow).ExitCode
if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $installOutputFilePath)) {
$keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object {$_ -match "reinstall" -OR $_ -match "already installed"}
if ($keywordsFound) {
@ -50,7 +50,10 @@ function Install-WinUtilProgramChoco {
}
# TODO: Implement the Upgrade part using 'choco upgrade' command, this will make choco consistent with WinGet, as WinGet tries to Upgrade when you use the install command.
if ($tryUpgrade) {
throw "Automatic Upgrade for Choco isn't implemented yet, a feature to make it consistent with WinGet, the install command using choco simply failed because $($Program.choco) is already installed."
$chocoUpdateStatus = $(Start-Process -FilePath "choco" -ArgumentList "upgrade $($Program.choco) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath -NoNewWindow).ExitCode
if ($chocoUpdateStatus -eq 0) {
Write-Host "$($Program.choco) was updated successfully using Chocolatey."
}
}
if(($chocoInstallStatus -eq 0) -AND ($tryUpgrade -eq $false)) {
Write-Host "$($Program.choco) installed successfully using Chocolatey."
@ -74,7 +77,7 @@ function Install-WinUtilProgramChoco {
try {
$uninstallOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.uninstall-command.output.txt"
New-Item -ItemType File -Path $uninstallOutputFilePath
$chocoUninstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "uninstall $($Program.choco) -y" -Wait -PassThru).ExitCode
$chocoUninstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "uninstall $($Program.choco) -y" -Wait -PassThru -NoNewWindow).ExitCode
if($chocoUninstallStatus -eq 0) {
Write-Host "$($Program.choco) uninstalled successfully using Chocolatey."
$x++
@ -92,7 +95,6 @@ function Install-WinUtilProgramChoco {
}
}
}
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed
# Cleanup leftovers files
if(Test-Path -Path $installOutputFilePath) { Remove-Item -Path $installOutputFilePath }

View File

@ -1,35 +0,0 @@
function Set-DownloadEngine {
$CheckBoxes = $sync.GetEnumerator() | Where-Object { $_.Value -is [System.Windows.Controls.CheckBox] }
foreach ($CheckBox in $CheckBoxes) {
if ($CheckBox.Key.StartsWith("WPFInstall")) {
Switch ($sync.DownloadEngine) {
"Winget" {
if ($($sync.configs.applications.$($CheckBox.Name).winget) -eq "na"){
$CheckBox.Value.Visibility = "Collapsed"
$sync."$($CheckBox.Key)Link".Visibility = "Collapsed"
}
else{
$CheckBox.Value.Visibility = "Visible"
$sync."$($CheckBox.Key)Link".Visibility = "Visible"
}
}
"Chocolatey"{
if ($($sync.configs.applications.$($CheckBox.Name).chocolatey) -eq "na"){
$CheckBox.Value.Visibility = "Collapsed"
$sync."$($CheckBox.Key)Link".Visibility = "Collapsed"
}
else {
$CheckBox.Value.Visibility = "Visible"
$sync."$($CheckBox.Key)Link".Visibility = "Visible"
}
}
default{
$CheckBox.Value.Visibility = "Visible"
$sync."$($CheckBox.Key)Link".Visibility = "Visible"
}
}
}
}
}

View File

@ -19,7 +19,9 @@ function Invoke-WPFGetInstalled {
if(((Test-WinUtilPackageManager -winget) -eq "not-installed") -and $checkbox -eq "winget") {
return
}
if ($sync.WPFpreferChocolatey.IsChecked) {
Write-Host "The Function `"Get Installed`" is only supported for Winget at the moment" -ForegroundColor Red
}
Invoke-WPFRunspace -ArgumentList $checkbox -DebugPreference $DebugPreference -ScriptBlock {
param($checkbox, $DebugPreference)

View File

@ -19,10 +19,9 @@ function Invoke-WPFInstall {
[System.Windows.MessageBox]::Show($WarningMsg, $AppTitle, [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
return
}
Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock {
param($PackagesToInstall, $DebugPreference)
$ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked)
Invoke-WPFRunspace -ArgumentList $PackagesToInstall,$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 {
@ -31,18 +30,20 @@ function Invoke-WPFInstall {
$packagesWinget, $packagesChoco = {
$packagesWinget = [System.Collections.ArrayList]::new()
$packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new()
foreach ($package in $PackagesToInstall) {
switch ($Sync.DownloadEngine){
"Chocolatey"{
# TODO: Handle Upgrade if version is already installed
$packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey install"
}
"Winget" {
$null = $packagesWinget.add($($package.winget))
if ($ChocoPreference) {
Write-Host "Prefer Choco"
if ($package.choco -eq "na") {
$packagesWinget.add($package.winget)
Write-Host "Queueing $($package.winget) for Winget install"
} else {
$null = $packagesChoco.add($package)
Write-Host "Queueing $($package) for Chocolatey install"
}
default {
}
else {
Write-Host "Prefer Winget"
if ($package.winget -eq "na") {
$packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey install"
@ -52,7 +53,7 @@ function Invoke-WPFInstall {
}
}
}
}
return $packagesWinget, $packagesChoco
}.Invoke($PackagesToInstall)

View File

@ -2,21 +2,20 @@ function Invoke-WPFInstallUpgrade {
<#
.SYNOPSIS
Invokes the function that upgrades all installed programs using winget
Invokes the function that upgrades all installed programs
#>
switch ($sync.DownloadEngine){
"Chocolatey"{
if ($sync.WPFpreferChocolatey.IsChecked) {
Install-WinUtilChoco
$chocoUpgradeStatus = (Start-Process "choco" -ArgumentList "upgrade all -y" -Wait -PassThru -NoNewWindow).ExitCode
if ($chocoUpgradeStatus -eq 0){
if ($chocoUpgradeStatus -eq 0) {
Write-Host "Upgrade Successful"
}
else{
Write-Host "Error Occured. Return Code: $chocoUpgradeStatus"
}
}
default{
else{
if((Test-WinUtilPackageManager -winget) -eq "not-installed") {
return
}
@ -27,8 +26,6 @@ function Invoke-WPFInstallUpgrade {
return
}
# Set-WinUtilTaskbaritem -state "Indeterminate"
Update-WinUtilProgramWinget
Write-Host "==========================================="
@ -36,5 +33,4 @@ function Invoke-WPFInstallUpgrade {
Write-Host "-- You can close this window if desired ---"
Write-Host "==========================================="
}
}
}

View File

@ -30,7 +30,9 @@ function Invoke-WPFRunspace {
# Add Scriptblock and Arguments to runspace
$script:powershell.AddScript($ScriptBlock)
$script:powershell.AddArgument($ArgumentList)
foreach ($Argument in $ArgumentList) {
$script:powershell.AddArgument($Argument)
}
$script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block
$script:powershell.RunspacePool = $sync.runspace

View File

@ -28,10 +28,10 @@ function Invoke-WPFUnInstall {
$confirm = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
if($confirm -eq "No") {return}
$ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked)
Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock {
param($PackagesToInstall, $DebugPreference)
Invoke-WPFRunspace -ArgumentList $PackagesToInstall, $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 {
@ -41,16 +41,17 @@ function Invoke-WPFUnInstall {
$packagesWinget = [System.Collections.Generic.List`1[System.Object]]::new()
$packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new()
foreach ($package in $PackagesToInstall) {
switch ($Sync.DownloadEngine){
"Chocolatey"{
if ($ChocoPreference) {
if ($packagesChoco.choco -eq "na") {
$null = $packagesWinget.add($package.winget)
Write-Host "Queueing $($package.winget) for Winget Uninstall"
}
else {
$packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey Uninstall"
}
"Winget" {
$null = $packagesWinget.add($($package.winget))
Write-Host "Queueing $($package.winget) for Winget Uninstall"
}
default {
else {
if ($package.winget -eq "na") {
$packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey Uninstall"
@ -60,7 +61,6 @@ function Invoke-WPFUnInstall {
}
}
}
}
return $packagesWinget, $packagesChoco
}.Invoke($PackagesToInstall)
try {

View File

@ -87,19 +87,6 @@ try {
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)}
#Initialize Download Engine
$selectedItem = $sync.WPFselectDownloadEngine.SelectedItem
$selectedText = $selectedItem.Content
$sync.DownloadEngine = $selectedText
Set-DownloadEngine
$sync.WPFselectDownloadEngine.Add_SelectionChanged({
$selectedItem = $sync.WPFselectDownloadEngine.SelectedItem
$selectedText = $selectedItem.Content
$sync.DownloadEngine = $selectedText
Set-DownloadEngine
})
$sync.keys | ForEach-Object {
if($sync.$psitem) {
if($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "CheckBox" `

View File

@ -793,11 +793,9 @@
<Button Name="WPFuninstall" Content=" Uninstall Selected" Margin="2"/>
<Button Name="WPFGetInstalled" Content=" Get Installed" Margin="2"/>
<Button Name="WPFclearWinget" Content=" Clear Selection" Margin="2"/>
<ComboBox Name="WPFselectDownloadEngine" SelectedIndex="0" Margin="2" ToolTip="Select the Download Engine to be used to install Apps">
<ComboBoxItem Content="Auto" />
<ComboBoxItem Content="Winget" />
<ComboBoxItem Content="Chocolatey" />
</ComboBox>
<CheckBox Name="WPFpreferChocolatey" VerticalAlignment="Center" VerticalContentAlignment="Center">
<TextBlock Text="Prefer Chocolatey" ToolTip="Prefers Chocolatey as Download Engine instead of Winget" VerticalAlignment="Center" />
</CheckBox>
</StackPanel>
<ScrollViewer x:Name="scrollViewer" Grid.Row="1" Grid.Column="0" Padding="-1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"