OneGet/Install-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
122
123
124
125
126
127
128

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

    Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package'))  
    Write-Debug -Message ($LocalizedData.FastPackageReference -f $fastPackageReference)

    $progress = 5
    Write-Progress -Activity $LocalizedData.InstallingPackage -PercentComplete $progress -Id $script:InstallPackageProgressId 

    $force = Get-ForceOption

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

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

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

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

    $arguments = @(
        $packageName
        '--version'
        $packageVersion
        "--source='$packageSource'"
        '--yes'
        '--prerelease'
    )

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

    #if (Get-PrereleaseOption) {
    # $arguments += '--prerelease'
    #}

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

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

    $cacheLocation = Get-CacheLocationOption
    if ($cacheLocation) {
        $arguments += '--cache-location'
        $arguments += "'$cacheLocation'"
    }

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

    $progress = 20
    Write-Progress -Activity $LocalizedData.InstallingPackage -PercentComplete $progress -Id $script:InstallPackageProgressId

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

    $progress = 80
    Write-Progress -Activity $LocalizedData.InstallingPackage -PercentComplete $progress -Id $script:InstallPackageProgressId

    $success = $false
    $alreadyInstalled = $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 'already installed') {
            $alreadyInstalled = $true
        }
    }

    if ($alreadyInstalled) {
        Write-Error ($LocalizedData.AlreadyInstalled -f $packageName, $packageVersion)
        return
    }
    elseif (-not $success) {
        Write-Error ($LocalizedData.InstallFailed -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               
}