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." 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 "==========================================="
Write-Host "-- Configuring Chocolatey pacakages ---" Write-Host "-- Configuring Chocolatey pacakages ---"
Write-Host "===========================================" Write-Host "==========================================="
Foreach ($Program in $ProgramsToInstall) { 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") { if($manage -eq "Installing") {
write-host "Starting install of $($Program.choco) with Chocolatey." write-host "Starting install of $($Program.choco) with Chocolatey."
try { try {
$tryUpgrade = $false $tryUpgrade = $false
$installOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.install-command.output.txt" $installOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.install-command.output.txt"
New-Item -ItemType File -Path $installOutputFilePath 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)) { if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $installOutputFilePath)) {
$keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object {$_ -match "reinstall" -OR $_ -match "already installed"} $keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object {$_ -match "reinstall" -OR $_ -match "already installed"}
if ($keywordsFound) { 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. # 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) { 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)) { if(($chocoInstallStatus -eq 0) -AND ($tryUpgrade -eq $false)) {
Write-Host "$($Program.choco) installed successfully using Chocolatey." Write-Host "$($Program.choco) installed successfully using Chocolatey."
@ -74,7 +77,7 @@ function Install-WinUtilProgramChoco {
try { try {
$uninstallOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.uninstall-command.output.txt" $uninstallOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.uninstall-command.output.txt"
New-Item -ItemType File -Path $uninstallOutputFilePath 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) { if($chocoUninstallStatus -eq 0) {
Write-Host "$($Program.choco) uninstalled successfully using Chocolatey." Write-Host "$($Program.choco) uninstalled successfully using Chocolatey."
$x++ $x++
@ -92,7 +95,6 @@ function Install-WinUtilProgramChoco {
} }
} }
} }
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed
# Cleanup leftovers files # Cleanup leftovers files
if(Test-Path -Path $installOutputFilePath) { Remove-Item -Path $installOutputFilePath } 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") { if(((Test-WinUtilPackageManager -winget) -eq "not-installed") -and $checkbox -eq "winget") {
return 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 { Invoke-WPFRunspace -ArgumentList $checkbox -DebugPreference $DebugPreference -ScriptBlock {
param($checkbox, $DebugPreference) 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) [System.Windows.MessageBox]::Show($WarningMsg, $AppTitle, [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
return return
} }
$ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked)
Invoke-WPFRunspace -ArgumentList $PackagesToInstall,$ChocoPreference -DebugPreference $DebugPreference -ScriptBlock {
Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $ChocoPreference, $DebugPreference)
param($PackagesToInstall, $DebugPreference)
if ($PackagesToInstall.count -eq 1) { if ($PackagesToInstall.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
} else { } else {
@ -31,29 +30,31 @@ function Invoke-WPFInstall {
$packagesWinget, $packagesChoco = { $packagesWinget, $packagesChoco = {
$packagesWinget = [System.Collections.ArrayList]::new() $packagesWinget = [System.Collections.ArrayList]::new()
$packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new() $packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new()
foreach ($package in $PackagesToInstall) {
switch ($Sync.DownloadEngine){ foreach ($package in $PackagesToInstall) {
"Chocolatey"{ if ($ChocoPreference) {
# TODO: Handle Upgrade if version is already installed Write-Host "Prefer Choco"
$packagesChoco.add($package) if ($package.choco -eq "na") {
Write-Host "Queueing $($package.choco) for Chocolatey install" $packagesWinget.add($package.winget)
} Write-Host "Queueing $($package.winget) for Winget install"
"Winget" { } else {
$null = $packagesWinget.add($($package.winget)) $null = $packagesChoco.add($package)
Write-Host "Queueing $($package.winget) for Winget install" Write-Host "Queueing $($package) for Chocolatey install"
}
default {
if ($package.winget -eq "na") {
$packagesChoco.add($package)
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 else {
Write-Host "Prefer Winget"
if ($package.winget -eq "na") {
$packagesChoco.add($package)
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) }.Invoke($PackagesToInstall)
try { try {

View File

@ -2,39 +2,35 @@ function Invoke-WPFInstallUpgrade {
<# <#
.SYNOPSIS .SYNOPSIS
Invokes the function that upgrades all installed programs using winget Invokes the function that upgrades all installed programs
#> #>
switch ($sync.DownloadEngine){ if ($sync.WPFpreferChocolatey.IsChecked) {
"Chocolatey"{ Install-WinUtilChoco
Install-WinUtilChoco $chocoUpgradeStatus = (Start-Process "choco" -ArgumentList "upgrade all -y" -Wait -PassThru -NoNewWindow).ExitCode
$chocoUpgradeStatus = (Start-Process "choco" -ArgumentList "upgrade all -y" -Wait -PassThru -NoNewWindow).ExitCode if ($chocoUpgradeStatus -eq 0) {
if ($chocoUpgradeStatus -eq 0){ Write-Host "Upgrade Successful"
Write-Host "Upgrade Successful"
}
else{
Write-Host "Error Occured. Return Code: $chocoUpgradeStatus"
}
} }
default{ else{
if((Test-WinUtilPackageManager -winget) -eq "not-installed") { Write-Host "Error Occured. Return Code: $chocoUpgradeStatus"
return
}
if(Get-WinUtilInstallerProcess -Process $global:WinGetInstall) {
$msg = "[Invoke-WPFInstallUpgrade] Install process is currently running. Please check for a powershell window labeled 'Winget Install'"
[System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
return
}
# Set-WinUtilTaskbaritem -state "Indeterminate"
Update-WinUtilProgramWinget
Write-Host "==========================================="
Write-Host "-- Updates started ---"
Write-Host "-- You can close this window if desired ---"
Write-Host "==========================================="
} }
} }
else{
if((Test-WinUtilPackageManager -winget) -eq "not-installed") {
return
}
if(Get-WinUtilInstallerProcess -Process $global:WinGetInstall) {
$msg = "[Invoke-WPFInstallUpgrade] Install process is currently running. Please check for a powershell window labeled 'Winget Install'"
[System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
return
}
Update-WinUtilProgramWinget
Write-Host "==========================================="
Write-Host "-- Updates started ---"
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 # Add Scriptblock and Arguments to runspace
$script:powershell.AddScript($ScriptBlock) $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.AddArgument($DebugPreference) # Pass DebugPreference to the script block
$script:powershell.RunspacePool = $sync.runspace $script:powershell.RunspacePool = $sync.runspace

View File

@ -28,10 +28,10 @@ function Invoke-WPFUnInstall {
$confirm = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon) $confirm = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
if($confirm -eq "No") {return} if($confirm -eq "No") {return}
$ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked)
Invoke-WPFRunspace -ArgumentList $PackagesToInstall, $ChocoPreference -DebugPreference $DebugPreference -ScriptBlock {
Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $ChocoPreference, $DebugPreference)
param($PackagesToInstall, $DebugPreference)
if ($PackagesToInstall.count -eq 1) { if ($PackagesToInstall.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
} else { } else {
@ -41,23 +41,23 @@ function Invoke-WPFUnInstall {
$packagesWinget = [System.Collections.Generic.List`1[System.Object]]::new() $packagesWinget = [System.Collections.Generic.List`1[System.Object]]::new()
$packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new() $packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new()
foreach ($package in $PackagesToInstall) { foreach ($package in $PackagesToInstall) {
switch ($Sync.DownloadEngine){ if ($ChocoPreference) {
"Chocolatey"{ if ($packagesChoco.choco -eq "na") {
$null = $packagesWinget.add($package.winget)
Write-Host "Queueing $($package.winget) for Winget Uninstall"
}
else {
$packagesChoco.add($package) $packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey Uninstall" Write-Host "Queueing $($package.choco) for Chocolatey Uninstall"
} }
"Winget" { }
else {
if ($package.winget -eq "na") {
$packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey Uninstall"
} else {
$null = $packagesWinget.add($($package.winget)) $null = $packagesWinget.add($($package.winget))
Write-Host "Queueing $($package.winget) for Winget Uninstall" Write-Host "Queueing $($package.winget) for Winget Uninstall"
}
default {
if ($package.winget -eq "na") {
$packagesChoco.add($package)
Write-Host "Queueing $($package.choco) for Chocolatey Uninstall"
} else {
$null = $packagesWinget.add($($package.winget))
Write-Host "Queueing $($package.winget) for Winget Uninstall"
}
} }
} }
} }

View File

@ -87,19 +87,6 @@ try {
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)} $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 { $sync.keys | ForEach-Object {
if($sync.$psitem) { if($sync.$psitem) {
if($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "CheckBox" ` 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="WPFuninstall" Content=" Uninstall Selected" Margin="2"/>
<Button Name="WPFGetInstalled" Content=" Get Installed" Margin="2"/> <Button Name="WPFGetInstalled" Content=" Get Installed" Margin="2"/>
<Button Name="WPFclearWinget" Content=" Clear Selection" 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"> <CheckBox Name="WPFpreferChocolatey" VerticalAlignment="Center" VerticalContentAlignment="Center">
<ComboBoxItem Content="Auto" /> <TextBlock Text="Prefer Chocolatey" ToolTip="Prefers Chocolatey as Download Engine instead of Winget" VerticalAlignment="Center" />
<ComboBoxItem Content="Winget" /> </CheckBox>
<ComboBoxItem Content="Chocolatey" />
</ComboBox>
</StackPanel> </StackPanel>
<ScrollViewer x:Name="scrollViewer" Grid.Row="1" Grid.Column="0" Padding="-1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" <ScrollViewer x:Name="scrollViewer" Grid.Row="1" Grid.Column="0" Padding="-1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"