Compare commits

...

4 Commits

Author SHA1 Message Date
Marterich
7f482d5e4b Code Formatting 2024-08-22 22:54:44 +02:00
Marterich
579030eeb9 Implement "Get-Installed" (quick-and-dirty) 2024-08-22 22:50:52 +02:00
Marterich
d111244347 Change Logging, Fix interactivity and optimize uninstall 2024-08-22 21:59:16 +02:00
Marterich
44c6ecc334 Persist Choco Preference across program restarts 2024-08-22 18:19:39 +02:00
5 changed files with 78 additions and 50 deletions

View File

@ -14,10 +14,10 @@ function Install-WinUtilProgramChoco {
#>
param(
[Parameter(Mandatory, Position=0)]
[Parameter(Mandatory, Position = 0)]
[PsCustomObject]$ProgramsToInstall,
[Parameter(Position=1)]
[Parameter(Position = 1)]
[String]$manage = "Installing"
)
@ -25,7 +25,7 @@ function Install-WinUtilProgramChoco {
$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) {
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."
}
@ -35,70 +35,78 @@ function Install-WinUtilProgramChoco {
Write-Host "==========================================="
Foreach ($Program in $ProgramsToInstall) {
if($manage -eq "Installing") {
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 -NoNewWindow).ExitCode
if(($chocoInstallStatus -eq 0) -AND (Test-Path -Path $installOutputFilePath)) {
$keywordsFound = Get-Content -Path $installOutputFilePath | Where-Object {$_ -match "reinstall" -OR $_ -match "already installed"}
$chocoInstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "install $($Program.choco) -y --log-file $($installOutputFilePath)" -Wait -PassThru).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) {
$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) {
$chocoUpdateStatus = $(Start-Process -FilePath "choco" -ArgumentList "upgrade $($Program.choco) -y" -Wait -PassThru -RedirectStandardOutput $installOutputFilePath -NoNewWindow).ExitCode
$chocoUpdateStatus = $(Start-Process -FilePath "choco" -ArgumentList "upgrade $($Program.choco) -y" -Wait -PassThru).ExitCode
if ($chocoUpdateStatus -eq 0) {
Write-Host "$($Program.choco) was updated successfully using Chocolatey."
}
else{
Write-Host "Failed upgdate of $($Program.choco) 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."
$X++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value ($x/$count) })
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Normal" -value ($x / $count) })
continue
} else {
Write-Host "Failed to install $($Program.choco) using Chocolatey, Chocolatey output:`n`n$(Get-Content -Path $installOutputFilePath)."
$X++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -value ($x/$count) })
}
} catch {
elseif (($chocoInstallStatus -ne 0) -AND ($tryUpgrade -eq $false)) {
Write-Host "Failed to install $($Program.choco) using Chocolatey"
$X++
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Error" -value ($x / $count) })
}
}
catch {
Write-Host "Failed to install $($Program.choco) due to an error: $_"
$X++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -value ($x/$count) })
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Error" -value ($x / $count) })
}
}
if($manage -eq "Uninstalling") {
write-host "Starting uninstall of $($Program.choco) with Chocolatey."
if ($manage -eq "Uninstalling") {
Write-Host "Searching for Metapackages of of $($Program.choco) (.install or .portable)"
$chocoPackages = ((choco list | Select-String -Pattern "$($Program.choco)(\.install|\.portable) {0,1}").Matches.Value) -join " "
Write-Host "Starting uninstall of $chocoPackages 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 -NoNewWindow).ExitCode
if($chocoUninstallStatus -eq 0) {
$chocoUninstallStatus = $(Start-Process -FilePath "choco" -ArgumentList "uninstall $chocoPackages -y" -Wait -PassThru).ExitCode
if ($chocoUninstallStatus -eq 0) {
Write-Host "$($Program.choco) uninstalled successfully using Chocolatey."
$x++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value ($x/$count) })
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Normal" -value ($x / $count) })
continue
} else {
}
else {
Write-Host "Failed to uninstall $($Program.choco) using Chocolatey, Chocolatey output:`n`n$(Get-Content -Path $uninstallOutputFilePath)."
$x++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -value ($x/$count) })
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Error" -value ($x / $count) })
}
} catch {
}
catch {
Write-Host "Failed to uninstall $($Program.choco) due to an error: $_"
$x++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -value ($x/$count) })
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Error" -value ($x / $count) })
}
}
}
# Cleanup leftovers files
if(Test-Path -Path $installOutputFilePath) { Remove-Item -Path $installOutputFilePath }
if(Test-Path -Path $uninstallOutputFilePath) { Remove-Item -Path $uninstallOutputFilePath }
if (Test-Path -Path $installOutputFilePath) { Remove-Item -Path $installOutputFilePath }
if (Test-Path -Path $uninstallOutputFilePath) { Remove-Item -Path $uninstallOutputFilePath }
return;
}

View File

@ -13,6 +13,16 @@ Function Invoke-WinUtilCurrentSystem {
param(
$CheckBox
)
if ($CheckBox -eq "choco") {
$apps = (choco list | Select-String -Pattern "^\S+").Matches.Value
$filter = Get-WinUtilVariables -Type Checkbox | Where-Object {$psitem -like "WPFInstall*"}
$sync.GetEnumerator() | Where-Object {$psitem.Key -in $filter} | ForEach-Object {
$dependencies = @($sync.configs.applications.$($psitem.Key).choco -split ";")
if ($dependencies -in $apps) {
Write-Output $psitem.name
}
}
}
if ($checkbox -eq "winget") {

View File

@ -16,14 +16,12 @@ function Invoke-WPFGetInstalled {
return
}
if(((Test-WinUtilPackageManager -winget) -eq "not-installed") -and $checkbox -eq "winget") {
if(($sync.WPFpreferChocolatey.IsChecked -eq $false) -and ((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)
$preferChoco = $sync.WPFpreferChocolatey.IsChecked
Invoke-WPFRunspace -ArgumentList $checkbox, $preferChoco -DebugPreference $DebugPreference -ScriptBlock {
param($checkbox, $preferChoco, $DebugPreference)
$sync.ProcessRunning = $true
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" })
@ -34,8 +32,12 @@ function Invoke-WPFGetInstalled {
if($checkbox -eq "tweaks") {
Write-Host "Getting Installed Tweaks..."
}
if ($preferChoco -and $checkbox -eq "winget") {
$Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox "choco"
}
else{
$Checkboxes = Invoke-WinUtilCurrentSystem -CheckBox $checkbox
}
$sync.form.Dispatcher.invoke({
foreach($checkbox in $Checkboxes) {

View File

@ -87,6 +87,14 @@ try {
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)}
#Persist the Chocolatey preference across winutil restarts
$ChocoPreferencePath = "$env:LOCALAPPDATA\winutil\preferChocolatey.ini"
$sync.WPFpreferChocolatey.Add_Checked({New-Item -Path $ChocoPreferencePath -Force })
$sync.WPFpreferChocolatey.Add_Unchecked({Remove-Item $ChocoPreferencePath -Force})
if (Test-Path $ChocoPreferencePath) {
$sync.WPFpreferChocolatey.IsChecked = $true
}
$sync.keys | ForEach-Object {
if($sync.$psitem) {
if($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "CheckBox" `