Пример #1
0
        protected override void AnnotateFile(AnalysisServices services, RepoFile file, BoundSourceFileBuilder binder)
        {
            string solutionDirectory = Path.GetDirectoryName(file.FilePath);

            try
            {
                var defaultRepoProject = file.PrimaryProject.Repo.DefaultRepoProject;
                var repoRoot           = defaultRepoProject.ProjectDirectory;
                var solutionFile       = SolutionFile.Parse(new SourceTextReader(binder.SourceText));
                foreach (var projectBlock in solutionFile.ProjectBlocks)
                {
                    var projectPath = projectBlock.ParsedProjectPath.Value;
                    if (string.IsNullOrEmpty(projectPath))
                    {
                        continue;
                    }

                    var fullProjectPath = Path.GetFullPath(Path.Combine(solutionDirectory, projectPath));

                    var projectFileReference = BoundSourceFileBuilder.CreateFileReferenceSymbol(
                        defaultRepoProject.GetLogicalPath(fullProjectPath),
                        defaultRepoProject.ProjectId);

                    var span = projectBlock.ParsedProjectName.Span;
                    AnnotateReference(binder, projectFileReference, span);

                    span = projectBlock.ParsedProjectPath.Span;
                    AnnotateReference(binder, projectFileReference, span);
                }
            }
            catch (Exception ex)
            {
                services.Logger.LogExceptionError("AnnotateSolution", ex);
            }
        }
Пример #2
0
        protected BoundSourceFileBuilder CreateBuilder(SourceFile sourceFile, RepoFile repoFile, string projectId)
        {
            var builder = CreateBuilderCore(sourceFile, projectId);

            if (sourceFile.Info.RepoRelativePath != null && repoFile.PrimaryProject != repoFile.PrimaryProject.Repo.DefaultRepoProject)
            {
                // Add a file definition for the repo relative path in the default repo project
                builder.AnnotateReferences(0, 0, BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                               sourceFile.Info.RepoRelativePath,
                                               repoFile.PrimaryProject.Repo.DefaultRepoProject.ProjectId,
                                               isDefinition: true));
            }

            return(builder);
        }
Пример #3
0
        private void AnalyzeEvaluation(
            AnalysisServices services,
            RepoFile file,
            XmlSourceFileBuilder binder,
            XmlDocumentSyntax document)
        {
            var repo = file.PrimaryProject.Repo;

            using (var scope = GetProjectCollectionScope())
            {
                try
                {
                    Project project = new Project(file.FilePath, null, null, scope.Collection,
                                                  ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.RecordDuplicateButNotCircularImports);

                    foreach (var item in project.ItemsIgnoringCondition)
                    {
                        if (item.IsImported || item.Xml?.IncludeLocation == null)
                        {
                            continue;
                        }

                        RepoFile otherRepoFile = null;

                        try
                        {
                            var fullPath = item.GetMetadataValue("FullPath");
                            otherRepoFile = repo.TryGetFile(fullPath);
                        }
                        catch
                        {
                        }

                        if (otherRepoFile == null)
                        {
                            continue;
                        }

                        var location = item.Xml.IncludeLocation;

                        var itemIncludeAttribute = binder
                                                   .TryGetElement(location.Line - 1, location.Column - 1)?
                                                   .Attribute("Include");

                        binder.AddAttributeNameReferences(itemIncludeAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }

                    foreach (var import in project.ImportsIncludingDuplicates)
                    {
                        if (import.IsImported || import.ImportingElement == null)
                        {
                            continue;
                        }

                        var      fullPath      = import.ImportedProject.FullPath;
                        RepoFile otherRepoFile = repo.TryGetFile(fullPath);
                        if (otherRepoFile == null && fullPath != null)
                        {
                            if (!AddImports)
                            {
                                continue;
                            }

                            lock (MSBuildSharedProject)
                            {
                                otherRepoFile = MSBuildSharedProject.AddFile(fullPath);
                            }

                            otherRepoFile.IsSingleton = true;
                            otherRepoFile.Analyze();
                        }

                        var location = import.ImportingElement.ProjectLocation;

                        var importProjectAttribute = binder
                                                     .TryGetElement(location.Line - 1, location.Column - 1)?
                                                     .Attribute("Project");

                        binder.AddAttributeNameReferences(importProjectAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }
                }
                catch (Exception ex)
                {
                    services.Logger.LogExceptionError($"Analyzing MSBuild file evaluation: {file.FilePath}", ex);
                }
            }
        }
Пример #4
0
        private void AnalyzeEvaluation(
            AnalysisServices services,
            RepoFile file,
            XmlSourceFileBuilder binder,
            XmlDocumentSyntax document)
        {
            var repo = file.PrimaryProject.Repo;

            using (var scope = GetProjectCollectionScope())
            {
                try
                {
                    Project project = new Project(file.FilePath, null, "Current", scope.Collection,
                                                  ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.RecordDuplicateButNotCircularImports);

                    foreach (var item in project.ItemsIgnoringCondition)
                    {
                        if (item.IsImported || item.Xml?.IncludeLocation == null)
                        {
                            continue;
                        }

                        RepoFile otherRepoFile = null;

                        try
                        {
                            var fullPath = item.GetMetadataValue("FullPath");
                            otherRepoFile = repo.TryGetFile(fullPath);
                        }
                        catch
                        {
                        }

                        if (otherRepoFile == null)
                        {
                            continue;
                        }

                        var location = item.Xml.IncludeLocation;

                        var itemIncludeAttribute = binder
                                                   .TryGetElement(location.Line - 1, location.Column - 1)?
                                                   .Attribute("Include");

                        binder.AddAttributeNameReferences(itemIncludeAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }

                    foreach (var import in project.ImportsIncludingDuplicates)
                    {
                        if (import.IsImported || import.ImportingElement == null)
                        {
                            continue;
                        }

                        var      fullPath      = import.ImportedProject.FullPath;
                        RepoFile otherRepoFile = repo.TryGetFile(fullPath);
                        if (otherRepoFile == null && fullPath != null)
                        {
                            if (!AddImports)
                            {
                                continue;
                            }

                            lock (MSBuildSharedProject)
                            {
                                otherRepoFile = MSBuildSharedProject.AddFile(fullPath);

                                // The file is marked with explicit in this lock so we can
                                // use that to know whether another thread has added the file already
                                // and we can skip redundant analysis here
                                if (otherRepoFile.HasExplicitAnalyzer)
                                {
                                    continue;
                                }

                                otherRepoFile.Analyzer            = this;
                                otherRepoFile.HasExplicitAnalyzer = true;

                                Placeholder.NotImplemented("Normalize imports to remove any references to repo specific files. Check if project is MSBuildSharedProject in order to determine if normalization is needed");
                            }

                            otherRepoFile.Analyze();
                        }

                        var location = import.ImportingElement.ProjectLocation;

                        var importProjectAttribute = binder
                                                     .TryGetElement(location.Line - 1, location.Column - 1)?
                                                     .Attribute("Project");

                        binder.AddAttributeNameReferences(importProjectAttribute,
                                                          BoundSourceFileBuilder.CreateFileReferenceSymbol(
                                                              otherRepoFile.LogicalPath,
                                                              otherRepoFile.PrimaryProject.ProjectId));
                    }
                }
                catch (Exception ex)
                {
                    services.Logger.LogExceptionError($"Analyzing MSBuild file evaluation: {file.FilePath}", ex);
                }
            }
        }