From e69048a7a0ad65818d0573435d68efe293be0de2 Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Sat, 11 May 2024 20:07:55 +0300 Subject: [PATCH] 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 8271c70c..792e7889 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; }