Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Uninstall Command for Choco and a Starting Point for Automatic Upgrade #1

Merged
merged 2 commits into from
May 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions functions/private/Install-WinUtilProgramChoco.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---"
Expand All @@ -30,23 +38,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;
}