functions/Invoke-ChocoInternalizePackage.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
<#
.SYNOPSIS
    Takes PSobject as input to internalize packages
.DESCRIPTION
    Long description
.EXAMPLE
S C:\> $Outdatedpkgs = Get-ChocoOutdatedPackages
PS C:\> Invoke-ChocoInternalizePackage -PackageNames $Outdatedpkgs -Path $Path `
-PurgeWorkingDirectory | Where-Object { $_.Result -Like 'Internalize Success' }
 
Name Result Version NuGetpkgs
---- ------ ------- ---------
curl Internalize Success 7.64.1 C:\Chocotemp\curl.7.64.1.nupkg
GoogleChrome Internalize Success 74.0.3729.131 {C:\Chocotemp\chocolatey-core.extension.1.3.3.nupkg, C:\Chocotemp\Googl...
 
#>

function Invoke-ChocoInternalizePackage {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$True)]
        [pscustomobject[]]$PackageNames,
        [Parameter(Mandatory=$true)]
        [string]$Path,
        [switch]$PurgeWorkingDirectory
    )
    begin {
        if (-not (Test-Path -Path  $env:ChocolateyInstall\license\chocolatey.license.xml)){
            Throw 'Chocolatey license not found. Internalizing is only supported in licensed versions.'
            return
        }
        if ($PurgeWorkingDirectory){
            Get-ChildItem -Path $Path -Recurse | Remove-Item -Recurse -Force
        }
    }
    process {
        Set-Location $Path
        foreach ($Package in $PackageNames){
            Write-Verbose ("Downloading " + $Package.Name)
            $Date = Get-Date
            choco download $Package.Name --internalize --no-progress --internalize-all-urls --source chocolatey -r | Write-Verbose
            $DownloadedPackages = Get-ChildItem -Path $Path | Where-Object {$_.Extension -eq '.nupkg' -AND $_.LastWriteTime -gt $Date} | Select-Object -ExpandProperty FullName
            if ($LASTEXITCODE -ne 0){
                $Result = 'Internalize Failed'
            }
            else {
                $Result = 'Internalize Success'
            }
            Write-Verbose ($Package.Name + ' internalize failed')
            [PSCustomObject]@{
                Name = $Package.Name
                Result = $Result
                Version = $Package.Version
                NuGetpkgs = $DownloadedPackages
                }
        }
    }
}