Example #1
0
 public static void AddAllItemsInSolutionToTree(Solution solution, ref SolutionNode tree)
 {
     ProjectTreeBuilder.AddDocumentsToProjects(solution, ref tree);
     if (!tree.Childs.Any())
         throw new NoCsharpProjectsFoundException();
     ClassTreeBuilder.AddClassesInProjectsToTree(tree);
 }
Example #2
0
 public static ProjectNode FindProject(SolutionNode solution, string projectName)
 {
     var projects = solution.DescendantNodes().OfType<ProjectNode>().ToList();
     var proj = projects.WithName(projectName);
     if(proj == null)
         throw new Exception($"Could not find project {projectName} among the following: {projects.Select(x => x.Name).ToDelimitedString()}");
     return proj;
 }
Example #3
0
 public static Node FindDocument(SolutionNode solution, string path)
 {
     var projectName = GetRootFolder(path);
     var documentName = Path.GetFileName(path);
     var tree = FindProject(solution, projectName);
     var docsMatching = tree.Documents.Where(x => x.Name == documentName).ToList();
     if (!docsMatching.Any())
         throw new Exception("Unable to find document: " + path);
     if(docsMatching.Count > 1)
         throw new NotImplementedException($"Got {docsMatching.Count} matching documents, dont know which one to pick");
     var doc = docsMatching.First();
     ///////////////////////////////////////////////////////////////////////
     return ClassTreeBuilder.AddClassesInDocument(doc);
 }
Example #4
0
        /*public static void AddDocumentsToProjects(Solution solution, ref SolutionNode tree)
        {
            var allreadyAddedProjects = tree.DescendantNodes().OfType<ProjectNode>().ToList();
            foreach (var project in allreadyAddedProjects)
            {
                project.Documents = solution.GetProject(ProjectId.CreateFromSerialized(project.ProjectId)).Documents.ToList();
            }
        }*/
        public static void AddDocumentsToProjects(Solution solution, ref SolutionNode tree)
        {
            var projects = solution.Projects.ToList();
            var allreadyAddedProjects = tree.DescendantNodes().OfType<ProjectNode>().ToList();

            foreach (var project in projects)
            {
                var existingProject = allreadyAddedProjects.WithName(project.Name);
                if (existingProject != null)
                {
                    existingProject.Documents = project.Documents.ToList();
                }
                else
                    throw new Exception($@"Project '{project.Name}' found by roslyn was not found by EnvDte or MSBuild");
            }
        }
Example #5
0
        public static Node FindNamespace(SolutionNode solution, string name)
        {
            var projectName = GetRootFolder(name);
            var tree = FindProject(solution, projectName) as Node;
            var names = name.Split('\\').ToList();
            names.RemoveAt(0);

            while (names.Any())
            {
                var nextName = names.First();
                var nextTree = tree.DescendantNodes().WithName(nextName);
                if (nextTree == null)
                    return tree;
                tree = nextTree;
                names.RemoveAt(0);
            }
            return tree;
        }
Example #6
0
        public void ContainsDependenciesFromDifferentProject()
        {
            var pidA = ProjectId.CreateNewId("A");
            var pidB = ProjectId.CreateNewId("B");
            var didA = DocumentId.CreateNewId(pidA);
            var didB = DocumentId.CreateNewId(pidB);

            const string docA = @"
            namespace A
            {
                public class Button {public void Bar(){}}
            }
            ";

            const string docB = @"
            namespace B
            {
                class GuiFacade
                {
                    public void Foo(){new A.Button().Bar();}
                    public static void Main(){}
                }
            }
            ";

            var solution = MSBuildWorkspace.Create(new Dictionary<string, string> { { "CheckForSystemRuntimeDependency", "true" } }).CurrentSolution
            .AddProject(pidA, "ProjectA", "A.dll", LanguageNames.CSharp)
            .AddDocument(didA, "A.cs", docA)
            .AddProject(pidB, "ProjectB", "B.dll", LanguageNames.CSharp)
            .AddDocument(didB, "B.cs", docB)
            .AddProjectReference(pidB, new ProjectReference(pidA));

            var tree = new SolutionNode();
            tree.AddChild(new ProjectNode {Documents = solution.GetProject(pidA).Documents });
            tree.AddChild(new ProjectNode { Documents = solution.GetProject(pidB).Documents });
            ClassTreeBuilder.AddClassesInProjectsToTree(tree);
            var button = tree.DescendantNodes().WithName("Button") as ClassNode;
            var guiFacade = tree.DescendantNodes().WithName("GuiFacade");
            button.Should().NotBeNull();
            guiFacade.Should().NotBeNull();
            guiFacade.Dependencies.Should().NotBeEmpty();
        }
Example #7
0
        public static void AddClassesInProjectsToTree(SolutionNode tree)
        {
            var projects = tree.DescendantNodes().OfType<ProjectNode>().ToList();
            var allClasses = new List<ClassNode>();

            foreach (var project in projects)
            {
                var documents = project.Documents.ToList();
                if (!documents.Any()) continue;

                var semanticModels = documents.SelectList(d => d.GetSemanticModelAsync().Result);
                var classes = SemanticModelWalker.GetClassesInModels(semanticModels);
                if (!classes.Any())
                    continue;
                allClasses.AddRange(classes);
                var classnodes = BuildTreeFromClasses(classes);
                project.AddChilds(classnodes);
            }

            DependencyResolver.ResolveDependencies(allClasses);
        }
Example #8
0
        public static DisposableResult BuildProjectTreeFromDocuments(params string[] documentContents)
        {
            var fakeWorkspace = new AdhocWorkspace();
            var project = fakeWorkspace.AddProject("ProjectA", LanguageNames.CSharp);
            var i = 0;
            foreach (var content in documentContents)
            {
                fakeWorkspace.AddDocument(project.Id, "doc" + i, SourceText.From(content));
                i++;
            }

            var projectA = new ProjectNode
            { Documents = fakeWorkspace.CurrentSolution
            .GetProject(project.Id).Documents };

            var tree = new SolutionNode();
            tree.AddChild(projectA);
            ClassTreeBuilder.AddClassesInProjectsToTree(tree);
            return new DisposableResult
            {Workspace = fakeWorkspace,
                result = projectA};
        }
Example #9
0
 public static Node FindClass(SolutionNode solution, string name)
 {
     throw new NotImplementedException();
 }