Create a gist now

Instantly share code, notes, and snippets.

function Get-NuGetConfig
{
Get-ChildItem . NuGet.config -Recurse | Select-Object -First 1
}
function Get-PackagesLocation
{
$configFile = Get-NuGetConfig
$configPath = Resolve-Path $configFile | Split-Path -Parent
[xml]$config = Get-Content $configFile
if($config -and $config.configuration -and $config.configuration.config)
{
$path = $config.configuration.config.add `
| ? { $_.key -eq "repositoryPath" } `
| Select-Object -ExpandProperty value
[io.path]::Combine($configPath, $path)
}
}
function Get-ProjectFiles
{
Get-ChildItem -Recurse . *.csproj | Select -ExpandProperty FullName
}
function Get-RelativePath
{
param(
[string]$file,
[string]$path
)
Push-Location (Split-Path -Path $file)
$relativePath = Resolve-Path $path -Relative
Pop-Location
$relativePath
}
function Combine-PackagePaths
{
param(
[string]$relativePath,
[string]$currentPath
)
$relativePath + $currentPath.SubString($currentPath.LastIndexOf("packages") + 8)
}
function Exec-Checkout
{
param([string]$file)
c:\"Program Files (x86)\Microsoft Visual Studio 14.0"\Common7\IDE\tf checkout $file
}
function Update-PacakgeRefereces
{
param(
[string]$packagesLocation,
[parameter(ValueFromPipeline)]$projectFile
)
Process {
Write-Host "Scanning: $projectFile"
$relativePath = Get-RelativePath $projectFile $packagesLocation
[xml]$file = Get-Content $projectFile
$edited = $false
$file.SelectNodes("//*") `
| ? { $_.Name -eq "Reference" -and $_.HintPath } `
| ? { $_.HintPath.Contains("packages") } `
| % {
$_.HintPath = Combine-PackagePaths $relativePath $_.HintPath
Write-Host "Updated: $($_.HintPath)" -ForegroundColor Red
$edited = $true
}
if($edited)
{
Exec-Checkout $projectFile
$file.save($projectFile)
}
}
}
$packagesLocation = Get-PackagesLocation
Get-ProjectFiles | Update-PacakgeRefereces $packagesLocation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment