From a3ab1409e5115c3a3d9ea3a84ee1dcfee5d2f2d3 Mon Sep 17 00:00:00 2001 From: Marterich <47688561+Marterich@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:49:12 +0200 Subject: [PATCH] Switch to PreferChocolatey Checkbox --- .../private/Install-WinUtilProgramChoco.ps1 | 14 +++-- functions/private/Set-DownloadEngine.ps1 | 35 ------------ functions/public/Invoke-WPFGetInstalled.ps1 | 4 +- functions/public/Invoke-WPFInstall.ps1 | 51 ++++++++--------- functions/public/Invoke-WPFInstallUpgrade.ps1 | 56 +++++++++---------- functions/public/Invoke-WPFRunspace.ps1 | 4 +- functions/public/Invoke-WPFUnInstall.ps1 | 34 +++++------ scripts/main.ps1 | 13 ----- xaml/inputXML.xaml | 8 +-- 9 files changed, 86 insertions(+), 133 deletions(-) delete mode 100644 functions/private/Set-DownloadEngine.ps1 diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index b66c88b9..efd2e491 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -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 } diff --git a/functions/private/Set-DownloadEngine.ps1 b/functions/private/Set-DownloadEngine.ps1 deleted file mode 100644 index 48e99424..00000000 --- a/functions/private/Set-DownloadEngine.ps1 +++ /dev/null @@ -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" - } - } - } - } -} \ No newline at end of file diff --git a/functions/public/Invoke-WPFGetInstalled.ps1 b/functions/public/Invoke-WPFGetInstalled.ps1 index 9b31e483..df03b808 100644 --- a/functions/public/Invoke-WPFGetInstalled.ps1 +++ b/functions/public/Invoke-WPFGetInstalled.ps1 @@ -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) diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index a1bf8b78..e4d65a57 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -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,29 +30,31 @@ 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)) - Write-Host "Queueing $($package.winget) for Winget 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" - } - } + + foreach ($package in $PackagesToInstall) { + 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" } } - 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) try { diff --git a/functions/public/Invoke-WPFInstallUpgrade.ps1 b/functions/public/Invoke-WPFInstallUpgrade.ps1 index 1d902c54..4f2713e5 100644 --- a/functions/public/Invoke-WPFInstallUpgrade.ps1 +++ b/functions/public/Invoke-WPFInstallUpgrade.ps1 @@ -2,39 +2,35 @@ 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"{ - Install-WinUtilChoco - $chocoUpgradeStatus = (Start-Process "choco" -ArgumentList "upgrade all -y" -Wait -PassThru -NoNewWindow).ExitCode - if ($chocoUpgradeStatus -eq 0){ - Write-Host "Upgrade Successful" - } - else{ - Write-Host "Error Occured. Return Code: $chocoUpgradeStatus" - } + if ($sync.WPFpreferChocolatey.IsChecked) { + Install-WinUtilChoco + $chocoUpgradeStatus = (Start-Process "choco" -ArgumentList "upgrade all -y" -Wait -PassThru -NoNewWindow).ExitCode + if ($chocoUpgradeStatus -eq 0) { + Write-Host "Upgrade Successful" } - default{ - 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 - } - - # Set-WinUtilTaskbaritem -state "Indeterminate" - - Update-WinUtilProgramWinget - - Write-Host "===========================================" - Write-Host "-- Updates started ---" - Write-Host "-- You can close this window if desired ---" - Write-Host "===========================================" + else{ + Write-Host "Error Occured. Return Code: $chocoUpgradeStatus" } } + 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 "===========================================" + } } \ No newline at end of file diff --git a/functions/public/Invoke-WPFRunspace.ps1 b/functions/public/Invoke-WPFRunspace.ps1 index dfb3a6cd..440a13b7 100644 --- a/functions/public/Invoke-WPFRunspace.ps1 +++ b/functions/public/Invoke-WPFRunspace.ps1 @@ -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 diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index aeb38480..ff6c5db9 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -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,23 +41,23 @@ 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" + 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)) - 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" - } + Write-Host "Queueing $($package.winget) for Winget Uninstall" } } } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 809810dc..c1d476f4 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -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" ` diff --git a/xaml/inputXML.xaml b/xaml/inputXML.xaml index db5c178e..6a37ea66 100644 --- a/xaml/inputXML.xaml +++ b/xaml/inputXML.xaml @@ -793,11 +793,9 @@