Internal/Process-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

function Process-Package {
    [CmdletBinding()]
    param (
        [string[]]
        $Packages,

        [string]
        $Name,

        [bool]
        $NameContainsWildCard = $false,

        [string]
        $Source,

        [string]
        $RequiredVersion,

        [string]
        $MinimumVersion,

        [string]
        $MaximumVersion,

        [string]
        $OperationMessage,

        [int]
        $ProgressStart = 0,

        [int]
        $ProgressEnd = 100,

        [int]
        $ProgressId
    )
  
    if ($packages) {
        $progressStep = ($ProgressEnd - $ProgressStart) / $packages.Length

        $index = 0
        foreach ($package in $packages) {
            if ($request.IsCanceled) {
                return
            } 
        
            $index++
            [int]$progress = $ProgressStart + ($index * $progressStep)
            $progress = [System.Math]::Min(100, $progress)
            Write-Progress -Activity $LocalizedData.ProcessingPackage -PercentComplete $progress -Id $ProgressId 
            #if (($package -match $script:packageRegex) -and ($package -notmatch $script:packageReportRegex)) {
            if ($package -match $script:packageRegex) {
                $packageName = $Matches.name
                $packageVersion = $Matches.version
                $packageSummary = $Matches.summary

                Write-Debug ("Choco message: '{0}'" -f $package)

                if ($packageName -and $packageVersion) {
                    if (-not (Test-Name -Name $Name -PackageName $packageName -NameContainsWildcard $NameContainsWildCard)) {
                        Write-Debug ("Skipping processing: '{0}'" -f $packageName)
                        continue
                    }

                    if ((Test-Version -Version $packageVersion `
                                      -RequiredVersion $requiredVersion `
                                      -MinimumVersion $minimumVersion `
                                      -MaximumVersion $maximumVersion )) {                                  
                        $swidObject = @{
                            FastPackageReference = [string]::Join('#', @($Source, $packageName, $packageVersion))
                            Name = $packageName
                            Version = $packageVersion
                            versionScheme  = 'MultiPartNumeric'
                            Summary = $packageSummary
                            Source = $Source
                            FromTrustedSource = $true
                        }

                        $swid = New-SoftwareIdentity @swidObject              
                        Write-Output -InputObject $swid
                    } 
                }
            }
        }
    }
}