public async Task<Target> FindTarget(Solution solution) { if (solution == null) { return new Target(); } var classDeclarationSyntaxes = solution.GetProjectDependencyGraph().GetTopologicallySortedProjects() .Select(async projectId => { var project = solution.GetProject(projectId); var compilation = (await project.GetCompilationAsync()); return compilation; }) .Select(compilationTask => compilationTask.Result) .Where(compilation => GetClassDeclarationSyntaxes(compilation).Any()) .Select(compilation => new Target() { Compilation = compilation, Node = GetClassDeclarationSyntaxes(compilation).First() }); var foundNamespaceDeclarationSyntax = new Target(); if (classDeclarationSyntaxes.Any()) { foundNamespaceDeclarationSyntax = classDeclarationSyntaxes.First(); } return foundNamespaceDeclarationSyntax; }
// Almost there, I think, but not currently working. public static async Task <(bool OverallSuccess, EmitResult?TriggeringFailure)> CompileWithRosyln(string solutionUrl, string outputDir, CancellationToken cancel) { MSBuildWorkspace workspace = MSBuildWorkspace.Create(); Microsoft.CodeAnalysis.Solution solution = workspace.OpenSolutionAsync(solutionUrl).Result; ProjectDependencyGraph projectGraph = solution.GetProjectDependencyGraph(); foreach (ProjectId projectId in projectGraph.GetTopologicallySortedProjects()) { Compilation?compilation = await solution.GetProject(projectId) !.GetCompilationAsync().ConfigureAwait(false); if (compilation == null || string.IsNullOrEmpty(compilation.AssemblyName)) { return(false, default);
public async void CompileSolution() { var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions); string solutionFileName = "C:\\MSBuildProjects-beta\\VStudio.sln"; VSSolutionLoader rw = new VSSolutionLoader(); System.Windows.Forms.TreeView vv = rw.LoadProject(solutionFileName); VSSolution vs = vv.Tag as VSSolution; MessageBox.Show("VSolution loaded.." + vs.Name); comp = new Dictionary <string, Compilation>(); named = new Dictionary <string, List <INamespaceOrTypeSymbol> >(); workspace = null; ProjectDependencyGraph projectGraph = null; Microsoft.CodeAnalysis.Solution solution = null; workspace = MSBuildWorkspace.Create(); solution = await workspace.OpenSolutionAsync(solutionFileName); projectGraph = solution.GetProjectDependencyGraph(); foreach (ProjectId projectId in projectGraph.GetTopologicallySortedProjects()) { Stopwatch sw = Stopwatch.StartNew(); Compilation projectCompilation = solution.GetProject(projectId).GetCompilationAsync().Result; sw.Stop(); System.Diagnostics.Debug.WriteLine("Time taken compilation creation: {0}ms", sw.Elapsed.TotalMilliseconds); Microsoft.CodeAnalysis.Project project = solution.GetProject(projectId); comp.Add(project.FilePath, projectCompilation); // List<INamespaceOrTypeSymbol> ns = GetAllTypes(project.FilePath); // named.Add(project.FilePath, ns); } }
public async Task<SyntaxNode> FindSource(Solution solution, string activeDocument, int cursorPosition) { if (solution == null || string.IsNullOrEmpty(activeDocument) || cursorPosition < 0) { return null; } var compilationTasks = solution.GetProjectDependencyGraph().GetTopologicallySortedProjects() .Select(projectId => { var project = solution.GetProject(projectId); var compilation = project.GetCompilationAsync(); return compilation; }); foreach (var task in compilationTasks) { task.Wait(); } var invocationExpressions = compilationTasks .Select(task => task.Result) .SelectMany(compilation => compilation.SyntaxTrees) .Where(compilation => FileNameComparer.SameFile(compilation.FilePath, activeDocument)) .Select(syntaxTree => syntaxTree.GetRoot()) .SelectMany(root => root.DescendantNodesAndSelf()) .Where(syntaxNode => syntaxNode.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.MethodDeclaration)) .SelectMany(methodDeclaration => methodDeclaration.DescendantNodesAndSelf()) .Where(IsSource) .Where(syntaxNode => syntaxNode.Span.Contains(cursorPosition)); SyntaxNode foundInvocationExpression = null; if (invocationExpressions.Any()) { foundInvocationExpression = invocationExpressions.First(); } return foundInvocationExpression; }
private async Task<IProjectMetric> InnerCalculate(Project project, Task<Compilation> compilationTask, Solution solution) { if (project == null) { return null; } var compilation = await compilationTask.ConfigureAwait(false); var metricsTask = _metricsCalculator.Calculate(project, solution); IEnumerable<string> dependencies; if (solution != null) { var dependencyGraph = solution.GetProjectDependencyGraph(); dependencies = dependencyGraph.GetProjectsThatThisProjectTransitivelyDependsOn(project.Id) .Select<ProjectId, Project>(id => solution.GetProject(id)) .SelectMany(x => x.MetadataReferences.Select(y => y.Display).Concat(new[] { x.AssemblyName })); } else { dependencies = project.AllProjectReferences.SelectMany(x => x.Aliases) .Concat(project.MetadataReferences.Select(y => y.Display)); } var assemblyTypes = compilation.Assembly.TypeNames; var metrics = (await metricsTask.ConfigureAwait(false)).AsArray(); var internalTypesUsed = from metric in metrics from coupling in metric.ClassCouplings where coupling.Assembly == project.AssemblyName select coupling; var relationalCohesion = (internalTypesUsed.Count() + 1.0) / assemblyTypes.Count; return new ProjectMetric(project.Name, metrics, dependencies, relationalCohesion); }
internal static async Task<Tuple<ProjectCodeProvider, IMethodSymbol, SyntaxTree>> GetProviderContainingEntryPointAsync(Solution solution, CancellationToken cancellationToken = default(CancellationToken)) { var projectIDs = solution.GetProjectDependencyGraph().GetTopologicallySortedProjects(cancellationToken); var continuations = new BlockingCollection<Task<Tuple<ProjectCodeProvider, IMethodSymbol>>>(); foreach (var projectId in projectIDs) { var project = solution.GetProject(projectId); var pair = await ProjectCodeProvider.GetProviderContainingEntryPointAsync(project); if (pair != null) { return pair; } } //foreach (var continuation in continuations) { // var pair = await continuation; // if (pair != null) // { // return pair; // } //} return null; }