PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Kudu.Core/Infrastructure/VsSolution.cs

https://github.com/moacap/kudu
C# | 88 lines | 67 code | 16 blank | 5 comment | 5 complexity | 42794a8651b0b7320528e2c1ac6e6137 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace Kudu.Core.Infrastructure
  8. {
  9. public class VsSolution
  10. {
  11. // internal class SolutionParser
  12. // Name: Microsoft.Build.Construction.SolutionParser
  13. // Assembly: Microsoft.Build, Version=4.0.0.0
  14. private const string SolutionParserTypeName = "Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
  15. private static readonly Type _solutionParser;
  16. private static readonly PropertyInfo _solutionReaderProperty;
  17. private static readonly MethodInfo _parseSolutionMethod;
  18. private static readonly PropertyInfo _projectsProperty;
  19. static VsSolution()
  20. {
  21. _solutionParser = Type.GetType(SolutionParserTypeName, throwOnError: false, ignoreCase: false);
  22. if (_solutionParser != null)
  23. {
  24. _solutionReaderProperty = ReflectionUtility.GetInternalProperty(_solutionParser, "SolutionReader");
  25. _projectsProperty = ReflectionUtility.GetInternalProperty(_solutionParser, "Projects");
  26. _parseSolutionMethod = ReflectionUtility.GetInternalMethod(_solutionParser, "ParseSolution");
  27. }
  28. }
  29. private IEnumerable<VsSolutionProject> _projects;
  30. public string Path { get; private set; }
  31. public IEnumerable<VsSolutionProject> Projects
  32. {
  33. get
  34. {
  35. EnsureProjects();
  36. return _projects;
  37. }
  38. }
  39. public VsSolution(string solutionPath)
  40. {
  41. Debug.Assert(_solutionParser != null, "Can not find type 'Microsoft.Build.Construction.SolutionParser' are you missing a assembly reference to 'Microsoft.Build.dll'?");
  42. Path = solutionPath;
  43. }
  44. private void EnsureProjects()
  45. {
  46. if (_projects != null)
  47. {
  48. return;
  49. }
  50. var solutionParser = GetSolutionParserInstance();
  51. using (var streamReader = new StreamReader(Path))
  52. {
  53. _solutionReaderProperty.SetValue(solutionParser, streamReader);
  54. _parseSolutionMethod.Invoke(solutionParser, null);
  55. }
  56. var projects = new List<VsSolutionProject>();
  57. var projectsArray = _projectsProperty.GetValue<object[]>(solutionParser);
  58. foreach (var project in projectsArray)
  59. {
  60. projects.Add(new VsSolutionProject(Path, project));
  61. }
  62. _projects = projects;
  63. }
  64. private static object GetSolutionParserInstance()
  65. {
  66. // Get the constructor for Solution parser
  67. var ctor = _solutionParser.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First();
  68. // Create an instance of the solution parser
  69. return ctor.Invoke(null);
  70. }
  71. }
  72. }