-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAdd-ChocoInternalizedPackage.ps1
More file actions
131 lines (122 loc) · 5.45 KB
/
Add-ChocoInternalizedPackage.ps1
File metadata and controls
131 lines (122 loc) · 5.45 KB
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
129
130
131
function Add-ChocoInternalizedPackage {
<#
.SYNOPSIS
Recompiles new Chocolatey packages to internal feed when new packages are released. This should be used on a test machine that has all of the packages that you want to recompile from the Chocolatey public feed. Updated packages and their dependencies are recompiled.
.PARAMETER RepositoryURL
Your internal NuGet repository URL for pushing packages to. For Example https://yourfeed/chocolatey/
.PARAMETER WorkingDirectory
The directory you will be downloading and recompile packages to.
.PARAMETER APIKeyPath
Your API key for your internal NuGet feed
.PARAMETER PurgeWorkingDirectory
Use if you want to remove all contents currently in the $WorkDirectory path. Otherwse, choco download will fail if package exists.
.EXAMPLE
Add-ChocoInternalizedPackage -RepositoryURL 'https://yourfeed/chocolatey/' -WorkingDirectory 'C:\Example\' -PurgeWorkingDirectory -ApiKeyPath 'c:\ChocoKey\choco.txt'
Description
-----------
This will remote the contents of c:\Example, get a list of outdated Chocolatey packages on the local machine, recompile them and then push to 'https://yourfeed/chocolatey/'.
#>
param (
[Parameter(Mandatory=$true)]
[string]$RepositoryURL,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path -Path $_})]
[string]$WorkingDirectory,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path -Path $_})]
[string]$APIKeyPath,
[Parameter(Mandatory=$false)]
[switch]$PurgeWorkingDirectory
)
#Convert API Key Path for use in choco push
$PushAPIKey = Get-Content $APIKeyPath | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PushAPIKey )
$ApiKey = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
#Removes content of working directory which may include previously recompiled package data
if ($PurgeWorkingDirectory)
{
Get-ChildItem -Path $WorkingDirectory -Recurse | Remove-Item -Recurse -Force
}
#Get outdated packages
Write-Output "Getting local outdated packages"
$OutdatedPackages = (choco outdated -r --ignore-pinned)
#If no updated packages are available then exit
if (!$OutdatedPackages)
{
Write-Warning -Message 'No new packages available. Exiting'
Exit
}
else
{
$NewPackages = foreach ($NewPackage in $OutdatedPackages)
{
[PSCustomObject]@{
Name = $NewPackage.Split('|')[0]
CurrentVersion = $NewPackage.Split('|')[1]
NewVersion = $NewPackage.Split('|')[2]
Pinned = $NewPackage.Split('|')[3]
}
}
#Write output to host
Write-Output "Packages to internalize"
$NewPackages | Out-Host
#If new packages are available then install, internalize, and push to local repository
Set-Location -Path $WorkingDirectory
[System.Collections.ArrayList]$Failure = @()
[System.Collections.ArrayList]$Success = @()
#Install new packages locally
foreach ($InstallPackage in $NewPackages)
{
#Skip *.install packages due to redundancy in virtual package
if ($InstallPackage.Name -like "*.install")
{
Write-Warning ($InstallPackage.Name + ' skipping')
Continue
}
#Get time to use with choco push
$DownloadTime = Get-Date
choco download $InstallPackage.Name --internalize --no-progress --internalize-all-urls -r
if ($LASTEXITCODE -ne 0)
{
Write-Warning ($InstallPackage.Name + ' internalize failed')
$Failure.Add($InstallPackage.Name) | Out-Null
Continue
}
Write-Output ("Upgrading " + $InstallPackage.Name)
choco upgrade $InstallPackage.Name --source=$WorkingDirectory --no-progress -r -y
#If failure detected in output continue to next package
if ($LASTEXITCODE -ne 0)
{
Write-Warning ($InstallPackage.Name + ' install failed')
$Failure.Add($InstallPackage.Name) | Out-Null
Continue
}
#If no failure detected than push to hosted repository
else
{
#Get package and all dependency package paths for push
$DownloadedPackages = Get-ChildItem -Path $WorkingDirectory | Where-Object {$_.Extension -eq '.nupkg' -AND $_.LastWriteTime -gt $DownloadTime} | Select-Object -ExpandProperty FullName
foreach ($DownloadedPackage in $DownloadedPackages)
{
sleep -seconds 03
choco push $DownloadedPackage --source=$RepositoryURL --api-key="$ApiKey" --force
if ($LASTEXITCODE -ne 0)
{
Write-Warning -Message "$DownloadedPackage failed push"
$Failure.Add((Split-Path -Path $DownloadedPackage -Leaf)) | Out-Null
}
else
{
Write-Output "$DownloadedPackage successfully pushed"
$Success.Add((Split-Path -Path $DownloadedPackage -Leaf)) | Out-Null
}
}
}
}
#Write successes and failures to console and/or email
Write-Output "Successful packages:"
$Success
Write-Output "Failed packages:"
$Failure
}
}