[英]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 为了分析现有的解决方案,我编写了powershell脚本来在解决方案中递归分析所有pacakges.config文件,现在我有了PSObject,其中包含以下内容
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. 在此输出中有2个包A和B,其中包A具有不同的版本,并且B具有相同的版本。
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. 现在我只需要以下列格式显示具有不同版本的软件包。应该从输出中删除具有相同版本的软件包B.
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.
我尝试使用PackageName进行分组,但我找不到一种方法来删除具有相同版本的软件包B.
Update: 更新:
Complete powershell script to find conflicting package version and its usage can be found here 可以在此处找到完整的PowerShell脚本以查找冲突的软件包版本及其用法
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: 我首先从您的示例中重新生成PSCustomObject数组
$a
,因此我们可以使用以下内容:
$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
的新数组中
$b = @()
We are then going through each element in the $a
array. 然后我们将遍历
$a
数组中的每个元素。 If this element is not found in $b
, we are going to analyze it. 如果在
$b
找不到此元素,我们将分析它。 We compare the package name and version of this element with all the elements in $a
looking for a different version number. 我们将此元素的包名称和版本与
$a
所有元素进行比较, $a
查找不同的版本号。 If we find a match, we save all the elements with the same PackageName in $b. 如果我们找到匹配项,我们会在$ b中保存具有相同PackageName的所有元素。
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.