From e69048a7a0ad65818d0573435d68efe293be0de2 Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Sat, 11 May 2024 20:07:55 +0300 Subject: [PATCH 1/3] Implement Uninstall Command for Chocolatey, and Made a Starting Point on the Automatic Upgrade when a Package is Already Installed, similar to WinGet Install Command --- .../private/Install-WinUtilProgramChoco.ps1 | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index 8271c70c0d..792e78897b 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -30,23 +30,52 @@ function Install-WinUtilProgramChoco { if($manage -eq "Installing"){ write-host "Starting install of $($Program.choco) with Chocolatey." try{ - $chocoStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y" -Wait -PassThru).ExitCode - if($chocoStatus -eq 0){ + $tryUpgrade = $false + $installOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.install-command.output.txt" + $chocoInstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath).ExitCode + if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $outputFilePath)) { + $keywordsFound = Get-Content -Path $outputFilePath | Where {$_ -match "reinstall" -OR $_ -match "already installed"} + if ($keywordsFound) { + $tryUpgrade = $true + } + } + # 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." + } + if(($chocoInstallStatus -eq 0) -AND ($tryUpgrade -eq $false)){ Write-Host "$($Program.choco) installed successfully using Chocolatey." continue } else { - Write-Host "Failed to install $($Program.choco) using Chocolatey." + Write-Host "Failed to install $($Program.choco) using Chocolatey, Chocolatey output:`n`n$(Get-Content -Path $installOutputFilePath)." } - Write-Host "Failed to install $($Program.choco)." } catch { Write-Host "Failed to install $($Program.choco) due to an error: $_" } } - if($manage -eq "Uninstalling"){ - throw "not yet implemented"; - } - $X++ + + if($manage -eq "Uninstalling"){ + write-host "Starting uninstall of $($Program.choco) with Chocolatey." + try{ + $uninstallOutputFilePath = "$env:TEMP\Install-WinUtilProgramChoco.uninstall-command.output.txt" + $chocoUninstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "uninstall $($Program.choco) -y" -Wait -PassThru).ExitCode + if($chocoUninstallStatus -eq 0){ + Write-Host "$($Program.choco) uninstalled successfully using Chocolatey." + continue + } else { + Write-Host "Failed to uninstall $($Program.choco) using Chocolatey, Chocolatey output:`n`n$(Get-Content -Path $uninstallOutputFilePath)." + } + } catch { + Write-Host "Failed to uninstall $($Program.choco) due to an error: $_" + } + } + $x++ } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed + + # Cleanup leftovers files + Remove-Item -Path $installOutputFilePath + Remove-Item -Path $uninstallOutputFilePath + return; } From 782fca962ee14216c97940dc0df121ee8149631e Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Sat, 11 May 2024 20:09:49 +0300 Subject: [PATCH 2/3] Add Extra Guards/Checks in 'Install-WinUtilProgramChoco' Private Function --- functions/private/Install-WinUtilProgramChoco.ps1 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index 792e78897b..b87f4c3f80 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -14,13 +14,21 @@ function Install-WinUtilProgramChoco { #> param( - $ProgramsToInstall, - $manage = "Installing" + [Parameter(Mandatory, Position=0)] + [PsCustomObject]$ProgramsToInstall, + + [Parameter(Mandatory, Position=1)] + [String]$manage = "Installing" ) $x = 0 $count = $ProgramsToInstall.Count - + + # This check isn't really necessary, as there's a couple of checks before this Private Function gets called, but just to make sure ;) + if($count -le 0) { + 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 "-- insstalling Chocolatey pacakages ---" From 532c7c85d50801116a85a0788534b091e72fb4d7 Mon Sep 17 00:00:00 2001 From: Kian Date: Sat, 11 May 2024 16:48:28 -0500 Subject: [PATCH 3/3] Fix fallback bug and merge choco uninstall rework - fixed return from wrong return from winget install Install-WinUtilProgramWinget - syntax/wording cleenup in Install-WinUtilProgramWinget - fix bugs in ty802/winutil#1 after merge --- .../private/Install-WinUtilProgramChoco.ps1 | 28 ++++++++++--------- .../private/Install-WinUtilProgramWinget.ps1 | 17 +++++++---- functions/public/Invoke-WPFUnInstall.ps1 | 6 ++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index b87f4c3f80..b58ddb783c 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -14,11 +14,11 @@ function Install-WinUtilProgramChoco { #> param( - [Parameter(Mandatory, Position=0)] - [PsCustomObject]$ProgramsToInstall, + [Parameter(Mandatory, Position=0)] + [PsCustomObject]$ProgramsToInstall, - [Parameter(Mandatory, Position=1)] - [String]$manage = "Installing" + [Parameter(Position=1)] + [String]$manage = "Installing" ) $x = 0 @@ -31,7 +31,7 @@ function Install-WinUtilProgramChoco { Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0 Write-Host "===========================================" - Write-Host "-- insstalling Chocolatey pacakages ---" + 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) @@ -40,13 +40,14 @@ function Install-WinUtilProgramChoco { 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 - if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $outputFilePath)) { - $keywordsFound = Get-Content -Path $outputFilePath | Where {$_ -match "reinstall" -OR $_ -match "already installed"} - if ($keywordsFound) { - $tryUpgrade = $true - } - } + if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $installOutputFilePath)) { + $keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object {$_ -match "reinstall" -OR $_ -match "already installed"} + if ($keywordsFound) { + $tryUpgrade = $true + } + } # 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." @@ -66,6 +67,7 @@ function Install-WinUtilProgramChoco { write-host "Starting uninstall of $($Program.choco) with Chocolatey." 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 if($chocoUninstallStatus -eq 0){ Write-Host "$($Program.choco) uninstalled successfully using Chocolatey." @@ -82,8 +84,8 @@ function Install-WinUtilProgramChoco { Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed # Cleanup leftovers files - Remove-Item -Path $installOutputFilePath - Remove-Item -Path $uninstallOutputFilePath + if(Test-Path -Path $installOutputFilePath){ Remove-Item -Path $installOutputFilePath } + if(Test-Path -Path $installOutputFilePath){ Remove-Item -Path $uninstallOutputFilePath } return; } diff --git a/functions/private/Install-WinUtilProgramWinget.ps1 b/functions/private/Install-WinUtilProgramWinget.ps1 index d26ba9a372..565d23ba47 100644 --- a/functions/private/Install-WinUtilProgramWinget.ps1 +++ b/functions/private/Install-WinUtilProgramWinget.ps1 @@ -15,15 +15,18 @@ Function Install-WinUtilProgramWinget { #> param( - $ProgramsToInstall, - $manage = "Installing" + [Parameter(Mandatory, Position=0)] + [PsCustomObject]$ProgramsToInstall, + + [Parameter(Position=1)] + [String]$manage = "Installing" ) $x = 0 $count = $ProgramsToInstall.Count Write-Progress -Activity "$manage Applications" -Status "Starting" -PercentComplete 0 Write-Host "===========================================" - Write-Host "-- installing winget packages ---" + Write-Host "-- Configuring winget packages ---" Write-Host "===========================================" Foreach ($Program in $ProgramsToInstall){ $failedPackages = @() @@ -60,9 +63,11 @@ Function Install-WinUtilProgramWinget { Write-Host "$($Program.winget) installed successfully with User prompt." continue } + Write-Host "Failed to install $($Program.winget). With winget" + $failedPackages += $Program } catch { Write-Host "Failed to install $($Program.winget). With winget" - $failedPackages += $($Program.winget) + $failedPackages += $Program } } if($manage -eq "Uninstalling"){ @@ -73,11 +78,11 @@ Function Install-WinUtilProgramWinget { Write-Host "Failed to uninstall $($Program.winget)." } else { Write-Host "$($Program.winget) uninstalled successfully." - $failedPackages += $($Program.winget) + $failedPackages += $Program } } catch { Write-Host "Failed to uninstall $($Program.winget) due to an error: $_" - $failedPackages += $($Program.winget) + $failedPackages += $Program } } $X++ diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index facd71552d..e7ba2b08d2 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -50,10 +50,10 @@ function Invoke-WPFUnInstall { # Install all selected programs in new window if($packagesWinget.Count -gt 0){ - Install-WinUtilProgramWinget -ProgramsToInstall $PackagesToInstall -Manage "Uninstalling" + Install-WinUtilProgramWinget -ProgramsToInstall $packagesWinget -Manage "Uninstalling" } if($packagesChoco.Count -gt 0){ - Install-WinUtilProgramChoco -ProgramsToInstall $PackagesToInstall -Manage "Uninstalling" + Install-WinUtilProgramChoco -ProgramsToInstall $packagesChoco -Manage "Uninstalling" } $ButtonType = [System.Windows.MessageBoxButton]::OK @@ -69,7 +69,7 @@ function Invoke-WPFUnInstall { } Catch { Write-Host "===========================================" - Write-Host "-- Winget failed to install ---" + Write-Host "Error: $_" Write-Host "===========================================" } $sync.ProcessRunning = $False