Skip to content

Instantly share code, notes, and snippets.

@JPRuskin
Created August 16, 2024 13:18
Show Gist options
  • Save JPRuskin/5df370d6f08ba2197757ce3270958464 to your computer and use it in GitHub Desktop.
Save JPRuskin/5df370d6f08ba2197757ce3270958464 to your computer and use it in GitHub Desktop.
Updates Chocolatey sources that look like Nexus v2 NuGet feeds to Nexus v3 NuGet endpoints.
#requires -RunAsAdministrator
function Update-ChocolateyNexusSource {
<#
.Synopsis
Updates Chocolatey sources that seem to be Sonatype Nexus Repository NuGet repositories to use the v3 'Index.json'
.Example
Update-ChocolateyNexusSource -WhatIf
# Preview changes that will be made.
.Example
Update-ChocolateyNexusSource -OutputPath C:\Temp\chocolatey.config
# Outputs the changed file to a different location, so you can compare it.
.Example
Update-ChocolateyNexusSource
# Updates the existing configuration. Will likely need to be run as an administrator.
#>
[CmdletBinding(SupportsShouldProcess)]
param(
# The path to the Chocolatey config file
$ConfigPath = (Join-Path $env:ChocolateyInstall "config\chocolatey.config"),
# The path to output the changed file to (defaults to $ConfigPath)
$OutputPath = $ConfigPath
)
$Config = [xml]::new()
$Config.PreserveWhitespace = $true
$Config.Load($ConfigPath)
$ConfigChanged = $false
foreach ($Source in $Config.SelectNodes("//source").GetEnumerator().Where{$_.id -notin 'chocolatey', 'chocolatey.licensed'}) {
if ($Source.Value -match "^https?://.+?/repository/.+/$" -and $PSCmdlet.ShouldProcess(
$Source.Value,
"Adding 'index.json'"
)
) {
$Source.Value = $Source.Value.TrimEnd('/') + '/index.json'
$ConfigChanged = $true
}
}
if ($ConfigChanged) {
if (-not ($OutputPath | Split-Path | Test-Path)) {$null = mkdir (Split-Path $OutputPath) -Force}
Copy-Item -Path $ConfigPath -Destination "$OutputPath.nugetv2.backup"
$Config.Save($OutputPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment