winutil/functions/private/Install-WinUtilProgramChoco.ps1
Mr.k 3903eaaa24
Code Formatting of Repo - Add Preprocessing to Compilation Process - Introduction of Dev/Build Tools to WinUtil (Although very simple at the moment) (#2383)
* Replace Tabs with Spaces to follow the conventions

* Add Preprocessing in Compiler

* Compile from Anywhere you want - Running 'Compile.ps1' Works in any directory you call it from

* Code Formatting Changes

* Result of Preprocessing Step in 'Compile.ps1' Script - Remove Trailing Whitespace Characters

* Make Preprocessing more advanced

* Move Preprocessing to a separate script file

* Make Self Modification impossible for 'tools/Do-PreProcessing.ps1' Script - Make the workingdir same as sync.PSScriptRoot for consistency

* Revert commit b5dffd671f

* Patched a Bug of some Excluded Files not actually get excluded in 'Get-ChildItem' PS Cmdlet

* Update Replace Regex for Code Formatting in 'Do-PreProcessing' Script Tool

* Rename 'Do-PreProcessing' to 'Invoke-Preprocessing' - Update some Comments

* Make 'Invoke-Preprocessing' Modular - Update RegEx to handle more cases - Update Documentation - Add Validations & Useful feedback upon error

* Replace Tabs with Spaces to follow the conventions - 'applications.json' File

* Code Formatting Changes - 'Copy-Files' Private Function

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool

* Replace Tabs with Spaces to follow the conventions - Make 'ExcludedFiles' validation step check all filepaths before finally checking if any has failed

* Result of 'Invoke-Preprocessing' Script

* Update Replace Regex for Code Formatting in 'Invoke-Preprocessing' Script Tool
2024-08-06 15:35:17 -05:00

103 lines
5.3 KiB
PowerShell

function Install-WinUtilProgramChoco {
<#
.SYNOPSIS
Manages the provided programs using Chocolatey
.PARAMETER ProgramsToInstall
A list of programs to manage
.PARAMETER manage
The action to perform on the programs, can be either 'Installing' or 'Uninstalling'
.NOTES
The triple quotes are required any time you need a " in a normal script block.
#>
param(
[Parameter(Mandatory, Position=0)]
[PsCustomObject]$ProgramsToInstall,
[Parameter(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 "-- 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
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."
}
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) })
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 {
Write-Host "Failed to install $($Program.choco) due to an error: $_"
$X++
$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."
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."
$x++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value ($x/$count) })
continue
} 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) })
}
} 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) })
}
}
}
Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed
# Cleanup leftovers files
if(Test-Path -Path $installOutputFilePath) { Remove-Item -Path $installOutputFilePath }
if(Test-Path -Path $uninstallOutputFilePath) { Remove-Item -Path $uninstallOutputFilePath }
return;
}