ProjectExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using GodotTools.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Microsoft.Build.Construction;
  7. using Microsoft.Build.Globbing;
  8. namespace GodotTools.ProjectEditor
  9. {
  10. public static class ProjectExtensions
  11. {
  12. public static ProjectItemElement FindItemOrNull(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
  13. {
  14. string normalizedInclude = include.NormalizePath();
  15. foreach (var itemGroup in root.ItemGroups)
  16. {
  17. if (noCondition && itemGroup.Condition.Length != 0)
  18. continue;
  19. foreach (var item in itemGroup.Items)
  20. {
  21. if (item.ItemType != itemType)
  22. continue;
  23. var glob = MSBuildGlob.Parse(item.Include.NormalizePath());
  24. if (glob.IsMatch(normalizedInclude))
  25. return item;
  26. }
  27. }
  28. return null;
  29. }
  30. public static ProjectItemElement FindItemOrNullAbs(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
  31. {
  32. string normalizedInclude = Path.GetFullPath(include).NormalizePath();
  33. foreach (var itemGroup in root.ItemGroups)
  34. {
  35. if (noCondition && itemGroup.Condition.Length != 0)
  36. continue;
  37. foreach (var item in itemGroup.Items)
  38. {
  39. if (item.ItemType != itemType)
  40. continue;
  41. var glob = MSBuildGlob.Parse(Path.GetFullPath(item.Include).NormalizePath());
  42. if (glob.IsMatch(normalizedInclude))
  43. return item;
  44. }
  45. }
  46. return null;
  47. }
  48. public static IEnumerable<ProjectItemElement> FindAllItemsInFolder(this ProjectRootElement root, string itemType, string folder)
  49. {
  50. string absFolderNormalizedWithSep = Path.GetFullPath(folder).NormalizePath() + Path.DirectorySeparatorChar;
  51. foreach (var itemGroup in root.ItemGroups)
  52. {
  53. foreach (var item in itemGroup.Items)
  54. {
  55. if (item.ItemType != itemType)
  56. continue;
  57. string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
  58. if (absPathNormalized.StartsWith(absFolderNormalizedWithSep))
  59. yield return item;
  60. }
  61. }
  62. }
  63. public static bool HasItem(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
  64. {
  65. return root.FindItemOrNull(itemType, include, noCondition) != null;
  66. }
  67. public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
  68. {
  69. if (!root.HasItem(itemType, include, noCondition: true))
  70. {
  71. root.AddItem(itemType, include);
  72. return true;
  73. }
  74. return false;
  75. }
  76. public static bool RemoveItemChecked(this ProjectRootElement root, string itemType, string include)
  77. {
  78. var item = root.FindItemOrNullAbs(itemType, include);
  79. if (item != null)
  80. {
  81. item.Parent.RemoveChild(item);
  82. return true;
  83. }
  84. return false;
  85. }
  86. public static Guid GetGuid(this ProjectRootElement root)
  87. {
  88. foreach (var property in root.Properties)
  89. {
  90. if (property.Name == "ProjectGuid")
  91. return Guid.Parse(property.Value);
  92. }
  93. return Guid.Empty;
  94. }
  95. public static bool AreDefaultCompileItemsEnabled(this ProjectRootElement root)
  96. {
  97. var enableDefaultCompileItemsProps = root.PropertyGroups
  98. .Where(g => string.IsNullOrEmpty(g.Condition))
  99. .SelectMany(g => g.Properties
  100. .Where(p => p.Name == "EnableDefaultCompileItems" && string.IsNullOrEmpty(p.Condition)));
  101. bool enableDefaultCompileItems = true;
  102. foreach (var prop in enableDefaultCompileItemsProps)
  103. enableDefaultCompileItems = prop.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
  104. return enableDefaultCompileItems;
  105. }
  106. }
  107. }