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

/Kudu.Core/Infrastructure/VsSolutionProject.cs

https://github.com/moacap/kudu
C# | 158 lines | 123 code | 26 blank | 9 comment | 9 complexity | 0320ff71a47358de823a9cc7eba24a08 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. namespace Kudu.Core.Infrastructure
  9. {
  10. [DebuggerDisplay("{ProjectName}")]
  11. public class VsSolutionProject
  12. {
  13. private const string ProjectInSolutionTypeName = "Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
  14. private static readonly Type _projectInSolutionType;
  15. private static readonly PropertyInfo _projectNameProperty;
  16. private static readonly PropertyInfo _relativePathProperty;
  17. private static readonly PropertyInfo _projectTypeProperty;
  18. private static readonly PropertyInfo _aspNetConfigurationsProperty;
  19. static VsSolutionProject()
  20. {
  21. _projectInSolutionType = Type.GetType(ProjectInSolutionTypeName, throwOnError: false, ignoreCase: false);
  22. if (_projectInSolutionType != null)
  23. {
  24. _projectNameProperty = ReflectionUtility.GetInternalProperty(_projectInSolutionType, "ProjectName");
  25. _relativePathProperty = ReflectionUtility.GetInternalProperty(_projectInSolutionType, "RelativePath");
  26. _projectTypeProperty = ReflectionUtility.GetInternalProperty(_projectInSolutionType, "ProjectType");
  27. _aspNetConfigurationsProperty = ReflectionUtility.GetInternalProperty(_projectInSolutionType, "AspNetConfigurations");
  28. }
  29. }
  30. private readonly string _solutionPath;
  31. private readonly object _projectInstance;
  32. private bool _isWap;
  33. private bool _isWebSite;
  34. private IEnumerable<Guid> _projectTypeGuids;
  35. private string _projectName;
  36. private string _absolutePath;
  37. private bool _initialized;
  38. public IEnumerable<Guid> ProjectTypeGuids
  39. {
  40. get
  41. {
  42. EnsureProperties();
  43. return _projectTypeGuids;
  44. }
  45. }
  46. public string ProjectName
  47. {
  48. get
  49. {
  50. EnsureProperties();
  51. return _projectName;
  52. }
  53. }
  54. public string AbsolutePath
  55. {
  56. get
  57. {
  58. EnsureProperties();
  59. return _absolutePath;
  60. }
  61. }
  62. public bool IsWebSite
  63. {
  64. get
  65. {
  66. EnsureProperties();
  67. return _isWebSite;
  68. }
  69. }
  70. public bool IsWap
  71. {
  72. get
  73. {
  74. EnsureProperties();
  75. return _isWap;
  76. }
  77. }
  78. public VsSolutionProject(string solutionPath, object project)
  79. {
  80. _solutionPath = solutionPath;
  81. _projectInstance = project;
  82. }
  83. private void EnsureProperties()
  84. {
  85. if (_initialized)
  86. {
  87. return;
  88. }
  89. _projectName = _projectNameProperty.GetValue<string>(_projectInstance);
  90. var projectType = _projectTypeProperty.GetValue<SolutionProjectType>(_projectInstance);
  91. var relativePath = _relativePathProperty.GetValue<string>(_projectInstance);
  92. _isWebSite = projectType == SolutionProjectType.WebProject;
  93. // When using websites with IISExpress, the relative path property becomes a URL.
  94. // When that happens we're going to grab the path from the Release.AspNetCompiler.PhysicalPath
  95. // property in the solution.
  96. Uri uri;
  97. if (_isWebSite && Uri.TryCreate(relativePath, UriKind.Absolute, out uri))
  98. {
  99. var aspNetConfigurations = _aspNetConfigurationsProperty.GetValue<Hashtable>(_projectInstance);
  100. // Use the release configuraiton and debug if it isn't available
  101. object configurationObject = aspNetConfigurations["Release"] ?? aspNetConfigurations["Debug"];
  102. // REVIEW: Is there always a configuration object (i.e. can this ever be null?)
  103. // The aspNetPhysicalPath contains the relative to the website
  104. FieldInfo aspNetPhysicalPathField = configurationObject.GetType().GetField("aspNetPhysicalPath", BindingFlags.NonPublic | BindingFlags.Instance);
  105. relativePath = (string)aspNetPhysicalPathField.GetValue(configurationObject);
  106. }
  107. _absolutePath = Path.Combine(Path.GetDirectoryName(_solutionPath), relativePath);
  108. if (projectType == SolutionProjectType.KnownToBeMSBuildFormat && File.Exists(_absolutePath))
  109. {
  110. // If the project is an msbuild project then extra the project type guids
  111. _projectTypeGuids = VsHelper.GetProjectTypeGuids(_absolutePath);
  112. // Check if it's a wap
  113. _isWap = VsHelper.IsWap(_projectTypeGuids);
  114. }
  115. else
  116. {
  117. _projectTypeGuids = Enumerable.Empty<Guid>();
  118. }
  119. _initialized = true;
  120. }
  121. // Microsoft.Build.Construction.SolutionProjectType
  122. private enum SolutionProjectType
  123. {
  124. Unknown,
  125. KnownToBeMSBuildFormat,
  126. SolutionFolder,
  127. WebProject,
  128. WebDeploymentProject,
  129. EtpSubProject,
  130. }
  131. }
  132. }