我们试图通过依赖图和拓扑排序来并行化我们的构建。
我们所有的构建逻辑都是用Msbuild编写的,我们使用PowerShell来调用它。
是否有任何一个使用PowerShell实现了依赖关系图和拓扑排序?
我知道在unix中有一个名为tsort的实用程序可用。在PowerShell中有类似的东西吗?
本文提供了一个好主意,但它是在C#“http://msdn.microsoft.com/en-us/magazine/dd569760.aspx"
中完成的答案 0 :(得分:6)
我快速翻译了Wikipedia's Topological Sort Algorithm:
function Get-TopologicalSort {
param(
[Parameter(Mandatory = $true, Position = 0)]
[hashtable] $edgeList
)
# Make sure we can use HashSet
Add-Type -AssemblyName System.Core
# Clone it so as to not alter original
$currentEdgeList = [hashtable] (Get-ClonedObject $edgeList)
# algorithm from http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
$topologicallySortedElements = New-Object System.Collections.ArrayList
$setOfAllNodesWithNoIncomingEdges = New-Object System.Collections.Queue
$fasterEdgeList = @{}
# Keep track of all nodes in case they put it in as an edge destination but not source
$allNodes = New-Object -TypeName System.Collections.Generic.HashSet[object] -ArgumentList (,[object[]] $currentEdgeList.Keys)
foreach($currentNode in $currentEdgeList.Keys) {
$currentDestinationNodes = [array] $currentEdgeList[$currentNode]
if($currentDestinationNodes.Length -eq 0) {
$setOfAllNodesWithNoIncomingEdges.Enqueue($currentNode)
}
foreach($currentDestinationNode in $currentDestinationNodes) {
if(!$allNodes.Contains($currentDestinationNode)) {
[void] $allNodes.Add($currentDestinationNode)
}
}
# Take this time to convert them to a HashSet for faster operation
$currentDestinationNodes = New-Object -TypeName System.Collections.Generic.HashSet[object] -ArgumentList (,[object[]] $currentDestinationNodes )
[void] $fasterEdgeList.Add($currentNode, $currentDestinationNodes)
}
# Now let's reconcile by adding empty dependencies for source nodes they didn't tell us about
foreach($currentNode in $allNodes) {
if(!$currentEdgeList.ContainsKey($currentNode)) {
[void] $currentEdgeList.Add($currentNode, (New-Object -TypeName System.Collections.Generic.HashSet[object]))
$setOfAllNodesWithNoIncomingEdges.Enqueue($currentNode)
}
}
$currentEdgeList = $fasterEdgeList
while($setOfAllNodesWithNoIncomingEdges.Count -gt 0) {
$currentNode = $setOfAllNodesWithNoIncomingEdges.Dequeue()
[void] $currentEdgeList.Remove($currentNode)
[void] $topologicallySortedElements.Add($currentNode)
foreach($currentEdgeSourceNode in $currentEdgeList.Keys) {
$currentNodeDestinations = $currentEdgeList[$currentEdgeSourceNode]
if($currentNodeDestinations.Contains($currentNode)) {
[void] $currentNodeDestinations.Remove($currentNode)
if($currentNodeDestinations.Count -eq 0) {
[void] $setOfAllNodesWithNoIncomingEdges.Enqueue($currentEdgeSourceNode)
}
}
}
}
if($currentEdgeList.Count -gt 0) {
throw "Graph has at least one cycle!"
}
return $topologicallySortedElements
}
这取决于
# Idea from http://stackoverflow.com/questions/7468707/deep-copy-a-dictionary-hashtable-in-powershell
function Get-ClonedObject {
param($DeepCopyObject)
$memStream = new-object IO.MemoryStream
$formatter = new-object Runtime.Serialization.Formatters.Binary.BinaryFormatter
$formatter.Serialize($memStream,$DeepCopyObject)
$memStream.Position=0
$formatter.Deserialize($memStream)
}
你就像这样使用它
# Sort the Wikipedia example:
Get-TopologicalSort @{11=@(7,5);8=@(7,3);2=@(11);9=@(11,8);10=@(11,3)}
哪个收益率:
7
5
3
11
8
10
2
9