|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.IO; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.VisualStudio.SolutionPersistence.Serializer; |
| 11 | +using Roslyn.Utilities; |
| 12 | + |
| 13 | +namespace Microsoft.CodeAnalysis.MSBuild; |
| 14 | + |
| 15 | +internal partial class SolutionFileReader |
| 16 | +{ |
| 17 | + public static Task<(string AbsoluteSolutionPath, ImmutableArray<(string ProjectPath, string ProjectGuid)> Projects)> ReadSolutionFileAsync(string solutionFilePath, CancellationToken cancellationToken) |
| 18 | + { |
| 19 | + return ReadSolutionFileAsync(solutionFilePath, new PathResolver(diagnosticReporter: null), cancellationToken); |
| 20 | + } |
| 21 | + |
| 22 | + public static async Task<(string AbsoluteSolutionPath, ImmutableArray<(string ProjectPath, string ProjectGuid)> Projects)> ReadSolutionFileAsync(string solutionFilePath, PathResolver pathResolver, CancellationToken cancellationToken) |
| 23 | + { |
| 24 | + Contract.ThrowIfFalse(pathResolver.TryGetAbsoluteSolutionPath(solutionFilePath, baseDirectory: Directory.GetCurrentDirectory(), DiagnosticReportingMode.Throw, out var absoluteSolutionPath)); |
| 25 | + |
| 26 | + // When passed a solution filter, we need to read the filter file to get the solution path and included project paths. |
| 27 | + var projectFilter = ImmutableHashSet<string>.Empty; |
| 28 | + if (SolutionFilterReader.IsSolutionFilterFilename(absoluteSolutionPath) && |
| 29 | + !SolutionFilterReader.TryRead(absoluteSolutionPath, pathResolver, out absoluteSolutionPath, out projectFilter)) |
| 30 | + { |
| 31 | + throw new Exception(string.Format(WorkspaceMSBuildResources.Failed_to_load_solution_filter_0, solutionFilePath)); |
| 32 | + } |
| 33 | + |
| 34 | + var projects = await TryReadSolutionFileAsync(absoluteSolutionPath, pathResolver, projectFilter, cancellationToken).ConfigureAwait(false); |
| 35 | + if (!projects.HasValue) |
| 36 | + { |
| 37 | + throw new Exception(string.Format(WorkspaceMSBuildResources.Failed_to_load_solution_0, absoluteSolutionPath)); |
| 38 | + } |
| 39 | + |
| 40 | + return (absoluteSolutionPath, projects.Value); |
| 41 | + } |
| 42 | + |
| 43 | + private static async Task<ImmutableArray<(string ProjectPath, string ProjectGuid)>?> TryReadSolutionFileAsync(string solutionFilePath, PathResolver pathResolver, ImmutableHashSet<string> projectFilter, CancellationToken cancellationToken) |
| 44 | + { |
| 45 | + var serializer = SolutionSerializers.GetSerializerByMoniker(solutionFilePath); |
| 46 | + if (serializer == null) |
| 47 | + { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + // The solution folder is the base directory for project paths. |
| 52 | + var baseDirectory = Path.GetDirectoryName(solutionFilePath); |
| 53 | + RoslynDebug.AssertNotNull(baseDirectory); |
| 54 | + |
| 55 | + var solutionModel = await serializer.OpenAsync(solutionFilePath, cancellationToken).ConfigureAwait(false); |
| 56 | + |
| 57 | + var builder = ImmutableArray.CreateBuilder<(string ProjectPath, string ProjectGuid)>(); |
| 58 | + foreach (var projectModel in solutionModel.SolutionProjects) |
| 59 | + { |
| 60 | + // If we are filtering based on a solution filter, then we need to verify the project is included. |
| 61 | + if (!projectFilter.IsEmpty) |
| 62 | + { |
| 63 | + Contract.ThrowIfFalse(pathResolver.TryGetAbsoluteProjectPath(projectModel.FilePath, baseDirectory, DiagnosticReportingMode.Throw, out var absoluteProjectPath)); |
| 64 | + if (!projectFilter.Contains(absoluteProjectPath)) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + builder.Add((projectModel.FilePath, projectModel.Id.ToString())); |
| 71 | + } |
| 72 | + |
| 73 | + return builder.ToImmutable(); |
| 74 | + } |
| 75 | +} |
0 commit comments