Nuget Packages Difference analysis of versions using powershell

We are using different package versions in my solution. However, now I would like to maintain one version of the package through a solution.

To analyze the existing solution, I wrote a powershell script to parse the entire pacakges.config file recursively in the solution and now I have a PSObject which has the following content

Config                PackageName   Version
proj1\packages.config   A              4
proj1\packages.config   B              1
proj2\packages.config   A              4
proj2\packages.config   B              1
proj3\packages.config   A              3
proj3\packages.config   B              1

      

In this output, there are 2 packages A and B where package A has different versions and B has the same version.

Now I only need to show packages with different versions in the following format. Package B, which is the same version, should be removed from the output.

PackageName Version Config
A             4     proj1\packages.config
A             4     proj2\packages.config
A             3     proj3\packages.config

      

How should I do it? I tried the group on PackageName but couldn't find a way to remove package B which has the same version.

Update:

Quit powershell script to find the version of the conflicting package and its usage can be found here

+3


source to share


2 answers


I solved it this way:



$groupedPackgeList = GetPackages | Group-Object -Property PackageName
$multipleVersionPackageList = @()
$groupedPackgeList | % {
    $originalCount =$_.Count
    $groupByVersion = $_.Group | Group-Object -Property Version
    if($groupByVersion.Count -ne $originalCount)
    {
        $packageName = $_.Name
        $groupByVersion |% {
            $_.Group |% {
            $tempObj = New-Object PSCustomObject  -Property @{Id=$packageName;Version=$_.Version;Config=$_.Config;}    
            $multipleVersionPackageList+=$tempObj  
            }
        } 
    }
}
$multipleVersionPackageList 

      

+4


source


There is probably an easier way to do this, but I couldn't find it. The problem is that we need to find all packages with different versions and save them, ignoring packages with a consistent version number.

I started by regenerating the PSCustomObject array $a

from your example, so we have something we can work with:

$a = @()

$a += New-Object PSCustomObject -Property @{
        Config = "proj1\packages.config";
        PackageName = "A";
        Version = 4
    }
$a += New-Object PSCustomObject -Property @{
        Config = "proj1\packages.config";
        PackageName = "B";
        Version = 1
    }
$a += New-Object PSCustomObject -Property @{
        Config = "proj1\packages.config";
        PackageName = "A";
        Version = 4
    }
$a += New-Object PSCustomObject -Property @{
        Config = "proj1\packages.config";
        PackageName = "B";
        Version = 1
    }
$a += New-Object PSCustomObject -Property @{
        Config = "proj1\packages.config";
        PackageName = "A";
        Version = 3
    }
$a += New-Object PSCustomObject -Property @{
        Config = "proj1\packages.config";
        PackageName = "B";
        Version = 1
    }

      

Then we will save the results in a new array $b

$b = @()

      



Then we loop through each element in the array $a

. If this item is not found in $b

, we will analyze it. We compare the package name and version of this item with all items in $a

, looking for a different version number. If we find a match, we will store all items with the same PackageName in $ b.

$a | % { 
    $found = $false
    foreach ($e in $b)
    {
        if ($e.PackageName -eq $_.PackageName)
        {
            $found = $true
            break
        }
    }
    if (!$found)
    {
        foreach ($i in $a)
        {
            if ($i.PackageName -eq $_.PackageName -and $i.Version -ne $_.Version)
            {
                foreach ($j in $a)
                {
                    if ($j.PackageName -eq $_.PackageName)
                    {
                        $b += $j                    
                    }
                }
                break
            }
        }
    }
}

      

Result:

Config                PackageName                       Version
------                -----------                       -------
proj1\packages.config A                                       4
proj1\packages.config A                                       4
proj1\packages.config A                                       3

      

+2


source







All Articles