Exemple #1
0
        public GraphBuilder(
            IEnumerable <ProjectGraphEntryPoint> entryPoints,
            ProjectCollection projectCollection,
            ProjectGraph.ProjectInstanceFactoryFunc projectInstanceFactory,
            ProjectInterpretation projectInterpretation,
            int degreeOfParallelism,
            CancellationToken cancellationToken)
        {
            var(actualEntryPoints, solutionDependencies) = ExpandSolutionIfPresent(entryPoints.ToImmutableArray());

            _solutionDependencies = solutionDependencies;

            _entryPointConfigurationMetadata = AddGraphBuildPropertyToEntryPoints(actualEntryPoints);

            IEqualityComparer <ConfigurationMetadata> configComparer = EqualityComparer <ConfigurationMetadata> .Default;

            _graphWorkSet = new ParallelWorkSet <ConfigurationMetadata, ParsedProject>(
                degreeOfParallelism - ImplicitWorkerCount,
                configComparer,
                cancellationToken);

            _projectCollection      = projectCollection;
            _projectInstanceFactory = projectInstanceFactory;
            _projectInterpretation  = projectInterpretation;
        }
Exemple #2
0
        private ProjectGraphNode CreateNewNode(
            ConfigurationMetadata configurationMetadata,
            ProjectCollection projectCollection,
            ProjectGraph.ProjectInstanceFactoryFunc projectInstanceFactory,
            ConcurrentDictionary <ConfigurationMetadata, ProjectGraphNode> allParsedProjects)
        {
            // TODO: ProjectInstance just converts the dictionary back to a PropertyDictionary, so find a way to directly provide it.
            var globalProperties = configurationMetadata.GlobalProperties.ToDictionary();

            var projectInstance = projectInstanceFactory(
                configurationMetadata.ProjectFullPath,
                globalProperties,
                projectCollection);

            if (projectInstance == null)
            {
                throw new InvalidOperationException(ResourceUtilities.GetResourceString("NullReferenceFromProjectInstanceFactory"));
            }

            var graphNode = new ProjectGraphNode(
                projectInstance);

            allParsedProjects[configurationMetadata] = graphNode;
            return(graphNode);
        }
Exemple #3
0
        public void BuildGraph(
            IEnumerable <ProjectGraphEntryPoint> entryPoints,
            ProjectCollection projectCollection,
            ProjectGraph.ProjectInstanceFactoryFunc projectInstanceFactory
            )
        {
            var entryPointConfigurationMetadata = AddGraphBuildPropertyToEntryPoints(entryPoints);

            var allParsedProjects  = new ConcurrentDictionary <ConfigurationMetadata, ProjectGraphNode>();
            var projectsToEvaluate = new ConcurrentQueue <ConfigurationMetadata>(entryPointConfigurationMetadata);

            if (FindGraphNodes(
                    projectsToEvaluate,
                    projectCollection,
                    projectInstanceFactory,
                    ProjectInterpretation,
                    allParsedProjects,
                    out var exceptions))
            {
                Edges = new GraphEdges();

                CreateEdgesAndDetectCycles(entryPointConfigurationMetadata, ProjectInterpretation, allParsedProjects, Edges);

                ProjectInterpretation.PostProcess(allParsedProjects, this);

                EntryPointNodes = entryPointConfigurationMetadata.Select(e => allParsedProjects[e]).ToList();
                RootNodes       = GetGraphRoots(EntryPointNodes);
                ProjectNodes    = allParsedProjects.Values.ToList();
            }
            else
            {
                throw new AggregateException(exceptions);
            }

            IReadOnlyCollection <ProjectGraphNode> GetGraphRoots(IReadOnlyCollection <ProjectGraphNode> entryPointNodes)
            {
                var graphRoots = new List <ProjectGraphNode>(entryPointNodes.Count);

                foreach (var entryPointNode in entryPointNodes)
                {
                    if (entryPointNode.ReferencingProjects.Count == 0)
                    {
                        graphRoots.Add(entryPointNode);
                    }
                }

                graphRoots.TrimExcess();

                return(graphRoots);
            }
        }
