OneGet/Uninstall-Package.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

function Uninstall-Package { 
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $FastPackageReference
    )

    Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Uninstall-Package'))
    Write-Debug -Message ($LocalizedData.FastPackageReference -f $FastPackageReference)
      
    $progress = 5
    Write-Progress -Activity $LocalizedData.UninstallingPackage -PercentComplete $progress -Id $script:UnInstallPackageProgressId

    $force = Get-ForceOption

    if (-not ($FastPackageReference -match $script:fastReferenceRegex)) {
        Write-Error ($LocalizedData.UninstallFailedInvalidFastReference -f $FastPackageReference)  
        return
    }

    $packageSource = $Matches.source
    $packageName = $Matches.name
    $packageVersion = $Matches.version

    if (-not ($packageSource -and $packageName -and $packageVersion)) {
        Write-Error ($LocalizedData.UninstallFailedInvalidFastReference -f $FastPackageReference)  
        return
    }

    #$shouldContinueCaption = $LocalizedData.ShouldContinueCaption
    #$shouldContinueQueryMessage = ($LocalizedData.UninstallPackageQuery -f $packageName)
    #if (-not ($force -or $request.ShouldContinue($shouldContinueQueryMessage, $shouldContinueCaption))) {
    # Write-Warning ($LocalizedData.UninstallCancelled -f $packageName)
    # return
    #}

    $arguments = @(
        $packageName
        '--yes'
    )
    if (Get-AllVersionsOption) {
        $arguments += '--allversions'
    }
    else {
        $arguments += '--version'
        $arguments += $packageVersion
    }

    if ($force) {
        $arguments += '--force'
    }

    if (Get-ForceDependenciesOption) {
        $arguments += '--force-dependencies'
    }
    elseif (Get-IgnoreDependenciesOption) {
        $arguments += '--ignore-dependencies'
    }

    if (Get-SkipPowershellOption) {
        $arguments += '--skip-powershell'
    }

    $packageParameters = Get-PackageParametersOption
    if ($packageParameters) {
        $arguments += '--package-parameters'
        $arguments += """$($packageParameters.Replace('"', '""'))"""
    }

    $progress = 20
    Write-Progress -Activity $LocalizedData.UninstallingPackage -PercentComplete $progress -Id $script:UnInstallPackageProgressId

    $chocoInstall = Invoke-Chocolatey -Command 'uninstall' -Arguments $arguments -Force $force

    $progress = 80
    Write-Progress -Activity $LocalizedData.UninstallingPackage -PercentComplete $progress -Id $script:UnInstallPackageProgressId

    $success = $false
    $notInstalled = $false
    $chocoInstall | ForEach-Object {
        if ($_ -match $script:packageReportRegex) {
            [int]$done = $Matches.done
            [int]$total = $Matches.total
            if ($done -ge 1 -and $done -eq $total) {
                $success = $true
            }
        }
        elseif ($_ -match 'not installed') {
            $notInstalled = $true
        }
    }

    if ($notInstalled) {
        Write-Error ($LocalizedData.NotInstalled -f $packageName, $packageVersion)
        return
    }
    elseif (-not $success) {
        Write-Error ($LocalizedData.UninstallFailed -f $packageName)  
        return
    }
    else {
        $swidObject = @{
            FastPackageReference = [string]::Join('#', @($packageSource, $packageName, $packageVersion))
            Name = $packageName
            Version = $packageVersion
            versionScheme  = 'MultiPartNumeric'
            #Summary = $packageSummary
            Source = $packageSource
            FromTrustedSource = $true
        }

        $swid = New-SoftwareIdentity @swidObject              
        Write-Output -InputObject $swid
    }
        
    Write-Progress -Activity $LocalizedData.Complete -PercentComplete 100 -Completed -Id $script:InstallPackageProgressId               
}