Last active
December 17, 2020 19:34
-
-
Save steviecoaster/c6dab0af5c2ab8e2269e02987b44d62d to your computer and use it in GitHub Desktop.
Internalize package icons for packages created with Chocolatey's Package Internalizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Internalize package icons for internalized packages | |
.EXAMPLE | |
$params = @{ | |
InternalizerDownloadPath = 'C:\internalized\download\' | |
IconRepository = 'http://nexus.fabrikam.com:8081/repository/icons/' | |
PackageRepository = 'http://nexus.fabrikam.com:8081/repository/choco/' | |
} | |
.\InternalizePackageIcons.ps1 @params | |
.NOTES | |
Run this script AFTER you have internalized your packages, and point it at the 'download' folder created during that process | |
#> | |
[cmdletBinding()] | |
param( | |
[Parameter(Mandatory)] | |
[String[]] | |
$InternalizerDownloadPath, | |
[Parameter(Mandatory)] | |
[String] | |
$IconRepository, | |
[Parameter(Mandatory)] | |
[String] | |
$PackageRepository, | |
[Parameter(Mandatory)] | |
[PSCredential] | |
$Credential, | |
[Parameter()] | |
[String] | |
$ApiKey | |
) | |
process { | |
$nuspecs = $(Get-ChildItem $InternalizerDownloadPath -Recurse -Include *.nuspec,chocolateyInstall.ps1) | |
Write-Verbose "Downloading icons and replacing values in nuspec files" | |
foreach ($nuspec in $nuspecs) { | |
[xml]$xml =$nuspec | Where-Object { $_.Extension -eq '.nuspec' } | Get-Content | |
$iconurl = $xml.package.metadata.iconUrl | |
$icon = ($iconurl -split ('/'))[-1] | |
$iconPath = Join-Path 'C:\icons' "$icon" | |
if ($iconurl) { | |
$null = Invoke-WebRequest -Uri $iconurl -OutFile "$($iconPath)" -ErrorAction SilentlyContinue | |
$user = $Credential.UserName | |
$password = $Credential.GetNetworkCredential().Password | |
$credPair = "{0}:{1}" -f $user, $password | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::Utf8.GetBytes($credPair)) | |
$params = @{ | |
Headers = @{ | |
Authorization = "Basic $encodedCreds" | |
} | |
UseBasicParsing = $true | |
ContentType = 'text/plain' | |
} | |
if ($iconPath -eq 'C:\icons\') { | |
$null | |
} | |
else { | |
$newUrl = "$($IconRepository)$icon" | |
Write-Verbose "Uploading: $iconPath" | |
$null = Invoke-WebRequest -Uri $newUrl -Method Put -infile $iconPath @params -ErrorAction SilentlyContinue | |
#Write new URL | |
$xml.package.metadata.iconUrl = $newUrl | |
$xml.Save($($nuspec.FullName)) | |
$Script:RepackageDirectory = Split-Path -Parent -Path $InternalizerDownloadPath | |
$chocoPackArgs = @('pack',"$($nuspec.FullName)","--output-directory='$RepackageDirectory'") | |
& choco @chocoPackArgs | |
} | |
} | |
} | |
Write-Verbose "Uploading modified packages to repository" | |
Get-ChildItem $RepackageDirectory -Recurse -Filter *.nupkg | Foreach-Object { | |
$chocoPushArgs = @('push',"$($_.FullName)","--source='$PackageRepository'") | |
if($ApiKey){ | |
$chocoPushArgs += "--api-key='$ApiKey'" | |
} | |
if($($PackageRepository.Split(':')[0]) -match 'http'){ | |
$chocoPushArgs += '--force' | |
} | |
& choco @chocoPushArgs | |
} | |
Remove-Item C:\icons -Recurse -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment