Add functions for uninstalling packages

This commit is contained in:
Manuel Thalmann 2024-09-03 22:14:21 +02:00
parent c483d13e73
commit 2025fa8afd

View file

@ -9,6 +9,64 @@ $null = New-Module {
. "$PSScriptRoot/../Types/InstallerAction.ps1";
$userArgument = "name";
$chocoRunner = {
param(
[string] $Action = 'install',
[string[]] $ArgumentList,
[scriptblock] $Guard = { $true },
[Parameter(Position = 0)]
[string] $Name,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $AdditionalNames = @()
)
[System.Collections.ArrayList] $Names = @();
$null = $Names.Add($Name);
$Names.AddRange($AdditionalNames);
if (-not ($Force.IsPresent)) {
for ($i = $Names.Count - 1; $i -ge 0; $i--) {
$name = $Names[$i];
if (-not (& $Guard $name)) {
$Names.RemoveAt($i);
}
}
}
if ($Names.Count -ge 1) {
choco $Action -y @ArgumentList @Names;
}
};
$wingetRunner = {
param(
[string] $Action = 'install',
[string[]] $ArgumentList,
[scriptblock] $Guard = { $true },
[Parameter(Position = 0)]
[string] $Name,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $AdditionalNames = @()
)
[System.Collections.ArrayList] $Names = @();
$null = $Names.Add($Name);
$Names.AddRange($AdditionalNames);
foreach ($name in $Names) {
if ($Force.IsPresent -or (& $Guard $name $PSBoundParameters)) {
winget $Action `
--accept-source-agreements `
--source winget `
$ArgumentList `
--exact --id $name ;
} else {
Write-Host "Package ``$name`` is already installed"
}
}
};
<#
.SYNOPSIS
Installs the specified packages using chocolatey.
@ -26,24 +84,39 @@ $null = New-Module {
[string[]] $AdditionalNames = @()
)
[System.Collections.ArrayList] $Names = @();
$null = $Names.Add($Name);
$Names.AddRange($AdditionalNames);
if (-not ($Force.IsPresent)) {
for ($i = $Names.Count - 1; $i -ge 0; $i--) {
$name = $Names[$i];
if (Test-ChocoPackage $name) {
Write-Host "Package ``$name`` is already installed"
$Names.RemoveAt($i);
}
& $chocoRunner @PSBoundParameters -Guard {
param($Name)
if (Test-ChocoPackage $Name) {
Write-Host "Package ``$Name`` is already installed"
$false;
} else {
$true;
}
};
}
if ($Names.Count -ge 1) {
choco install -y @ArgumentList @Names;
<#
.SYNOPSIS
Uninstalls the specified packages using chocolatey.
#>
function Uninstall-ChocoPackage {
param(
[string[]] $ArgumentList,
[Parameter(Position=0)]
[string] $Name,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $AdditionalNames = @()
)
& $chocoRunner @PSBoundParameters -Action 'uninstall' -Guard {
param($Name)
if (Test-ChocoPackage $Name) {
$true;
} else {
Write-Host "Package ``$Name`` is not installed";
$false;
}
};
}
<#
@ -63,21 +136,41 @@ $null = New-Module {
[string[]] $AdditionalNames = @()
)
[System.Collections.ArrayList] $Names = @();
$null = $Names.Add($Name);
$Names.AddRange($AdditionalNames);
foreach ($name in $Names) {
if ($Force.IsPresent -or -not (Test-WingetPackage @PSBoundParameters)) {
winget install `
--accept-source-agreements --accept-package-agreements `
--source winget `
$ArgumentList `
--exact --id $name ;
& $wingetRunner @PSBoundParameters `
-ArgumentList @($PSBoundParameters.ArgumentList + @("--accept-package-agreements")) `
-Guard {
param($Name, $Parameters)
if (Test-WingetPackage @Parameters) {
Write-Host "Package ``$Name`` is already installed"
$false;
} else {
Write-Host "Package ``$name`` is already installed"
$true;
}
};
}
<#
.SYNOPSIS
Uninstalls the specified packages using `winget`.
#>
function Uninstall-WingetPackage {
param(
[string[]] $ArgumentList,
[Parameter(Position=0)]
[string] $Name,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $AdditionalNames = @()
)
& $wingetRunner @PSBoundParameters -Action 'uninstall' -Guard {
param($Name, $Parameters)
if (Test-WingetPackage @Parameters) {
$true;
} else {
Write-Host "Package ``$Name`` is not installed"
$false;
}
};
}
<#