コード例 #1
0
        private static IDictionary <string, string> ComputeCommonGlobalProperties(RuntimeGraph runtimeGraph)
        {
            if (runtimeGraph.Nodes.Count == 0)
            {
                return(ImmutableDictionary <string, string> .Empty);
            }

            // The common global properties are included in all projects. So take the global properties from the first project and prune the uncommon ones.
            var commonGlobalProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            // solution nodes are not a good template for common global properties because they set many verbose global properties on their references
            var meaningfulNodes = runtimeGraph.Nodes.Where(n => !n.Project.ProjectFile.EndsWith(".sln")).ToArray();

            var templateNode = meaningfulNodes.FirstOrDefault();

            if (templateNode?.Project.GlobalProperties == null || templateNode.Project.GlobalProperties.Count == 0)
            {
                return(ImmutableDictionary <string, string> .Empty);
            }

            foreach (var globalProperty in templateNode.Project.GlobalProperties)
            {
                commonGlobalProperties[globalProperty.Key] = globalProperty.Value;
            }

            foreach (var node in meaningfulNodes.Skip(1))
            {
                var nodeProperties = node.Project.GlobalProperties;
                var keysToRemove   = new HashSet <string>();

                foreach (var commonGlobalProperty in commonGlobalProperties)
                {
                    if (
                        !(nodeProperties.TryGetValue(commonGlobalProperty.Key, out var commonValue) &&
                          commonValue.Equals(commonGlobalProperty.Value, StringComparison.Ordinal)))
                    {
                        keysToRemove.Add(commonGlobalProperty.Key);
                    }
                }

                foreach (var key in keysToRemove)
                {
                    commonGlobalProperties.Remove(key);
                }
            }

            return(commonGlobalProperties.ToImmutableDictionary());
        }
        public Graph FromBuild(Build build)
        {
            var runtimeGraph = RuntimeGraph.FromBuild(build);

            var commonGlobalProperties = ComputeCommonGlobalProperties(runtimeGraph);

            var graph = new Graph {
                Attr = { LayerSeparation = 100 }
            };

            foreach (var root in runtimeGraph.SortedRoots)
            {
                RecursiveAddNode(root, graph, commonGlobalProperties);
            }

            AddVirtualBuildRequestNodes(graph);

            if (commonGlobalProperties.Any())
            {
                AddNodeForCommonGlobalProperties(commonGlobalProperties, graph);
            }

            return(graph);
        }