PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Kudu.Core/Infrastructure/VsHelper.cs

https://github.com/moacap/kudu
C# | 85 lines | 65 code | 13 blank | 7 comment | 5 complexity | ebf6be11da5458e7fa50e2d3ea71e2fa MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. namespace Kudu.Core.Infrastructure
  7. {
  8. internal static class VsHelper
  9. {
  10. private static readonly Guid _wapGuid = new Guid("349c5851-65df-11da-9384-00065b846f21");
  11. private static readonly List<VsSolution> _noSolutions = new List<VsSolution>();
  12. public static IList<VsSolution> GetSolutions(string path)
  13. {
  14. if (!Directory.Exists(path))
  15. {
  16. return _noSolutions;
  17. }
  18. return (from solutionFile in Directory.GetFiles(path, "*.sln", SearchOption.AllDirectories)
  19. select new VsSolution(solutionFile)).ToList();
  20. }
  21. /// <summary>
  22. /// Locates the solutin(s) where the specified project is
  23. /// </summary>
  24. public static IList<VsSolution> FindContainingSolutions(string searchPath, string targetPath)
  25. {
  26. return (from solution in GetSolutions(searchPath)
  27. where ExistsInSolution(solution, targetPath)
  28. select solution).ToList();
  29. }
  30. /// <summary>
  31. /// Locates the unambiguous solution matching this project
  32. /// </summary>
  33. public static VsSolution FindContainingSolution(string searchPath, string targetPath)
  34. {
  35. var solutions = FindContainingSolutions(searchPath, targetPath);
  36. // Don't want to use SingleOrDefault since that throws
  37. if (solutions.Count == 0 || solutions.Count > 1)
  38. {
  39. return null;
  40. }
  41. return solutions[0];
  42. }
  43. public static bool IsWap(string projectPath)
  44. {
  45. return IsWap(GetProjectTypeGuids(projectPath));
  46. }
  47. public static bool IsWap(IEnumerable<Guid> projectTypeGuids)
  48. {
  49. return projectTypeGuids.Contains(_wapGuid);
  50. }
  51. public static IEnumerable<Guid> GetProjectTypeGuids(string path)
  52. {
  53. var document = XDocument.Parse(File.ReadAllText(path));
  54. var guids = from propertyGroup in document.Root.Elements(GetName("PropertyGroup"))
  55. let projectTypeGuids = propertyGroup.Element(GetName("ProjectTypeGuids"))
  56. where projectTypeGuids != null
  57. from guid in projectTypeGuids.Value.Split(';')
  58. select new Guid(guid.Trim('{', '}'));
  59. return guids;
  60. }
  61. private static bool ExistsInSolution(VsSolution solution, string targetPath)
  62. {
  63. return (from p in solution.Projects
  64. where PathUtility.NormalizePath(p.AbsolutePath).Equals(PathUtility.NormalizePath(targetPath))
  65. select p).Any();
  66. }
  67. private static XName GetName(string name)
  68. {
  69. return XName.Get(name, "http://schemas.microsoft.com/developer/msbuild/2003");
  70. }
  71. }
  72. }