Exemple #4
0
        public GraphBuilder(
            IEnumerable <ProjectGraphEntryPoint> entryPoints,
            ProjectCollection projectCollection,
            ProjectGraph.ProjectInstanceFactoryFunc projectInstanceFactory,
            ProjectInterpretation projectInterpretation,
            int degreeOfParallelism,
            CancellationToken cancellationToken)
        {
            _entryPointConfigurationMetadata = AddGraphBuildPropertyToEntryPoints(entryPoints);
            IEqualityComparer <ConfigurationMetadata> configComparer = EqualityComparer <ConfigurationMetadata> .Default;

            _graphWorkSet = new ParallelWorkSet <ConfigurationMetadata, ProjectGraphNode>(
                degreeOfParallelism - ImplicitWorkerCount,
                configComparer,
                cancellationToken);
            _projectCollection      = projectCollection;
            _projectInstanceFactory = projectInstanceFactory;
            _projectInterpretation  = projectInterpretation;
        }
Exemple #5
0
        /// <summary>
        ///     Load a graph with root node at entryProjectFile
        ///     Maintain a queue of projects to be processed and evaluate projects in parallel
        ///     Returns false if loading the graph is not successful
        /// </summary>
        private bool FindGraphNodes(
            ConcurrentQueue <ConfigurationMetadata> projectsToEvaluate,
            ProjectCollection projectCollection,
            ProjectGraph.ProjectInstanceFactoryFunc projectInstanceFactory,
            ProjectInterpretation projectInterpretation,
            ConcurrentDictionary <ConfigurationMetadata, ProjectGraphNode> allParsedProjects,
            out List <Exception> exceptions)
        {
            var tasksInProgress      = new ConcurrentDictionary <ConfigurationMetadata, object>();
            var evaluationWaitHandle = new AutoResetEvent(false);
            var exceptionsInTasks    = new ConcurrentBag <Exception>();

            while (projectsToEvaluate.Count != 0 || tasksInProgress.Count != 0)
            {
                ConfigurationMetadata projectToEvaluate;
                if (projectsToEvaluate.Count != 0)
                {
                    projectToEvaluate = projectsToEvaluate.Dequeue();
                    var task = new Task(
                        () =>
                    {
                        var parsedProject = CreateNewNode(projectToEvaluate, projectCollection, projectInstanceFactory, allParsedProjects);

                        foreach (var(referenceConfig, _) in projectInterpretation.GetReferences(parsedProject.ProjectInstance))
                        {
                            /*todo: fix the following double check-then-act concurrency bug: one thread can pass the two checks, loose context,
                             * meanwhile another thread passes the same checks with the same data and inserts its reference. The initial thread regains context
                             * and duplicates the information, leading to wasted work
                             */
                            if (!tasksInProgress.ContainsKey(referenceConfig))
                            {
                                if (!allParsedProjects.ContainsKey(referenceConfig))
                                {
                                    projectsToEvaluate.Enqueue(referenceConfig);
                                    evaluationWaitHandle.Set();
                                }
                            }
                        }
                    });

                    if (tasksInProgress.TryAdd(projectToEvaluate, null))
                    {
                        // once the task completes, remove it from tasksInProgress using a chained task
                        // signal the wait handle to process new projects that have been discovered by this task or exit if all projects have been evaluated
                        task.ContinueWith(
                            _ =>
                        {
                            if (task.IsFaulted)
                            {
                                exceptionsInTasks.Add(task.Exception.InnerException);
                            }
                            tasksInProgress.TryRemove(projectToEvaluate, out var _);
                            evaluationWaitHandle.Set();
                        });
                        task.Start();
                    }
                }
                else
                {
                    // if projectsToEvaluate is empty but there are tasks in progress, there is nothing to do till a task completes and discovers new projects
                    // wait till a task completes and sends a signal
                    evaluationWaitHandle.WaitOne();
                }
            }

            if (exceptionsInTasks.Count != 0)
            {
                exceptions = exceptionsInTasks.ToList();
                return(false);
            }

            exceptions = null;
            return(true);
        }