简体   繁体   中英

Nuget Packages Version difference analysis using powershell

In my solution, we use different versions of packages.However, I would now like to maintain a single version of a package through out the solution.

To analyze existing solution, I wrote powershell script to analyze all pacakges.config file recursively in a solution and I now have the 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 same version throughout.

Now I need to show only the packages that has different versions in the following format.The package B which has the same version throughout should be removed from output.

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

How do I do that ? I tried group by PackageName , but I could not find a way to remove the package B which has the same version through out.

Update:

Complete powershell script to find conflicting package version and its usage can be found here

I solved it in the following 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 

There is probably an easier way to do this, but I could not find it. Issue is that we need to find all the packages with various versions, and save those, while ignoring packages that have 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
    }

We will then save the results in a new array called $b

$b = @()

We are then going through each element in the $a array. If this element is not found in $b , we are going to analyze it. We compare the package name and version of this element with all the elements in $a looking for a different version number. If we find a match, we save all the elements 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 then is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM